Skip to content

Commit

Permalink
feat: regulator upgrade gametest
Browse files Browse the repository at this point in the history
  • Loading branch information
starforcraft committed Oct 11, 2024
1 parent ba387b3 commit 488b367
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.apiguardian.api.API;

/**
Expand All @@ -21,7 +22,7 @@ default List<Item> getUpgradeItems() {
return Collections.emptyList();
}

default boolean addUpgradeItem(Item upgradeItem) {
default boolean addUpgradeItem(ItemStack upgradeStack) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ private void tryTransferUpgrades(final Player player,
final ConfigurationCardTarget target,
final List<Item> upgradeItems) {
for (final Item upgradeItem : upgradeItems) {
final int upgradeIndexInPlayerInventory = player.getInventory().findSlotMatchingItem(
new ItemStack(upgradeItem)
);
if (upgradeIndexInPlayerInventory >= 0 && target.addUpgradeItem(upgradeItem)) {
final ItemStack upgradeStack = new ItemStack(upgradeItem);
final int upgradeIndexInPlayerInventory = player.getInventory().findSlotMatchingItem(upgradeStack);
if (upgradeIndexInPlayerInventory >= 0 && target.addUpgradeItem(upgradeStack)) {
player.getInventory().removeItem(upgradeIndexInPlayerInventory, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

public boolean isPickupItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public List<Item> getUpgradeItems() {
}

@Override
public boolean addUpgradeItem(final Item upgradeItem) {
return upgradeContainer.addUpgradeItem(upgradeItem);
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return upgradeContainer.addUpgradeItem(upgradeStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,22 @@ public Optional<TooltipComponent> getTooltipImage(final ItemStack stack) {
return Optional.of(new RegulatorTooltipComponent(getDestinations(), HELP, getConfiguredResource(stack)));
}

public void setAmount(final ItemStack stack, final double amount) {
private void setAmount(final ItemStack stack, final double amount) {
final RegulatorUpgradeState state = stack.getOrDefault(
DataComponents.INSTANCE.getRegulatorUpgradeState(),
RegulatorUpgradeState.EMPTY
);
stack.set(DataComponents.INSTANCE.getRegulatorUpgradeState(), state.withAmount(amount));
}

public void setAmount(final ItemStack regulatorStack, final PlatformResourceKey resource, final double amount) {
final RegulatorUpgradeState state = regulatorStack.getOrDefault(
DataComponents.INSTANCE.getRegulatorUpgradeState(),
new RegulatorUpgradeState(amount, Optional.of(resource))
);
regulatorStack.set(DataComponents.INSTANCE.getRegulatorUpgradeState(), state);
}

@Override
public long getEnergyUsage() {
return Platform.INSTANCE.getConfig().getUpgrade().getRegulatorUpgradeEnergyUsage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public List<Item> getUpgradeItems() {
return upgradeItems;
}

public boolean addUpgradeItem(final Item upgradeItem) {
return addItem(new ItemStack(upgradeItem)).isEmpty();
public boolean addUpgradeItem(final ItemStack upgradeStack) {
return addItem(upgradeStack).isEmpty();
}

public NonNullList<ItemStack> getDrops() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void shouldDropItemWithStackUpgrade(final GameTestHelper helper) {
// Act
constructor.setDropItems(true);
constructor.setFilters(List.of(asResource(DIRT)));
constructor.addUpgradeItem(RSITEMS.getStackUpgrade());
constructor.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static void shouldBreakBlockWithSilkTouchUpgrade(final GameTestHelper hel

// Act
helper.setBlock(pos.east(), Blocks.DIAMOND_ORE);
destructor.addUpgradeItem(RSITEMS.getSilkTouchUpgrade());
destructor.addUpgradeItem(RSITEMS.getSilkTouchUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.upgrade.AbstractUpgradeItem;
import com.refinedmods.refinedstorage.common.upgrade.RegulatorUpgradeItem;
import com.refinedmods.refinedstorage.common.util.IdentifierUtil;

Expand Down Expand Up @@ -81,7 +80,7 @@ public static void shouldExportItemWithStackUpgrade(final GameTestHelper helper)

// Act
exporter.setFilters(List.of(asResource(DIRT)));
exporter.addUpgradeItem(RSITEMS.getStackUpgrade());
exporter.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down Expand Up @@ -124,17 +123,18 @@ public static void shouldExportItemWithRegulatorUpgrade(final GameTestHelper hel
}));

// Act
final AbstractUpgradeItem upgrade = RSITEMS.getRegulatorUpgrade();
if (upgrade instanceof RegulatorUpgradeItem regulatorUpgrade) {
regulatorUpgrade.setAmount(DIRT.getDefaultInstance(), 10);
}
exporter.setFilters(List.of(asResource(DIRT.getDefaultInstance())));

exporter.setFilters(List.of(asResource(DIRT)));
final ItemStack upgrade = RSITEMS.getRegulatorUpgrade().getDefaultInstance();
if (upgrade.getItem() instanceof RegulatorUpgradeItem upgradeItem) {
upgradeItem.setAmount(upgrade, asResource(DIRT.getDefaultInstance()), 10);
}
exporter.addUpgradeItem(upgrade);

// Assert
sequence
.thenWaitUntil(containerContainsExactly(
.thenIdle(95)
.thenExecute(containerContainsExactly(
helper,
pos.east(),
new ResourceAmount(asResource(DIRT), 10)
Expand Down Expand Up @@ -224,7 +224,7 @@ public static void shouldExportFluidWithStackUpgrade(final GameTestHelper helper

// Act
exporter.setFilters(List.of(asResource(WATER)));
exporter.addUpgradeItem(RSITEMS.getStackUpgrade());
exporter.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.upgrade.AbstractUpgradeItem;
import com.refinedmods.refinedstorage.common.upgrade.RegulatorUpgradeItem;
import com.refinedmods.refinedstorage.common.util.IdentifierUtil;

Expand Down Expand Up @@ -91,7 +90,7 @@ public static void shouldImportItemWithStackUpgrade(final GameTestHelper helper)
DIRT.getDefaultInstance()
);

importer.addUpgradeItem(RSITEMS.getStackUpgrade());
importer.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down Expand Up @@ -137,20 +136,21 @@ public static void shouldImportItemWithRegulatorUpgrade(final GameTestHelper hel
DIRT.getDefaultInstance().copyWithCount(15)
);

final AbstractUpgradeItem upgrade = RSITEMS.getRegulatorUpgrade();
if (upgrade instanceof RegulatorUpgradeItem regulatorUpgrade) {
regulatorUpgrade.setAmount(DIRT.getDefaultInstance(), 10);
final ItemStack upgrade = RSITEMS.getRegulatorUpgrade().getDefaultInstance();
if (upgrade.getItem() instanceof RegulatorUpgradeItem upgradeItem) {
upgradeItem.setAmount(upgrade, asResource(DIRT.getDefaultInstance()), 10);
}
importer.addUpgradeItem(upgrade);

// Assert
sequence
.thenWaitUntil(containerContainsExactly(
.thenIdle(95)
.thenExecute(containerContainsExactly(
helper,
pos.east(),
new ResourceAmount(asResource(DIRT), 10)
))
.thenWaitUntil(storageContainsExactly(
.thenExecute(storageContainsExactly(
helper,
pos,
new ResourceAmount(asResource(DIRT), 15),
Expand Down Expand Up @@ -371,7 +371,7 @@ public static void shouldImportFluidWithStackUpgrade(final GameTestHelper helper
new ResourceAmount(asResource(WATER), Platform.INSTANCE.getBucketAmount() * 15),
new ResourceAmount(asResource(LAVA), Platform.INSTANCE.getBucketAmount())
);
importer.addUpgradeItem(RSITEMS.getStackUpgrade());
importer.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void shouldInsertItemsIntoNetworkWithStackUpgrade(final GameTestHe

// Act
diskInterface.setTransferMode(StorageTransferMode.INSERT_INTO_NETWORK);
diskInterface.addUpgradeItem(RSITEMS.getStackUpgrade());
diskInterface.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down Expand Up @@ -217,7 +217,7 @@ public static void shouldExtractItemsFromNetworkWithStackUpgrade(final GameTestH

// Act
diskInterface.setTransferMode(StorageTransferMode.EXTRACT_FROM_NETWORK);
diskInterface.addUpgradeItem(RSITEMS.getStackUpgrade());
diskInterface.addUpgradeItem(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down

0 comments on commit 488b367

Please sign in to comment.