package com.yolp900.itsjustacharm.util;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class NBTHelper {
// ItemStack NBT Data
public static NBTTagCompound getTag(ItemStack stack) {
if (!stack.hasTagCompound())
stack.setTagCompound(new NBTTagCompound());
return stack.getTagCompound();
}
public static void setString(ItemStack stack, String key, String value) {
getTag(stack).setString(key, value);
}
public static String getString(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
if (!tag.hasKey(key)) return null;
return tag.getString(key);
}
public static void setInt(ItemStack stack, String key, int value) {
getTag(stack).setInteger(key, value);
}
public static int getInt(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
if (!tag.hasKey(key)) return -1;
return tag.getInteger(key);
}
public static void setIntArray(ItemStack stack, String key, int[] value) {
getTag(stack).setIntArray(key, value);
}
public static int[] getIntArray(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
if (!tag.hasKey(key)) return new int[0];
return tag.getIntArray(key);
}
public static void setBoolean(ItemStack stack, String key, boolean value) {
getTag(stack).setBoolean(key, value);
}
public static boolean getBoolean(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
return tag.hasKey(key) && tag.getBoolean(key);
}
public static void setNBTTag(ItemStack stack, String key, NBTTagCompound value) {
getTag(stack).setTag(key, value);
}
public static NBTTagCompound getNBTTag(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
if (!tag.hasKey(key)) return null;
return tag.getCompoundTag(key);
}
public static void setItemStack(ItemStack stack, String key, ItemStack value) {
setNBTTag(stack, key, value.writeToNBT(new NBTTagCompound()));
}
public static ItemStack getItemStack(ItemStack stack, String key) {
NBTTagCompound tag = getTag(stack);
if (!tag.hasKey(key)) return null;
NBTTagCompound nbtStack = getNBTTag(stack, key);
if (nbtStack == null) return null;
return ItemStack.loadItemStackFromNBT(nbtStack);
}
// Player NBT Data
public static NBTTagCompound getTag(EntityPlayer player) {
return player.getEntityData();
}
public static void setString(EntityPlayer player, String key, String value) {
getTag(player).setString(key, value);
}
public static String getString(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
if (!tag.hasKey(key)) return null;
return tag.getString(key);
}
public static void setInt(EntityPlayer player, String key, int value) {
getTag(player).setInteger(key, value);
}
public static int getInt(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
if (!tag.hasKey(key)) return -1;
return tag.getInteger(key);
}
public static void setIntArray(EntityPlayer player, String key, int[] value) {
getTag(player).setIntArray(key, value);
}
public static int[] getIntArray(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
if (!tag.hasKey(key)) return new int[0];
return tag.getIntArray(key);
}
public static void setBoolean(EntityPlayer player, String key, boolean value) {
getTag(player).setBoolean(key, value);
}
public static boolean getBoolean(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
return tag.hasKey(key) && tag.getBoolean(key);
}
public static void setNBTTag(EntityPlayer player, String key, NBTTagCompound value) {
getTag(player).setTag(key, value);
}
public static NBTTagCompound getNBTTag(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
if (!tag.hasKey(key)) return null;
return tag.getCompoundTag(key);
}
public static void setItemStack(EntityPlayer player, String key, ItemStack value) {
setNBTTag(player, key, value.writeToNBT(new NBTTagCompound()));
}
public static ItemStack getItemStack(EntityPlayer player, String key) {
NBTTagCompound tag = getTag(player);
if (!tag.hasKey(key)) return null;
NBTTagCompound nbtStack = getNBTTag(player, key);
if (nbtStack == null) return null;
return ItemStack.loadItemStackFromNBT(nbtStack);
}
}