Posted August 13, 201510 yr Hi I want to make an Entity (a Minecart) with a Inventory, but the inventory is always empty on Client side. I wrote a Sync Packet for my Entity but the Minecart does not keep the data. Minecart Code: public class ComputerCart extends EntityMinecart implements ISyncEntity, IInventory{ private boolean firstUpdate = true; private int tier = -1; private ItemStack[] inv = new ItemStack[20]; public ComputerCart(World p_i1712_1_) { super(p_i1712_1_); } public ComputerCart(World w, double x, double y, double z, Iterable<Pair<Integer, ItemStack>> components, int tier) { super(w,x,y,z); this.tier=tier; Iterator<Pair<Integer, ItemStack>> list = components.iterator(); while(list.hasNext()){ Pair<Integer, ItemStack> pair = list.next(); if(pair.getKey() < 20 && pair.getValue() != null){ inv[pair.getKey()] = pair.getValue(); } } } public void readEntityFromNBT(NBTTagCompound nbt){ super.readEntityFromNBT(nbt); this.loadInventory((NBTTagList) nbt.getTag("inventory")); } public void writeEntityToNBT(NBTTagCompound nbt){ super.writeEntityToNBT(nbt); nbt.setTag("inventory", this.storeInventory()); } private NBTTagList storeInventory(){ NBTTagList tag = new NBTTagList(); for(int i=0; i < inv.length; i+=1){ NBTTagCompound invslot = new NBTTagCompound(); NBTTagCompound invstack = new NBTTagCompound(); invslot.setInteger("slot", i); if(inv[i] != null) inv[i].writeToNBT(invstack); invslot.setTag("stack", invstack); tag.appendTag(invslot); } return tag; } private void loadInventory(NBTTagList list){ for(int i=0; i < list.tagCount(); i+=1){ NBTTagCompound invslot = list.getCompoundTagAt(i); NBTTagCompound invstack = invslot.getCompoundTag("stack"); int slot = invslot.getInteger("slot"); if(slot<20){ inv[slot] = ItemStack.loadItemStackFromNBT(invstack); } } } public void onUpdate(){ if(this.firstUpdate){ this.firstUpdate=false; if(!this.worldObj.isRemote) { ModNetwork.sendToNearPlayers(new EntitySyncData(this.worldObj.provider.dimensionId, this.getEntityId()), this.posX, this.posY, this.posZ, this.worldObj); } else if(this.worldObj.isRemote){ ModNetwork.channel.sendToServer(new EntitySyncRequest(this.worldObj.provider.dimensionId, this.getEntityId())); } } } @Override public void writeSyncData(NBTTagCompound nbt) { nbt.setTag("inventory", this.storeInventory()); } @Override public void readSyncData(NBTTagCompound nbt) { this.loadInventory((NBTTagList) nbt.getTag("inventory")); int itemcount = 0; for(int i=0;i<this.inv.length;i+=1){ if(this.inv[i]!=null) itemcount+=1; } OCMinecart.logger.log(Level.INFO, "ItemCount: "+itemcount+" Remote: "+this.worldObj.isRemote); } @Override public int getMinecartType() { return -1; } public static EntityMinecart create(World w, double x, double y, double z, Iterable<Pair<Integer, ItemStack>> components, int tier) { return new ComputerCart(w, x, y, z, components, tier); } public boolean interactFirst(EntityPlayer p){ int itemcount = 0; for(int i=0;i<this.inv.length;i+=1){ if(this.inv[i]!=null) itemcount+=1; } OCMinecart.logger.log(Level.INFO, "ItemCount: "+itemcount+" Remote: "+this.worldObj.isRemote); return true; } @Override public int getSizeInventory() { return inv.length; } @Override public ItemStack getStackInSlot(int slot) { if(slot < inv.length) return inv[slot]; return null; } @Override public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) { return null; } @Override public ItemStack getStackInSlotOnClosing(int p_70304_1_) { return null; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { if(slot<20) this.inv[slot] = stack; } @Override public String getInventoryName() { return null; } @Override public int getInventoryStackLimit() { return 64; } @Override public void markDirty() { // TODO Auto-generated method stub } @Override public boolean isUseableByPlayer(EntityPlayer p_70300_1_) { return true; } @Override public void openInventory() { } @Override public void closeInventory() { } @Override public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) { return true; } } The data from the Sync Packet are correct. Output from the readSyncData() function: [Client thread/INFO] [OC-Minecarts]: ItemCount: 3 Remote: false I don't know why the world is not remote, but the Thread is running on Client Side. Output on RightClick: [Client thread/INFO] [OC-Minecarts]: ItemCount: 0 Remote: true [server thread/INFO] [OC-Minecarts]: ItemCount: 3 Remote: false
August 13, 201510 yr don't do that. Lookup tutorials on creating a block with inventory and guihandler. just substitute the inventory from the block with inventory from the entity. Long time Bukkit & Forge Programmer Happy to try and help
August 13, 201510 yr don't do that. Lookup tutorials on creating a block with inventory and guihandler. just substitute the inventory from the block with inventory from the entity. No, that is not good. You can use the entities ID instead of the GuiID. In you GuiHandler, do if(world.getEntityByID(ID) instanceof yourEntityClass.class){ open GUI in CLIENT and CONTAINER in SERVER here }
August 13, 201510 yr That is another way to do it, but what I said is fine. Depending on what you are doing, there are advantages. Long time Bukkit & Forge Programmer Happy to try and help
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.