Skip to content

Commit

Permalink
feat: relay gametest
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed Dec 13, 2024
1 parent acfc501 commit 2691d8f
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState
public AutocrafterManagerData getMenuData() {
return new AutocrafterManagerData(
getGroups().stream().map(AutocrafterManagerData.Group::of).toList(),
mainNetworkNode.isActive()
isActive()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ public void removeWatcher(final AutocraftingMonitorWatcher watcher) {

@Override
public boolean isAutocraftingMonitorActive() {
return mainNetworkNode.isActive();
return isActive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public AbstractContainerMenu createMenu(final int syncId, final Inventory invent
@Override
public void updateActiveness(final BlockState state, @Nullable final BooleanProperty activenessProperty) {
super.updateActiveness(state, activenessProperty);
final boolean powered = mainNetworkNode.isActive() && mainNetworkNode.isActivated();
final boolean powered = isActive() && mainNetworkNode.isActivated();
final boolean needToUpdatePowered = state.getValue(DetectorBlock.POWERED) != powered;
if (level != null && needToUpdatePowered && poweredChangeTicks++ % POWERED_CHANGE_TICK_RATE == 0) {
level.setBlockAndUpdate(getBlockPos(), state.setValue(DetectorBlock.POWERED, powered));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public GridOperations createOperations(final ResourceType resourceType, final Se

@Override
public boolean isGridActive() {
return mainNetworkNode.isActive();
return isActive();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public final NonNullList<ItemStack> getDrops() {
}

private Optional<Network> getNetwork() {
if (!mainNetworkNode.isActive()) {
if (!isActive()) {
return Optional.empty();
}
return Optional.ofNullable(mainNetworkNode.getNetwork());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected InWorldNetworkNodeContainer createMainContainer(final SimpleNetworkNod
@Override
public void addOutgoingConnections(final ConnectionSink sink) {
super.addOutgoingConnections(sink);
if (receiverKey != null && NetworkTransmitterBlockEntity.this.mainNetworkNode.isActive()) {
if (receiverKey != null && NetworkTransmitterBlockEntity.this.isActive()) {
sink.tryConnect(receiverKey.pos());
}
}
Expand All @@ -108,7 +108,7 @@ public void updateStateInLevel(final BlockState state) {
}

private NetworkTransmitterState calculateState() {
if (!mainNetworkNode.isActive()) {
if (!isActive()) {
return NetworkTransmitterState.INACTIVE;
}
if (receiverKey == null) {
Expand All @@ -124,7 +124,7 @@ private NetworkTransmitterState calculateState() {

NetworkTransmitterData getStatus() {
final Network network = mainNetworkNode.getNetwork();
if (!mainNetworkNode.isActive() || network == null || level == null) {
if (!isActive() || network == null || level == null) {
return INACTIVE;
}
if (receiverKey == null) {
Expand All @@ -145,7 +145,7 @@ NetworkTransmitterData getStatus() {
@Override
public void doWork() {
super.doWork();
if (!mainNetworkNode.isActive() || mainNetworkNode.getNetwork() == null || receiverKey == null) {
if (!isActive() || mainNetworkNode.getNetwork() == null || receiverKey == null) {
return;
}
final boolean receiverFound = isReceiverFoundInNetwork(mainNetworkNode.getNetwork(), receiverKey);
Expand Down
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 @@ -88,7 +88,7 @@ boolean isFuzzyMode() {
}

void setFuzzyMode(final boolean fuzzyMode) {
final boolean wasActive = mainNetworkNode.isActive();
final boolean wasActive = isActive();
// Updating fuzzy mode will call the filter's listener as the normalizer will yield different outputs.
// However, when updating a filter the storage resets and "self-removes". If the normalizer yields different
// outputs too early, the self-remove operation will partially fail as the expected resources will be different.
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 Expand Up @@ -191,7 +191,7 @@ void setPassThrough(final boolean passThrough) {
}

boolean isActiveInternal() {
return mainNetworkNode.isActive();
return isActive();
}

Direction getDirectionInternal() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,4 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState
final BlockState newBlockState) {
return AbstractDirectionalBlock.didDirectionChange(oldBlockState, newBlockState);
}

boolean isActive() {
return mainNetworkNode.isActive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void doWork() {

private void trySendDisplayUpdate(final Level level) {
final long amount = getAmount();
final boolean active = mainNetworkNode.isActive();
final boolean active = isActive();
if ((amount != currentAmount || active != currentlyActive) && displayUpdateRateLimiter.tryAcquire()) {
sendDisplayUpdate(level, amount, active);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ private void sendDisplayUpdate() {
if (level == null) {
return;
}
sendDisplayUpdate(level, getAmount(), mainNetworkNode.isActive());
sendDisplayUpdate(level, getAmount(), isActive());
}

private void sendDisplayUpdate(final Level level, final long amount, final boolean active) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ protected boolean calculateActive() {
&& mainNetworkNode.getNetwork().getComponent(EnergyNetworkComponent.class).getStored() >= energyUsage;
}

public boolean isActive() {
return mainNetworkNode.isActive();
}

public void updateActiveness(final BlockState state, @Nullable final BooleanProperty activenessProperty) {
final boolean newActive = calculateActive();
final boolean nodeActivenessNeedsUpdate = newActive != mainNetworkNode.isActive();
final boolean nodeActivenessNeedsUpdate = newActive != isActive();
final boolean blockStateActivenessNeedsUpdate = activenessProperty != null
&& state.getValue(activenessProperty) != newActive;
final boolean activenessNeedsUpdate = nodeActivenessNeedsUpdate || blockStateActivenessNeedsUpdate;
Expand All @@ -102,7 +106,7 @@ protected void activenessChanged(final boolean newActive) {
LOGGER.debug(
"Activeness change for node at {}: {} -> {}",
getBlockPos(),
mainNetworkNode.isActive(),
isActive(),
newActive
);
mainNetworkNode.setActive(newActive);
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,15 @@
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.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 +73,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.isActive();
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 +244,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
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.common;

@FunctionalInterface
public interface QuadConsumer<T, U, V, W> {
void accept(T t, U u, V v, W w);
}
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
Loading

0 comments on commit 2691d8f

Please sign in to comment.