Jump to content

[1.10] Issues with NBT


KeeganDeathman

Recommended Posts

So, I have an issue.

I am attempting to update, and my code for saving inventory and an int in my 1st tier power gen doesn't work! I copied in the one from a working block, and the vanilla chest, but no cigar!

Here's my code

package keegan.labstuff.tileentity;

import keegan.labstuff.LabStuffMain;
import keegan.labstuff.blocks.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.network.*;
import net.minecraft.network.play.server.*;
import net.minecraft.tileentity.*;
import net.minecraft.util.ITickable;

public class TileEntityPowerFurnace extends TileEntity implements IInventory, ITickable
{

private ItemStack[] chestContents = new ItemStack[1];
private int burnTime;

public TileEntityPowerFurnace()
{
	burnTime = 0;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);
    
    NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < this.chestContents.length; ++i)
        {
            if (this.chestContents[i] != null)
            {
                NBTTagCompound nbttagcompound = new NBTTagCompound();
                nbttagcompound.setByte("Slot", (byte)i);
                this.chestContents[i].writeToNBT(nbttagcompound);
                nbttaglist.appendTag(nbttagcompound);
            }
        }

        nbt.setTag("Items", nbttaglist);
    nbt.setInteger("burnTime", getBurnTime());
    
    return nbt;
}


@Override
public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(nbt);
    
    NBTTagList list = nbt.getTagList("Items", 10);
    NBTTagList nbttaglist = nbt.getTagList("Items", 10);

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound.getByte("Slot") & 255;

            if (j >= 0 && j < this.chestContents.length)
            {
                this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
            }
        }
    setBurnTime(nbt.getInteger("burnTime"));
}

@Override
public void update()
{
	if(worldObj.isRemote == false)
	{
		if(getBurnTime() == 0)
		{
			if(getStackInSlot(0) != null && TileEntityFurnace.isItemFuel(getStackInSlot(0)))
			{
				setBurnTime(getBurnTime() + TileEntityFurnace.getItemBurnTime(getStackInSlot(0)));
				decrStackSize(0, 1);
				worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockPowerFurnace.POWERED, true), 1+2);
			}
			else
				worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockPowerFurnace.POWERED, false), 1+2);
		}
		else
		{
			burnTime -= 1;
			if(worldObj.getTileEntity(pos.up()) instanceof TileEntityPower)
				((TileEntityPower)worldObj.getTileEntity(pos.up())).addPower(10, this);
			worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockPowerFurnace.POWERED, true), 1+2);
		}
	}

}

@Override
public int getSizeInventory() 
{
	// TODO Auto-generated method stub
	return chestContents.length;
}

@Override
public ItemStack getStackInSlot(int slot) 
{
	// TODO Auto-generated method stub
	return chestContents[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amt) 
{
	// TODO Auto-generated method stub
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
			if (stack.stackSize <= amt)
			{
				setInventorySlotContents(slot, null);
			}
			else
			{
				stack = stack.splitStack(amt);
				if (stack.stackSize == 0)
				{
					setInventorySlotContents(slot, null);
				}
			}
	}
	return stack;
}

@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
	NBTTagCompound syncData = new NBTTagCompound();
	syncData.setInteger("burnTime", burnTime);
	return new SPacketUpdateTileEntity(pos, 1, syncData);
}

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
	burnTime = pkt.getNbtCompound().getInteger("burnTime");
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) 
{
	chestContents[slot] = itemstack;
	if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
	{
		itemstack.stackSize = getInventoryStackLimit();
	}

}

@Override
public int getInventoryStackLimit() 
{
	// TODO Auto-generated method stub
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) 
{
	// TODO Auto-generated method stub
	return true;
}


@Override
public boolean isItemValidForSlot(int slot, ItemStack item) {
	return TileEntityFurnace.isItemFuel(item);
}

@Override
public String getName() {
	// TODO Auto-generated method stub
	return "Power Furnace";
}

@Override
public boolean hasCustomName() {
	// TODO Auto-generated method stub
	return true;
}

@Override
public ItemStack removeStackFromSlot(int index) {
	// TODO Auto-generated method stub
        return ItemStackHelper.getAndRemove(this.chestContents, index);
}

@Override
public int getField(int id) {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public void setField(int id, int value) {
	// TODO Auto-generated method stub

}

@Override
public int getFieldCount() {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public void clear() {

        for (int i = 0; i < this.chestContents.length; ++i)
        {
            this.chestContents[i] = null;
        }		
}

@Override
public void openInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}

@Override
public void closeInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}

public int getBurnTime() {
	return burnTime;
}

public void setBurnTime(int burnTime) {
	this.burnTime = burnTime;
}

}

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

Link to comment
Share on other sites

Don't use

IInventory

, use the new

Capability

system.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Use

SlotItemHandler

instead of

Slot

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Thanks!

The inventory still doesn't persist, but I have an error now!

java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_91]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_91]
at net.minecraft.util.Util.runTask(Util.java:26) [util.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1108) [Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:406) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.NullPointerException
at net.minecraftforge.items.ItemStackHandler.deserializeNBT(ItemStackHandler.java:179) ~[itemStackHandler.class:?]
at keegan.labstuff.tileentity.TileEntityPowerFurnace.readFromNBT(TileEntityPowerFurnace.java:53) ~[TileEntityPowerFurnace.class:?]
at net.minecraft.tileentity.TileEntity.handleUpdateTag(TileEntity.java:374) ~[TileEntity.class:?]
at net.minecraft.client.network.NetHandlerPlayClient.handleChunkData(NetHandlerPlayClient.java:803) ~[NetHandlerPlayClient.class:?]
at net.minecraft.network.play.server.SPacketChunkData.processPacket(SPacketChunkData.java:110) ~[sPacketChunkData.class:?]
at net.minecraft.network.play.server.SPacketChunkData.processPacket(SPacketChunkData.java:20) ~[sPacketChunkData.class:?]
at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_91]
at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_91]
at net.minecraft.util.Util.runTask(Util.java:25) ~[util.class:?]
... 15 more

 

And the updated NBT code

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);


        nbt.setTag("Items", handler.serializeNBT());
    nbt.setInteger("burnTime", getBurnTime());
    
    return nbt;
}


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

    handler.deserializeNBT((NBTTagCompound) nbt.getTag("Items"));
    
    setBurnTime(nbt.getInteger("burnTime"));
}

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

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.