Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

TileEntity inventory without using IInventory?


memcallen
 Share

Recommended Posts

I see a few of problems,

 

You're storing the ItemStack's NBTTagCompound twice.

You never call markDirty()

You never do null checks.

You do a lot of stuff on both Sides, which can cause one Side to have the wrong information.

You are setting the player's held Item, without making sure it's empty first.

Link to comment
Share on other sites

This is the Itemstack writeToNBT method:

 

    /**
     * Write the stack fields to a NBT object. Return the new NBT object.
     */
    public NBTTagCompound writeToNBT(NBTTagCompound p_77955_1_)
    {
        p_77955_1_.setShort("id", (short)Item.getIdFromItem(this.field_151002_e));
        p_77955_1_.setByte("Count", (byte)this.stackSize);
        p_77955_1_.setShort("Damage", (short)this.itemDamage);

        if (this.stackTagCompound != null)
        {
            p_77955_1_.setTag("tag", this.stackTagCompound);
        }

        return p_77955_1_;
    }

 

It checks and stores the stackTagCompound, which you store a second time in your writeToNBT:

 

			nbt.setTag("ItemStack"+i,NBT); // Stores the NBT.stackTagCompound Tag
		nbt.setTag("ItemStackData"+i, inv[i].stackTagCompound); // Duplicate Tag

 

 

 

markDirty() is a method on TileEntity. It ensures that the TileEntity is saved. Even if Minecraft would normally not save it, for example, when no blocks have changed.

Link to comment
Share on other sites

ok cool I got it to work. I changed my code to:

package com.example.gammacraft.TileEntity;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class MultiToolModifierTileEntity extends TileEntity {

public ItemStack[] inv;

public MultiToolModifierTileEntity() {
	inv=new ItemStack[10];
}

public Packet getDescriptionPacket() {
        NBTTagCompound nbtTag = new NBTTagCompound();
        this.writeToNBT(nbtTag);
        return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}

public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
        readFromNBT(packet.func_148857_g());
}

public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);

	for(int i =0;i!=inv.length;i++){
		NBTTagCompound NBT = new NBTTagCompound();
		if(inv[i]!=null){
		inv[i].writeToNBT(NBT);
		nbt.setTag("ItemStack"+i,NBT);
		}
		System.out.println("saving:"+i);
	}

	this.markDirty();
}

public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	for(int i =0;i!=inv.length;i++){
		NBTTagCompound itemstack = nbt.getCompoundTag("ItemStack"+i);

		try{
		inv[i]=ItemStack.loadItemStackFromNBT(itemstack);
		}catch(Exception e){
			e.printStackTrace();
		}

		System.out.println("reading:"+i);

	}

}

}

The proud(ish) developer of Ancients

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.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.