Jump to content

Recommended Posts

Posted

I have an Item in an IExtendedEntityProperties that I want to save into the NBT.... how would I go about doing this?

 

I have absolutely no clue what so ever on how to do this, so all help is appreciated.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Posted

Look at the code from playerinventory.  If you just have a single itemstack, you don't need the list approach, but you can leverage it to what you need.

 

 

 

public NBTTagList writeToNBT(NBTTagList par1NBTTagList)

    {

        int i;

        NBTTagCompound nbttagcompound;

 

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

        {

            if (this.mainInventory != null)

            {

                nbttagcompound = new NBTTagCompound();

                nbttagcompound.setByte("Slot", (byte)i);

                this.mainInventory.writeToNBT(nbttagcompound);

                par1NBTTagList.appendTag(nbttagcompound);

            }

        }

 

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

        {

            if (this.armorInventory != null)

            {

                nbttagcompound = new NBTTagCompound();

                nbttagcompound.setByte("Slot", (byte)(i + 100));

                this.armorInventory.writeToNBT(nbttagcompound);

                par1NBTTagList.appendTag(nbttagcompound);

            }

        }

 

        return par1NBTTagList;

    }

 

    /**

    * Reads from the given tag list and fills the slots in the inventory with the correct items.

    */

    public void readFromNBT(NBTTagList par1NBTTagList)

    {

        this.mainInventory = new ItemStack[36];

        this.armorInventory = new ItemStack[4];

 

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

        {

            NBTTagCompound nbttagcompound = par1NBTTagList.getCompoundTagAt(i);

            int j = nbttagcompound.getByte("Slot") & 255;

            ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound);

 

            if (itemstack != null)

            {

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

                {

                    this.mainInventory[j] = itemstack;

                }

 

                if (j >= 100 && j < this.armorInventory.length + 100)

                {

                    this.armorInventory[j - 100] = itemstack;

                }

            }

        }

    }

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I have already tried that. When I log off and back on, the item has been set to null again. Also, that saves an ITEMSTACK - I just want to save an item...

 

Here is the code:

package com.kwibble.dendri.entity.player;

import com.kwibble.dendri.item.ItemDendrikBelt;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

import com.kwibble.dendri.ModDendri;

public class PlayerInformation implements IExtendedEntityProperties
{
public static final String IDENTIFIER = ModDendri.MODID + "_playerProperties";

private static final String IS_OVERLAY_MINIMIZED_IDENTIFIER = IDENTIFIER + "_isOverlayMinimized";
private static final String CURRENT_DENDRIK_BELT_IDENTIFIER = IDENTIFIER + "_currentDendrikbelt";

public static PlayerInformation forPlayer(Entity player) {
        return (PlayerInformation) player.getExtendedProperties(IDENTIFIER);
    }

private EntityPlayer player;

private ItemStack currentDendrikBelt;

private boolean isOverlayMinimized;

public PlayerInformation(EntityPlayer player) {
        this.player = player;
    }

@Override
public void init(Entity entity, World world)
{

}

@Override
public void saveNBTData(NBTTagCompound compound)
{
	System.out.println("Saving NBT Data:");
	compound.setBoolean(IS_OVERLAY_MINIMIZED_IDENTIFIER, this.isOverlayMinimized);
	System.out.println("Saved isOverlayMinimized with value of: " + this.isOverlayMinimized);
	if (this.currentDendrikBelt != null)
	{
		compound.setTag(CURRENT_DENDRIK_BELT_IDENTIFIER, this.currentDendrikBelt.writeToNBT(new NBTTagCompound()));
		System.out.println("Saved currentDendrikbelt with value of: " + this.currentDendrikBelt);
	}
	System.out.println("NBT Data Saved");
}

@Override
public void loadNBTData(NBTTagCompound compound)
{
	System.out.println("Loading NBT Data:");
	this.isOverlayMinimized = compound.getBoolean(IS_OVERLAY_MINIMIZED_IDENTIFIER);
	System.out.println("Loaded isOverlayMinimized with value of: " + this.isOverlayMinimized);
	this.currentDendrikBelt = ItemStack.loadItemStackFromNBT(compound.getCompoundTag(CURRENT_DENDRIK_BELT_IDENTIFIER));
	System.out.println("Loaded currentDendrikBelt with value of: " + this.currentDendrikBelt);
	System.out.println("Loaded NBT Data");
}

public void setCurrentDendrikBelt(Item belt)
{
	if (belt instanceof ItemDendrikBelt)
		this.currentDendrikBelt = new ItemStack(belt);
	else
		System.out.println("A Dendrik Belt was not passed in");
}

public Item getCurrentDendrikBelt()
{
	return this.currentDendrikBelt != null ? this.currentDendrikBelt.getItem() : new ItemStack(Blocks.stone).getItem();
}

public void setIsOverlayMinimized(boolean isOverlayMinimized)
{
	this.isOverlayMinimized = isOverlayMinimized;
}

public boolean getIsOverlayMinimized()
{
	return this.isOverlayMinimized;
}
}

 

Thanks delpi

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Posted

I'm a little confused as to why you want to store just the item.  Anytime you are using it from that, you are converting it to an itemstack as well as its the norm.  If you want to do an item, you are going to have to save its ID or name.

 

 

Anyhow, your problem is

 

this.currentDendrikBelt = ItemStack.loadItemStackFromNBT(compound.getCompoundTag(CURRENT_DENDRIK_BELT_IDENTIFIER));

 

should be

 

currentDendrikBelt.loadItemStackFromNBT(compound.getCompoundTag(CURRENT_DENDRIK_BELT_IDENTIFIER));

 

but you would need to ahve initilized it before that or I think you will get some wierd error.

 

Its possible yours will work, but I don't think so.  Might work if you copy the itemstack or such.

 

I assume your System messages indicate from the prints it should ahve worked?

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Oh - I derped so bad. I had an event that was setting it to null accidentally... Thanks for the help delpi!

We all stuff up sometimes... But I seem to be at the bottom of that pot.

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.