From 5601b26dc2fc9ac81d7f308ca8c8853391def27e Mon Sep 17 00:00:00 2001 From: Raoul Van den Berge Date: Sun, 31 Jan 2016 14:26:09 +0100 Subject: [PATCH] external storage has nice storage GUI now too --- .../java/storagecraft/StorageCraftGUI.java | 1 - .../block/BlockExternalStorage.java | 2 +- .../container/ContainerExternalStorage.java | 13 -- .../storagecraft/gui/GuiExternalStorage.java | 43 ----- .../java/storagecraft/gui/GuiHandler.java | 6 +- .../network/MessageStoragePriorityUpdate.java | 5 + .../tile/TileExternalStorage.java | 172 +++++++++++++++--- .../storagecraft/util/InventoryUtils.java | 17 ++ .../textures/gui/external_storage.png | Bin 1788 -> 0 bytes 9 files changed, 168 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/storagecraft/container/ContainerExternalStorage.java delete mode 100644 src/main/java/storagecraft/gui/GuiExternalStorage.java delete mode 100644 src/main/resources/assets/storagecraft/textures/gui/external_storage.png diff --git a/src/main/java/storagecraft/StorageCraftGUI.java b/src/main/java/storagecraft/StorageCraftGUI.java index 5e305be63..c822298c0 100644 --- a/src/main/java/storagecraft/StorageCraftGUI.java +++ b/src/main/java/storagecraft/StorageCraftGUI.java @@ -5,7 +5,6 @@ public final class StorageCraftGUI public static final int CONTROLLER = 0; public static final int GRID = 1; public static final int DRIVE = 2; - public static final int EXTERNAL_STORAGE = 3; public static final int IMPORTER = 4; public static final int EXPORTER = 5; public static final int DETECTOR = 6; diff --git a/src/main/java/storagecraft/block/BlockExternalStorage.java b/src/main/java/storagecraft/block/BlockExternalStorage.java index 0f901712d..2078e26ad 100644 --- a/src/main/java/storagecraft/block/BlockExternalStorage.java +++ b/src/main/java/storagecraft/block/BlockExternalStorage.java @@ -28,7 +28,7 @@ public class BlockExternalStorage extends BlockMachine { if (!world.isRemote) { - player.openGui(StorageCraft.INSTANCE, StorageCraftGUI.EXTERNAL_STORAGE, world, pos.getX(), pos.getY(), pos.getZ()); + player.openGui(StorageCraft.INSTANCE, StorageCraftGUI.STORAGE, world, pos.getX(), pos.getY(), pos.getZ()); } return true; diff --git a/src/main/java/storagecraft/container/ContainerExternalStorage.java b/src/main/java/storagecraft/container/ContainerExternalStorage.java deleted file mode 100644 index 5ef928417..000000000 --- a/src/main/java/storagecraft/container/ContainerExternalStorage.java +++ /dev/null @@ -1,13 +0,0 @@ -package storagecraft.container; - -import net.minecraft.entity.player.EntityPlayer; - -public class ContainerExternalStorage extends ContainerBase -{ - public ContainerExternalStorage(EntityPlayer player) - { - super(player); - - addPlayerInventory(8, 50); - } -} diff --git a/src/main/java/storagecraft/gui/GuiExternalStorage.java b/src/main/java/storagecraft/gui/GuiExternalStorage.java deleted file mode 100644 index 421a1b81e..000000000 --- a/src/main/java/storagecraft/gui/GuiExternalStorage.java +++ /dev/null @@ -1,43 +0,0 @@ -package storagecraft.gui; - -import storagecraft.container.ContainerExternalStorage; -import storagecraft.gui.sidebutton.SideButtonRedstoneMode; -import storagecraft.tile.TileExternalStorage; - -public class GuiExternalStorage extends GuiBase -{ - private TileExternalStorage externalStorage; - - public GuiExternalStorage(ContainerExternalStorage container, TileExternalStorage externalStorage) - { - super(container, 176, 131); - - this.externalStorage = externalStorage; - } - - @Override - public void init(int x, int y) - { - addSideButton(new SideButtonRedstoneMode(externalStorage)); - } - - @Override - public void update(int x, int y) - { - } - - @Override - public void drawBackground(int x, int y, int mouseX, int mouseY) - { - bindTexture("gui/external_storage.png"); - - drawTexture(x, y, 0, 0, xSize, ySize); - } - - @Override - public void drawForeground(int mouseX, int mouseY) - { - drawString(7, 7, t("gui.storagecraft:external_storage")); - drawString(7, 39, t("container.inventory")); - } -} diff --git a/src/main/java/storagecraft/gui/GuiHandler.java b/src/main/java/storagecraft/gui/GuiHandler.java index 46f169d89..7cbff3c0b 100644 --- a/src/main/java/storagecraft/gui/GuiHandler.java +++ b/src/main/java/storagecraft/gui/GuiHandler.java @@ -8,8 +8,8 @@ import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; import storagecraft.StorageCraftGUI; import storagecraft.container.*; -import storagecraft.tile.*; import storagecraft.storage.IStorageGui; +import storagecraft.tile.*; public class GuiHandler implements IGuiHandler { @@ -23,8 +23,6 @@ public class GuiHandler implements IGuiHandler return new ContainerGrid(player, (TileGrid) tile); case StorageCraftGUI.DRIVE: return new ContainerDrive(player, (TileDrive) tile); - case StorageCraftGUI.EXTERNAL_STORAGE: - return new ContainerExternalStorage(player); case StorageCraftGUI.IMPORTER: return new ContainerImporter(player, (TileImporter) tile); case StorageCraftGUI.EXPORTER: @@ -65,8 +63,6 @@ public class GuiHandler implements IGuiHandler return new GuiGrid((ContainerGrid) getContainer(ID, player, tile), (TileGrid) tile); case StorageCraftGUI.DRIVE: return new GuiDrive((ContainerDrive) getContainer(ID, player, tile), (TileDrive) tile); - case StorageCraftGUI.EXTERNAL_STORAGE: - return new GuiExternalStorage((ContainerExternalStorage) getContainer(ID, player, tile), (TileExternalStorage) tile); case StorageCraftGUI.IMPORTER: return new GuiImporter((ContainerImporter) getContainer(ID, player, tile), (TileImporter) tile); case StorageCraftGUI.EXPORTER: diff --git a/src/main/java/storagecraft/network/MessageStoragePriorityUpdate.java b/src/main/java/storagecraft/network/MessageStoragePriorityUpdate.java index 4681f4f57..f16faa2c3 100644 --- a/src/main/java/storagecraft/network/MessageStoragePriorityUpdate.java +++ b/src/main/java/storagecraft/network/MessageStoragePriorityUpdate.java @@ -5,6 +5,7 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import storagecraft.tile.TileExternalStorage; import storagecraft.tile.TileStorage; public class MessageStoragePriorityUpdate extends MessageHandlerPlayerToServer implements IMessage @@ -53,5 +54,9 @@ public class MessageStoragePriorityUpdate extends MessageHandlerPlayerToServer items) { - IInventory inventory = getInventory(); + IInventory connectedInventory = getConnectedInventory(); - if (inventory != null) + if (connectedInventory != null) { - for (int i = 0; i < inventory.getSizeInventory(); ++i) + for (int i = 0; i < connectedInventory.getSizeInventory(); ++i) { - if (inventory.getStackInSlot(i) != null) + if (connectedInventory.getStackInSlot(i) != null) { - items.add(new StorageItem(inventory.getStackInSlot(i))); + items.add(new StorageItem(connectedInventory.getStackInSlot(i))); } } } @@ -54,31 +59,31 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider @Override public void push(ItemStack stack) { - IInventory inventory = getInventory(); + IInventory connectedInventory = getConnectedInventory(); - if (inventory == null) + if (connectedInventory == null) { return; } - InventoryUtils.pushToInventory(inventory, stack); + InventoryUtils.pushToInventory(connectedInventory, stack); } @Override public ItemStack take(ItemStack stack, int flags) { - IInventory inventory = getInventory(); + IInventory connectedInventory = getConnectedInventory(); - if (inventory == null) + if (connectedInventory == null) { return null; } int quantity = stack.stackSize; - for (int i = 0; i < inventory.getSizeInventory(); ++i) + for (int i = 0; i < connectedInventory.getSizeInventory(); ++i) { - ItemStack slot = inventory.getStackInSlot(i); + ItemStack slot = connectedInventory.getStackInSlot(i); if (slot != null && InventoryUtils.compareStack(slot, stack, flags)) { @@ -91,7 +96,7 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider if (slot.stackSize == 0) { - inventory.setInventorySlotContents(i, null); + connectedInventory.setInventorySlotContents(i, null); } ItemStack newItem = slot.copy(); @@ -108,21 +113,78 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider @Override public boolean canPush(ItemStack stack) { - IInventory inventory = getInventory(); + IInventory connectedInventory = getConnectedInventory(); - if (inventory == null) + if (connectedInventory == null) { return false; } - return InventoryUtils.canPushToInventory(inventory, stack); + return InventoryUtils.canPushToInventory(connectedInventory, stack); + } + + public IInventory getConnectedInventory() + { + TileEntity tile = worldObj.getTileEntity(pos.offset(getDirection())); + + if (tile instanceof IInventory) + { + return (IInventory) tile; + } + + return null; + } + + @Override + public void toBytes(ByteBuf buf) + { + super.toBytes(buf); + + buf.writeInt(priority); + buf.writeInt(getConnectedInventory() == null ? 0 : InventoryUtils.getInventoryItems(getConnectedInventory())); + } + + @Override + public void fromBytes(ByteBuf buf) + { + super.fromBytes(buf); + + priority = buf.readInt(); + stored = buf.readInt(); + } + + @Override + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + + InventoryUtils.restoreInventory(inventory, nbt); + + if (nbt.hasKey(NBT_PRIORITY)) + { + priority = nbt.getInteger(NBT_PRIORITY); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + + InventoryUtils.saveInventory(inventory, nbt); + + nbt.setInteger(NBT_PRIORITY, priority); } @Override public int getPriority() { - // @TODO: Priority on this stuff - return 0; + return priority; + } + + public void setPriority(int priority) + { + this.priority = priority; } @Override @@ -130,4 +192,58 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider { storages.add(this); } + + @Override + public String getName() + { + return "gui.storagecraft:external_storage"; + } + + @Override + public IStorage getStorage() + { + return this; + } + + @Override + public IRedstoneModeSetting getRedstoneModeSetting() + { + return this; + } + + @Override + public int getStored() + { + return stored; + } + + @Override + public int getCapacity() + { + if (getConnectedInventory() == null) + { + return 0; + } + + return getConnectedInventory().getSizeInventory() * 64; + } + + @Override + public IPriorityHandler getPriorityHandler() + { + return new IPriorityHandler() + { + @Override + public void onPriorityChanged(int priority) + { + StorageCraft.NETWORK.sendToServer(new MessageStoragePriorityUpdate(pos, priority)); + } + }; + } + + @Override + public IInventory getInventory() + { + return inventory; + } } diff --git a/src/main/java/storagecraft/util/InventoryUtils.java b/src/main/java/storagecraft/util/InventoryUtils.java index 5a6ccfbc8..de68806f5 100644 --- a/src/main/java/storagecraft/util/InventoryUtils.java +++ b/src/main/java/storagecraft/util/InventoryUtils.java @@ -199,6 +199,23 @@ public class InventoryUtils return toGo == 0; } + public static int getInventoryItems(IInventory inventory) + { + int size = 0; + + for (int i = 0; i < inventory.getSizeInventory(); ++i) + { + ItemStack slot = inventory.getStackInSlot(i); + + if (slot != null) + { + size += slot.stackSize; + } + } + + return size; + } + public static boolean compareStack(ItemStack first, ItemStack second) { return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE | COMPARE_QUANTITY); diff --git a/src/main/resources/assets/storagecraft/textures/gui/external_storage.png b/src/main/resources/assets/storagecraft/textures/gui/external_storage.png deleted file mode 100644 index c0c97a66ec2dc4b9a32b246d39f270a9ea63f774..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1788 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5D>38$lZxy-8q?;Kn_c~qpu?a z!^VE@KZ&di49pAxJ|V6^adUI?(9qEC?(Sp9j{X1tA1E>kMnhnPgh10{aTQRmDGBlm z1}6TI3>bpHv#SE5owL9rvY3H^?=T269?xHq0u+=eag8Vm&QB{TPb^AhC`ioAE78kK zEm1JhGte{p)02Fkfq^O1)5S5QBJS<&i&?iFBw7Nm_TB$K@$RmfQHtTm-uQ1m^OJ+; zP3sLA7DmUu-+yl`|NX1)cj4{jjE%|5@Bja{G46r>f#Z6ydZ$_C7%G3U0ShyQ?{Q+F zWccKzGz$Ym!=36#5O2crd;uU;^gb0--Z{+M2Q1SVPVAiyEYcWoYh?QHy88N?oAq`x z*>=>u{&v|9q#6Wve%}OP0LgXp|1bEzbbgr;&xH4S>CN}cHl^#&^6}jb3Xje`}St(