Neuro Posted September 8, 2014 Posted September 8, 2014 http://cdn.makeagif.com/media/9-08-2014/5wGA2P.gif The gif above illustrates my conundrum: When I click Done! An item should appear in the bottom slot, which works. But, when I grab it, it disappears an instant after being in my hand and the above stacksize increases until I pick that up and then goes back down. The item in the bottom slot is called "reel_written" and the item above is "reel_blank" (They are not the same items and so I do not place the item in the top slot as is the illusion) I suspect it is a syncing problem, but I really don't enjoy the thought of getting into packet handling (besides, people have said that this is already taken care of with guis, maybe I'm/they're wrong, idk) TileEntityMusicDesk Reveal hidden contents package neuro.musicbox.gui; import net.minecraft.client.Minecraft; import net.minecraft.entity.item.EntityItem; 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.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import neuro.musicbox.main.MusicboxCore; public class TileEntityMusicDesk extends TileEntity implements IInventory { String unsavedSong[] = new String[] {"", "", "", ""}; ItemStack[] inventory = new ItemStack[2]; public void writeToNBT(NBTTagCompound par1NBTTagCompound) { super.writeToNBT(par1NBTTagCompound); par1NBTTagCompound.setString("SongLayer1", this.unsavedSong[0]); par1NBTTagCompound.setString("SongLayer2", this.unsavedSong[1]); par1NBTTagCompound.setString("SongLayer3", this.unsavedSong[2]); par1NBTTagCompound.setString("SongLayer4", this.unsavedSong[3]); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.inventory.length; ++i) { if (this.inventory != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.setByte("Slot", (byte)i); this.inventory.writeToNBT(nbttagcompound1); nbttaglist.appendTag(nbttagcompound1); } } par1NBTTagCompound.setTag("Items", nbttaglist); } public void readFromNBT(NBTTagCompound par1NBTTagCompound) { super.readFromNBT(par1NBTTagCompound); this.unsavedSong[0] = par1NBTTagCompound.getString("SongLayer1"); this.unsavedSong[1] = par1NBTTagCompound.getString("SongLayer2"); this.unsavedSong[2] = par1NBTTagCompound.getString("SongLayer3"); this.unsavedSong[3] = par1NBTTagCompound.getString("SongLayer4"); NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items", 10); for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); int j = nbttagcompound1.getByte("Slot") & 255; if (j >= 0 && j < this.inventory.length) { this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } } } public Packet getDescriptionPacket() { NBTTagCompound nbttagcompound = new NBTTagCompound(); writeToNBT(nbttagcompound); return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.blockMetadata, nbttagcompound); } public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { readFromNBT(pkt.func_148857_g()); } public void updateEntity() { super.updateEntity(); } @Override public int getSizeInventory() { return inventory.length; } @Override public ItemStack getStackInSlot(int i) { return inventory; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { if (inventory[slot] != null && stack != null) { stack.stackSize += inventory[slot].stackSize; } inventory[slot] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } } @Override public ItemStack decrStackSize(int slot, int amt) { ItemStack stack = getStackInSlot(slot); if (stack != null) { if (stack.stackSize <= amt) { setInventorySlotContents(slot, null); } else { stack = stack.splitStack(amt); if (stack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return stack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack stack = getStackInSlot(slot); if (stack != null) { setInventorySlotContents(slot, null); } return stack; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64; } @Override public String getInventoryName() { return "gui.musicdesk.name"; } @Override public boolean hasCustomInventoryName() { return false; } @Override public void openInventory() {} @Override public void closeInventory() {} @Override public boolean isItemValidForSlot(int slot, ItemStack itemStack) { return false; } public void createSongReel() { //Minecraft.getMinecraft().thePlayer.closeScreen(); ItemStack songItemStack = new ItemStack(MusicboxCore.reel_written); NBTTagCompound nbttagcompound = new NBTTagCompound(); for (int i = 0; i < 4; i++) { nbttagcompound.setString("SongLayer" + String.valueOf(i), this.unsavedSong); this.unsavedSong = ""; } songItemStack.setTagCompound(nbttagcompound); this.setInventorySlotContents(1, songItemStack); } } ContainerMusicDesk Reveal hidden contents package neuro.musicbox.gui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ContainerBrewingStand; import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import neuro.musicbox.main.MusicboxCore; public class ContainerMusicDesk extends Container { protected TileEntityMusicDesk tileEntity; public ContainerMusicDesk(InventoryPlayer inventoryPlayer, TileEntityMusicDesk temd) { tileEntity = temd; for (int i = 0; i < 2; i++) { addSlotToContainer(new SlotMusicDesk(tileEntity, i, 228, 15 + 28 * i, i == 1)); } for (int l = 0; l < 3; ++l) { for (int i1 = 0; i1 < 9; ++i1) { this.addSlotToContainer(new Slot(inventoryPlayer, i1 + l * 9 + 9, 48 + i1 * 18, 145 + l * 18)); } } for (int l = 0; l < 9; ++l) { this.addSlotToContainer(new Slot(inventoryPlayer, l, 48 + l * 18, 203)); } } @Override public boolean canInteractWith(EntityPlayer player) { return tileEntity.isUseableByPlayer(player); } public ItemStack transferStackInSlot(EntityPlayer player, int slotNum) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(slotNum); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (slotNum == 0) { if (!this.mergeItemStack(itemstack1, 2, 38, false)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else if (slotNum >= 2 && itemstack1.getItem().equals(MusicboxCore.reel_blank)) { if (!this.mergeItemStack(itemstack1, 0, 1, false)) { return null; } } else if (slotNum >= 2 && slotNum < 29) { if (!this.mergeItemStack(itemstack1, 29, 37, false)) { return null; } } else if (slotNum >= 29 && slotNum < 37) { if (!this.mergeItemStack(itemstack1, 2, 29, false)) { return null; } } else if (!this.mergeItemStack(itemstack1, 2, 37, false)) { return null; } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(player, itemstack1); } return itemstack; } } CommonProxy Reveal hidden contents package neuro.musicbox.main; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import neuro.musicbox.gui.ContainerMusicDesk; import neuro.musicbox.gui.GuiMusicDesk; import neuro.musicbox.gui.TileEntityMusicDesk; import cpw.mods.fml.common.network.IGuiHandler; public class CommonProxy implements IGuiHandler { public void registerProxies() {} @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 TileEntityMusicDesk) { return new ContainerMusicDesk(player.inventory, (TileEntityMusicDesk) tileEntity); } return null; } @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 TileEntityMusicDesk) { return new GuiMusicDesk(player.inventory, (TileEntityMusicDesk) tileEntity); } return null; } } GuiMusicBox Reveal hidden contents package neuro.musicbox.gui; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import neuro.musicbox.main.MusicboxCore; import neuro.musicbox.main.TileEntityMusicbox; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; public class GuiMusicDesk extends GuiContainer { private static final ResourceLocation texture = new ResourceLocation("musicbox", "textures/gui/music_desk.png"); private static final float[][] colour = {{1,0,0},{1,0.6F,0},{0,1,0.5F},{0,0.25F,1}}; private TileEntityMusicDesk tileEntity; public GuiMusicDesk(InventoryPlayer inventoryPlayer, TileEntityMusicDesk tileEntity) { //the container is instanciated and passed to the superclass for handling super(new ContainerMusicDesk(inventoryPlayer, tileEntity)); this.tileEntity = tileEntity; this.xSize = 256; this.ySize = 227; } @Override protected void drawGuiContainerForegroundLayer(int param1, int param2) { //draw text and stuff here //the parameters for drawString are: string, x, y, color fontRendererObj.drawString(StatCollector.translateToLocal("Music Desk"), 6, 6, 4210752); //draws "Inventory" or your regional equivalent fontRendererObj.drawString(StatCollector.translateToLocal("container.inventory"), 48, ySize - 96 + 2, 4210752); } @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); //int xSize2 = 256; //int ySize2 = 227; this.mc.renderEngine.bindTexture(texture); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); if (this.tileEntity.inventory[0] != null && this.tileEntity.inventory[0].getItem().equals(MusicboxCore.reel_blank)) { for (int i = 0; i < 4; i++) { this.drawTexturedModalRect(k + 64, l + 7 + 30 * i, 0, 227, 148, 29); final ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight); int ii = scaledresolution.getScaledWidth(); int jj = scaledresolution.getScaledHeight(); final int kk = Mouse.getX() * ii / this.mc.displayWidth; final int ll = jj - Mouse.getY() * jj / this.mc.displayHeight - 1; for (int o = 0; o < 13; o++) { int p = l + 8 + i * 30 + o * 2; if (ll > p && ll <= p + 2 && kk > k + 64 && kk <= k + 212) { GL11.glColor3f(colour[0], colour[1], colour[2]); this.drawTexturedModalRect(k + 64, p + 1, 0, 229, 147, 1); GL11.glColor3f(1, 1, 1); if (Mouse.isButtonDown(0)) { tileEntity.unsavedSong += String.valueOf(TileEntityMusicbox.notes.charAt(o)); } } } } } } /** * Adds the buttons (and other controls) to the screen in question. */ public void initGui() { super.initGui(); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.buttonList.add(new GuiButton(0, k + 8, l + 20, 50, 20, "Play")); this.buttonList.add(new GuiButton(1, k + 8, l + 42, 50, 20, "Pause")); this.buttonList.add(new GuiButton(2, k + 8, l + 64, 50, 20, "Stop")); this.buttonList.add(new GuiButton(3, k + 8, l + 100, 50, 20, "Done!")); } /** * Called from the main game loop to update the screen. */ public void updateScreen() { super.updateScreen(); if (this.tileEntity.inventory[0] != null && this.tileEntity.inventory[0].getItem().equals(MusicboxCore.reel_blank)) { for (int i = 0; i < 3; i++) ((GuiButton) this.buttonList.get(i)).enabled = true; } else { for (int i = 0; i < 3; i++) ((GuiButton) this.buttonList.get(i)).enabled = false; } } protected void actionPerformed(GuiButton button) { switch (button.id) { case 3: { this.tileEntity.createSongReel(); } } } } And in my main mod class, I register the proxy as the guihandler in the initialisation method since it implements IGuiHandler. If anyone knows what is going on, please help me to understand how to fix this issue. I have been googling this problem 3 days and all the relevant links are purple'd out and not one helped. I am new on these forums, so if I have done anything incorrectly, I'd prefer you not to be harsh, just tell me calmly, thanks. Quote
Neuro Posted September 8, 2014 Author Posted September 8, 2014 Ok, thank you. So, this is what I have for my packet. (I followed the forge netty packet example). Reveal hidden contents package neuro.musicbox.netty.packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import net.minecraft.entity.player.EntityPlayer; import neuro.musicbox.gui.TileEntityMusicDesk; public class PacketMB001GivePlayerOutputSong extends AbstractPacket { int x, y, z; public PacketMB001GivePlayerOutputSong(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { buffer.writeInt(x); buffer.writeInt(y); buffer.writeInt(z); } @Override public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) { this.x = buffer.getInt(0); this.y = buffer.getInt(1); this.z = buffer.getInt(2); } @Override public void handleClientSide(EntityPlayer player) { TileEntityMusicDesk te = ((TileEntityMusicDesk) player.worldObj.getTileEntity(x, y, z)); te.createSongReel(); } @Override public void handleServerSide(EntityPlayer player) { TileEntityMusicDesk te = ((TileEntityMusicDesk) player.worldObj.getTileEntity(x, y, z)); te.createSongReel(); } } If I remove the "this.createSOngReel();" in the GuiMusicDesk.class, this will do it for me since I set it to do that on the client side above AND the server side? Here is the modified code: Reveal hidden contents protected void actionPerformed(GuiButton button) { switch (button.id) { case 3: { AbstractPacket packetOfButtonPress = new PacketMB001GivePlayerOutputSong(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord); MusicboxCore.packetPipeline.sendToAll(packetOfButtonPress); } } } I've never used packets before. So I expect to be completely wrong. Quote
Whov Posted September 8, 2014 Posted September 8, 2014 This should fix it (since all the code is there): add tile.markDirty(); after you change the inventory slots. That should send the packet automatically! Quote Check out my blog! http://www.whov.altervista.org
Neuro Posted September 8, 2014 Author Posted September 8, 2014 I thank you all for your effort and help, it now works like a charm!! Thank you again! Quote
Whov Posted September 8, 2014 Posted September 8, 2014 On 9/8/2014 at 12:47 PM, diesieben07 said: @Whov: What? markDirty is something completely different. Oops, derp.. It actually is completely different. I thought your tile wasn't updating because I didn't consider (as diesieben07 said) that you are using a button (that is client side only, so server overrides client). Sorry my bad. To create buttons for my guis I use CodeChickenCore API: if you think it might help give it a try. This is my code using CCC (extending GuiScreenWidget): @Override public void actionPerformed(String ident, Object... params) { if (ident=="dismantle") { ((Fabricator)tile.getWorldObj().getBlock(tile.xCoord, tile.yCoord, tile.zCoord)).breakMultiBlock(tile.getWorldObj(), tile); this.mc.thePlayer.closeScreen(); } } @Override public void addWidgets() { GuiCCButton widget = new GuiCCButton(30, (height-ySize)/2, 199, 20, "Dismantle multiblock"); widget.setActionCommand("dismantle"); this.add(widget); } EDIT: how did you solve out of curiosity? Quote Check out my blog! http://www.whov.altervista.org
Neuro Posted September 9, 2014 Author Posted September 9, 2014 In my GUI actionPerformed method: MusicboxCore.network.sendToServer(new MusicBoxMessage(this.tileEntity.xCoord,this.tileEntity.yCoord,this.tileEntity.zCoord, this.nameField.getText(), this.tileEntity.unsavedSong[0], this.tileEntity.unsavedSong[1], this.tileEntity.unsavedSong[2], this.tileEntity.unsavedSong[3])); Here is the packet to be sent: package neuro.musicbox.main; import io.netty.buffer.ByteBuf; import net.minecraft.nbt.NBTTagCompound; import neuro.musicbox.gui.TileEntityMusicDesk; import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; public class MusicBoxMessage implements IMessage { int x, y, z; public String song1,song2,song3,song4; public String name; public MusicBoxMessage(int i, int j, int k, String name, String... song) { x = i; y = j; z = k; song1 = song[0]; song2 = song[1]; song3 = song[2]; song4 = song[3]; this.name = name; } @Override public void fromBytes(ByteBuf buf) { //Reads the data in the same order it was written. x, y, z, song1-4, then name x = buf.readInt(); y = buf.readInt(); z = buf.readInt(); song1 = ByteBufUtils.readUTF8String(buf); song2 = ByteBufUtils.readUTF8String(buf); song3 = ByteBufUtils.readUTF8String(buf); song4 = ByteBufUtils.readUTF8String(buf); name = ByteBufUtils.readUTF8String(buf); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(x); buf.writeInt(y); buf.writeInt(z); ByteBufUtils.writeUTF8String(buf, song1); ByteBufUtils.writeUTF8String(buf, song2); ByteBufUtils.writeUTF8String(buf, song3); ByteBufUtils.writeUTF8String(buf, song4); ByteBufUtils.writeUTF8String(buf, name); } public static class Handler implements IMessageHandler<MusicBoxMessage, IMessage> { @Override public IMessage onMessage(MusicBoxMessage message, MessageContext ctx) { //Get the tileentity at the correct coordinates server-side: TileEntityMusicDesk te = ((TileEntityMusicDesk)ctx.getServerHandler().playerEntity.worldObj.getTileEntity(message.x, message.y, message.z)); //Call the method server-side: te.createSongReel(ctx.getServerHandler().playerEntity, new String[] {message.song1,message.song2,message.song3,message.song4,message.song2,message.song3,message.song4}, message.name); } } } Quote
Neuro Posted September 9, 2014 Author Posted September 9, 2014 Thanks for the help again, however I have yet another minor issue. It seems to still be adding the items on the client as well, possibly more than it needs to. When I pick up an item, it will re-add the items to the inventory. For illustration: Slot 1: Has 6 items. Slot 2: Has 3 item. I take one item from slot 2 so it has 2 left. Now: Slot 1: Has 12 items. Slot 2: Has 4 items. However, the extra items are just fake duplicates which disappear once I grab 'em. Quote
Whov Posted September 9, 2014 Posted September 9, 2014 Try to figure out where (client side) those items are added and don't add them anymore (because server seems fine, right? They are actually not there (=>client-only) and not needed (=>fine)) Quote Check out my blog! http://www.whov.altervista.org
Neuro Posted September 10, 2014 Author Posted September 10, 2014 I had a look through it. Never called outside of the packet. Quote
Recommended Posts
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.