diff options
Diffstat (limited to 'src/Java/gtPlusPlus/core/item')
-rw-r--r-- | src/Java/gtPlusPlus/core/item/base/BaseItemBrain.java | 108 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/item/general/ItemBlueprint.java | 225 |
2 files changed, 284 insertions, 49 deletions
diff --git a/src/Java/gtPlusPlus/core/item/base/BaseItemBrain.java b/src/Java/gtPlusPlus/core/item/base/BaseItemBrain.java new file mode 100644 index 0000000000..86cd1c8046 --- /dev/null +++ b/src/Java/gtPlusPlus/core/item/base/BaseItemBrain.java @@ -0,0 +1,108 @@ +package gtPlusPlus.core.item.base; + +import java.util.List; + +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.StatCollector; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +/* + * + * + Key Point: You can access the NBT compound data from the Item class (in those methods that pass an ItemStack), but the NBT compound can only be set on an ItemStack. + + The steps to add NBT data to an ItemStack: + Create or otherwise get an ItemStack of the desired item + Create an NBTTagCompound and fill it with the appropriate data + Call ItemStack#setTagCompound() method to set it. + + * + */ + +public class BaseItemBrain extends Item{ + // This is an array of all the types I am going to be adding. + String[] brainTypes = { "dead", "preserved", "fresh", "tasty" }; + + // This method allows us to have different language translation keys for + // each item we add. + @Override + public String getUnlocalizedName(ItemStack stack) + { + // This makes sure that the stack has a tag compound. This is how data + // is stored on items. + if (stack.hasTagCompound()) + { + // This is the object holding all of the item data. + NBTTagCompound itemData = stack.getTagCompound(); + // This checks to see if the item has data stored under the + // brainType key. + if (itemData.hasKey("brainType")) + { + // This retrieves data from the brainType key and uses it in + // the return value + return "item." + itemData.getString("brainType"); + } + } + // This will be used if the item is obtained without nbt data on it. + return "item.nullBrain"; + } + + + // This is a fun method which allows us to run some code when our item is + // shown in a creative tab. I am going to use it to add all the brain + // types. + @SuppressWarnings("unchecked") + @Override + @SideOnly(Side.CLIENT) + public void getSubItems(Item item, CreativeTabs tab, List itemList) + { + // This creates a loop with a counter. It will go through once for + // every listing in brainTypes, and gives us a number associated + // with each listing. + for (int pos = 0; pos < brainTypes.length; pos++) + { + // This creates a new ItemStack instance. The item parameter + // supplied is this item. + ItemStack brainStack = new ItemStack(item); + // By default, a new ItemStack does not have any nbt compound data. + // We need to give it some. + brainStack.setTagCompound(new NBTTagCompound()); + // Now we set the type of the item, brainType is the key, and + // brainTypes[pos] is grabbing a + // entry from the brainTypes array. + brainStack.getTagCompound().setString("brainType", + brainTypes[pos]); + // And this adds it to the itemList, which is a list of all items + // in the creative tab. + itemList.add(brainStack); + } + } + + // This code will allow us to tell the items apart in game. You can change + @SuppressWarnings("unchecked") + // texture based on nbt data, but I won't be covering that. + @Override + @SideOnly(Side.CLIENT) + public void addInformation(ItemStack stack, EntityPlayer player, List tooltip, boolean isAdvanced){ + if ( stack.hasTagCompound() + && stack.getTagCompound().hasKey("brainType")) + { + // StatCollector is a class which allows us to handle string + // language translation. This requires that you fill out the + // translation in you language class. + tooltip.add(StatCollector.translateToLocal("tooltip.yourmod." + + stack.getTagCompound().getString("brainType") + ".desc")); + } + else // If the brain does not have valid tag data, a default message + { + tooltip.add(StatCollector.translateToLocal( + "tooltip.yourmod.nullbrain.desc")); + } + } +} + diff --git a/src/Java/gtPlusPlus/core/item/general/ItemBlueprint.java b/src/Java/gtPlusPlus/core/item/general/ItemBlueprint.java index a15616711d..b721672aa4 100644 --- a/src/Java/gtPlusPlus/core/item/general/ItemBlueprint.java +++ b/src/Java/gtPlusPlus/core/item/general/ItemBlueprint.java @@ -4,6 +4,7 @@ import gtPlusPlus.core.creative.AddToCreativeTab; import gtPlusPlus.core.interfaces.IItemBlueprint; import gtPlusPlus.core.lib.CORE; import gtPlusPlus.core.util.Utils; +import gtPlusPlus.core.util.item.UtilsItems; import gtPlusPlus.core.util.math.MathUtils; import java.util.List; @@ -21,49 +22,59 @@ import cpw.mods.fml.common.registry.GameRegistry; public class ItemBlueprint extends Item implements IItemBlueprint{ - protected String mName = ""; - protected boolean mHasBlueprint = false; - private final int bpID; - - /** - * The inventory of items the blueprint holds~ - */ - protected ItemStack[] blueprint = new ItemStack[9]; - public ItemBlueprint(String unlocalizedName) { this.setUnlocalizedName(unlocalizedName); this.setTextureName(CORE.MODID + ":" + unlocalizedName); this.setMaxStackSize(1); this.setCreativeTab(AddToCreativeTab.tabMachines); - this.bpID = MathUtils.randInt(0, 1000); + //this.bpID = MathUtils.randInt(0, 1000); GameRegistry.registerItem(this, unlocalizedName); } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override - public void addInformation(ItemStack stack, EntityPlayer aPlayer, List list, boolean bool) { - if (bpID >= 0){ - list.add(EnumChatFormatting.GRAY+"Technical Document No. "+bpID); + public void addInformation(ItemStack itemStack, EntityPlayer aPlayer, List list, boolean bool) { + //Create some NBT if it's not there, otherwise this does nothing. + if (!itemStack.hasTagCompound()){ + createNBT(itemStack); } - if(mHasBlueprint){ - list.add(EnumChatFormatting.BLUE+"Currently holding a blueprint for "+mName); + //Set up some default variables. + int id = -1; + String name = ""; + boolean blueprint = false; + //Get proper display vars from NBT if it's there + if (itemStack.hasTagCompound()){ + //Utils.LOG_INFO("Found TagCompound"); + id = (int) getNBT(itemStack, "mID"); + name = (String) getNBT(itemStack, "mName"); + blueprint = (boolean) getNBT(itemStack, "mBlueprint"); } + //Write to tooltip list for each viable setting. + if (itemStack.hasTagCompound()) { + if (id != -1){ + list.add(EnumChatFormatting.GRAY+"Technical Document No. "+id); + } + if(blueprint){ + list.add(EnumChatFormatting.BLUE+"Currently holding a blueprint for "+name); + } + else { + list.add(EnumChatFormatting.RED+"Currently not holding a blueprint for anything."); + } + } else { list.add(EnumChatFormatting.RED+"Currently not holding a blueprint for anything."); } - super.addInformation(stack, aPlayer, list, bool); + super.addInformation(itemStack, aPlayer, list, bool); } - + @Override public String getItemStackDisplayName(ItemStack p_77653_1_) { - return "Blueprint"; + return "Blueprint"; } @Override - public void onCreated(ItemStack itemStack, World world, EntityPlayer player) { - itemStack.stackTagCompound = new NBTTagCompound(); - //this.inventory = null; - //itemStack.stackTagCompound.set("pos_x", bed_X); TODO + public void onCreated(ItemStack itemStack, World world, EntityPlayer player) { + createNBT(itemStack); } @Override @@ -74,38 +85,59 @@ public class ItemBlueprint extends Item implements IItemBlueprint{ @Override public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer par3Entity) { //Let the player know what blueprint is held - Utils.messagePlayer(par3Entity, "This is a placeholder."); + if (itemStack.hasTagCompound()) { + Utils.messagePlayer(par3Entity, "This Blueprint holds NBT data. "+"|"+getNBT(itemStack, "mID")+"|"+getNBT(itemStack, "mBlueprint")+"|"+getNBT(itemStack, "mName")+"|"+UtilsItems.getArrayStackNames(readItemsFromNBT(itemStack))); + } + else { + createNBT(itemStack); + Utils.messagePlayer(par3Entity, "This is a placeholder. "+getNBT(itemStack, "mID")); + } + + return super.onItemRightClick(itemStack, world, par3Entity); } - public void readFromNBT(NBTTagCompound nbt){ - NBTTagList list = nbt.getTagList("Items", 10); - blueprint = new ItemStack[INV_SIZE]; - for(int i = 0;i<list.tagCount();i++) - { - NBTTagCompound data = list.getCompoundTagAt(i); - int slot = data.getInteger("Slot"); - if(slot >= 0 && slot < INV_SIZE) + public ItemStack[] readItemsFromNBT(ItemStack itemStack){ + ItemStack[] blueprint = new ItemStack[9]; + if (itemStack.hasTagCompound()){ + NBTTagCompound nbt = itemStack.getTagCompound(); + NBTTagList list = nbt.getTagList("Items", 10); + blueprint = new ItemStack[INV_SIZE]; + for(int i = 0;i<list.tagCount();i++) { - blueprint[slot] = ItemStack.loadItemStackFromNBT(data); + NBTTagCompound data = list.getCompoundTagAt(i); + int slot = data.getInteger("Slot"); + if(slot >= 0 && slot < INV_SIZE) + { + blueprint[slot] = ItemStack.loadItemStackFromNBT(data); + } } + return blueprint; } + return null; } - public void writeToNBT(NBTTagCompound nbt){ - NBTTagList list = new NBTTagList(); - for(int i = 0;i<INV_SIZE;i++) - { - ItemStack stack = blueprint[i]; - if(stack != null) + public ItemStack writeItemsToNBT(ItemStack itemStack, ItemStack[] craftingGrid){ + ItemStack[] blueprint = craftingGrid; + if (itemStack.hasTagCompound()){ + NBTTagCompound nbt = itemStack.getTagCompound(); + NBTTagList list = new NBTTagList(); + for(int i = 0;i<INV_SIZE;i++) { - NBTTagCompound data = new NBTTagCompound(); - stack.writeToNBT(data); - data.setInteger("Slot", i); - list.appendTag(data); + ItemStack stack = blueprint[i]; + if(stack != null) + { + NBTTagCompound data = new NBTTagCompound(); + stack.writeToNBT(data); + data.setInteger("Slot", i); + list.appendTag(data); + } } + nbt.setTag("Items", list); + itemStack.setTagCompound(nbt); + return itemStack; } - nbt.setTag("Items", list); + return null; } @Override @@ -115,7 +147,15 @@ public class ItemBlueprint extends Item implements IItemBlueprint{ @Override public boolean setBlueprint(ItemStack stack, IInventory craftingTable, ItemStack output) { - if (!mHasBlueprint){ + boolean hasBP = false; + ItemStack[] blueprint = new ItemStack[9]; + + if (stack.hasTagCompound()){ + hasBP = (boolean) getNBT(stack, "mBlueprint"); + blueprint = readItemsFromNBT(stack); + } + + if (!hasBP){ try { for (int o=0; o<craftingTable.getSizeInventory(); o++){ blueprint[o] = craftingTable.getStackInSlot(o); @@ -123,9 +163,20 @@ public class ItemBlueprint extends Item implements IItemBlueprint{ blueprint[0].stackSize = 0; } } + writeItemsToNBT(stack, blueprint); + if (stack.hasTagCompound()){ + if(stack.getTagCompound().getCompoundTag("Items") != null){ + stack.stackTagCompound.setBoolean("mBlueprint", true); + } + else { + //Invalid BP saved? + } + hasBP = (boolean) getNBT(stack, "mBlueprint"); + } + if (output != null){ - setBlueprintName(output.getDisplayName()); - return (mHasBlueprint = true); + setBlueprintName(stack, output.getDisplayName()); + return (hasBP = true); } return false; } catch (Throwable t){ @@ -136,17 +187,24 @@ public class ItemBlueprint extends Item implements IItemBlueprint{ } @Override - public void setBlueprintName(String name) { - this.mName = name; + public void setBlueprintName(ItemStack stack, String name) { + stack.stackTagCompound.setString("mName", name); } @Override public boolean hasBlueprint(ItemStack stack) { - return mHasBlueprint; + if (stack.hasTagCompound()){ + return (boolean) getNBT(stack, "mBlueprint"); + } + return false; } @Override public ItemStack[] getBlueprint(ItemStack stack) { + ItemStack[] blueprint = new ItemStack[9]; + if (stack.hasTagCompound()){ + blueprint = readItemsFromNBT(stack); + } try { ItemStack[] returnStack = new ItemStack[9]; for (int o=0; o<blueprint.length; o++){ @@ -161,4 +219,73 @@ public class ItemBlueprint extends Item implements IItemBlueprint{ } } + public boolean createNBT(ItemStack itemStack){ + if (itemStack.hasTagCompound()){ + if (!itemStack.stackTagCompound.getBoolean("mBlueprint") && !itemStack.stackTagCompound.getString("mName").equals("")){ + //No Blueprint and no name Set + Utils.LOG_INFO("No Blueprint and no name Set"); + return false; + } + else if (itemStack.stackTagCompound.getBoolean("mBlueprint") && !itemStack.stackTagCompound.getString("mName").equals("")){ + //Has Blueprint but invalid name set + Utils.LOG_INFO("Has Blueprint but invalid name set"); + //itemStack.stackTagCompound = null; + //createNBT(itemStack); + return false; + } + else if (!itemStack.stackTagCompound.getBoolean("mBlueprint") && itemStack.stackTagCompound.getString("mName").equals("")){ + //Has no Blueprint, but strangely has a name + Utils.LOG_INFO("Has no Blueprint, but strangely has a name"); + //itemStack.stackTagCompound = null; + //createNBT(itemStack); + return false; + } + return false; + } + else if(!itemStack.hasTagCompound()){ + int bpID = MathUtils.randInt(0, 1000); + boolean hasRecipe = false; + String recipeName = ""; + Utils.LOG_INFO("Creating Blueprint, setting up it's NBT data. "+bpID); + itemStack.stackTagCompound = new NBTTagCompound(); + itemStack.stackTagCompound.setInteger("mID", bpID); + itemStack.stackTagCompound.setBoolean("mBlueprint", hasRecipe); + itemStack.stackTagCompound.setString("mName", recipeName); + return true; + } + else { + int bpID = MathUtils.randInt(0, 1000); + boolean hasRecipe = false; + String recipeName = ""; + Utils.LOG_INFO("Creating a Blueprint, setting up it's NBT data. "+bpID); + itemStack.stackTagCompound = new NBTTagCompound(); + itemStack.stackTagCompound.setInteger("mID", bpID); + itemStack.stackTagCompound.setBoolean("mBlueprint", hasRecipe); + itemStack.stackTagCompound.setString("mName", recipeName); + return true; + } + } + + public Object getNBT(ItemStack itemStack, String tagNBT){ + if (!itemStack.hasTagCompound()){ + return null; + } + Object o = null; + if (tagNBT.equals("mID")){ + o = itemStack.stackTagCompound.getInteger(tagNBT); + } + else if (tagNBT.equals("mBlueprint")){ + o = itemStack.stackTagCompound.getBoolean(tagNBT); + } + else if (tagNBT.equals("mName")){ + o = itemStack.stackTagCompound.getString(tagNBT); + } + else if (tagNBT.equals("")){ + //For More Tag Support + //o = itemStack.stackTagCompound.getInteger(tagNBT); + } + if (o != null) + return o; + return null; } + } |