Skip to content

Commit

Permalink
Merge pull request #748 from starforcraft/feat/GH-248/relaytests
Browse files Browse the repository at this point in the history
Relay gametest
  • Loading branch information
raoulvdberge authored Dec 14, 2024
2 parents acfc501 + 1f2b1b6 commit 6ad6d0d
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public RelayBlockEntity(final BlockPos pos, final BlockState state) {
this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters(
ResourceContainerImpl.createForFilter(),
this::setChanged,
this::filterContainerChanged
this::setFilters
);
this.mainNetworkNode.setFilterNormalizer(filter.createNormalizer());
this.containers.addContainer(
Expand All @@ -98,7 +98,7 @@ void setFuzzyMode(final boolean fuzzyMode) {
mainNetworkNode.setActive(wasActive);
}

private void filterContainerChanged(final Set<ResourceKey> filters) {
void setFilters(final Set<ResourceKey> filters) {
mainNetworkNode.setFilters(filters);
setChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage.api.core.Action;
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.energy.EnergyNetworkComponent;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
Expand All @@ -17,13 +17,16 @@
import com.refinedmods.refinedstorage.common.content.Items;
import com.refinedmods.refinedstorage.common.iface.ExportedResourcesContainer;
import com.refinedmods.refinedstorage.common.iface.InterfaceBlockEntity;
import com.refinedmods.refinedstorage.common.support.AbstractActiveColoredDirectionalBlock;
import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage.common.support.resource.ItemResource;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -71,6 +74,19 @@ public static Runnable networkIsAvailable(final GameTestHelper helper,
};
}

public static void checkBlockEntityActiveness(final GameTestHelper helper,
final BlockPos pos,
final boolean expectedActive) {
final var blockEntity = requireBlockEntity(
helper,
pos,
AbstractBaseNetworkNodeContainerBlockEntity.class
);
final boolean actualActive = blockEntity.getBlockState().getValue(AbstractActiveColoredDirectionalBlock.ACTIVE);
helper.assertTrue(actualActive == expectedActive, "Activeness of Block Entity should be " + expectedActive
+ " but is " + actualActive);
}

public static void insert(final GameTestHelper helper,
final Network network,
final Item resource,
Expand Down Expand Up @@ -229,14 +245,25 @@ private static Runnable assertResourceContainerEmpty(final Component displayName
};
}

public static Runnable energyStoredExactly(final EnergyStorage storage,
final long energyAmount) {
return () -> {
if (storage.getStored() != energyAmount) {
throw new GameTestAssertException("Energy stored should be: " + energyAmount
+ " but is " + storage.getStored());
}
};
public static Runnable checkEnergyInNetwork(final GameTestHelper helper,
final BlockPos pos,
final Function<Long, Long> storedConsumer) {
return networkIsAvailable(helper, pos, network -> {
final EnergyNetworkComponent energyComponent = network.getComponent(EnergyNetworkComponent.class);

long storedEnergy = energyComponent.getStored();
storedEnergy = storedConsumer.apply(storedEnergy);

energyStoredExactly(storedEnergy, energyComponent.getCapacity());
});
}

public static void energyStoredExactly(final long storedEnergy,
final long energyAmount) {
if (storedEnergy != energyAmount) {
throw new GameTestAssertException("Energy stored should be: " + energyAmount
+ " but is " + storedEnergy);
}
}

public static Runnable interfaceContainsExactly(final GameTestHelper helper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public static void shouldConsumeEnergy(final GameTestHelper helper) {
// Assert
sequence
.thenIdle(20)
.thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity()))
.thenExecute(() -> energyStoredExactly(energyStorage.getStored(), energyStorage.getCapacity()))
.thenWaitUntil(() -> helper.setBlock(pos.above(), RSBLOCKS.getGrid().getDefault()))
.thenIdle(1)
.thenExecute(energyStoredExactly(
energyStorage,
.thenExecute(() -> energyStoredExactly(
energyStorage.getStored(),
energyStorage.getCapacity() - Platform.INSTANCE.getConfig().getGrid().getEnergyUsage()
))
.thenIdle(9)
.thenExecute(energyStoredExactly(
energyStorage,
.thenExecute(() -> energyStoredExactly(
energyStorage.getStored(),
energyStorage.getCapacity() - Platform.INSTANCE.getConfig().getGrid().getEnergyUsage() * 10
))
.thenSucceed();
Expand All @@ -62,10 +62,10 @@ public static void shouldNotConsumeEnergy(final GameTestHelper helper) {
// Assert
sequence
.thenIdle(20)
.thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity()))
.thenExecute(() -> energyStoredExactly(energyStorage.getStored(), energyStorage.getCapacity()))
.thenWaitUntil(() -> helper.setBlock(pos.above(), RSBLOCKS.getGrid().getDefault()))
.thenIdle(20)
.thenExecute(energyStoredExactly(energyStorage, energyStorage.getCapacity()))
.thenExecute(() -> energyStoredExactly(energyStorage.getStored(), energyStorage.getCapacity()))
.thenSucceed();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.refinedmods.refinedstorage.common.networking;

import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.GameTestSequence;

@FunctionalInterface
public interface RelayConsumer {
void accept(RelayBlockEntity relayBlockEntity,
BlockPos pos,
BlockPos subnetworkPos,
GameTestSequence gameTestSequence);
}
Loading

0 comments on commit 6ad6d0d

Please sign in to comment.