diff options
Diffstat (limited to 'src/main/java')
7 files changed, 292 insertions, 37 deletions
diff --git a/src/main/java/gmail/Lance5057/blocks/CrestMount.java b/src/main/java/gmail/Lance5057/blocks/CrestMount.java index c2b6326..9675433 100644 --- a/src/main/java/gmail/Lance5057/blocks/CrestMount.java +++ b/src/main/java/gmail/Lance5057/blocks/CrestMount.java @@ -45,7 +45,7 @@ public class CrestMount extends BlockContainer { return false; } //code to open gui explained later - player.openGui(mod_TinkersDefense.instance, mod_TinkersDefense.GUI_ITEM_INV, world, x, y, z); + player.openGui(mod_TinkersDefense.instance, mod_TinkersDefense.GUI_ITEM_INV, player.worldObj, x, y, z); return true; } diff --git a/src/main/java/gmail/Lance5057/containers/Container_CrestMount.java b/src/main/java/gmail/Lance5057/containers/Container_CrestMount.java index 06ecb5e..46ece0b 100644 --- a/src/main/java/gmail/Lance5057/containers/Container_CrestMount.java +++ b/src/main/java/gmail/Lance5057/containers/Container_CrestMount.java @@ -2,17 +2,163 @@ package gmail.Lance5057.containers; import gmail.Lance5057.tileentities.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 { private static final int INV_START = TileEntity_CrestMount.invSize, INV_END = INV_START+26, HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8; + public final TileEntity_CrestMount inventory; + + public Container_CrestMount(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, TileEntity_CrestMount TileEntity_CrestMount) + { + this.inventory = TileEntity_CrestMount; + + int i; + + // ITEM INVENTORY - you'll need to adjust the slot locations to match your texture file + // I have them set vertically in columns of 4 to the right of the player model + for (i = 0; i < TileEntity_CrestMount.invSize; ++i) + { + // You can make a custom Slot if you need different behavior, + // such as only certain item types can be put into this slot + // We made a custom slot to prevent our inventory-storing item + // from being stored within itself, but if you want to allow that and + // you followed my advice at the end of the above step, then you + // could get away with using the vanilla Slot class + this.addSlotToContainer(new Slot(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(i%4)))); + } + + // If you want, you can add ARMOR SLOTS here as well, but you need to + // make a public version of SlotArmor. I won't be doing that in this tutorial. + /* + for (i = 0; i < 4; ++i) + { + // These are the standard positions for survival inventory layout + this.addSlotToContainer(new SlotArmor(this.player, inventoryPlayer, inventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18, i)); + } + */ + + // PLAYER INVENTORY - uses default locations for standard inventory texture file + for (i = 0; i < 3; ++i) + { + for (int j = 0; j < 9; ++j) + { + this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + + // PLAYER ACTION BAR - uses default locations for standard action bar texture file + for (i = 0; i < 9; ++i) + { + this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142)); + } + } + @Override - public boolean canInteractWith(EntityPlayer p_75145_1_) { - // TODO Auto-generated method stub - return false; + public boolean canInteractWith(EntityPlayer player) { + // be sure to return the inventory's isUseableByPlayer method + // if you defined special behavior there: + return inventory.isUseableByPlayer(player); } + /** + * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. + * Only real change we make to this is to set needsUpdate to true at the end + */ + public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) + { + ItemStack itemstack = null; + Slot slot = (Slot) this.inventorySlots.get(par2); + + if (slot != null && slot.getHasStack()) + { + ItemStack itemstack1 = slot.getStack(); + itemstack = itemstack1.copy(); + + // If item is in our custom Inventory or armor slot + if (par2 < INV_START) + { + // try to place in player inventory / action bar + if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true)) + { + return null; + } + + slot.onSlotChange(itemstack1, itemstack); + } + // Item is in inventory / hotbar, try to place in custom inventory or armor slots + else + { + /* If your inventory only stores certain instances of Items, + * you can implement shift-clicking to your inventory like this: + // Check that the item is the right type + if (itemstack1.getItem() instanceof ItemCustom) + { + // Try to merge into your custom inventory slots + // We use 'InventoryItem.INV_SIZE' instead of INV_START just in case + // you also add armor or other custom slots + if (!this.mergeItemStack(itemstack1, 0, InventoryItem.INV_SIZE, false)) + { + return null; + } + } + // If you added armor slots, check them here as well: + // Item being shift-clicked is armor - try to put in armor slot + if (itemstack1.getItem() instanceof ItemArmor) + { + int type = ((ItemArmor) itemstack1.getItem()).armorType; + if (!this.mergeItemStack(itemstack1, ARMOR_START + type, ARMOR_START + type + 1, false)) + { + return null; + } + } + * Otherwise, you have basically 2 choices: + * 1. shift-clicking between action bar and inventory + * 2. shift-clicking between player inventory and custom inventory + * I've implemented number 1: + */ + // item is in player's inventory, but not in action bar + if (par2 >= INV_START && par2 < HOTBAR_START) + { + // place in action bar + if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END + 1, false)) + { + return null; + } + } + // item in action bar - place in player inventory + else if (par2 >= HOTBAR_START && par2 < HOTBAR_END + 1) + { + if (!this.mergeItemStack(itemstack1, INV_START, INV_END + 1, false)) + { + return null; + } + } + } + + if (itemstack1.stackSize == 0) + { + slot.putStack((ItemStack) null); + } + else + { + slot.onSlotChanged(); + } + + if (itemstack1.stackSize == itemstack.stackSize) + { + return null; + } + + slot.onPickupFromSlot(par1EntityPlayer, itemstack1); + } + + return itemstack; + } } + 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..1ab1356 --- /dev/null +++ b/src/main/java/gmail/Lance5057/gui/Gui_CrestMount.java @@ -0,0 +1,70 @@ +package gmail.Lance5057.gui; + +import gmail.Lance5057.containers.Container_CrestMount; +import gmail.Lance5057.tileentities.TileEntity_CrestMount; + +import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler; + +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.OpenGlHelper; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.client.resources.I18n; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.ResourceLocation; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; + +public class Gui_CrestMount extends GuiContainer +{ +/** x and y size of the inventory window in pixels. Defined as float, passed as int +* These are used for drawing the player model. */ +private float xSize_lo; +private float ySize_lo; + +/** ResourceLocation takes 2 parameters: ModId, path to texture at the location: +* "src/minecraft/assets/modid/" */ +private static final ResourceLocation iconLocation = new ResourceLocation("TileEntity_CrestMountmod", "textures/gui/TileEntity_CrestMount.png"); + +/** The inventory to render on screen */ +private final TileEntity_CrestMount inventory; + +public Gui_CrestMount(Container_CrestMount Container_CrestMount) +{ +super(Container_CrestMount); +this.inventory = Container_CrestMount.inventory; +} + +/** +* Draws the screen and all the components in it. +*/ +public void drawScreen(int par1, int par2, float par3) +{ +super.drawScreen(par1, par2, par3); +this.xSize_lo = (float)par1; +this.ySize_lo = (float)par2; +} + +/** +* Draw the foreground layer for the GuiContainer (everything in front of the items) +*/ +protected void drawGuiContainerForegroundLayer(int par1, int par2) +{ + String s = inventory.hasCustomInventoryName() ? inventory.getInventoryName() : I18n.format(inventory.getInventoryName()); + this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 0, 4210752); + this.fontRendererObj.drawString(I18n.format("container.inventory"), 26, this.ySize - 96 + 4, 4210752); +} + +/** +* Draw the background layer for the GuiContainer (everything behind the items) +*/ +protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) +{ +GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); +this.mc.getTextureManager().bindTexture(iconLocation); +int k = (this.width - this.xSize) / 2; +int l = (this.height - this.ySize) / 2; +this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); +} +}
\ No newline at end of file diff --git a/src/main/java/gmail/Lance5057/network/Message_CrestMount.java b/src/main/java/gmail/Lance5057/network/Message_CrestMount.java new file mode 100644 index 0000000..f556a31 --- /dev/null +++ b/src/main/java/gmail/Lance5057/network/Message_CrestMount.java @@ -0,0 +1,57 @@ +package gmail.Lance5057.network; + +import net.minecraft.item.Item; +import gmail.Lance5057.tileentities.TileEntity_CrestMount; +import io.netty.buffer.ByteBuf; +import cpw.mods.fml.common.network.simpleimpl.IMessage; + +public class Message_CrestMount implements IMessage +{ + public int x,y,z; + public int[] inv; + + Message_CrestMount() {} + + Message_CrestMount(TileEntity_CrestMount te) + { + this.x = te.xCoord; + this.y = te.yCoord; + this.z = te.zCoord; + + inv = new int[te.invSize*3]; + for(int i = 0; i<te.invSize; i+=3) + { + if(te.inventory[i]!=null) + { + inv[i] = Item.getIdFromItem(te.inventory[i].getItem()); + inv[i+1] = te.inventory[i].getItemDamage(); + inv[i+2] = te.inventory[i].stackSize; + } + } + } + + @Override + public void fromBytes(ByteBuf buf) { + x = buf.readInt(); + y = buf.readInt(); + z = buf.readInt(); + + for(int i = 0; i<inv.length; i++) + { + inv[i] = buf.readInt(); + } + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(x); + buf.writeInt(y); + buf.writeInt(z); + + for(int i = 0; i<inv.length; i++) + { + buf.writeInt(inv[i]); + } + } + +} diff --git a/src/main/java/gmail/Lance5057/proxy/ClientProxy.java b/src/main/java/gmail/Lance5057/proxy/ClientProxy.java index 0ddabce..7c78794 100644 --- a/src/main/java/gmail/Lance5057/proxy/ClientProxy.java +++ b/src/main/java/gmail/Lance5057/proxy/ClientProxy.java @@ -1,6 +1,8 @@ package gmail.Lance5057.proxy; import gmail.Lance5057.blocks.Renderer_CrestMount; +import gmail.Lance5057.containers.Container_CrestMount; +import gmail.Lance5057.gui.Gui_CrestMount; import gmail.Lance5057.items.ModelTinkerArmor; import gmail.Lance5057.tileentities.TileEntity_CrestMount; import net.minecraft.client.model.ModelBiped; @@ -11,7 +13,8 @@ import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; -public class ClientProxy extends CommonProxy { +public class ClientProxy extends CommonProxy +{ private static final ModelTinkerArmor tutChest = new ModelTinkerArmor(1.0f); @Override @@ -19,16 +22,7 @@ public class ClientProxy extends CommonProxy { // This is for rendering entities and so forth later on ClientRegistry.bindTileEntitySpecialRenderer(TileEntity_CrestMount.class, new Renderer_CrestMount()); } - @Override - public World getClientWorld() - { - return FMLClientHandler.instance().getClient().theWorld; - } - public void registerTileEntitySpecialRenderer() - { - - } @Override public ModelBiped getArmorModel(int id) { @@ -39,19 +33,4 @@ public class ClientProxy extends CommonProxy { } return tutChest; //default, if whenever you should have passed on a wrong id } - - @Override - public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) - { - TileEntity te = world.getTileEntity(x, y, z); - if (te != null && te instanceof TileEntity_CrestMount) - { - return null; - //return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player.inventory, new TileEntity_CrestMount())); - } - else - { - return null; - } - } - }
\ No newline at end of file +}
\ No newline at end of file diff --git a/src/main/java/gmail/Lance5057/proxy/CommonProxy.java b/src/main/java/gmail/Lance5057/proxy/CommonProxy.java index 69d30cc..2171029 100644 --- a/src/main/java/gmail/Lance5057/proxy/CommonProxy.java +++ b/src/main/java/gmail/Lance5057/proxy/CommonProxy.java @@ -1,6 +1,9 @@ package gmail.Lance5057.proxy; import gmail.Lance5057.com.mod_TinkersDefense; +import gmail.Lance5057.containers.Container_CrestMount; +import gmail.Lance5057.gui.Gui_CrestMount; +import gmail.Lance5057.tileentities.TileEntity_CrestMount; import net.minecraft.client.model.ModelBiped; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; @@ -35,20 +38,21 @@ public class CommonProxy implements IGuiHandler { if (ID == mod_TinkersDefense.GUI_ITEM_INV) { // Use the player's held item to create the inventory - //return new Container_CrestMount(player.inventory, new TileEntity_CrestMount()); + return new Container_CrestMount(player, player.inventory, new TileEntity_CrestMount()); } return null; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, - World world, int x, int y, int z) { + World world, int x, int y, int z) + { if (ID == mod_TinkersDefense.GUI_ITEM_INV) { // We have to cast the new container as our custom class // and pass in currently held item for the inventory - //return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player.inventory, new TileEntity_CrestMount())); + return new Gui_CrestMount((Container_CrestMount) new Container_CrestMount(player, player.inventory, new TileEntity_CrestMount())); } return null; - } - }
\ No newline at end of file + } +}
\ No newline at end of file diff --git a/src/main/java/gmail/Lance5057/tileentities/TileEntity_CrestMount.java b/src/main/java/gmail/Lance5057/tileentities/TileEntity_CrestMount.java index 3a325f7..924d982 100644 --- a/src/main/java/gmail/Lance5057/tileentities/TileEntity_CrestMount.java +++ b/src/main/java/gmail/Lance5057/tileentities/TileEntity_CrestMount.java @@ -10,7 +10,7 @@ import net.minecraft.tileentity.TileEntity; public class TileEntity_CrestMount extends TileEntity implements IInventory { public static int invSize = 4; - private ItemStack[] inventory = new ItemStack[invSize]; + public ItemStack[] inventory = new ItemStack[invSize]; private final String name = "Crest Inventory"; @@ -145,5 +145,4 @@ public class TileEntity_CrestMount extends TileEntity implements IInventory } } } - } |
