Named Binary Tags - 1.8
NBT is a system that a lot of programs use to store data. In minecraft, it's used to store information in the player.dat and level.dat files for unique items and entity data. And example is a creeper. When a creeper starts the explode animation, it writes a tag to the .dat folder, saying it's been exploding for "x" amount of seconds. Once "x" equals the time that the creeper fuse is, the NBT lets minecraft know that the creeper can explode. In this tutorial, I will show you how to write NBTs for items. And maybe for blocks later :P
For items it's quite simple. You don't need any tile entities or anything :D
Anyway, add this to your item class:
@Override
public void onUpdate(ItemStack itemstack, World world, Entity entity, int metadata, boolean bool)
{
if (itemstack.getTagCompund() == null)
{
itemstack.getTagCompund() = new NBTTagCompound(); //or itemstack.setTagCompound(new NBTTagCompound());
}
}
This just tells minecraft that you want a new NBT tag written for this item. To create an NBT, add this directly below the if block:
itemstack.getTagCompund() .setString("Owner", par3EntityPlayer.username);
This is an example of a string NBT. The first argument is a string, no matter what type of NBT you create. This the NBT's name.
The next tag depends on the NBT. Here it's a string, so I have it becoming the users in game name. You can set this to any string you want.
itemstack.getTagCompund() .setInteger("Coins", 0);
This is an example of an integer NBT. You must declare an NBT in the onCreated method before you change it or write it to a dat file.
Now to get or set the NBT:
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player)
{
itemstack.getTagCompund() .setInteger( "Coins", 1);
itemstack.damageItem(1, player);
return itemstack;
}
This line of code sets the NBT "Coins" to one and damages the item you're holding by one.
Next we need to get the value:
public void onUpdate(ItemStack itemstack, World world, Entity entity, int metadata, boolean bool)
{
if (itemstack.getTagCompund() .getInteger("Coins") >= 100)
{
((EntityLiving) entity).addPotionEffect((new PotionEffect(Potion.nightVision.getId(), 20 * 15, 0)));
}
}
If the NBT "Coins" is greater than or equal to 100, then the player will receive the potion effect Night Vision. Change this method to fit your needs.
That's pretty much it. Try some advanced tutorials now!
For items it's quite simple. You don't need any tile entities or anything :D
Anyway, add this to your item class:
@Override
public void onUpdate(ItemStack itemstack, World world, Entity entity, int metadata, boolean bool)
{
if (itemstack.getTagCompund() == null)
{
itemstack.getTagCompund() = new NBTTagCompound(); //or itemstack.setTagCompound(new NBTTagCompound());
}
}
This just tells minecraft that you want a new NBT tag written for this item. To create an NBT, add this directly below the if block:
itemstack.getTagCompund() .setString("Owner", par3EntityPlayer.username);
This is an example of a string NBT. The first argument is a string, no matter what type of NBT you create. This the NBT's name.
The next tag depends on the NBT. Here it's a string, so I have it becoming the users in game name. You can set this to any string you want.
itemstack.getTagCompund() .setInteger("Coins", 0);
This is an example of an integer NBT. You must declare an NBT in the onCreated method before you change it or write it to a dat file.
Now to get or set the NBT:
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player)
{
itemstack.getTagCompund() .setInteger( "Coins", 1);
itemstack.damageItem(1, player);
return itemstack;
}
This line of code sets the NBT "Coins" to one and damages the item you're holding by one.
Next we need to get the value:
public void onUpdate(ItemStack itemstack, World world, Entity entity, int metadata, boolean bool)
{
if (itemstack.getTagCompund() .getInteger("Coins") >= 100)
{
((EntityLiving) entity).addPotionEffect((new PotionEffect(Potion.nightVision.getId(), 20 * 15, 0)));
}
}
If the NBT "Coins" is greater than or equal to 100, then the player will receive the potion effect Night Vision. Change this method to fit your needs.
That's pretty much it. Try some advanced tutorials now!