diff --git a/src/main/java/vazkii/botania/client/gui/bag/ContainerFlowerBag.java b/src/main/java/vazkii/botania/client/gui/bag/ContainerFlowerBag.java index 5586739aa..4466c61cb 100644 --- a/src/main/java/vazkii/botania/client/gui/bag/ContainerFlowerBag.java +++ b/src/main/java/vazkii/botania/client/gui/bag/ContainerFlowerBag.java @@ -11,16 +11,16 @@ package vazkii.botania.client.gui.bag; import net.minecraft.block.Block; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Inventory; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; import net.minecraft.util.Hand; -import net.minecraftforge.items.CapabilityItemHandler; -import net.minecraftforge.items.IItemHandlerModifiable; -import net.minecraftforge.items.SlotItemHandler; import vazkii.botania.common.block.BlockModFlower; +import vazkii.botania.common.item.ItemFlowerBag; import vazkii.botania.common.item.ModItems; import javax.annotation.Nonnull; @@ -32,6 +32,7 @@ public class ContainerFlowerBag extends Container { } private final ItemStack bag; + public final IInventory flowerBagInv; public ContainerFlowerBag(int windowId, PlayerInventory playerInv, ItemStack bag) { super(ModItems.FLOWER_BAG_CONTAINER, windowId); @@ -39,12 +40,21 @@ public class ContainerFlowerBag extends Container { int j; this.bag = bag; - IItemHandlerModifiable flowerBagInv = (IItemHandlerModifiable) bag.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null); + if (!playerInv.player.world.isRemote) { + flowerBagInv = ItemFlowerBag.getInventory(bag); + } else { + flowerBagInv = new Inventory(16); + } for (i = 0; i < 2; ++i) { for (j = 0; j < 8; ++j) { int k = j + i * 8; - addSlot(new SlotItemHandler(flowerBagInv, k, 17 + j * 18, 26 + i * 18)); + addSlot(new Slot(flowerBagInv, k, 17 + j * 18, 26 + i * 18) { + @Override + public boolean isItemValid(@Nonnull ItemStack stack) { + return ItemFlowerBag.isValid(k, stack); + } + }); } } diff --git a/src/main/java/vazkii/botania/client/gui/bag/GuiFlowerBag.java b/src/main/java/vazkii/botania/client/gui/bag/GuiFlowerBag.java index ae802907a..1fc39d10e 100644 --- a/src/main/java/vazkii/botania/client/gui/bag/GuiFlowerBag.java +++ b/src/main/java/vazkii/botania/client/gui/bag/GuiFlowerBag.java @@ -58,7 +58,7 @@ public class GuiFlowerBag extends ContainerScreen { func_238474_b_(ms, k, l, 0, 0, xSize, ySize); for (Slot slot : container.inventorySlots) { - if (slot instanceof SlotItemHandler && !slot.getHasStack()) { + if (slot.inventory == container.flowerBagInv && !slot.getHasStack()) { DyeColor color = DyeColor.byId(slot.getSlotIndex()); ItemStack stack = new ItemStack(ModBlocks.getFlower(color)); int x = guiLeft + slot.xPos; diff --git a/src/main/java/vazkii/botania/common/item/ItemBackedInventory.java b/src/main/java/vazkii/botania/common/item/ItemBackedInventory.java new file mode 100644 index 000000000..766a3d74d --- /dev/null +++ b/src/main/java/vazkii/botania/common/item/ItemBackedInventory.java @@ -0,0 +1,42 @@ +package vazkii.botania.common.item; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; +import vazkii.botania.common.core.helper.ItemNBTHelper; + +/** + * An inventory that writes into the provided stack's NBT on save + */ +public class ItemBackedInventory extends Inventory { + private static final String TAG_ITEMS = "Items"; + private final ItemStack stack; + + public ItemBackedInventory(ItemStack stack, int expectedSize) { + super(expectedSize); + this.stack = stack; + + ListNBT lst = ItemNBTHelper.getList(stack, TAG_ITEMS, 10, false); + int i = 0; + for (; i < expectedSize && i < lst.size(); i++) { + setInventorySlotContents(i, ItemStack.read(lst.getCompound(i))); + } + } + + @Override + public boolean isUsableByPlayer(PlayerEntity player) { + return !stack.isEmpty(); + } + + @Override + public void markDirty() { + super.markDirty(); + ListNBT list = new ListNBT(); + for (int i = 0; i < getSizeInventory(); i++) { + list.add(getStackInSlot(i).write(new CompoundNBT())); + } + ItemNBTHelper.setList(stack, TAG_ITEMS, list); + } +} diff --git a/src/main/java/vazkii/botania/common/item/ItemFlowerBag.java b/src/main/java/vazkii/botania/common/item/ItemFlowerBag.java index 5d8fa4d0e..b1a17f19b 100644 --- a/src/main/java/vazkii/botania/common/item/ItemFlowerBag.java +++ b/src/main/java/vazkii/botania/common/item/ItemFlowerBag.java @@ -12,6 +12,7 @@ import net.minecraft.block.Block; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Inventory; import net.minecraft.inventory.container.INamedContainerProvider; import net.minecraft.inventory.container.SimpleNamedContainerProvider; import net.minecraft.item.Item; @@ -35,6 +36,7 @@ import net.minecraftforge.items.*; import net.minecraftforge.items.wrapper.InvWrapper; import vazkii.botania.client.gui.bag.ContainerFlowerBag; +import vazkii.botania.common.Botania; import vazkii.botania.common.block.BlockModFlower; import javax.annotation.Nonnull; @@ -46,40 +48,40 @@ public class ItemFlowerBag extends Item { MinecraftForge.EVENT_BUS.addListener(this::onPickupItem); } + public static boolean isValid(int slot, ItemStack stack) { + Block blk = Block.getBlockFromItem(stack.getItem()); + return !stack.isEmpty() + && blk.getClass() == BlockModFlower.class + && slot == ((BlockModFlower) blk).color.getId(); + } + + public static Inventory getInventory(ItemStack stack) { + return new ItemBackedInventory(stack, 16) { + @Override + public boolean isItemValidForSlot(int slot, @Nonnull ItemStack stack) { + return isValid(slot, stack); + } + }; + } + @Nonnull @Override public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT oldCapNbt) { - return new InvProvider(); + return new InvProvider(stack); } - private static class InvProvider implements ICapabilitySerializable { + private static class InvProvider implements ICapabilityProvider { + private final LazyOptional opt; - private final IItemHandler inv = new ItemStackHandler(16) { - @Override - public boolean isItemValid(int slot, @Nonnull ItemStack stack) { - Block blk = Block.getBlockFromItem(stack.getItem()); - return !stack.isEmpty() - && blk.getClass() == BlockModFlower.class - && slot == ((BlockModFlower) blk).color.getId(); - } - }; - private final LazyOptional opt = LazyOptional.of(() -> inv); + private InvProvider(ItemStack stack) { + opt = LazyOptional.of(() -> new InvWrapper(getInventory(stack))); + } @Nonnull @Override public LazyOptional getCapability(@Nonnull Capability capability, @Nullable Direction facing) { return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.orEmpty(capability, opt); } - - @Override - public INBT serializeNBT() { - return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(inv, null); - } - - @Override - public void deserializeNBT(INBT nbt) { - CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(inv, null, nbt); - } } private void onPickupItem(EntityItemPickupEvent event) { @@ -94,14 +96,22 @@ public class ItemFlowerBag extends Item { ItemStack bag = event.getPlayer().inventory.getStackInSlot(i); if (!bag.isEmpty() && bag.getItem() == this) { - IItemHandler bagInv = bag.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseThrow(NullPointerException::new); - - ItemStack result = bagInv.insertItem(color, entityStack, false); - int numPickedUp = entityStack.getCount() - result.getCount(); - - event.getItem().setItem(result); + Inventory bagInv = getInventory(bag); + ItemStack existing = bagInv.getStackInSlot(color); + int newCount = Math.min(existing.getCount() + entityStack.getCount(), + Math.min(existing.getMaxStackSize(), bagInv.getInventoryStackLimit())); + int numPickedUp = newCount - existing.getCount(); if (numPickedUp > 0) { + if (existing.isEmpty()) { + bagInv.setInventorySlotContents(color, entityStack.split(numPickedUp)); + } else { + existing.grow(numPickedUp); + entityStack.shrink(numPickedUp); + } + event.getItem().setItem(entityStack); + bagInv.markDirty(); + event.setCanceled(true); if (!event.getItem().isSilent()) { event.getItem().world.playSound(null, event.getPlayer().getPosX(), event.getPlayer().getPosY(), event.getPlayer().getPosZ(), @@ -150,12 +160,12 @@ public class ItemFlowerBag extends Item { return ActionResultType.FAIL; } - ctx.getItem().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(bagInv -> { - for (int i = 0; i < bagInv.getSlots(); i++) { - ItemStack flower = bagInv.getStackInSlot(i); - ((IItemHandlerModifiable) bagInv).setStackInSlot(i, ItemHandlerHelper.insertItemStacked(tileInv, flower, false)); - } - }); + IInventory bagInv = getInventory(ctx.getItem()); + for (int i = 0; i < bagInv.getSizeInventory(); i++) { + ItemStack flower = bagInv.getStackInSlot(i); + ItemStack rem = ItemHandlerHelper.insertItemStacked(tileInv, flower, false); + bagInv.setInventorySlotContents(i, rem); + } }