From d1c6352629b07ecd41803b8cce50b67dfe6146cd Mon Sep 17 00:00:00 2001 From: grimmauld Date: Sat, 12 Sep 2020 16:27:48 +0200 Subject: [PATCH] Creative Crate on contraptions --- .../structureMovement/MountedStorage.java | 10 +++- .../inventories/CreativeCrateInventory.java | 60 +++++++++++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MountedStorage.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MountedStorage.java index 81a43c60e..27e3df9bc 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MountedStorage.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MountedStorage.java @@ -3,6 +3,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement; import com.simibubi.create.AllTileEntities; import com.simibubi.create.content.logistics.block.inventories.AdjustableCrateBlock; +import com.simibubi.create.content.logistics.block.inventories.CreativeCrateInventory; import net.minecraft.block.ChestBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; @@ -85,14 +86,17 @@ public class MountedStorage { public MountedStorage(CompoundNBT nbt) { handler = new ItemStackHandler(); working = nbt != null; - if (working) + if (working) { + if (nbt.contains("isCreativeCrate") && nbt.getBoolean("isCreativeCrate")) + handler = new CreativeCrateInventory(); handler.deserializeNBT(nbt); + } } public void fill(TileEntity te) { IItemHandler teHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) .orElse(dummyHandler); - if (teHandler != dummyHandler && teHandler instanceof IItemHandlerModifiable) { + if (teHandler != dummyHandler && teHandler instanceof IItemHandlerModifiable && !(teHandler instanceof CreativeCrateInventory)) { IItemHandlerModifiable inv = (IItemHandlerModifiable) teHandler; for (int slot = 0; slot < Math.min(inv.getSlots(), handler.getSlots()); slot++) inv.setStackInSlot(slot, handler.getStackInSlot(slot)); @@ -116,6 +120,8 @@ public class MountedStorage { return false; if (AllTileEntities.ADJUSTABLE_CRATE.is(te)) return true; + if (AllTileEntities.CREATIVE_CRATE.is(te)) + return true; if (te instanceof ShulkerBoxTileEntity) return true; if (te instanceof ChestTileEntity) diff --git a/src/main/java/com/simibubi/create/content/logistics/block/inventories/CreativeCrateInventory.java b/src/main/java/com/simibubi/create/content/logistics/block/inventories/CreativeCrateInventory.java index e8da1b86d..64e568da7 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/inventories/CreativeCrateInventory.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/inventories/CreativeCrateInventory.java @@ -1,16 +1,30 @@ package com.simibubi.create.content.logistics.block.inventories; +import mcp.MethodsReturnNonnullByDefault; +import net.minecraft.inventory.ItemStackHelper; import net.minecraft.item.ItemStack; -import net.minecraftforge.items.IItemHandler; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.NonNullList; +import net.minecraftforge.items.ItemStackHandler; -public class CreativeCrateInventory implements IItemHandler { +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; - private CreativeCrateTileEntity te; +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +public class CreativeCrateInventory extends ItemStackHandler { - public CreativeCrateInventory(CreativeCrateTileEntity te) { + private ItemStack filter = null; + private final CreativeCrateTileEntity te; + + public CreativeCrateInventory(@Nullable CreativeCrateTileEntity te) { this.te = te; } + public CreativeCrateInventory() { + this(null); + } + @Override public int getSlots() { return 2; @@ -20,12 +34,13 @@ public class CreativeCrateInventory implements IItemHandler { public ItemStack getStackInSlot(int slot) { if (slot == 1) return ItemStack.EMPTY; - ItemStack filter = te.filter.getFilter().copy(); - if (!filter.isEmpty()) + if (getFilter() == null) + return ItemStack.EMPTY; + if (!getFilter().isEmpty()) filter.setCount(filter.getMaxStackSize()); return filter; } - + @Override public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { return ItemStack.EMPTY; @@ -33,10 +48,11 @@ public class CreativeCrateInventory implements IItemHandler { @Override public ItemStack extractItem(int slot, int amount, boolean simulate) { - ItemStack filter = te.filter.getFilter().copy(); - if (!filter.isEmpty()) - filter.setCount(Math.min(filter.getMaxStackSize(), amount)); - return filter; + if (getFilter() == null) + return ItemStack.EMPTY; + if (!getFilter().isEmpty()) + filter.setCount(Math.min(getFilter().getMaxStackSize(), amount)); + return getFilter(); } @Override @@ -49,4 +65,26 @@ public class CreativeCrateInventory implements IItemHandler { return true; } + @Override + public CompoundNBT serializeNBT() { + CompoundNBT nbt = new CompoundNBT(); + nbt.putBoolean("isCreativeCrate", true); + if (getFilter() != null) + ItemStackHelper.saveAllItems(nbt, NonNullList.from(ItemStack.EMPTY, getFilter())); + return nbt; + } + + @Override + public void deserializeNBT(CompoundNBT nbt) { + NonNullList filterList = NonNullList.withSize(1, ItemStack.EMPTY); + ItemStackHelper.loadAllItems(nbt, filterList); + filter = filterList.get(0); + } + + @Nullable + public ItemStack getFilter() { + if (te != null) + filter = te.filter.getFilter(); + return filter; + } }