Jump to content

Recommended Posts

Posted

I made a tile entity with gui, gui handler, container, etc, and everything works except (and this is a big except) I can put items in the inventory, move them around, etc, but if I close the gui and reopen it, the item isn't there! Please Help! 

Tile Entity

 

package com.rabidfox.syntheticgems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;


public class TileMachineElectrolyzer extends TileEntity implements ISidedInventory{
private int electrolysistime = 0;
private ItemStack[] slots = new ItemStack[3];
private ItemStack slotStack = null;
public TileMachineElectrolyzer(){
	System.out.println("Tile Placed");
}
@Override
public ItemStack decrStackSize(int slot, int amt)
{
// we only have one stack here
ItemStack stack = null;
    slotStack = slots[slot];
if (slotStack != null)
{
if (slotStack.stackSize <= amt)
{
stack = slotStack;
slotStack = null;
System.out.println("Returned " + stack.getDisplayName() + " x " + String.valueOf(stack.stackSize) + " from slot # " + String.valueOf(slot) + " with a maximum size of " + String.valueOf(amt) + ".");
slots[slot] = null;
return stack;
}
else
{
stack = slotStack.splitStack(amt);
if (slotStack.stackSize == 0)
{
slotStack = null;
return null;
}
}
}
return null;
}
  @Override
    public ItemStack getStackInSlotOnClosing(int p_70304_1_)
    {
        if (this.slots[p_70304_1_] != null)
        {
            ItemStack itemstack = this.slots[p_70304_1_];
            this.slots[p_70304_1_] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }
   @Override
   public void writeToNBT(NBTTagCompound nbt)
   {
      super.writeToNBT(nbt);
      NBTTagList nbttaglist = new NBTTagList();
      for (int i = 0; i < this.slots.length; ++i)
      {
          if (this.slots[i] != null)
          {
              NBTTagCompound nbttagcompound1 = new NBTTagCompound();
              nbttagcompound1.setByte("Slot", (byte)i);
              this.slots[i].writeToNBT(nbttagcompound1);
              nbttaglist.appendTag(nbttagcompound1);
          }
      }
      nbt.setTag("Items", nbttaglist);

      if (this.hasCustomInventoryName())
      {
          nbt.setString("CustomName", "Electrolyzer");
      }
  }

   @Override
   public void readFromNBT(NBTTagCompound par1)
   {
      super.readFromNBT(par1);
      NBTTagList nbttaglist = par1.getTagList("Items", 10);
      for (int i = 0; i < nbttaglist.tagCount(); ++i)
      {
          NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
          byte b0 = nbttagcompound1.getByte("Slot");

          if (b0 >= 0 && b0 < this.slots.length)
          {
              this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
          }
      }
   }
@Override
public int getSizeInventory() {
// TODO Auto-generated method stub
return slots.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
slotStack = slots[slot];
return slotStack;
}
@Override
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_)
{
    this.slots[p_70299_1_] = p_70299_2_;

    if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
    {
        p_70299_2_.stackSize = this.getInventoryStackLimit();
    }
}
@Override
public String getInventoryName() {
// TODO Auto-generated method stub
return "Electrolysis Chamber";
}
@Override
public boolean hasCustomInventoryName() {
// TODO Auto-generated method stub
return true;
}
@Override
public int getInventoryStackLimit() {
// TODO Auto-generated method stub
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 openInventory() {
System.out.println(slots);
}
@Override
public void closeInventory() {
System.out.println(slots);
}
@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
// TODO Auto-generated method stub
return true;
}
@Override
public int[] getAccessibleSlotsFromSide(int p_94128_1_) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_,
	int p_102007_3_) {
// TODO Auto-generated method stub
    return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
}
@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_,
	int p_102008_3_) {
// TODO Auto-generated method stub
return false;
}
}

 

Container

 

package com.rabidfox.syntheticgems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerElectrolyzer extends Container{
@Override
public boolean canInteractWith(EntityPlayer p_75145_1_) {
	// TODO Auto-generated method stub
	return true;
}
public ContainerElectrolyzer(InventoryPlayer inventoryplayer, TileMachineElectrolyzer elec){
int i;
addSlotToContainer(new Slot(elec, 0, 80, 57));
addSlotToContainer(new Slot(elec, 1, 62, 17));
addSlotToContainer(new Slot(elec, 2, 98, 17));
 for (i = 0; i < 3; ++i)
     {
         for (int j = 0; j < 9; ++j)
         {
             this.addSlotToContainer(new Slot(inventoryplayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
         }
     }

     for (i = 0; i < 9; ++i)
     {
         this.addSlotToContainer(new Slot(inventoryplayer, i, 8 + i * 18, 142));
     }
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
if(!((Slot)this.inventorySlots.get(slot)).getHasStack()){
	return null;
}
if(slot == 0){
	Slot stack1 = (Slot)this.inventorySlots.get(0);
	if(!player.inventory.addItemStackToInventory(stack1.getStack())){
		return null;
	}else
		return stack1.getStack();
}
Slot stack = (Slot)this.inventorySlots.get(slot);
if(stack != null){
	if(stack.getStack().getItem() == Items.water_bucket){
		if(!((Slot)this.inventorySlots.get(0)).getHasStack()){
			return stack.getStack();
		}
	}
}
return null;
}
}

 

Gui

 

package com.rabidfox.syntheticgems;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiElectrolyzer extends GuiContainer
{
    private static final ResourceLocation electrolyzergui = new ResourceLocation("syntheticgems:textures/gui/electrolyzergui.png");
    private TileMachineElectrolyzer tileelec;
    private static final String __OBFID = "CL_00000758";
public static final int GUI_ID = 9002;

    public GuiElectrolyzer(InventoryPlayer p_i1091_1_, TileMachineElectrolyzer p_i1091_2_)
    {
        super(new ContainerElectrolyzer(p_i1091_1_, p_i1091_2_));
        this.tileelec = p_i1091_2_;
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items)
     */
    protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
    { 
        String s = this.tileelec.hasCustomInventoryName() ? this.tileelec.getInventoryName() : I18n.format(this.tileelec.getInventoryName(), new Object[0]);
        this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);
        this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96 + 2, 4210752);
    }

    protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(electrolyzergui);
        int k = ((this.width - this.xSize) / 2);
        int l = ((this.height - this.ySize) / 2);
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
    }
}

 

Handler

 

package com.rabidfox.syntheticgems;

import net.minecraft.client.gui.GuiMerchant;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerMerchant;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class GuiHandlerElectrolyzer implements IGuiHandler {

    public Object getServerGuiElement(int ID, EntityPlayer player, World world,
    int x, int y, int z) {
    // TODO Auto-generated method stub
    	System.out.println("Server");
    return new ContainerElectrolyzer(player.inventory, new TileMachineElectrolyzer());
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        if (ID == com.rabidfox.syntheticgems.GuiElectrolyzer.GUI_ID) {
            System.out.print("create gui.\n");
            return new com.rabidfox.syntheticgems.GuiElectrolyzer(player.inventory, new TileMachineElectrolyzer());
        }
        return null;
    }
}

 

Main Class

 

package com.rabidfox.syntheticgems;

import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "syntheticgems", version = "1.0.0")
public class SyntheticGems {
//Instance
@Mod.Instance("syntheticgems") public static SyntheticGems instance;
//Tile Variables//
public static Class<? extends TileEntity> tilemachineelectrolyzer;
//Block Variables//
public static Block orebauxite;
public static Block oresodium;
public static Block machineelectrolyzer;
//Item Variables//
public static Item itemoxybottle;
public static Item itemhydbottle;
public static Item itemsodiumraw;
public static Item itemlye;
public static Item itemlyesolution;
@EventHandler
public void preinit(FMLPreInitializationEvent event)
{
	//Register Gui Handlers//
	NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandlerElectrolyzer());
	//Block Variable Defining//
	orebauxite = new OreBauxite();
	orebauxite.setHarvestLevel("pickaxe", 1);
	oresodium = new OreSodium();
	oresodium.setHarvestLevel("pickaxe", 0);
	machineelectrolyzer = new MachineElectrolyzer();
	//Item Variable Defining//
	itemoxybottle = new ItemOxygenBottle();
	itemhydbottle = new ItemHydrogenBottle();
	itemsodiumraw = new ItemSodiumRaw();
	itemlye = new ItemLye();
	itemlyesolution = new ItemLyeSolution();
	//Tile Variable Registering//
	GameRegistry.registerTileEntity(TileMachineElectrolyzer.class, "tileelectrolyzer");
	//Block Variable Registering//
	GameRegistry.registerBlock(orebauxite, "orebauxite");
	GameRegistry.registerBlock(oresodium, "oresodium");
	GameRegistry.registerBlock(machineelectrolyzer, "machineelectrolyzer");
	//Item Variable Registering//
	GameRegistry.registerItem(itemsodiumraw, "itemsodiumraw");   
	GameRegistry.registerItem(itemlye, "itemlye");  
	GameRegistry.registerItem(itemlyesolution, "itemlyesolution");  
	GameRegistry.registerItem(itemoxybottle, "itemoxy");
	GameRegistry.registerItem(itemhydbottle, "itemhyd");
	//Smelting//
	GameRegistry.addSmelting(oresodium, new ItemStack(itemsodiumraw), (float) 0.1);
	//Shapeless//
	GameRegistry.addShapelessRecipe(new ItemStack(itemlye,1), itemsodiumraw, Items.water_bucket);
	GameRegistry.addShapelessRecipe(new ItemStack(itemlyesolution,1), new ItemStack(itemlye), new ItemStack(Items.potionitem, 0));
}
	//Shaped//
@EventHandler
public void init(FMLInitializationEvent event)
{
	//Generator Registry//
	 GameRegistry.registerWorldGenerator(new SynthOreGen(), 5);
}
}

 

Help is MASSIVELY APPRECIATED!!!

 

Posted

NEVER create a new TileEntity instance. Always retrieve it from the world:

// in both of your IGuiHandler methods, you have something similar to this:
return new ContainerElectrolyzer(player.inventory, new TileMachineElectrolyzer());

// which should instead look like this:
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof TileMachineElectrolyzer) {
   return new ContainerElectrolyzer(player.inventory, (TileMachineElectrolyzer) te);
}

The reason is that the data is all stored with the TileEntity in existence already; if you create a new one, obviously it has no data yet, and any data you give it will never be saved because it doesn't exist in the world, only in memory.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.