Posted March 25, 201411 yr I am having trouble getting my block to behave. The basic gist is you select some elements and hit a button. This then checks the elements you select against some combinations and puts an itemstack in a slot. This all works, however when I try to take the item out of the slot it simply disappears. I've already searched around in other people's posts with similar issues but their cases are too different for me to have found a working solution. I've already established its something to do with packet handling (which I have no experience in). I currently have it set up like so: 1) Button click is detected in GUI class. 2) Calls a method in PacketHandler class with button ID as the parameter 3) This method tries to write an id and the button ID to a dataStream, and then send this packet to the PacketHandler class (as suggested by VSWE in his tutorials) (If it fails it prints that it has failed) 4) The PacketHandler onPacketData method receives this packet and does a switch for the id (currently only 1 case but open for future expansion) 5) Then the button ID is sent to the container class where a switch is done for the button ID 6) This then calls the relevant class which runs the combination check method, and then sets the contents of the relevant container slot. These are the classes that are involved. (Note this was a summer project to learn java which turned into a school coursework project as I couldn't think of anything else to do, therefore the code is probably really messy and I also had to simplify some things (mainly combination check) so try to just ignore those bits) I've only included the bits of code that are relevant to the situation...otherwise it would be cluttered. GuiSciBench // Listens for button click protected void actionPerformed(GuiButton guibutton) { // Checks GUI ID switch (guibutton.id) { case 31: PacketHandler.sendButtonPacket((byte)guibutton.id); break; case 32: PacketHandler.sendButtonPacket((byte)guibutton.id); break; default: addToElementList(guibutton.id); } } PacketHandler package com.atomictim.atomiccraft.network; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import com.atomictim.atomiccraft.Info; import com.atomictim.atomiccraft.client.container.ContainerSciBench; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { ByteArrayDataInput reader = ByteStreams.newDataInput(packet.data); EntityPlayer entityPlayer = (EntityPlayer)player; byte packetID = reader.readByte(); switch (packetID) { case 0: byte buttonID = reader.readByte(); Container container = entityPlayer.openContainer; if(container != null && container instanceof ContainerSciBench) { ContainerSciBench.receiveButtonEvent(buttonID); } break; } } public static void sendButtonPacket(byte id) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { dataStream.writeByte((byte)0); dataStream.writeByte(id); PacketDispatcher.sendPacketToServer(PacketDispatcher.getPacket(Info.CHANNEL, byteStream.toByteArray())); } catch(IOException io) { System.out.println("Failed to send packet"); } } } ContainerSciBench public static void receiveButtonEvent(byte buttonID) { switch (buttonID) { //breakdown case 31: breakdown(); break; //combine case 32: combine(); break; } } public static void breakdown() { IInventory inventory = (IInventory) ContainerSciBench.bench; ItemStack itemstack = inventory.getStackInSlot(0); if (itemstack != null) { if (itemstack.stackSize > 1) { itemstack.stackSize -= 1; } else { inventory.setInventorySlotContents(0, null); itemstack = null; } } } public static void combine() { IInventory inventory = (IInventory) ContainerSciBench.bench; ItemStack itemstack = createNewItem(GuiSciBench.elements); inventory.setInventorySlotContents(1, itemstack); GuiSciBench.elements.clear(); System.out.println("Array list cleared"); } public static ItemStack createNewItem(ArrayList<Integer> elements) { Assume the code I have removed from this bit works because it does (Its too long otherwise) and that an itemstack is returned. } The tutorial of VSWE's I was following originally had some code in the TileEntitySciBench class, but I was told that it should be put in the container class. here is the TileEntity class anyway: TileEntitySciBench package com.atomictim.atomiccraft.tileentity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.tileentity.TileEntity; public class TileEntitySciBench extends TileEntity implements IInventory { // Create new array private ItemStack[] inventory; // Set the size of the array to 2 (allowing 2 new Itemstacks within the // inventory, which will be represented as the slots) public TileEntitySciBench() { inventory = new ItemStack[2]; } // Gets the size of inventory array and sets it as the size of the inventory @Override public int getSizeInventory() { return inventory.length; } // Gets the itemstack currently in the slot of inventory. @Override public ItemStack getStackInSlot(int i) { return inventory[i]; } // Handles decreasing the size of the stack in the slot. For example when // the player right clicks a stack, it halves it into 2 stacks. @Override public ItemStack decrStackSize(int slot, int count) { ItemStack itemstack = getStackInSlot(slot); if (itemstack != null) { if (itemstack.stackSize <= count) { setInventorySlotContents(slot, null); } else { itemstack = itemstack.splitStack(count); onInventoryChanged(); } } return itemstack; } // Creates new itemstacks for each slot of the inventory then sets the // existing stacks to null. @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack itemstack = getStackInSlot(slot); setInventorySlotContents(slot, null); return itemstack; } @Override public void setInventorySlotContents(int slot, ItemStack itemstack) { inventory[slot] = itemstack; if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) { itemstack.stackSize = getInventoryStackLimit(); } onInventoryChanged(); } // Sets a localized name for the inventory @Override public String getInvName() { return "Scientific Workbench"; } // Ensures the inventory name set above is localized @Override public boolean isInvNameLocalized() { return true; } // Sets the stack size limit to 64, which is the maximum size of a stack @Override public int getInventoryStackLimit() { return 64; } // Checks within a radius using pythagoras to check if the player can use // the inventory. @Override public boolean isUseableByPlayer(EntityPlayer player) { return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64; } @Override public void openChest() { } @Override public void closeChest() { } // Allows every item to be used in every slot. @Override public boolean isItemValidForSlot(int i, ItemStack itemstack) { return true; } // Saves the contents of the tile entity to an NBT file linked with the tile // entity. @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); NBTTagList list = new NBTTagList(); for (int i = 0; i < getSizeInventory(); i++) { ItemStack itemstack = getStackInSlot(i); if (itemstack != null) { NBTTagCompound item = new NBTTagCompound(); item.setByte("SlotSciBench", (byte) i); itemstack.writeToNBT(item); list.appendTag(item); } } compound.setTag("ItemsSciBench", list); } // Sets the contents of a tile entity from an NBT file linked with the tile // entity. @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); NBTTagList list = compound.getTagList("ItemsSciBench"); for (int i = 0; i < list.tagCount(); i++) { NBTTagCompound item = (NBTTagCompound) list.tagAt(i); int slot = item.getByte("SlotSciBench"); if (slot >= 0 && slot < getSizeInventory()) { setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item)); } } } @Override public Packet getDescriptionPacket() { NBTTagCompound nbtTag = new NBTTagCompound(); this.writeToNBT(nbtTag); return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag); } @Override public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) { readFromNBT(packet.data); } } Any help would be greatly appreciated as this has been bugging me for 5 months now (haven't been trying to solve it all the time but I feel as though I should solve it) Thanks! (Say if anything else is needed)
March 26, 201411 yr Author Ok that makes so much more sense now. So, if I understand correctly, I currently have calls to static methods which will not work with Minecraft as it will create a new instance, right? So what I need to do is get the existing instance and run the method there correct? Now I have one small problem here...as much as I'd like to say oh yeah I can do that, I can't. If you could give me any pointers that would really help me out. Thanks.
March 26, 201411 yr Author ahhk I get it now. Yeah there is one in my packethandler I'll use that. thanks!
March 26, 201411 yr Author Sorry for being so retarded (got a whacking headache so not on top form mentally right now) but I think I've done it right yet the problem of items disappearing persists. I now have this in my PacketHandler class: case 0: byte buttonID = reader.readByte(); Container container = entityPlayer.openContainer; if(container != null && container instanceof ContainerSciBench) { ((ContainerSciBench) container).receiveButtonEvent(buttonID); } break; } This is the only thing I can seem to get to work without an error, but inevitably I'm still doing something wrong. If you could give me a little push in the right direction or just kick me right down the track that would be epic...really starting to get tired of this problem
March 26, 201411 yr Author oh shit sorry..here you go: http://pastebin.com/nz4w3DLm (ignore the random useless comments which were for coursework)
March 26, 201411 yr Author didn't notice that bit as i mentioned, I'm not thinking straight right now
March 26, 201411 yr Author dude..you are an absolute god..thank you so much for your help...I had a feeling the problem was so simple...too simple for a beginner to notice (at times)...and its been bugging me for ages...thank you
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.