Posted August 22, 201411 yr So I am trying to get an integer from my gui class when someone hits the save button to my tile entity class and save it with nbt however it always equals 0 or nothing. Tile Entity package com.robert.instaport.common.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.tileentity.TileEntity; import com.robert.instaport.gui.GuiTeleporter; public class TileEntityTeleporter extends TileEntity implements IInventory { private ItemStack[] inv; public int id; public TileEntityTeleporter(){ inv = new ItemStack[1]; } @Override public int getSizeInventory() { return inv.length; } @Override public ItemStack getStackInSlot(int slot) { return inv[slot]; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { inv[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 1; } @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 void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); NBTTagList tagList = tagCompound.getTagList("Inventory", 10); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inv.length) { inv[slot] = ItemStack.loadItemStackFromNBT(tag); } } id = tagCompound.getInteger("ids"); } GuiTeleporter gui; @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inv.length; i++) { ItemStack stack = inv[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); tagCompound.setInteger("ids", id); System.out.println("ID: " + id); } @Override public String getInventoryName() { return null; } @Override public boolean hasCustomInventoryName() { return false; } @Override public void openInventory() { } @Override public void closeInventory() { } @Override public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) { return false; } } Gui package com.robert.instaport.gui; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import org.lwjgl.opengl.GL11; import com.robert.instaport.common.container.ContainerTeleporter; import com.robert.instaport.common.tileentity.TileEntityTeleporter; import com.robert.instaport.lib.Reference; public class GuiTeleporter extends GuiContainer { public final int xSizeOfTexture = 176; public final int ySizeOfTexture = 228; private GuiTextField idField; public int id; @SuppressWarnings("unused") private TileEntityTeleporter tileEntityKeyPad; private TileEntityTeleporter tileEntityTeleporter = new TileEntityTeleporter(); public GuiTeleporter (InventoryPlayer inventoryPlayer, TileEntityTeleporter tileEntity) { //the container is instanciated and passed to the superclass for handling super(new ContainerTeleporter(inventoryPlayer, tileEntity)); tileEntityTeleporter = tileEntity; this.xSize = xSizeOfTexture; this.ySize = ySizeOfTexture; TileEntityTeleporter tile = (TileEntityTeleporter) tileEntityTeleporter.getWorldObj().getTileEntity(tileEntityTeleporter.xCoord, tileEntityTeleporter.yCoord, tileEntityTeleporter.zCoord); id = new TileEntityTeleporter().id; } @Override protected void drawGuiContainerForegroundLayer(int param1, int param2) { String title = "Teleporter"; fontRendererObj.drawString(title, 7, ySize - 220, 4210752); fontRendererObj.drawString("Security Card:", 7, ySize - 118, 4210752); } @SuppressWarnings("unchecked") @Override public void initGui() { super.initGui(); //make buttons buttonList.add(new GuiButton(1, width / 2 - 20 / 2 - 60, this.height / 2 - 65, 20, 20, "+")); buttonList.add(new GuiButton(2, width / 2 - 20 / 2 + 60, this.height / 2 - 65, 20, 20, "-")); buttonList.add(new GuiButton(3, width / 2 - 100 / 2, this.height / 2 - 35, 100, 20, "Save")); idField = new GuiTextField(fontRendererObj, this.width / 2 - 70 / 2, this.height / 2 - 65, 70, 20); idField.setFocused(false); idField.setMaxStringLength(10); } public void updateScreen() { idField.setText(Integer.toString(id)); } public void drawScreen(int i, int j, float f) { super.drawScreen(i, j, f); idField.drawTextBox(); } protected void actionPerformed(GuiButton guibutton) { TileEntityTeleporter tile = (TileEntityTeleporter) tileEntityTeleporter.getWorldObj().getTileEntity(tileEntityTeleporter.xCoord, tileEntityTeleporter.yCoord, tileEntityTeleporter.zCoord); switch(guibutton.id) { case 1://add id += 1; break; case 2:// subtract if(!(id <= 0)) id -= 1; break; case 3://save new TileEntityTeleporter().id = id; } //Packet code here //PacketDispatcher.sendPacketToServer(packet); //send packet } ResourceLocation texture = new ResourceLocation(Reference.MOD_ID.toLowerCase() + ":" + "textures/gui/Teleporter.png"); @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); //this.mc.renderEngine.bindTexture(texture); this.mc.getTextureManager().bindTexture(texture); int x = (width - xSizeOfTexture) / 2; int y = (height - ySizeOfTexture) / 2; this.drawTexturedModalRect(x, y, 0, 0, xSizeOfTexture, ySizeOfTexture); } }
August 22, 201411 yr I had this issue too, You need to use packets. See the thing is is you are saving it but in the wrong place (clientSide) which is only a display (thus why GUIs are client side) This should help you get started also I suggest the latest 1.7.2 and up versions for this https://gist.github.com/CatDany/4a3df7fcb3c8270cf70b also check out the tutorials sub forum about the simpleMessageWrapper for a better understanding how to use the packets. TL;DR You need your GUI to send a packet to the Server, inwhich the server will handle the rest (aka find the tileentity in question and make the changes) Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 22, 201411 yr Author Ok thank you I will try that. But you said it sends to the server will that still work in single player?
August 22, 201411 yr Well as far as I know (and don't take my word 100% and I hope someone will correct me if I'm wrong) single player is basically a server, client already (I think once they updated minecraft to let single player worlds open to LAN they made this happen, once again hope someone corrects me) I would say yes still works single player because my TileEntities save information this way. (just make sure the tileEntity is reading and writing to the NBT so it persists on server closing and loading) Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 22, 201411 yr Author Well as far as I know (and don't take my word 100% and I hope someone will correct me if I'm wrong) single player is basically a server, client already (I think once they updated minecraft to let single player worlds open to LAN they made this happen, once again hope someone corrects me) I would say yes still works single player because my TileEntities save information this way. (just make sure the tileEntity is reading and writing to the NBT so it persists on server closing and loading) How would I send it to the client I ended up using a slightly different tutorial because the one you gave me kept giving me errors but I never figured out how to set the variable in my tile entity from it. Do i do it from the onMessage method and set it like i did from the gui or do I have to have something in the tileentity setting it?
August 22, 201411 yr what kindof error? is it crashing complaining about not being able to find class Side? what forge source are you using? I remember someone telling me that, so I hope someone can learn from my mistakes here is my thread on this very issue http://www.minecraftforge.net/forum/index.php/topic,22347.msg113330.html#msg113330 If that is not the issue I can show you how I use mine but just want to make sure that it's just a forge version error because I don't want to taint your learning by feeding you copy paste ready code. Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 22, 201411 yr Author what kindof error? is it crashing complaining about not being able to find class Side? what forge source are you using? I remember someone telling me that, so I hope someone can learn from my mistakes here is my thread on this very issue http://www.minecraftforge.net/forum/index.php/topic,22347.msg113330.html#msg113330 If that is not the issue I can show you how I use mine but just want to make sure that it's just a forge version error because I don't want to taint your learning by feeding you copy paste ready code. Well it was more of syntax errors. Ill put the code im using now below. main package com.robert.instaport; import net.minecraft.creativetab.CreativeTabs; import com.robert.instaport.common.blocks.ModBlocks; import com.robert.instaport.common.crafting.Crafting; import com.robert.instaport.common.creativetabs.InstaportTab; import com.robert.instaport.common.items.ModItems; import com.robert.instaport.gui.GuiHandler; import com.robert.instaport.lib.Reference; import com.robert.instaport.packet.MyMessage; import com.robert.instaport.proxy.CommonProxy; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; @Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.MOD_VER) public class Instaport { @Instance(value = Reference.MOD_ID) public static Instaport instance; @SidedProxy(clientSide="com.robert.instaport.proxy.ClientProxy", serverSide="com.robert.instaport.proxy.CommonProxy") public static CommonProxy proxy; public static CreativeTabs tabInstaport = new InstaportTab("Instaport"); public static SimpleNetworkWrapper network; @EventHandler public void preInit(FMLPreInitializationEvent event) { ModBlocks.init(); ModItems.init(); Crafting.init(); NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler()); network = NetworkRegistry.INSTANCE.newSimpleChannel("MyChannel"); network.registerMessage(MyMessage.Handler.class, MyMessage.class, 0, Side.SERVER); } @EventHandler public void load(FMLInitializationEvent event) { proxy.registerRenderers(); } @EventHandler public void postInit(FMLPostInitializationEvent event) { } } packet package com.robert.instaport.packet; import com.robert.instaport.common.tileentity.TileEntityTeleporter; import io.netty.buffer.ByteBuf; 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 MyMessage implements IMessage { private String text; public MyMessage() { } public MyMessage(String text) { this.text = text; } @Override public void fromBytes(ByteBuf buf) { text = ByteBufUtils.readUTF8String(buf); // this class is very useful in general for writing more complex objects } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeUTF8String(buf, text); } public static class Handler implements IMessageHandler<MyMessage, IMessage> { @Override public IMessage onMessage(MyMessage message, MessageContext ctx) { System.out.println(String.format("Received %s from %s", message.text, ctx.getServerHandler().playerEntity.getDisplayName())); return null; // no response in this case } } } gui package com.robert.instaport.gui; import java.util.EnumMap; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import org.lwjgl.opengl.GL11; import com.robert.instaport.Instaport; import com.robert.instaport.common.container.ContainerTeleporter; import com.robert.instaport.common.tileentity.TileEntityTeleporter; import com.robert.instaport.lib.Reference; import com.robert.instaport.packet.MyMessage; import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; public class GuiTeleporter extends GuiContainer { public final int xSizeOfTexture = 176; public final int ySizeOfTexture = 228; private GuiTextField idField; public int id; @SuppressWarnings("unused") private TileEntityTeleporter tileEntityKeyPad; private TileEntityTeleporter tileEntityTeleporter = new TileEntityTeleporter(); public GuiTeleporter (InventoryPlayer inventoryPlayer, TileEntityTeleporter tileEntity) { //the container is instanciated and passed to the superclass for handling super(new ContainerTeleporter(inventoryPlayer, tileEntity)); tileEntityTeleporter = tileEntity; this.xSize = xSizeOfTexture; this.ySize = ySizeOfTexture; TileEntityTeleporter tile = (TileEntityTeleporter) tileEntityTeleporter.getWorldObj().getTileEntity(tileEntityTeleporter.xCoord, tileEntityTeleporter.yCoord, tileEntityTeleporter.zCoord); id = tile.getId(); } @Override protected void drawGuiContainerForegroundLayer(int param1, int param2) { String title = "Teleporter"; fontRendererObj.drawString(title, 7, ySize - 220, 4210752); fontRendererObj.drawString("Security Card:", 7, ySize - 118, 4210752); } @SuppressWarnings("unchecked") @Override public void initGui() { super.initGui(); //make buttons buttonList.add(new GuiButton(1, width / 2 - 20 / 2 - 60, this.height / 2 - 65, 20, 20, "+")); buttonList.add(new GuiButton(2, width / 2 - 20 / 2 + 60, this.height / 2 - 65, 20, 20, "-")); buttonList.add(new GuiButton(3, width / 2 - 100 / 2, this.height / 2 - 35, 100, 20, "Save")); idField = new GuiTextField(fontRendererObj, this.width / 2 - 70 / 2, this.height / 2 - 65, 70, 20); idField.setFocused(false); idField.setMaxStringLength(10); } public void updateScreen() { idField.setText(Integer.toString(id)); } public void drawScreen(int i, int j, float f) { super.drawScreen(i, j, f); idField.drawTextBox(); } protected void actionPerformed(GuiButton guibutton) { TileEntityTeleporter tile = (TileEntityTeleporter) tileEntityTeleporter.getWorldObj().getTileEntity(tileEntityTeleporter.xCoord, tileEntityTeleporter.yCoord, tileEntityTeleporter.zCoord); switch(guibutton.id) { case 1://add id += 1; break; case 2:// subtract if(!(id <= 0)) id -= 1; break; case 3://save Instaport.network.sendToServer(new MyMessage(Integer.toString(id))); } //Packet code here //PacketDispatcher.sendPacketToServer(packet); //send packet } ResourceLocation texture = new ResourceLocation(Reference.MOD_ID.toLowerCase() + ":" + "textures/gui/Teleporter.png"); @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); //this.mc.renderEngine.bindTexture(texture); this.mc.getTextureManager().bindTexture(texture); int x = (width - xSizeOfTexture) / 2; int y = (height - ySizeOfTexture) / 2; this.drawTexturedModalRect(x, y, 0, 0, xSizeOfTexture, ySizeOfTexture); } }
August 22, 201411 yr Author what kindof error? is it crashing complaining about not being able to find class Side? what forge source are you using? I remember someone telling me that, so I hope someone can learn from my mistakes here is my thread on this very issue http://www.minecraftforge.net/forum/index.php/topic,22347.msg113330.html#msg113330 If that is not the issue I can show you how I use mine but just want to make sure that it's just a forge version error because I don't want to taint your learning by feeding you copy paste ready code. Well I think I got the packet tutorial that you sent working but I had to change a couple things. Im also pretty sure im sending my id from the gui to it but how do I get it to the tile entity?
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.