I have a problem with store data on items...
What I want to do is...
When you sneak & right click, GUI will popup and ask player to enter some data. When click Done (or OK) ,data that player entered will be stored within items. And when they sneak & right click again, they can edit their entered data. Right click will use data stored in the item.
Problems is...
Data is stored but still being reset.
This is what I tried to simplify it (So it easier to understand and helps me out)...
When player right click, the number will be told. And when player sneak & right click, GUI will open and store edited data.
Testing...
Right click several times without sneak & right click.
Expectation:
A random number called X being told when right click and does not change when right click again.
Reality:
As screenshot showing...
Source code for item
private void useItem(ItemStack itemStack, World world, EntityPlayer player, boolean addMode) {
data = new NBTData(itemStack);
if(world.isRemote){
//Is client
if(!data.hasKey("test")){
data.set("test", new Random().nextInt(100));
data.save();
}
if(player.isSneaking()){
player.openGui(MyMod.instance, 1, world, (int) player.posX, (int) player.posY, (int) player.posZ);
}else{
player.addChatMessage("Number is " + data.get("test", -1));
}
}
}
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
if(!player.capabilities.isCreativeMode){
itemStack.stackSize = 0;
return itemStack;
}
//On air
useItem(itemStack, world, player, false);
return itemStack;
}
public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int blockX, int blockY, int blockZ, int face, float par8, float par9, float par10) {
if(!player.capabilities.isCreativeMode){
--itemStack.stackSize;
return false;
}
//On block
useItem(itemStack, world, player, true);
return true;
}
Screenshot:
http://s9.postimg.org/cu96dubjv/2013_04_25_02_32_20.jpg[/img]
Notes:
NBTData is my own wrapper of NBTTagCompound for easier use
That "data" will be used with GUI. Store data will do the same (data.save()) on GUI class.
data.save() will store all data on NBTTagCompound to ItemStack (using ItemStack.setTagCompound)