Posted May 1, 201312 yr Hello everyone. I need your help, I have a problem with TileEntity, it doesn't save any information after logging out. So you can enter string, int (number) or any other information, which can be contained in NBTTagCompound, then click save-button. And all this information will be saved until you relogging. After that all information will be cleared. I think this is due to the fact that my TileEntity doesn't send new values to the server side, so they cannot be saved. My another assumption is that my TileEntity doesn't get updated properly. Anyway I can't solve this problem by myself, hope for your help. Source: BlockAltar.java: package mods.nordwest.blocks; import java.util.List; import java.util.Random; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import mods.nordwest.common.Config; import mods.nordwest.common.CustomBlocks; import mods.nordwest.common.NordWest; import mods.nordwest.tileentity.TileEntityAltar; import mods.nordwest.utils.EffectsLibrary; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Icon; import net.minecraft.world.World; public class BlockAltar extends BaseBlock { @SideOnly(Side.CLIENT) private Icon[] iconArray; public BlockAltar(int par1) { super(par1, Material.rock); this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.75F, 1.0F); this.setLightOpacity(0); setHardness(2500.0F); setResistance(20.0F); setCreativeTab(NordWest.tabNW); } @SideOnly(Side.CLIENT) @Override public void registerIcons(IconRegister par1IconRegister) { this.iconArray = new Icon[3]; this.iconArray[0] = par1IconRegister.registerIcon("nordwest:" + this.getUnlocalizedName2() + ".side"); this.iconArray[1] = par1IconRegister.registerIcon("nordwest:" + this.getUnlocalizedName2() + ".top"); this.iconArray[2] = par1IconRegister.registerIcon("nordwest:" + this.getUnlocalizedName2() + ".buttom"); } @Override public TileEntity createTileEntity(World world, int meta) { try { return new TileEntityAltar(); } catch (Exception e) { throw new RuntimeException(e); } } @Override public boolean hasTileEntity(int metadata) { return true; } public boolean isOpaqueCube() { return false; } public boolean renderAsNormalBlock() { return false; } @Override public void onBlockAdded(World world, int x, int y, int z) { Random random = new Random(); if (world.provider.dimensionId == 1) { int dropChance = random.nextInt(10); world.createExplosion((Entity)null, x, y, z, 2.0F, false); if (dropChance <= 7) { world.destroyBlock(x, y, z, true); } else { world.destroyBlock(x, y, z, false); } } else { world.setBlockTileEntity(x, y, z, createTileEntity(world, world.getBlockMetadata(x, y, z))); boolean onBlockActivated; } } @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int a, float par1, float par2, float par3){ ItemStack itemstack = player.inventory.getCurrentItem(); TileEntityAltar tileEntity = (TileEntityAltar) world.getBlockTileEntity(x, y, z); if (itemstack != null){ if (itemstack.itemID == Item.flintAndSteel.itemID ){ if (tileEntity == null || player.isSneaking() || tileEntity.stage != 0) { return false; } EffectsLibrary.smokeCloud(player, x, y, z, 48); EffectsLibrary.playSoundOnEntity(player, "fire.ignite"); itemstack.damageItem(3, player); player.openGui(NordWest.instance, 0, world, x, y, z); return true; } } return false; } @SideOnly(Side.CLIENT) @Override public Icon getIcon(int par1, int par2) { if (par1 == 0) return this.iconArray[2]; if (par1 == 1) return this.iconArray[1]; return this.iconArray[0]; } public int getRenderType() { return Config.AltarRendererID; } @Override public void randomDisplayTick(World par1World, int x, int y, int z, Random random) { super.randomDisplayTick(par1World, x, y, z, random); /* Easter Egg */ if (testBlock(par1World, x, y, z)) { for (int i = x - 2; i < x + 3; i++) { for (int j = y; j < y + 4; j++) { for (int k = z - 2; k < z + 3; k++) { if (random.nextInt(16) == 0) { par1World.spawnParticle("portal", i + random.nextDouble(), j + random.nextDouble(), k + random.nextDouble(), 0, 0, 0); } } } } } } private boolean testBlock(World world, int x, int y, int z) { int id = world.getBlockId(x, y, z); if (id != CustomBlocks.blockhome.blockID) { return false; } else { boolean test = true; for (int i = 0; i < 4; i++) { test &= world.getBlockId(x + 2, y + i, z + 2) == Block.obsidian.blockID; test &= world.getBlockId(x - 2, y + i, z + 2) == Block.obsidian.blockID; test &= world.getBlockId(x + 2, y + i, z - 2) == Block.obsidian.blockID; test &= world.getBlockId(x - 2, y + i, z - 2) == Block.obsidian.blockID; } return test; } } } GuiAltar.java: package mods.nordwest.client.gui; import java.awt.Color; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.util.Random; import mods.nordwest.blocks.BlockAltar; import mods.nordwest.client.renders.BlockCandleRenderer; import mods.nordwest.common.NordWest; import mods.nordwest.utils.EnumFormatting; import mods.nordwest.utils.EffectsLibrary; import mods.nordwest.tileentity.TileEntityAltar; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.StringTranslate; import net.minecraft.world.World; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.registry.LanguageRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class GuiAltar extends GuiScreen { public TileEntityAltar tileEntity; private static EntityPlayer player; private static World world; private GuiTextField textfield; private int xSize = 176; private int ySize = 166; String doublePoint = ": "; Random random = new Random(); private int variability = random.nextInt(10); public GuiAltar(TileEntity tileEntity, EntityPlayer entityplayer, World currentworld) { this.tileEntity = (TileEntityAltar) tileEntity; player = entityplayer; world = currentworld; } @Override public void initGui() { String DefaultText = LanguageRegistry.instance().getStringLocalization("homing.default") + " " + tileEntity.stage; DefaultText = DefaultText.replaceAll("%p", player.getEntityName()); buttonList.clear(); /* Buttons with their settings! */ buttonList.add(new GuiButton(0, width / 2 - 82, height / 2 + 45, 80, 20, LanguageRegistry.instance().getStringLocalization("homing.done"))); buttonList.add(new GuiButton(1, width / 2 + 2, height / 2 + 45, 80, 20, LanguageRegistry.instance().getStringLocalization("homing.back"))); /* Text field & its settings! */ textfield = new GuiTextField(fontRenderer, width / 2 - 72, height / 2 - 37, 105, 20); textfield.setFocused(false); textfield.setCanLoseFocus(true); textfield.setText(DefaultText); textfield.setMaxStringLength(28); textfield.setCursorPosition(0); textfield.setEnableBackgroundDrawing(false); } public void mouseClicked(int i, int j, int k) { super.mouseClicked(i, j, k); textfield.mouseClicked(i, j, k); if (j >= height / 2 - 41 && j <= height / 2 - 27 && i >= width / 2 + 30 && i <= width / 2 + 46) { player.playSound("random.click", 0.5F, 1.2F); /* Previous color */ } else if (j >= height / 2 - 41 && j <= height / 2 - 27 && i >= width / 2 + 60 && i <= width / 2 + 76) { player.playSound("random.click", 0.5F, 1.2F); /* Next color */ } } @Override public void onGuiClosed() { player.playSound("random.fizz", 0.5F, 1.2F); } @Override public void drawScreen(int i, int j, float f) { /* Background and Title */ GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); mc.renderEngine.bindTexture("/mods/nordwest/textures/gui/GuiAltar.png"); drawTexturedModalRect(width / 2 - 88, height / 2 - 80, 0, 0, 176, 166); fontRenderer.drawString(LanguageRegistry.instance().getStringLocalization("homing.title"), width / 2 - 75, height / 2 - 73, 4210752); textfield.drawTextBox(); /* General Content */ drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.subtitle") + doublePoint, width / 2 - 75, height / 2 - 52, 0xF0DC82); drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.remember") + doublePoint, width / 2 - 75, height / 2 - 22, 0xF0DC82); drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.desc.s1"), width / 2 - 75, height / 2 - 10, 0xF0DC82); drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.desc.s2"), width / 2 - 75, height / 2, 0xF0DC82); drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.desc.s3"), width / 2 - 75, height / 2 + 10, 0xF0DC82); if (StringTranslate.getInstance().getCurrentLanguage().equals("ru_RU")) { drawString(fontRenderer, LanguageRegistry.instance().getStringLocalization("homing.desc.s4"), width / 2 - 75, height / 2 + 20, 0xF0DC82); } else { String randomAlert = "Something's wrong!"; if (variability <= 3) { randomAlert = LanguageRegistry.instance().getStringLocalization("homing.alert.1"); } else if (variability <= { randomAlert = LanguageRegistry.instance().getStringLocalization("homing.alert.2"); } else { randomAlert = LanguageRegistry.instance().getStringLocalization("homing.alert.rare"); } drawString(fontRenderer, randomAlert, width / 2 - 75, height / 2 + 25, 0xF0DC82); } super.drawScreen(i, j, f); } @Override public void keyTyped(char c, int i) { super.keyTyped(c, i); textfield.textboxKeyTyped(c, i); if (i == 1) { mc.displayGuiScreen(null); } } @Override public boolean doesGuiPauseGame() { return false; } @Override public void actionPerformed(GuiButton guibutton) { if (guibutton.id == 0) { tileEntity.owner = player.getEntityName(); tileEntity.name = textfield.getText(); tileEntity.stage = 2; //world.notifyBlockChange(tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord, 2); tileEntity.sendUpdate(); mc.displayGuiScreen(null); } if (guibutton.id == 1) { mc.displayGuiScreen(null); } } } AltarTileEntity.java: package mods.nordwest.tileentity; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import com.google.common.io.ByteArrayDataInput; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.registry.LanguageRegistry; public class TileEntityAltar extends TileEntity { public String owner = "Undefined"; public String name = "Undefined"; public int stage = 0; @Override public void readFromNBT(NBTTagCompound tag) { super.readFromNBT(tag); name = tag.getString("name"); owner = tag.getString("owner"); stage = tag.getInteger("stage"); } @Override public void writeToNBT(NBTTagCompound tag) { super.writeToNBT(tag); tag.setString("name", name); tag.setString("owner", owner); tag.setInteger("stage", stage); } public void sendUpdate() { worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); worldObj.notifyBlockChange(xCoord, yCoord, zCoord, 2); } @Override public Packet getDescriptionPacket() { NBTTagCompound tag = new NBTTagCompound(); this.writeToNBT(tag); return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 0, tag); } @Override public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt) { super.onDataPacket(net, pkt); NBTTagCompound tag = pkt.customParam1; this.readFromNBT(tag); } } ClientProxy.java: package mods.nordwest.client; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; import cpw.mods.fml.common.registry.TickRegistry; import cpw.mods.fml.relauncher.Side; import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.client.IItemRenderer; import net.minecraftforge.client.MinecraftForgeClient; import mods.nordwest.client.gui.GuiAltar; import mods.nordwest.client.renders.BlockCandleRenderer; import mods.nordwest.client.renders.BlockExtractorRenderer; import mods.nordwest.common.CommonProxy; public class ClientProxy extends CommonProxy { @Override public void registerRenderers() { RenderingRegistry.registerBlockHandler(new BlockCandleRenderer()); RenderingRegistry.registerBlockHandler(new BlockExtractorRenderer()); } @Override public GuiScreen getClientGui(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); switch(ID) { case 0: return new GuiAltar(tileEntity, player, world); } return null; } @Override public int addArmor(String armor) { return RenderingRegistry.addNewArmourRendererPrefix("../mods/nordwest/textures/armor/"+armor); } public void registerServerTickHandler() { //TickRegistry.registerTickHandler(new ClientTickHandler(), Side.CLIENT); } public String getStringLocalization(String key){ return LanguageRegistry.instance().getStringLocalization(key); } } Part of NordWest.java (Our main class): /** Registering GUI handler */ NetworkRegistry.instance().registerGuiHandler(this, new GuiHandler()); /** Our TileEntities */ GameRegistry.registerTileEntity(TileEntityAltar.class, "tileEntityAltar"); GuiHandler.java: package mods.nordwest.common; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import cpw.mods.fml.common.network.IGuiHandler; /** * Client and server GUI hander for our mod. * Uses CommonProxy to get the server GUI and ClientProxy for the client GUI. * */ public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { return NordWest.proxy.getServerGui(ID, player, world, x, y, z); } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { return NordWest.proxy.getClientGui(ID, player, world, x, y, z); } } Thanks in advance. And sorry for my english. With best regards, Alexey.
May 1, 201312 yr You need to send a custom packet within the GUI to the server. To do so, you need a Packet Handler. Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.
May 2, 201312 yr Author Thank you for your answer. I've never used the packets before, and I couldn't find any guides, which can be useful for me. So could you help me? According to this common guide - http://www.minecraftforge.net/wiki/Packet_Handling, I added the following code to my GUI: /* Button Actions */ @Override public void actionPerformed(GuiButton guibutton) { if (guibutton.id == 0) { tileEntity.owner = player.getEntityName(); tileEntity.name = textfield.getText(); tileEntity.stage = 2; /* Packet Sending*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(64); DataOutputStream outputStream = new DataOutputStream(bos); try { outputStream.writeInt(tileEntity.stage); outputStream.writeUTF(tileEntity.name); outputStream.writeUTF(tileEntity.owner); } catch (Exception ex) { ex.printStackTrace(); System.err.println("[Mekanism] Error while writing tile entity packet."); } Packet250CustomPayload packet = new Packet250CustomPayload(); packet.channel = "NordWest"; packet.data = bos.toByteArray(); packet.length = bos.size(); Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.SERVER) { // We're on the server side. EntityPlayerMP MPplayer = (EntityPlayerMP) player; } else if (side == Side.CLIENT) { // We're on the client side. EntityClientPlayerMP MPplayer = (EntityClientPlayerMP) player; PacketDispatcher.sendPacketToServer(packet); } else { // We're on the Bukkit server. } mc.displayGuiScreen(null); //Activation button. } /* Packet Sending End */ if (guibutton.id == 1) { mc.displayGuiScreen(null); //Cancel button. } } And created this simple Packet Handler: package mods.nordwest.common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import mods.nordwest.tileentity.TileEntityAltar; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { if (packet.channel.equals("NordWest")) { handleAltar(packet); } } private void handleAltar(Packet250CustomPayload packet) { DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); int stage; String name; String owner; try { stage = inputStream.readInt(); name = inputStream.readUTF(); owner = inputStream.readUTF(); } catch (IOException e) { e.printStackTrace(); return; } /* Debug Console Message */ System.out.println("Okay, now stage is " + stage + ". And the name - " + name + " by " + owner +"."); } } Debug message works fine, so I can send and handle the packet, but there is a question - how can I save all this information and use it later? All useful guides are extremely welcome. Thanks in advance.
May 2, 201312 yr If you wanna see how I did it with several blocks, look at the fresh Turret Mod 3 source on github, here: https://github.com/SanAndreasP/TurretModv3/tree/master/sanandreasp/mods/TurretMod3 Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.
May 2, 201312 yr Author Thank you very much guys! That works. Problem was due to the fact that I didn't send coordinates of the TileEntity in the Packets.
May 3, 201312 yr Well, i suggest more easy way — just add these few lines to tileEntity class, instead of that pile of code public Packet getDescriptionPacket() { NBTTagCompound nbtTag = new NBTTagCompound(); this.writeToNBT(nbtTag); return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag); } public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) { readFromNBT(packet.customParam1); } If i helped you, don't forget pressing "Thank You" button. Thanks for your time.
May 3, 201312 yr diesieben07, don't know what you mean, but code works fine for me. Everything saves and loads perfectly. If i helped you, don't forget pressing "Thank You" button. Thanks for your time.
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.