Skip to content

Commit

Permalink
feat: external storage gametest
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed Aug 22, 2024
1 parent 667a278 commit 0feee57
Show file tree
Hide file tree
Showing 10 changed files with 1,176 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.refinedmods.refinedstorage.common.storage.externalstorage;

import com.refinedmods.refinedstorage.api.network.impl.node.externalstorage.ExternalStorageNetworkNode;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage.api.storage.AccessMode;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.content.BlockEntities;
Expand All @@ -13,6 +16,7 @@
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerData;
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl;

import java.util.Set;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -145,6 +149,31 @@ public void readConfiguration(final CompoundTag tag, final HolderLookup.Provider
configContainer.load(tag);
}

void setFilters(final Set<ResourceKey> filters) {
mainNetworkNode.getStorageConfiguration().setFilters(filters);
}

void setFilterMode(final FilterMode mode) {
mainNetworkNode.getStorageConfiguration().setFilterMode(mode);
setChanged();
}

void setFuzzyMode(final boolean fuzzyMode) {
filter.setFuzzyMode(fuzzyMode);
}

void setAccessMode(final AccessMode accessMode) {
mainNetworkNode.getStorageConfiguration().setAccessMode(accessMode);
}

void setPriority(final int priority) {
mainNetworkNode.getStorageConfiguration().setPriority(priority);
}

void setVoidExcess(final boolean voidExcess) {
mainNetworkNode.getStorageConfiguration().setVoidExcess(voidExcess);
}

@Override
public ResourceContainerData getMenuData() {
return ResourceContainerData.of(filter.getFilterContainer());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package com.refinedmods.refinedstorage.common.storage.externalstorage;

import com.google.common.util.concurrent.RateLimiter;

class ExternalStorageWorkRate {
private static final double[] RATE_LIMITERS = new double[] {
0.5D, // slowest, every 2 sec
0.75D, // faster
1D, // medium, every 1 sec
2D, // faster, every 0.5 sec
3D // fastest
private static final int[] OPERATION_COUNTS = new int[] {
40, // slowest, every 2 sec
30, // faster, every 1.5 sec
20, // medium, every 1 sec
10, // faster, every 0.5 sec
5 // fastest, every 0.25 sec
};

private int idx = 2; // medium
private final RateLimiter rateLimiter = RateLimiter.create(RATE_LIMITERS[idx]);
private int counter = 0;
private int threshold = OPERATION_COUNTS[idx];

boolean canDoWork() {
return rateLimiter.tryAcquire();
counter++;
if (counter >= threshold) {
counter = 0;
return true;
}
return false;
}

void faster() {
if (idx + 1 >= RATE_LIMITERS.length) {
return;
if (idx + 1 < OPERATION_COUNTS.length) {
idx++;
updateThreshold();
}
idx++;
updateRate();
}

void slower() {
if (idx - 1 < 0) {
return;
if (idx - 1 >= 0) {
idx--;
updateThreshold();
}
idx--;
updateRate();
}

private void updateRate() {
rateLimiter.setRate(RATE_LIMITERS[idx]);
private void updateThreshold() {
threshold = OPERATION_COUNTS[idx];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.UUID;
import javax.annotation.Nullable;

import com.google.common.util.concurrent.RateLimiter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
Expand Down Expand Up @@ -45,10 +44,11 @@ public abstract class AbstractBaseNetworkNodeContainerBlockEntity<T extends Abst
private static final String TAG_CUSTOM_NAME = "CustomName";
private static final String TAG_PLACED_BY_PLAYER_ID = "pbpid";
private static final String TAG_REDSTONE_MODE = "rm";
private static final int ACTIVENESS_CHANGE_TICK_RATE = 20;

protected NetworkNodeTicker ticker = NetworkNodeTicker.IMMEDIATE;

private final RateLimiter activenessChangeRateLimiter = RateLimiter.create(1);
private int activenessChangeTicks;

@Nullable
private Component name;
Expand Down Expand Up @@ -87,7 +87,7 @@ public void updateActiveness(final BlockState state, @Nullable final BooleanProp
final boolean blockStateActivenessNeedsUpdate = activenessProperty != null
&& state.getValue(activenessProperty) != newActive;
final boolean activenessNeedsUpdate = nodeActivenessNeedsUpdate || blockStateActivenessNeedsUpdate;
if (activenessNeedsUpdate && activenessChangeRateLimiter.tryAcquire()) {
if (activenessNeedsUpdate && activenessChangeTicks++ % ACTIVENESS_CHANGE_TICK_RATE == 0) {
if (nodeActivenessNeedsUpdate) {
activenessChanged(newActive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage.common.content.Blocks;
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.resource.FluidResource;
import com.refinedmods.refinedstorage.common.support.resource.ItemResource;
Expand Down Expand Up @@ -68,27 +69,99 @@ public static Runnable networkIsAvailable(final GameTestHelper helper,
};
}

public static void insert(final GameTestHelper helper,
final Network network,
final Item resource,
final long amount,
final boolean shouldSucceed) {
insert(helper, network, new ItemResource(resource), amount, shouldSucceed);
}

public static void insert(final GameTestHelper helper,
final Network network,
final Item resource,
final long amount) {
insert(helper, network, new ItemResource(resource), amount);
insert(helper, network, new ItemResource(resource), amount, true);
}

public static void insert(final GameTestHelper helper,
final Network network,
final Fluid resource,
final long amount,
final boolean shouldSucceed) {
insert(helper, network, new FluidResource(resource), amount, shouldSucceed);
}

public static void insert(final GameTestHelper helper,
final Network network,
final Fluid resource,
final long amount) {
insert(helper, network, new FluidResource(resource), amount);
insert(helper, network, new FluidResource(resource), amount, true);
}

public static void insert(final GameTestHelper helper,
final Network network,
final ResourceKey resource,
final long amount) {
insert(helper, network, resource, amount, true);
}

public static void insert(final GameTestHelper helper,
final Network network,
final ResourceKey resource,
final long amount,
final boolean shouldSucceed) {
final StorageNetworkComponent storage = network.getComponent(StorageNetworkComponent.class);
final long inserted = storage.insert(resource, amount, Action.EXECUTE, EmptyActor.INSTANCE);
helper.assertTrue(inserted == amount, "Resource couldn't be inserted");
if (shouldSucceed) {
helper.assertTrue(inserted == amount, "Resource couldn't be inserted");
} else {
helper.assertFalse(inserted == amount, "Resource could be inserted");
}
}

public static void extract(final GameTestHelper helper,
final Network network,
final Item resource,
final long amount,
final boolean shouldSucceed) {
extract(helper, network, new ItemResource(resource), amount, shouldSucceed);
}

public static void extract(final GameTestHelper helper,
final Network network,
final Item resource,
final long amount) {
extract(helper, network, new ItemResource(resource), amount, true);
}

public static void extract(final GameTestHelper helper,
final Network network,
final Fluid resource,
final long amount,
final boolean shouldSucceed) {
extract(helper, network, new FluidResource(resource), amount, shouldSucceed);
}

public static void extract(final GameTestHelper helper,
final Network network,
final Fluid resource,
final long amount) {
extract(helper, network, new FluidResource(resource), amount, true);
}

public static void extract(final GameTestHelper helper,
final Network network,
final ResourceKey resource,
final long amount,
final boolean shouldSucceed) {
final StorageNetworkComponent storage = network.getComponent(StorageNetworkComponent.class);
final long extracted = storage.extract(resource, amount, Action.EXECUTE, EmptyActor.INSTANCE);
if (shouldSucceed) {
helper.assertTrue(extracted == amount, "Resource couldn't be extracted");
} else {
helper.assertFalse(extracted == amount, "Resource could be extracted");
}
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -234,6 +307,47 @@ private static void listContainsExactly(final ResourceList given, final Resource
}
}

public static void prepareChest(final GameTestHelper helper,
final BlockPos pos,
final ItemStack... stacks) {
helper.setBlock(pos, net.minecraft.world.level.block.Blocks.CHEST.defaultBlockState());
final var chestBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class);
for (int i = 0; i < stacks.length; i++) {
chestBlockEntity.setItem(i, stacks[i]);
}
}

public static void removeItemFromChest(final GameTestHelper helper,
final BlockPos pos,
final ItemStack stack) {
final var chestBlockEntity = requireBlockEntity(helper, pos, BaseContainerBlockEntity.class);
for (int i = 0; i < chestBlockEntity.getContainerSize(); i++) {
if (chestBlockEntity.getItem(i).is(stack.getItem())) {
chestBlockEntity.removeItem(i, stack.getCount());
}
}
}

public static void prepareInterface(final GameTestHelper helper,
final BlockPos pos,
final ResourceAmount... resource) {
helper.setBlock(pos, RSBLOCKS.getInterface());
final var interfaceBlockEntity = requireBlockEntity(helper, pos, InterfaceBlockEntity.class);
final ExportedResourcesContainer exportedResources = interfaceBlockEntity.getExportedResources();

for (int i = 0; i < resource.length; i++) {
exportedResources.set(i, resource[i]);
}
}

public static ItemStack[] createStacks(final Item item, final int count, final int amount) {
final ItemStack[] stacks = new ItemStack[amount];
for (int i = 0; i < amount; i++) {
stacks[i] = item.getDefaultInstance().copyWithCount(count);
}
return stacks;
}

public static ItemResource asResource(final Item item) {
return new ItemResource(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ public static void shouldExportFluidWithStackUpgrade(final GameTestHelper helper
.thenExecute(interfaceContainsExactly(
helper,
pos.east(),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16),
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 16)
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 64)
))

.thenSucceed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import static com.refinedmods.refinedstorage.common.GameTestUtil.insert;
import static com.refinedmods.refinedstorage.common.GameTestUtil.interfaceContainsExactly;
import static com.refinedmods.refinedstorage.common.GameTestUtil.networkIsAvailable;
import static com.refinedmods.refinedstorage.common.GameTestUtil.prepareChest;
import static com.refinedmods.refinedstorage.common.GameTestUtil.prepareInterface;
import static com.refinedmods.refinedstorage.common.GameTestUtil.storageContainsExactly;
import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.prepareChest;
import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.prepareInterface;
import static com.refinedmods.refinedstorage.common.importer.ImporterTestPlots.preparePlot;
import static net.minecraft.world.item.Items.COBBLESTONE;
import static net.minecraft.world.item.Items.DIAMOND_CHESTPLATE;
Expand Down Expand Up @@ -87,6 +87,7 @@ public static void shouldImportItemWithStackUpgrade(final GameTestHelper helper)
COBBLESTONE.getDefaultInstance().copyWithCount(64),
DIRT.getDefaultInstance()
);

importer.addUpgradeItem(RSITEMS.getStackUpgrade());

// Assert
Expand Down Expand Up @@ -132,7 +133,8 @@ public static void shouldImportItemBlocklist(final GameTestHelper helper) {
helper,
pos.east(),
DIRT.getDefaultInstance(),
DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate
DIAMOND_CHESTPLATE.getDefaultInstance(),
damagedDiamondChestplate
);

importer.setFuzzyMode(false);
Expand Down Expand Up @@ -173,7 +175,8 @@ public static void shouldImportItemFuzzyBlocklist(final GameTestHelper helper) {
helper,
pos.east(),
DIRT.getDefaultInstance(),
DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate
DIAMOND_CHESTPLATE.getDefaultInstance(),
damagedDiamondChestplate
);

importer.setFuzzyMode(true);
Expand Down Expand Up @@ -215,7 +218,8 @@ public static void shouldImportItemAllowlist(final GameTestHelper helper) {
helper,
pos.east(),
DIRT.getDefaultInstance(),
DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate
DIAMOND_CHESTPLATE.getDefaultInstance(),
damagedDiamondChestplate
);

importer.setFuzzyMode(false);
Expand Down Expand Up @@ -258,7 +262,8 @@ public static void shouldImportItemFuzzyAllowlist(final GameTestHelper helper) {
helper,
pos.east(),
DIRT.getDefaultInstance(),
DIAMOND_CHESTPLATE.getDefaultInstance(), damagedDiamondChestplate
DIAMOND_CHESTPLATE.getDefaultInstance(),
damagedDiamondChestplate
);

importer.setFuzzyMode(true);
Expand Down
Loading

0 comments on commit 0feee57

Please sign in to comment.