Jump to content

[1.7.10] Using NBT to store items in a tileentity ?


Looke81

Recommended Posts

hello all, I have made a tile entity that can store items but when i unload the world the items are gone. I know you have to use read to and write to NBT but im not sure how.

As of right now i just took the tileentitychest code but it doesn't seem to work.I really don't have a great understanding of nbt so please be patient with me.

 

My Code:

 

public void writeToNBT(NBTTagCompound tag)

{

    super.writeToNBT(tag);

    NBTTagList nbttaglist = new NBTTagList();

 

    for (int i = 0; i < this.inv.length; ++i)

    {

        if (this.inv != null)

        {

            NBTTagCompound nbttagcompound1 = new NBTTagCompound();

            nbttagcompound1.setByte("slot", (byte)i);

            this.inv.writeToNBT(nbttagcompound1);

            nbttaglist.appendTag(nbttagcompound1);

        }

    }

    }

 

public void readFromNBT(NBTTagCompound p_145839_1_)

{

    super.readFromNBT(p_145839_1_);

    NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);

    this.inv = new ItemStack[this.getSizeInventory()];

 

 

    for (int i = 0; i < nbttaglist.tagCount(); ++i)

    {

        NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);

        int j = nbttagcompound1.getByte("slot") & 255;

 

        if (j >= 0 && j < this.inv.length)

        {

            this.inv[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);

        }

    }

}

 

 

 

 

BioWarfare Mod: http://goo.gl/BYWQty

Link to comment
Share on other sites

I think you forgot to add brackets after inv sometimes.

For example you have this:

     for (int i = 0; i < this.inv.length; ++i)
    {
        if (this.inv != null)
        {
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
            nbttagcompound1.setByte("slot", (byte)i);
            this.inv.writeToNBT(nbttagcompound1);
            nbttaglist.appendTag(nbttagcompound1);
        }
    }

 

It should be this:

 

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

 

Do the same for readNBT and it should work

Link to comment
Share on other sites

Ah I see, this tutorial gives some insight in NBT

http://www.minecraftforge.net/wiki/Tile_Entities

 

Your whole code should be like this:

 

public void writeToNBT(NBTTagCompound tag)
{
    super.writeToNBT(tag);
    NBTTagList nbttaglist = new NBTTagList();

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

public void readFromNBT(NBTTagCompound p_145839_1_)
{
    super.readFromNBT(p_145839_1_);
    NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
    this.inv = new ItemStack[this.getSizeInventory()];


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

        if (j >= 0 && j < this.inv.length)
        {
            this.inv[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
        }
    }
}

 

If that code doesn't work the problem lies elsewhere. Did you register the TileEntity in the GameRegistry?

Link to comment
Share on other sites

If that code doesn't work the problem lies elsewhere. Did you register the TileEntity in the GameRegistry?

 

Game crashes if you don't, hehe.  I've forgotten twice in my current project and when I did the game would immediately crash as soon as the block was placed in the world.

 

Other two functions you should go ahead and implement now are:

 

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

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

 

And you'll also need this line any time you change the inventory (important when dealing with hoppers and similar!)

 

if (worldObj != null) worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

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.

Link to comment
Share on other sites

Ok i have implemented everything but now when i use the inventory this happens:

 

 

 

 

[21:17:19] [server thread/INFO]: Player238 joined the game

[21:17:21] [server thread/ERROR]: Encountered an unexpected exception

net.minecraft.util.ReportedException: Exception ticking world

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:708) ~[MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624) ~[MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762) [MinecraftServer$2.class:?]

Caused by: java.lang.RuntimeException: class com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate is missing a mapping! This is a bug!

at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100) ~[TileEntity.class:?]

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.writeToNBT(TileEntityAgarPlate.java:61) ~[TileEntityAgarPlate.class:?]

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.getDescriptionPacket(TileEntityAgarPlate.java:97) ~[TileEntityAgarPlate.class:?]

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendTileToAllPlayersWatchingChunk(PlayerManager.java:638) ~[PlayerManager$PlayerInstance.class:?]

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendChunkUpdate(PlayerManager.java:580) ~[PlayerManager$PlayerInstance.class:?]

at net.minecraft.server.management.PlayerManager.updatePlayerInstances(PlayerManager.java:84) ~[PlayerManager.class:?]

at net.minecraft.world.WorldServer.tick(WorldServer.java:195) ~[WorldServer.class:?]

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:702) ~[MinecraftServer.class:?]

... 4 more

[21:17:21] [server thread/ERROR]: This crash report has been saved to: C:\Users\Daniel\Desktop\MineCraft Java Script\Minecraft Mods\eclipse\.\crash-reports\crash-2014-11-24_21.17.21-server.txt

[21:17:21] [server thread/INFO]: Stopping server

[21:17:21] [server thread/INFO]: Saving players

---- Minecraft Crash Report ----

// I feel sad now :(

 

Time: 24/11/14 21:17

Description: Exception ticking world

 

java.lang.RuntimeException: class com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate is missing a mapping! This is a bug!

at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100)

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.writeToNBT(TileEntityAgarPlate.java:61)

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.getDescriptionPacket(TileEntityAgarPlate.java:97)

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendTileToAllPlayersWatchingChunk(PlayerManager.java:638)

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendChunkUpdate(PlayerManager.java:580)

at net.minecraft.server.management.PlayerManager.updatePlayerInstances(PlayerManager.java:84)

at net.minecraft.world.WorldServer.tick(WorldServer.java:195)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:702)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100)

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.writeToNBT(TileEntityAgarPlate.java:61)

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.getDescriptionPacket(TileEntityAgarPlate.java:97)

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendTileToAllPlayersWatchingChunk(PlayerManager.java:638)

at net.minecraft.server.management.PlayerManager$PlayerInstance.sendChunkUpdate(PlayerManager.java:580)

at net.minecraft.server.management.PlayerManager.updatePlayerInstances(PlayerManager.java:84)

at net.minecraft.world.WorldServer.tick(WorldServer.java:195)

 

-- Affected level --

Details:

Level name: just another test

All players: 1 total; [EntityPlayerMP['Player238'/394, l='just another test', x=30.36, y=71.25, z=167.88]]

Chunk stats: ServerChunkCache: 625 Drop: 0

Level seed: -5804106722026325298

Level generator: ID 00 - default, ver 1. Features enabled: true

Level generator options:

Level spawn location: World: (32,64,180), Chunk: (at 0,4,4 in 2,11; contains blocks 32,0,176 to 47,255,191), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)

Level time: 92388 game time, 12854 day time

Level dimension: 0

Level storage version: 0x04ABD - Anvil

Level weather: Rain time: 78930 (now: false), thunder time: 20926 (now: false)

Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true

Stacktrace:

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:702)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

 

-- System Details --

Details:

Minecraft Version: 1.7.10

Operating System: Windows 7 (amd64) version 6.1

Java Version: 1.7.0_21, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 772486952 bytes (736 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

IntCache: cache: 0, tcache: 0, allocated: 13, tallocated: 95

FML: MCP v9.05 FML v7.10.24.1187 Minecraft Forge 10.13.0.1187 4 mods loaded, 4 mods active

mcp{9.05} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

FML{7.10.24.1187} [Forge Mod Loader] (forgeSrc-1.7.10-10.13.0.1187.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Forge{10.13.0.1187} [Minecraft Forge] (forgeSrc-1.7.10-10.13.0.1187.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

BioWarfare{0.0.1 Alpha} [bioWarfare] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Profiler Position: N/A (disabled)

Vec3 Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used

Player Count: 1 / 8; [EntityPlayerMP['Player238'/394, l='just another test', x=30.36, y=71.25, z=167.88]]

Type: Integrated Server (map_client.txt)

Is Modded: Definitely; Client brand changed to 'fml,forge'

#@!@# Game crashed! Crash report saved to: #@!@# .\crash-reports\crash-2014-11-24_21.17.21-server.txt

[21:17:21] [Client thread/INFO] [FML]: Waiting for the server to terminate/save.

[21:17:21] [server thread/INFO]: Saving worlds

[21:17:21] [server thread/INFO]: Saving chunks for level 'just another test'/Overworld

[21:17:21] [server thread/ERROR] [FML]: A TileEntity type com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate has throw an exception trying to write state. It will not persist. Report this to the mod author

java.lang.RuntimeException: class com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate is missing a mapping! This is a bug!

at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100) ~[TileEntity.class:?]

at com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate.writeToNBT(TileEntityAgarPlate.java:61) ~[TileEntityAgarPlate.class:?]

at net.minecraft.world.chunk.storage.AnvilChunkLoader.writeChunkToNBT(AnvilChunkLoader.java:395) [AnvilChunkLoader.class:?]

at net.minecraft.world.chunk.storage.AnvilChunkLoader.saveChunk(AnvilChunkLoader.java:204) [AnvilChunkLoader.class:?]

at net.minecraft.world.gen.ChunkProviderServer.safeSaveChunk(ChunkProviderServer.java:289) [ChunkProviderServer.class:?]

at net.minecraft.world.gen.ChunkProviderServer.saveChunks(ChunkProviderServer.java:342) [ChunkProviderServer.class:?]

at net.minecraft.world.WorldServer.saveAllChunks(WorldServer.java:876) [WorldServer.class:?]

at net.minecraft.server.MinecraftServer.saveAllWorlds(MinecraftServer.java:380) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.stopServer(MinecraftServer.java:415) [MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.stopServer(IntegratedServer.java:266) [integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762) [MinecraftServer$2.class:?]

[21:17:21] [server thread/INFO]: Saving chunks for level 'just another test'/Nether

[21:17:21] [server thread/INFO]: Saving chunks for level 'just another test'/The End

[21:17:21] [server thread/INFO] [FML]: Unloading dimension 0

[21:17:21] [server thread/INFO] [FML]: Unloading dimension -1

[21:17:21] [server thread/INFO] [FML]: Unloading dimension 1

[21:17:21] [server thread/INFO] [FML]: Applying holder lookups

[21:17:21] [server thread/INFO] [FML]: Holder lookups applied

[21:17:21] [server thread/INFO] [FML]: The state engine was in incorrect state SERVER_STOPPING and forced into state SERVER_STOPPED. Errors may have been discarded.

[21:17:21] [Client thread/INFO] [FML]: Server terminated.

AL lib: (EE) alc_cleanup: 1 device not closed

BioWarfare Mod: http://goo.gl/BYWQty

Link to comment
Share on other sites

java.lang.RuntimeException: class com.Looke81.BioWarfare.tileentity.TileEntityAgarPlate is missing a mapping! This is a bug!

 

You didn't register your TE. :)

 

If that code doesn't work the problem lies elsewhere. Did you register the TileEntity in the GameRegistry?

 

Game crashes if you don't, hehe.

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.

Link to comment
Share on other sites

You're missing:

tc.setTag("Items", nbttaglist);

in writeToNBT.  Blame Ives.

 

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

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

        tc.setTag("Items", nbttaglist); //you're missing this line
        //tc.setInteger("siftTime", siftTime); //this is how you would set other variables that need to be saved, such as burnTime for a furnace
    }

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.

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.



×
×
  • Create New...

Important Information

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