From b7e01960ef3f33b1a161eaf47af32a3e1d5c28f2 Mon Sep 17 00:00:00 2001 From: 90 Date: Sun, 17 Nov 2024 15:15:23 +0000 Subject: [PATCH] Switch to Supplier-based inventory --- .../cell/PortableCellWorkbenchInventory.java | 200 ++++++++++++++++++ .../cell/PortableCellWorkbenchMenuHost.java | 186 ++-------------- 2 files changed, 214 insertions(+), 172 deletions(-) create mode 100644 src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchInventory.java diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchInventory.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchInventory.java new file mode 100644 index 00000000..06be6283 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchInventory.java @@ -0,0 +1,200 @@ +package gripe._90.megacells.item.cell; + +import java.util.List; + +import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.ItemContainerContents; +import net.minecraft.world.level.ItemLike; + +import appeng.api.config.CopyMode; +import appeng.api.config.Settings; +import appeng.api.ids.AEComponents; +import appeng.api.inventories.InternalInventory; +import appeng.api.storage.cells.ICellWorkbenchItem; +import appeng.api.upgrades.IUpgradeInventory; +import appeng.api.upgrades.UpgradeInventories; +import appeng.api.util.IConfigManager; +import appeng.blockentity.misc.CellWorkbenchBlockEntity; +import appeng.helpers.externalstorage.GenericStackInv; +import appeng.util.ConfigInventory; +import appeng.util.inv.AppEngInternalInventory; +import appeng.util.inv.InternalInventoryHost; +import appeng.util.inv.filter.IAEItemFilter; + +public class PortableCellWorkbenchInventory extends AppEngInternalInventory implements InternalInventoryHost { + private final ItemStack stack; + + private final GenericStackInv config = + new GenericStackInv(this::onConfigChanged, GenericStackInv.Mode.CONFIG_TYPES, 63); + + public PortableCellWorkbenchInventory(ItemStack stack) { + super(null, 1, 1, new Filter()); + this.stack = stack; + + setHost(this); + setEnableClientEvents(true); + fromItemContainerContents(stack.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY)); + config.readFromList(stack.getOrDefault(AEComponents.EXPORTED_CONFIG_INV, List.of())); + } + + ICellWorkbenchItem getCell() { + if (getStackInSlot(0).isEmpty()) { + return null; + } + + return getStackInSlot(0).getItem() instanceof ICellWorkbenchItem cell ? cell : null; + } + + GenericStackInv getConfig() { + return config; + } + + IConfigManager getConfigManager() { + return IConfigManager.builder(stack) + .registerSetting(Settings.COPY_MODE, CopyMode.CLEAR_ON_REMOVE) + .build(); + } + + private ConfigInventory getCellConfigInventory() { + var cell = getCell(); + return cell != null ? cell.getConfigInventory(getStackInSlot(0)) : null; + } + + @Override + public void onChangeInventory(AppEngInternalInventory inv, int slot) { + var configInventory = getCellConfigInventory(); + + if (configInventory != null) { + if (!configInventory.isEmpty()) { + CellWorkbenchBlockEntity.copy(configInventory, config); + } else { + CellWorkbenchBlockEntity.copy(config, configInventory); + CellWorkbenchBlockEntity.copy(configInventory, config); + } + } else if (getConfigManager().getSetting(Settings.COPY_MODE) == CopyMode.CLEAR_ON_REMOVE) { + config.clear(); + } + } + + @Override + public void saveChangedInventory(AppEngInternalInventory inv) { + saveChanges(); + } + + private void onConfigChanged() { + var c = getCellConfigInventory(); + + if (c != null) { + CellWorkbenchBlockEntity.copy(config, c); + CellWorkbenchBlockEntity.copy(c, config); + } + + saveChanges(); + } + + private void saveChanges() { + stack.set(DataComponents.CONTAINER, toItemContainerContents()); + stack.set(AEComponents.EXPORTED_CONFIG_INV, config.toList()); + } + + @Override + public boolean isClientSide() { + return false; + } + + IUpgradeInventory getCellUpgrades() { + var cell = getCell(); + + if (cell == null) { + return UpgradeInventories.empty(); + } + + if (getStackInSlot(0).isEmpty()) { + return UpgradeInventories.empty(); + } + + var inv = cell.getUpgrades(getStackInSlot(0)); + return inv == null ? UpgradeInventories.empty() : new ProxiedUpgradeInventory(inv, this); + } + + private static class ProxiedUpgradeInventory extends AppEngInternalInventory implements IUpgradeInventory { + private final IUpgradeInventory delegate; + + public ProxiedUpgradeInventory(IUpgradeInventory delegate, InternalInventoryHost host) { + super(host, delegate.size(), 1); + this.delegate = delegate; + } + + @Override + public ItemLike getUpgradableItem() { + return delegate.getUpgradableItem(); + } + + @Override + public int getInstalledUpgrades(ItemLike u) { + return delegate.getInstalledUpgrades(u); + } + + @Override + public int getMaxInstalled(ItemLike u) { + return delegate.getMaxInstalled(u); + } + + @Override + public void readFromNBT(CompoundTag data, String subtag, HolderLookup.Provider registries) { + delegate.readFromNBT(data, subtag, registries); + } + + @Override + public void writeToNBT(CompoundTag data, String subtag, HolderLookup.Provider registries) { + delegate.writeToNBT(data, subtag, registries); + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public ItemStack getStackInSlot(int slotIndex) { + return delegate.getStackInSlot(slotIndex); + } + + @Override + public void setItemDirect(int slotIndex, ItemStack stack) { + delegate.setItemDirect(slotIndex, stack); + onContentsChanged(slotIndex); + } + + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) { + var extracted = delegate.extractItem(slot, amount, simulate); + + if (!simulate && !extracted.isEmpty()) { + onContentsChanged(slot); + } + + return extracted; + } + + @Override + public boolean isItemValid(int slot, ItemStack stack) { + return delegate.isItemValid(slot, stack); + } + + @Override + protected boolean eventsEnabled() { + return true; + } + } + + private static class Filter implements IAEItemFilter { + @Override + public boolean allowInsert(InternalInventory inv, int slot, ItemStack stack) { + return stack.getItem() instanceof ICellWorkbenchItem; + } + } +} diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java index 22a81dfc..e96357fd 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java @@ -1,217 +1,59 @@ package gripe._90.megacells.item.cell; -import java.util.Collections; +import java.util.function.Supplier; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.component.DataComponents; -import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.component.ItemContainerContents; -import net.minecraft.world.level.ItemLike; -import appeng.api.config.CopyMode; -import appeng.api.config.Settings; -import appeng.api.ids.AEComponents; import appeng.api.implementations.menuobjects.ItemMenuHost; import appeng.api.inventories.ISegmentedInventory; import appeng.api.inventories.InternalInventory; import appeng.api.storage.cells.ICellWorkbenchItem; import appeng.api.upgrades.IUpgradeInventory; -import appeng.api.upgrades.UpgradeInventories; import appeng.api.util.IConfigManager; import appeng.api.util.IConfigurableObject; -import appeng.blockentity.misc.CellWorkbenchBlockEntity; import appeng.helpers.IConfigInvHost; import appeng.helpers.externalstorage.GenericStackInv; +import appeng.items.contents.StackDependentSupplier; import appeng.menu.locator.ItemMenuHostLocator; -import appeng.util.ConfigInventory; -import appeng.util.inv.AppEngInternalInventory; -import appeng.util.inv.InternalInventoryHost; /** * See {@link appeng.blockentity.misc.CellWorkbenchBlockEntity} */ public class PortableCellWorkbenchMenuHost extends ItemMenuHost - implements InternalInventoryHost, ISegmentedInventory, IConfigurableObject, IConfigInvHost { - private final AppEngInternalInventory cellInv = new AppEngInternalInventory(this, 1); - private final GenericStackInv config = - new GenericStackInv(this::configChanged, GenericStackInv.Mode.CONFIG_TYPES, 63); + implements ISegmentedInventory, IConfigurableObject, IConfigInvHost { + private final Supplier cellInv = + new StackDependentSupplier<>(this::getItemStack, PortableCellWorkbenchInventory::new); public PortableCellWorkbenchMenuHost(PortableCellWorkbenchItem item, Player player, ItemMenuHostLocator locator) { super(item, player, locator); - cellInv.setEnableClientEvents(true); - cellInv.fromItemContainerContents( - getItemStack().getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY)); - config.readFromList(getItemStack().getOrDefault(AEComponents.EXPORTED_CONFIG_INV, Collections.emptyList())); - } - - public ItemStack getContainedStack() { - return cellInv.getStackInSlot(0); } public ICellWorkbenchItem getCell() { - if (getContainedStack().isEmpty()) { - return null; - } - - return getContainedStack().getItem() instanceof ICellWorkbenchItem cell ? cell : null; + return cellInv.get().getCell(); } - @Override - public InternalInventory getSubInventory(ResourceLocation id) { - return id.equals(ISegmentedInventory.CELLS) ? cellInv : null; - } - - @Override - public void onChangeInventory(AppEngInternalInventory inv, int slot) { - var configInventory = getCellConfigInventory(); - - if (configInventory != null) { - if (!configInventory.isEmpty()) { - CellWorkbenchBlockEntity.copy(configInventory, config); - } else { - CellWorkbenchBlockEntity.copy(config, configInventory); - CellWorkbenchBlockEntity.copy(configInventory, config); - } - } else if (getConfigManager().getSetting(Settings.COPY_MODE) == CopyMode.CLEAR_ON_REMOVE) { - config.clear(); - } - } - - @Override - public void saveChangedInventory(AppEngInternalInventory inv) { - saveChanges(); - } - - private void saveChanges() { - getItemStack().set(DataComponents.CONTAINER, cellInv.toItemContainerContents()); - getItemStack().set(AEComponents.EXPORTED_CONFIG_INV, config.toList()); - } - - private void configChanged() { - var c = getCellConfigInventory(); - - if (c != null) { - CellWorkbenchBlockEntity.copy(config, c); - CellWorkbenchBlockEntity.copy(c, config); - } - - saveChanges(); - } - - private ConfigInventory getCellConfigInventory() { - var cell = getCell(); - - if (cell == null) { - return null; - } - - var is = cellInv.getStackInSlot(0); - - if (is.isEmpty()) { - return null; - } - - return cell.getConfigInventory(is); + ItemStack getContainedStack() { + return cellInv.get().getStackInSlot(0); } @Override public IConfigManager getConfigManager() { - return IConfigManager.builder(this::getItemStack) - .registerSetting(Settings.COPY_MODE, CopyMode.CLEAR_ON_REMOVE) - .build(); + return cellInv.get().getConfigManager(); } @Override public GenericStackInv getConfig() { - return config; + return cellInv.get().getConfig(); } public IUpgradeInventory getCellUpgrades() { - var cell = getCell(); - - if (cell == null) { - return UpgradeInventories.empty(); - } - - if (getContainedStack().isEmpty()) { - return UpgradeInventories.empty(); - } - - var inv = cell.getUpgrades(getContainedStack()); - return inv == null ? UpgradeInventories.empty() : new ProxiedUpgradeInventory(inv, this); + return cellInv.get().getCellUpgrades(); } - private static class ProxiedUpgradeInventory extends AppEngInternalInventory implements IUpgradeInventory { - private final IUpgradeInventory delegate; - - public ProxiedUpgradeInventory(IUpgradeInventory inventory, InternalInventoryHost host) { - super(host, inventory.size(), 1); - delegate = inventory; - } - - @Override - public ItemLike getUpgradableItem() { - return delegate.getUpgradableItem(); - } - - @Override - public int getInstalledUpgrades(ItemLike u) { - return delegate.getInstalledUpgrades(u); - } - - @Override - public int getMaxInstalled(ItemLike u) { - return delegate.getMaxInstalled(u); - } - - @Override - public void readFromNBT(CompoundTag data, String subtag, HolderLookup.Provider registries) { - delegate.readFromNBT(data, subtag, registries); - } - - @Override - public void writeToNBT(CompoundTag data, String subtag, HolderLookup.Provider registries) { - delegate.writeToNBT(data, subtag, registries); - } - - @Override - public int size() { - return delegate.size(); - } - - @Override - public ItemStack getStackInSlot(int slotIndex) { - return delegate.getStackInSlot(slotIndex); - } - - @Override - public void setItemDirect(int slotIndex, ItemStack stack) { - delegate.setItemDirect(slotIndex, stack); - onContentsChanged(slotIndex); - } - - @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) { - var extracted = delegate.extractItem(slot, amount, simulate); - - if (!simulate && !extracted.isEmpty()) { - onContentsChanged(slot); - } - - return extracted; - } - - @Override - public boolean isItemValid(int slot, ItemStack stack) { - return delegate.isItemValid(slot, stack); - } - - @Override - protected boolean eventsEnabled() { - return true; - } + @Override + public InternalInventory getSubInventory(ResourceLocation id) { + return id.equals(ISegmentedInventory.CELLS) ? cellInv.get() : null; } }