Skip to content

Commit

Permalink
Merge pull request #705 from refinedmods/fix/GH-703/config-card-stack
Browse files Browse the repository at this point in the history
Configuration card is now stack sensitive
  • Loading branch information
raoulvdberge authored Oct 12, 2024
2 parents cb4d54a + eff65a8 commit 65c6198
Show file tree
Hide file tree
Showing 24 changed files with 65 additions and 58 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 @@ -54,6 +54,7 @@

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -207,4 +208,6 @@ EnergyStorage asBlockItemEnergyStorage(
Optional<Pattern> getPattern(ItemStack stack, Level level);

void openAutocraftingPreview(List<ResourceAmount> requests, @Nullable Object parentScreen);

ResourceLocation getCreativeModeTabId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -408,6 +409,11 @@ public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullab
ensureLoaded().openAutocraftingPreview(requests, parentScreen);
}

@Override
public ResourceLocation getCreativeModeTabId() {
return ensureLoaded().getCreativeModeTabId();
}

private RefinedStorageApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("API not loaded yet");
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 @@ -50,6 +50,7 @@
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReferenceProvider;
import com.refinedmods.refinedstorage.common.api.upgrade.UpgradeRegistry;
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage.common.content.ContentIds;
import com.refinedmods.refinedstorage.common.grid.NoopGridSynchronizer;
import com.refinedmods.refinedstorage.common.grid.screen.hint.GridInsertionHintsImpl;
import com.refinedmods.refinedstorage.common.grid.screen.hint.ItemGridInsertionHint;
Expand Down Expand Up @@ -104,6 +105,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -605,4 +607,9 @@ public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullab
}
ClientPlatformUtil.openCraftingPreview(requests, parentScreen);
}

@Override
public ResourceLocation getCreativeModeTabId() {
return ContentIds.CREATIVE_MODE_TAB;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -230,12 +229,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> upgrades) {
for (final ItemStack upgrade : upgrades) {
final int upgradeIndexInPlayerInventory = player.getInventory().findSlotMatchingItem(upgrade);
if (upgradeIndexInPlayerInventory >= 0 && target.addUpgrade(upgrade)) {
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 @@ -36,7 +36,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 @@ -84,12 +83,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 @@ -9,6 +9,8 @@
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;

public final class ContentIds {
public static final ResourceLocation CREATIVE_MODE_TAB = createIdentifier("general");

public static final ResourceLocation DISK_DRIVE = createIdentifier("disk_drive");
public static final ResourceLocation MACHINE_CASING = createIdentifier("machine_casing");
public static final ResourceLocation CABLE = createIdentifier("cable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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;
import org.slf4j.Logger;
Expand Down Expand Up @@ -87,12 +86,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 @@ -40,7 +40,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;
import org.slf4j.Logger;
Expand Down Expand Up @@ -81,12 +80,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 @@ -27,7 +27,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 @@ -74,12 +73,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 @@ -28,7 +28,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 @@ -109,12 +108,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 @@ -400,7 +400,7 @@ public boolean allowComponentsUpdateAnimation(final Player player,
private void registerCreativeModeTab() {
Registry.register(
BuiltInRegistries.CREATIVE_MODE_TAB,
createIdentifier("general"),
RefinedStorageApi.INSTANCE.getCreativeModeTabId(),
CreativeModeTab.builder(CreativeModeTab.Row.TOP, 0)
.title(ContentNames.MOD)
.icon(() -> new ItemStack(Blocks.INSTANCE.getCreativeController().getDefault()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public void onRegister(final RegisterEvent e) {
helper -> registerLootFunctions(new DirectRegistryCallback<>(BuiltInRegistries.LOOT_FUNCTION_TYPE))
);
e.register(Registries.CREATIVE_MODE_TAB, helper -> helper.register(
createIdentifier("general"),
RefinedStorageApi.INSTANCE.getCreativeModeTabId(),
CreativeModeTab.builder()
.title(ContentNames.MOD)
.icon(() -> new ItemStack(Blocks.INSTANCE.getCreativeController().getDefault()))
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
Loading

0 comments on commit 65c6198

Please sign in to comment.