Jump to content

Ghost Item Problem


Moritz

Recommended Posts

Now i have a problem with my Ghost item.

 

It does Dissapier after you relog the game.

 

Here is my code. i did find out which function it is but not directly what directly the problem is.

 

 

Here my Tile Entity. I know it is a tile problem.

package speiger.src.tinychest.common.tileentity.tinychest;

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.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class TransDimensionaleTinyChest extends TileEntity implements IInventory
{
public ItemStack[] chest;
public String invName;
public int facing = 0;
public int[] storedItemStackSize = new int[9];
public ItemStack[] ID = new ItemStack[9];
public int sizes;
public int MaxStackSize = 64;
public boolean update = false;

public TransDimensionaleTinyChest(String name, int size)
{
	chest = new ItemStack[(size*3)];
	invName = name;
	sizes = size;
}

@SideOnly(Side.CLIENT)
public int getStorage(int par1)
{
	return (int)storedItemStackSize[par1];
}


@Override
public int getSizeInventory()
{
	return chest.length;
}


    public ItemStack getStackInSlot(int par1)
    {
        return this.chest[par1];
    }


    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.chest[par1] != null)
        {
            ItemStack var3;

            if (this.chest[par1].stackSize <= par2)
            {
                var3 = this.chest[par1];
                this.chest[par1] = null;
                return var3;
            }
            else
            {
                var3 = this.chest[par1].splitStack(par2);

                if (this.chest[par1].stackSize == 0)
                {
                    this.chest[par1] = null;
                }

                return var3;
            }
        }
        else
        {
            return null;
        }
    }

    
    public ItemStack getStackInSlotOnClosing(int par1)
    {
        if (this.chest[par1] != null)
        {
            ItemStack var2 = this.chest[par1];
            this.chest[par1] = null;
            return var2;
        }
        else
        {
            return null;
        }
    }

    
    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
    {
        this.chest[par1] = par2ItemStack;

        if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
        {
            par2ItemStack.stackSize = this.getInventoryStackLimit();
        }
    }

    
    public String getInvName()
    {
        return invName;
    }



public boolean isUseableByPlayer(EntityPlayer var1) 
{
	return true;
}


public void openChest() 
{
}

@Override
    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readFromNBT(par1NBTTagCompound);
        
        NBTTagList var5 = par1NBTTagCompound.getTagList("Items2");
        this.ID = new ItemStack[this.getSizeInventory()];

        for (int var6 = 0; var6 < var5.tagCount(); ++var6)
        {
            NBTTagCompound var7 = (NBTTagCompound)var5.tagAt(var6);
            byte var8 = var7.getByte("Slot2");

            if (var8 >= 0 && var8 < this.ID.length)
            {
                this.ID[var8] = ItemStack.loadItemStackFromNBT(var7);
            }
        }
        
        
        NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
        this.chest = new ItemStack[this.getSizeInventory()];
        for (int var3 = 0; var3 < var2.tagCount(); ++var3)
        {
            NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
            byte var6 = var4.getByte("Slot");

            if (var6 >= 0 && var6 < this.chest.length)
            {
                this.chest[var6] = ItemStack.loadItemStackFromNBT(var4);
            }
        }
        

        
        for(int i = 0;i<this.storedItemStackSize.length;i++)
        {
        	this.storedItemStackSize[i] = par1NBTTagCompound.getInteger("StoredItems"+i);
        }
        
        facing = par1NBTTagCompound.getInteger("faceing");
        MaxStackSize = par1NBTTagCompound.getInteger("MaxStackSizes");
    }

    /**
     * Writes a tile entity to NBT.
     */
@Override
    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeToNBT(par1NBTTagCompound);
        

        
        NBTTagList var5 = new NBTTagList();
        for (int var6 = 0; var6 < this.ID.length; ++var6)
        {
            if (this.ID[var6] != null)
            {
                NBTTagCompound var7 = new NBTTagCompound();
                var7.setByte("Slot2", (byte)var6);
                this.ID[var6].writeToNBT(var7);
                var5.appendTag(var7);
            }
        }
        par1NBTTagCompound.setTag("Items2", var5);
        
        NBTTagList var2 = new NBTTagList();
        for (int var3 = 0; var3 < this.chest.length; ++var3)
        {
            if (this.chest[var3] != null)
            {
                NBTTagCompound var4 = new NBTTagCompound();
                var4.setByte("Slot", (byte)var3);
                this.chest[var3].writeToNBT(var4);
                var2.appendTag(var4);
            }
        }
        par1NBTTagCompound.setTag("Items", var2);
        
        for(int i = 0;i<this.storedItemStackSize.length;i++)
        {
        	par1NBTTagCompound.setInteger("StoredItems"+i, this.storedItemStackSize[i]);
        }
        par1NBTTagCompound.setInteger("faceing", facing);
        par1NBTTagCompound.setInteger("MaxStackSizes", MaxStackSize);
    }

public void closeChest() 
{
}


@Override
public int getInventoryStackLimit()
{
	return 64;
}

    @Override
public Packet getDescriptionPacket() 
    {
    	NBTTagCompound var1 = new NBTTagCompound();
    	super.writeToNBT(var1);
    	var1.setInteger("faceing", facing);
        for(int i = 0;i<this.storedItemStackSize.length;i++)
        {
        	var1.setInteger("StoredItems"+i, this.storedItemStackSize[i]);
        }
    	return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, var1);
}

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt) 
{
	this.readFromNBT(pkt.customParam1);
}

//Eloraams code (1.2.5)
public void updateBlock()
    {
        int var1 = this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);
        this.worldObj.markBlockForRenderUpdate(this.xCoord, this.yCoord, this.zCoord);
        markBlockDirty(this.worldObj, this.xCoord, this.yCoord, this.zCoord);
    }

    public void markBlockDirty(World var0, int var1, int var2, int var3)
    {
        if (var0.blockExists(var1, var2, var3))
        {
            var0.getChunkFromBlockCoords(var1, var3).setChunkModified();
        }
    }


@Override
public void updateEntity()
{
	super.updateEntity();
	updateBlock();
	addItems();
	eatItems();
	showItems();
	if(update)
	{
		update = false;
		this.onInventoryChanged();
	}

}

   
//This function is the problem
public void showItems()
{


	for(int i = 0;i<sizes;i++)
	{
		if(ID[i] != null)
		{
			chest[i+(sizes*2)] = new ItemStack(ID[i].itemID, 1, ID[i].getItemDamage());
			update = true;
		}
		else
		{
			chest[i+(sizes*2)] = null;
			update = true;
		}
	}
}


public void eatItems()
{
	for(int i = 0; i<sizes;i++)
	{
		if(storedItemStackSize[i] < MaxStackSize)
		{
			ItemStack current = ID[i];
			if(chest[i] != null)
			{
				if(current != null && chest[i].itemID == current.itemID && chest[i].getItemDamage() == current.getItemDamage())
				{
					if(storedItemStackSize[i] + 1 <= MaxStackSize)
					{
						storedItemStackSize[i]+=1;
						chest[i].stackSize--;
						update = true;
						if(chest[i].stackSize <= 0)
						{
							chest[i] = null;
							update = true;
						}
					}
				}
				else if(current == null)
				{
					ID[i] = new ItemStack(chest[i].getItem(), 1, chest[i].getItemDamage());
					storedItemStackSize[i]+=1;
					chest[i].stackSize--;
					update = true;
					if(chest[i].stackSize <= 0)
					{
						chest[i] = null;
						update = true;
					}
				}
			}
		}
	}
}


public void addItems()
{
	for(int i = 0; i<sizes;i++)
	{
		ItemStack current = ID[i];
		if(current != null)
		{
			if(this.storedItemStackSize[i] > 0)
			{
				if(chest[i+sizes] != null && chest[i+sizes].itemID == current.itemID && chest[i+sizes].getItemDamage() == current.getItemDamage())
				{
					if(chest[i+sizes].stackSize + 1 <= 64 && chest[i+sizes].stackSize + 1 <= chest[i+sizes].getMaxStackSize())
					{
						chest[i+sizes].stackSize++;
						this.storedItemStackSize[i]-=1;
						update = true;
						if(this.storedItemStackSize[i] <= 0)
						{
							ID[i] = null;
							update = true;
						}
					}
				}
				else if(chest[i+sizes] == null)
				{
					chest[i+sizes] = new ItemStack(current.itemID, 1, current.getItemDamage());
					this.storedItemStackSize[i]-=1;
					update = true;
					if(this.storedItemStackSize[i] <= 0)
					{
						ID[i] = null;
						update = true;
					}
				}
			}
		}
	}
}
                
                //This function is will called by my packethandler
public void addStorage(int storage)
{
	int between = 64;

	between *= storage;
	between = between / 2;
	int secand = between / sizes;
	this.MaxStackSize += secand;

}
    
    

}


 

Here an extra video what showes the problem.

 

 

 

i hope you can help me.

 

Speiger.

Link to comment
Share on other sites

Well I see what you mean in the video, not sure exactly what's causing your problem, but what I can tell you is what I know about containers.

 

You do not need packets to sync items from client to server, whatever is placed inside a slot that's inside a container actually gets sync via the container class you have to create for your block.

 

So for instance on your block class file you would have a function called onBlockActivated and inside there you call your gui

you need to setup a guihandler that would switch between server and client and handle the packets for you. Now you most likely got all of this, but the syncing of items in the container takes place via the server side container class.

 

Secondly on your code you have public ItemStack[] chest; and public ItemStack[] ID = new ItemStack[9];

you then set your chest to a sizex3 and I have no idea what that size it suppose that depends on some condition.

 

Now you only still only have 1 container class assuming you are using a guihandler that is used for this and my guess is somewhere in there your container server side class file does not know what to do with all your slots, because it might not be mapped correctly or updated correctly. That woudl explain why once you close your gui and reopen it the gui doesn't display your item, so check inside the server side container file you created for this Tile Entity you will find your answer there.

 

 

Link to comment
Share on other sites

Ghost anything is almost always due to a mismatch between server and client.

When you log on again the server tells the client what it's inventor contains.

 

I'm guessing that you are giving the player the item on the Client side and noth on server/both :P

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

Ok my block activate function which has client/server packet inside is only for extending the storage. (MaxStackSize).

That is what my packet handler does.

 

The sizes*3 is the is only the thing what my chest.

I made the same system as ironchest.

The size of the chest has always 3 or you can / 3.

But thats not the problem.

 

Now to the ID thing. The ID stores the id of the item which is iniside of the inifinet extende able storage.

Thats it.

 

Now Mazetars comments:

 

I use an extra slot for the items. And i do block the slot so you can not click on it.

Thats why i could not get items out of my inventory as i opened the big version of the

Transdimensional tiny chest^^"

 

So what could be the problem^^?

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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