Skip to content

Commit

Permalink
fix: the configuration card is now stack sensitive
Browse files Browse the repository at this point in the history
To transfer a configured Regulator Upgrade
for example.
  • Loading branch information
raoulvdberge committed Oct 12, 2024
1 parent cb4d54a commit a2da917
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fixed upgrade destinations not being shown on upgrades.
- Fixed resources with changed data format or ID causing entire storage to fail to load.
- Fixed crash when trying to export fluids from an External Storage on Fabric.
- The Configuration Card can now also transfer the (configured) Regulator Upgrade.

## [2.0.0-milestone.4.7] - 2024-08-11

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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 @@ -18,11 +17,11 @@ public interface ConfigurationCardTarget {

void readConfiguration(CompoundTag tag, HolderLookup.Provider provider);

default List<Item> getUpgradeItems() {
default List<ItemStack> getUpgrades() {
return Collections.emptyList();
}

default boolean addUpgradeItem(ItemStack upgradeStack) {
default boolean addUpgrade(ItemStack upgradeStack) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ protected boolean hasRedstoneMode() {
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PatternItemOverrides extends ItemOverrides {
private final BakedModel stonecutterModel;
private final BakedModel smithingTableModel;

@SuppressWarnings({"DataFlowIssue"}) // null is allowed as long as we don't pass overrides
@SuppressWarnings({"DataFlowIssue", "deprecation"}) // null is allowed as long as we don't pass overrides
public PatternItemOverrides(final ModelBaker modelBaker,
final BakedModel emptyModel,
final BakedModel craftingModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public InteractionResult useOn(final UseOnContext ctx) {
stack.set(DataComponents.INSTANCE.getConfigurationCardState(), new ConfigurationCardState(
blockEntity.getType(),
createConfigTag(target, ctx.getLevel().registryAccess()),
target.getUpgradeItems()
target.getUpgrades()
));
sendCopiedConfigurationMessage(ctx.getPlayer(), blockEntity.getType());
return InteractionResult.CONSUME;
Expand All @@ -74,19 +74,18 @@ private InteractionResult applyConfiguration(
return configurationCardIsConfiguredForDifferentType(player, state.blockEntityType());
}
target.readConfiguration(state.config(), provider);
tryTransferUpgrades(player, target, state.upgradeItems());
tryTransferUpgrades(player, target, state.upgrades());
targetBlockEntity.setChanged();
player.sendSystemMessage(createTranslation("item", "configuration_card.applied_configuration"));
return InteractionResult.SUCCESS;
}

private void tryTransferUpgrades(final Player player,
final ConfigurationCardTarget target,
final List<Item> upgradeItems) {
for (final Item upgradeItem : upgradeItems) {
final ItemStack upgradeStack = new ItemStack(upgradeItem);
final int upgradeIndexInPlayerInventory = player.getInventory().findSlotMatchingItem(upgradeStack);
if (upgradeIndexInPlayerInventory >= 0 && target.addUpgradeItem(upgradeStack)) {
final List<ItemStack> upgradeItems) {
for (final ItemStack upgradeItem : upgradeItems) {
final int upgradeIndexInPlayerInventory = player.getInventory().findSlotMatchingItem(upgradeItem);
if (upgradeIndexInPlayerInventory >= 0 && target.addUpgrade(upgradeItem)) {
player.getInventory().removeItem(upgradeIndexInPlayerInventory, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,24 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntityType;

public record ConfigurationCardState(BlockEntityType<?> blockEntityType,
CompoundTag config,
List<Item> upgradeItems) {
public record ConfigurationCardState(BlockEntityType<?> blockEntityType, CompoundTag config, List<ItemStack> upgrades) {
public static final Codec<ConfigurationCardState> CODEC = RecordCodecBuilder.create(instance -> instance.group(
BuiltInRegistries.BLOCK_ENTITY_TYPE.byNameCodec().fieldOf("blockEntityType")
.forGetter(ConfigurationCardState::blockEntityType),
CompoundTag.CODEC.fieldOf("config")
.forGetter(ConfigurationCardState::config),
Codec.list(BuiltInRegistries.ITEM.byNameCodec()).fieldOf("upgradeItems")
.forGetter(ConfigurationCardState::upgradeItems)
Codec.list(ItemStack.SINGLE_ITEM_CODEC).fieldOf("upgrades")
.forGetter(ConfigurationCardState::upgrades)
).apply(instance, ConfigurationCardState::new));

public static final StreamCodec<RegistryFriendlyByteBuf, ConfigurationCardState> STREAM_CODEC =
StreamCodec.composite(
ByteBufCodecs.registry(Registries.BLOCK_ENTITY_TYPE), ConfigurationCardState::blockEntityType,
ByteBufCodecs.COMPOUND_TAG, ConfigurationCardState::config,
ByteBufCodecs.collection(ArrayList::new, ByteBufCodecs.registry(Registries.ITEM)),
ConfigurationCardState::upgradeItems,
ByteBufCodecs.collection(ArrayList::new, ItemStack.STREAM_CODEC), ConfigurationCardState::upgrades,
ConfigurationCardState::new
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ void setFilters(final List<ResourceKey> filters) {
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;

Expand Down Expand Up @@ -75,12 +74,12 @@ protected AbstractDestructorBlockEntity(final BlockPos pos, final BlockState sta
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ private void schedulingModeChanged(final SchedulingMode schedulingMode) {
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ protected AbstractImporterBlockEntity(final BlockPos pos, final BlockState state
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public void loadAdditional(final CompoundTag tag, final HolderLookup.Provider pr
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public void writeConfiguration(final CompoundTag tag, final HolderLookup.Provide
}

@Override
public List<Item> getUpgradeItems() {
public List<ItemStack> getUpgrades() {
return upgradeContainer.getUpgradeItems();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class UpgradeContainer extends SimpleContainer implements UpgradeState {
private final UpgradeContainerListener listener;
private final int defaultWorkTickRate;
private final ThrottledNetworkNodeTicker ticker;

public UpgradeContainer(final UpgradeDestination destination) {
this(destination, null);
}
Expand Down Expand Up @@ -145,20 +145,20 @@ public long getEnergyUsage() {
return usage;
}

public List<Item> getUpgradeItems() {
final List<Item> upgradeItems = new ArrayList<>();
public List<ItemStack> getUpgradeItems() {
final List<ItemStack> upgradeItems = new ArrayList<>();
for (int i = 0; i < getContainerSize(); ++i) {
final ItemStack itemStack = getItem(i);
if (itemStack.isEmpty()) {
continue;
}
upgradeItems.add(itemStack.getItem());
upgradeItems.add(itemStack.copy());
}
return upgradeItems;
}

public boolean addUpgradeItem(final ItemStack upgradeStack) {
return addItem(upgradeStack).isEmpty();
public boolean addUpgradeItem(final ItemStack upgradeItem) {
return addItem(upgradeItem).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().getDefaultInstance());
constructor.addUpgrade(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().getDefaultInstance());
destructor.addUpgrade(RSITEMS.getSilkTouchUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void shouldExportItemWithStackUpgrade(final GameTestHelper helper)

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

// Assert
sequence
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().getDefaultInstance());
exporter.addUpgrade(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void shouldImportItemWithStackUpgrade(final GameTestHelper helper)
DIRT.getDefaultInstance()
);

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

// Assert
sequence
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().getDefaultInstance());
importer.addUpgrade(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().getDefaultInstance());
diskInterface.addUpgrade(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().getDefaultInstance());
diskInterface.addUpgrade(RSITEMS.getStackUpgrade().getDefaultInstance());

// Assert
sequence
Expand Down

0 comments on commit a2da917

Please sign in to comment.