From 0fe568fc1800274fa3f0c68a09e0ad8d6b5e3e62 Mon Sep 17 00:00:00 2001 From: Lance5057 Date: Mon, 9 Feb 2015 08:04:46 -0600 Subject: Attempt at making crest mount have an inventory. A failed attempt --- .../gmail/Lance5057/gui/Container_CrestMount.java | 88 ++++++++++++++++++++++ .../gmail/Lance5057/gui/GuiHandler_CrestMount.java | 32 ++++++++ .../java/gmail/Lance5057/gui/Gui_CrestMount.java | 28 +++++++ 3 files changed, 148 insertions(+) create mode 100644 src/main/java/gmail/Lance5057/gui/Container_CrestMount.java create mode 100644 src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java create mode 100644 src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java (limited to 'src/main/java/gmail/Lance5057/gui') diff --git a/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java b/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java new file mode 100644 index 0000000..8db0940 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/Container_CrestMount.java @@ -0,0 +1,88 @@ +package gmail.Lance5057.gui; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class Container_CrestMount extends Container +{ + protected TileEntity_CrestMount tileEntity; + + public Container_CrestMount (InventoryPlayer inventoryPlayer, TileEntity_CrestMount te){ + tileEntity = te; + te.openInventory(); + + //the Slot constructor takes the IInventory and the slot number in that it binds to + //and the x-y coordinates it resides on-screen + for (int i = 0; i < 4; i++) { + addSlotToContainer(new Slot(tileEntity, i, 62 + 18, 17 + i * 18)); + } + + //commonly used vanilla code that adds the player's inventory + bindPlayerInventory(inventoryPlayer); + } + + protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 9; j++) { + addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, + 8 + j * 18, 84 + i * 18)); + } + } + + for (int i = 0; i < 9; i++) { + addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142)); + } +} + + @Override + public boolean canInteractWith(EntityPlayer p_75145_1_) { + return tileEntity.isUseableByPlayer(p_75145_1_); + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slot) { + ItemStack stack = null; + Slot slotObject = (Slot) inventorySlots.get(slot); + + //null checks and checks if the item can be stacked (maxStackSize > 1) + if (slotObject != null && slotObject.getHasStack()) { + ItemStack stackInSlot = slotObject.getStack(); + stack = stackInSlot.copy(); + + //merges the item into player inventory since its in the tileEntity + if (slot < 4) { + if (!this.mergeItemStack(stackInSlot, 0, 35, true)) { + return null; + } + } + //places it into the tileEntity is possible since its in the player inventory + else if (!this.mergeItemStack(stackInSlot, 0, 4, false)) { + return null; + } + + if (stackInSlot.stackSize == 0) { + slotObject.putStack(null); + } else { + slotObject.onSlotChanged(); + } + + if (stackInSlot.stackSize == stack.stackSize) { + return null; + } + slotObject.onPickupFromSlot(player, stackInSlot); + } + return stack; + } + + @Override + public void onContainerClosed(EntityPlayer entityplayer) + { + super.onContainerClosed(entityplayer); + tileEntity.closeInventory(); + } + +} diff --git a/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java b/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java new file mode 100644 index 0000000..604f088 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/GuiHandler_CrestMount.java @@ -0,0 +1,32 @@ +package gmail.Lance5057.gui; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import cpw.mods.fml.common.network.IGuiHandler; + +public class GuiHandler_CrestMount implements IGuiHandler { + //returns an instance of the Container you made earlier + @Override + public Object getServerGuiElement(int id, EntityPlayer player, World world, + int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + if(tileEntity instanceof TileEntity_CrestMount){ + return new Container_CrestMount(player.inventory, (TileEntity_CrestMount) tileEntity); + } + return null; + } + + //returns an instance of the Gui you made earlier + @Override + public Object getClientGuiElement(int id, EntityPlayer player, World world, + int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + if(tileEntity instanceof TileEntity_CrestMount){ + return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player.inventory, new TileEntity_CrestMount())); + } + return null; + + } +} diff --git a/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java b/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java new file mode 100644 index 0000000..221f5c1 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java @@ -0,0 +1,28 @@ +package gmail.Lance5057.gui; + +import org.lwjgl.opengl.GL11; + +import gmail.Lance5057.blocks.TileEntity_CrestMount; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.util.ResourceLocation; + +public class Gui_CrestMount extends GuiContainer { + + private ResourceLocation RL = new ResourceLocation("tinkersdefense", "textures/gui/inv_crestmount.png"); + + public Gui_CrestMount(Container_CrestMount container) { + super(container); + } + + @Override + protected void drawGuiContainerBackgroundLayer(float p_146976_1_, + int p_146976_2_, int p_146976_3_) { + this.mc.renderEngine.bindTexture(RL); + int x = (width - xSize) / 2; + int y = (height - ySize) / 2; + this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize); + + } + +} -- cgit v1.2.3