Posted April 11, 20169 yr I made this tile entity which has a GUI and stores items but when I leave the map and go back in THe stuff I put in is missing ModBlockTileEntity.java package com.mattjuior.tutorial.blocks; import com.mattjuior.tutorial.Class; import com.mattjuior.tutorial.network.ModGuiHandler; import com.mattjuior.tutorial.tileentity.ModTileEntity; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.InventoryHelper; import net.minecraft.item.ItemStack; import net.minecraft.server.gui.IUpdatePlayerListBox; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; public class ModBlockTileEntity extends BlockContainer{ protected ModBlockTileEntity(String unlocalizedName) { super(Material.iron); this.setCreativeTab(CreativeTabs.tabBlock); this.setUnlocalizedName(unlocalizedName); this.setHardness(2.0f); this.setResistance(6.0f); this.setHarvestLevel("pickaxe", 2); } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new ModTileEntity(); } @Override public void breakBlock(World world, BlockPos pos, IBlockState blockstate) { ModTileEntity te = (ModTileEntity) world.getTileEntity(pos); InventoryHelper.dropInventoryItems(world, pos, te); super.breakBlock(world, pos, blockstate); } @Override public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { if (stack.hasDisplayName()) { ((ModTileEntity) worldIn.getTileEntity(pos)).setCustomName(stack.getDisplayName()); } } @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) { if (!world.isRemote) { player.openGui(Class.instance, ModGuiHandler.MOD_TILE_ENTITY_GUI, world, pos.getX(), pos.getY(), pos.getZ()); } return true; } } GuiModTileEntity.java package com.mattjuior.tutorial.gui; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.inventory.IInventory; import net.minecraft.util.ResourceLocation; import com.mattjuior.tutorial.guicontainer.ContainerModTileEntity; import com.mattjuior.tutorial.tileentity.ModTileEntity; public class GuiModTileEntity extends GuiContainer { private IInventory playerInv; private ModTileEntity te; public GuiModTileEntity(IInventory playerInv, ModTileEntity te) { super(new ContainerModTileEntity(playerInv, te)); this.playerInv = playerInv; this.te = te; this.xSize = 176; this.ySize = 166; } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f); this.mc.getTextureManager().bindTexture(new ResourceLocation("tutorial:textures/gui/container/mod_tile_entity.png")); this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { String s = this.te.getDisplayName().getUnformattedText(); this.fontRendererObj.drawString(s, 88 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752); //#404040 this.fontRendererObj.drawString(this.playerInv.getDisplayName().getUnformattedText(), 8, 72, 4210752); //#404040 } } ContainerModTileEntity.java package com.mattjuior.tutorial.guicontainer; import com.mattjuior.tutorial.tileentity.ModTileEntity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class ContainerModTileEntity extends Container { private ModTileEntity te; public ContainerModTileEntity(IInventory playerInv, ModTileEntity te) { this.te = te; // Tile Entity, Slot 0-8, Slot IDs 0-8 for (int y = 0; y < 3; ++y) { for (int x = 0; x < 3; ++x) { this.addSlotToContainer(new Slot(te, x + y * 3, 62 + x * 18, 17 + y * 18)); } } // Player Inventory, Slot 9-35, Slot IDs 9-35 for (int y = 0; y < 3; ++y) { for (int x = 0; x < 9; ++x) { this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)); } } // Player Inventory, Slot 0-8, Slot IDs 36-44 for (int x = 0; x < 9; ++x) { this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142)); } } @Override public boolean canInteractWith(EntityPlayer playerIn) { return this.te.isUseableByPlayer(playerIn); } @Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) { ItemStack previous = null; Slot slot = (Slot) this.inventorySlots.get(fromSlot); if (slot != null && slot.getHasStack()) { ItemStack current = slot.getStack(); previous = current.copy(); if (fromSlot < 9) { // From TE Inventory to Player Inventory if (!this.mergeItemStack(current, 9, 45, true)) return null; } else { // From Player Inventory to TE Inventory if (!this.mergeItemStack(current, 0, 9, false)) return null; } if (current.stackSize == 0) slot.putStack((ItemStack) null); else slot.onSlotChanged(); if (current.stackSize == previous.stackSize) return null; slot.onPickupFromSlot(playerIn, current); } return previous; } } ModGuiHandler.java package com.mattjuior.tutorial.network; import com.mattjuior.tutorial.gui.GuiModTileEntity; import com.mattjuior.tutorial.guicontainer.ContainerModTileEntity; import com.mattjuior.tutorial.tileentity.ModTileEntity; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; public class ModGuiHandler implements IGuiHandler { public static final int MOD_TILE_ENTITY_GUI = 0; @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == MOD_TILE_ENTITY_GUI) return new ContainerModTileEntity(player.inventory, (ModTileEntity) world.getTileEntity(new BlockPos(x, y, z))); return null; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == MOD_TILE_ENTITY_GUI) return new GuiModTileEntity(player.inventory, (ModTileEntity) world.getTileEntity(new BlockPos(x, y, z))); return null; } } ModTileEntities.java package com.mattjuior.tutorial.tileentity; import net.minecraftforge.fml.common.registry.GameRegistry; public final class ModTileEntities { public static void init() { GameRegistry.registerTileEntity(ModTileEntity.class, "tutorial_tile_entity"); } } ModTileEntity.java package com.mattjuior.tutorial.tileentity; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.server.gui.IUpdatePlayerListBox; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.IChatComponent; import net.minecraft.world.World; public class ModTileEntity extends TileEntity implements IInventory{ private ItemStack[] inventory; private String customName; public ModTileEntity() { this.inventory = new ItemStack[this.getSizeInventory()]; } public String getCustomName() { return this.customName; } public void setCustomName(String customName) { this.customName = customName; } @Override public String getName() { return this.hasCustomName() ? this.customName : "container.tutorial_tile_entity"; } @Override public boolean hasCustomName() { return this.customName != null && !this.customName.equals(""); } @Override public IChatComponent getDisplayName() { return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName()); } @Override public int getSizeInventory() { return 9; } @Override public ItemStack getStackInSlot(int index) { if (index < 0 || index >= this.getSizeInventory()) return null; return this.inventory[index]; } @Override public ItemStack decrStackSize(int index, int count) { if (this.getStackInSlot(index) != null) { ItemStack itemstack; if (this.getStackInSlot(index).stackSize <= count) { itemstack = this.getStackInSlot(index); this.setInventorySlotContents(index, null); this.markDirty(); return itemstack; } else { itemstack = this.getStackInSlot(index).splitStack(count); if (this.getStackInSlot(index).stackSize <= 0) { this.setInventorySlotContents(index, null); } else { //Just to show that changes happened this.setInventorySlotContents(index, this.getStackInSlot(index)); } this.markDirty(); return itemstack; } } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int index) { ItemStack stack = this.getStackInSlot(index); this.setInventorySlotContents(index, null); return stack; } @Override public void setInventorySlotContents(int index, ItemStack stack) { if (index < 0 || index >= this.getSizeInventory()) return; if (stack != null && stack.stackSize > this.getInventoryStackLimit()) stack.stackSize = this.getInventoryStackLimit(); if (stack != null && stack.stackSize == 0) stack = null; this.inventory[index] = stack; this.markDirty(); } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { } @Override public int getField(int id) { return 0; } @Override public void setField(int id, int value) { } @Override public int getFieldCount() { return 0; } @Override public void clear() { for (int i = 0; i < this.getSizeInventory(); i++) this.setInventorySlotContents(i, null); } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); NBTTagList list = new NBTTagList(); for (int i = 0; i < this.getSizeInventory(); ++i) { if (this.getStackInSlot(i) != null) { NBTTagCompound stackTag = new NBTTagCompound(); stackTag.setByte("Slot", (byte) i); this.getStackInSlot(i).writeToNBT(stackTag); list.appendTag(stackTag); } } nbt.setTag("Items", list); if (this.hasCustomName()) { nbt.setString("CustomName", this.getCustomName()); } } @Override public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); NBTTagList list = nbt.getTagList("Items", 10); for (int i = 0; i < list.tagCount(); ++i) { NBTTagCompound stackTag = list.getCompoundTagAt(i); int slot = stackTag.getByte("Slot") & 255; this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag)); } if (nbt.hasKey("CustomName", ) { this.setCustomName(nbt.getString("CustomName")); } } } Class.java package com.mattjuior.tutorial; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @Mod(modid = Class.MODID, name = Class.MODNAME, version = Class.VERSION) public class Class { public static final String MODID = "tutorial"; public static final String MODNAME = "Tutorial Mod"; public static final String VERSION = "1.0.0"; @SidedProxy(clientSide="com.mattjuior.tutorial.ClientProxy", serverSide="com.mattjuior.tutorial.ServerProxy" ) public static CommonProxy proxy; @Instance public static Class instance = new Class(); @EventHandler public void preInit(FMLPreInitializationEvent e) { this.proxy.preInit(e); } @EventHandler public void init(FMLInitializationEvent e) { this.proxy.init(e); } @EventHandler public void postInit(FMLPostInitializationEvent e) { this.proxy.postInit(e); } } ClientProxy.java package com.mattjuior.tutorial; import com.mattjuior.tutorial.client.render.blocks.BlockRenderRegister; import com.mattjuior.tutorial.client.render.items.ItemRenderRegister; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; public class ClientProxy extends CommonProxy{ @Override public void preInit(FMLPreInitializationEvent e) { super.preInit(e); } @Override public void init(FMLInitializationEvent e) { super.init(e); ItemRenderRegister.registerItemRenderer(); BlockRenderRegister.registerBlockRenderer(); } @Override public void postInit(FMLPostInitializationEvent e) { super.postInit(e); } } CommonProxy.java package com.mattjuior.tutorial; import com.mattjuior.tutorial.blocks.ModBlocks; import com.mattjuior.tutorial.items.Ball; import com.mattjuior.tutorial.network.ModGuiHandler; import net.minecraft.client.main.Main; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.network.NetworkRegistry; public class CommonProxy { public void preInit(FMLPreInitializationEvent e) { Ball.createItems(); ModBlocks.createBlocks(); } public void init(FMLInitializationEvent e) { NetworkRegistry.INSTANCE.registerGuiHandler(Class.instance, new ModGuiHandler()); } public void postInit(FMLPostInitializationEvent e) { } } ServerProxy.java package com.mattjuior.tutorial; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; public class ServerProxy extends CommonProxy{ @Override public void preInit(FMLPreInitializationEvent e) { super.preInit(e); } @Override public void init(FMLInitializationEvent e) { super.init(e); } @Override public void postInit(FMLPostInitializationEvent e) { super.postInit(e); } } Please Help me!
April 11, 20169 yr I don't see anything wrong with your read/write NBT methods, but holy hell your class names are a mess. ModBlockTileEntity.java isn't the tile entity class as I first expected, then neither was ContainerModTileEntity.java The "best" naming convention will be one that everyone working on Minecraft is familiar with: BlockWhatever , TileEntityThingamajig , ContainerWhatThing , etc. Also, Class.java ? Seriously? Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
April 11, 20169 yr Which version of 1.8 are you using? If you are developing for 1.8.9 you should not use IInventory, but Capabilities
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.