Skip to content

Commit

Permalink
Anvil NBT cleanup. Fixed issue with hot, unworked items that are acci…
Browse files Browse the repository at this point in the history
…dentally given a red action (which doesn't do anything since the arrow is already at the far left) not displaying the "This item has been worked" tooltip. Removed unnecessary client-side adding of NBT to the input every time the anvil screen is drawn. Added additional check for the crafting rule tag on items as a failsafe for the "This item has been worked" tooltip.
  • Loading branch information
Kittychanley committed Dec 27, 2015
1 parent b65b8a8 commit cf7c6bf
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 77 deletions.
10 changes: 7 additions & 3 deletions src/Common/com/bioxx/tfc/Items/ItemIngot.java
Expand Up @@ -19,6 +19,7 @@
import com.bioxx.tfc.Reference;
import com.bioxx.tfc.Core.TFCTabs;
import com.bioxx.tfc.Core.Metal.MetalRegistry;
import com.bioxx.tfc.TileEntities.TEAnvil;
import com.bioxx.tfc.TileEntities.TEIngotPile;
import com.bioxx.tfc.api.Metal;
import com.bioxx.tfc.api.TFCBlocks;
Expand Down Expand Up @@ -388,10 +389,13 @@ public EnumTier getSmeltTier(ItemStack is) {
public int getItemStackLimit(ItemStack is)
{
// hot or worked ingots cannot stack
if (is.hasTagCompound() && (TFC_ItemHeat.hasTemp(is) ||
is.getTagCompound().hasKey("itemCraftingValue") && is.getTagCompound().getShort("itemCraftingValue") != 0))
if (is.hasTagCompound())
{
return 1;
NBTTagCompound tag = is.getTagCompound();
if (TFC_ItemHeat.hasTemp(is) || tag.hasKey(TEAnvil.ITEM_CRAFTING_VALUE_TAG) || tag.hasKey(TEAnvil.ITEM_CRAFTING_RULE_1_TAG))
{
return 1;
}
}

return super.getItemStackLimit(is);
Expand Down
11 changes: 8 additions & 3 deletions src/Common/com/bioxx/tfc/Items/ItemMetalSheet.java
Expand Up @@ -3,10 +3,12 @@
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

import com.bioxx.tfc.Core.TFCTabs;
import com.bioxx.tfc.Core.Metal.MetalRegistry;
import com.bioxx.tfc.TileEntities.TEAnvil;
import com.bioxx.tfc.TileEntities.TEMetalSheet;
import com.bioxx.tfc.api.Metal;
import com.bioxx.tfc.api.TFCBlocks;
Expand Down Expand Up @@ -199,10 +201,13 @@ public EnumTier getSmeltTier(ItemStack is)
public int getItemStackLimit(ItemStack is)
{
// hot or worked sheets cannot stack
if (is.hasTagCompound() && (TFC_ItemHeat.hasTemp(is) ||
is.getTagCompound().hasKey("itemCraftingValue") && is.getTagCompound().getShort("itemCraftingValue") != 0))
if (is.hasTagCompound())
{
return 1;
NBTTagCompound tag = is.getTagCompound();
if (TFC_ItemHeat.hasTemp(is) || tag.hasKey(TEAnvil.ITEM_CRAFTING_VALUE_TAG) || tag.hasKey(TEAnvil.ITEM_CRAFTING_RULE_1_TAG))
{
return 1;
}
}

return super.getItemStackLimit(is);
Expand Down
6 changes: 5 additions & 1 deletion src/Common/com/bioxx/tfc/Items/ItemTerra.java
Expand Up @@ -7,13 +7,15 @@
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.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import com.bioxx.tfc.Reference;
import com.bioxx.tfc.Core.TFCTabs;
import com.bioxx.tfc.Core.TFC_Core;
import com.bioxx.tfc.TileEntities.TEAnvil;
import com.bioxx.tfc.api.HeatIndex;
import com.bioxx.tfc.api.HeatRegistry;
import com.bioxx.tfc.api.TFCItems;
Expand Down Expand Up @@ -178,7 +180,9 @@ public void addInformation(ItemStack is, EntityPlayer player, List arraylist, bo

if (is.hasTagCompound())
{
if(is.getTagCompound().hasKey("itemCraftingValue") && is.getTagCompound().getShort("itemCraftingValue") != 0)
NBTTagCompound tag = is.getTagCompound();
// Use either tag as a failsafe to display the tooltip
if (tag.hasKey(TEAnvil.ITEM_CRAFTING_VALUE_TAG) || tag.hasKey(TEAnvil.ITEM_CRAFTING_RULE_1_TAG))
arraylist.add(TFC_Core.translate("gui.ItemWorked"));
}

Expand Down
158 changes: 88 additions & 70 deletions src/Common/com/bioxx/tfc/TileEntities/TEAnvil.java
Expand Up @@ -71,6 +71,11 @@ public class TEAnvil extends NetworkTileEntity implements IInventory
public static final int FLUX_SLOT = 6;
public static final int HAMMER_SLOT = 0;

public static final String ITEM_CRAFTING_VALUE_TAG = "itemCraftingValue";
public static final String ITEM_CRAFTING_RULE_1_TAG = "itemCraftingRule1";
public static final String ITEM_CRAFTING_RULE_2_TAG = "itemCraftingRule2";
public static final String ITEM_CRAFTING_RULE_3_TAG = "itemCraftingRule3";

public TEAnvil()
{
anvilItemStacks = new ItemStack[19];
Expand Down Expand Up @@ -118,7 +123,6 @@ public void updateEntity()
//This is where the crafting is completed and the result is added to the anvil
if(result != null)
{

AnvilCraftEvent eventCraft = new AnvilCraftEvent(lastWorker, this, anvilItemStacks[INPUT1_SLOT], anvilItemStacks[INPUT2_SLOT], result);
MinecraftForge.EVENT_BUS.post(eventCraft);
if(!eventCraft.isCanceled())
Expand All @@ -134,7 +138,6 @@ public void updateEntity()
{
AnvilManager.setDurabilityBuff(output, recipe.getSkillMult(lastWorker));
AnvilManager.setDamageBuff(output, recipe.getSkillMult(lastWorker));

}
else if (output.getItem() instanceof ItemTFCArmor)
{
Expand Down Expand Up @@ -168,8 +171,8 @@ else if (bucket == TFCItems.redSteelBucketEmpty)

if(anvilItemStacks[INPUT2_SLOT] != null)
anvilItemStacks[INPUT2_SLOT].stackSize--;

}

workRecipe = null;
craftingPlan = "";
craftingValue = 0;
Expand Down Expand Up @@ -306,38 +309,42 @@ public void updateRules(int rule, int slot)
NBTTagCompound tag = anvilItemStacks[slot].getTagCompound();
int rule1 = -1;
int rule2 = -1;
if(tag.hasKey("itemCraftingRule1"))
rule1 = tag.getByte("itemCraftingRule1");
if(tag.hasKey("itemCraftingRule2"))
rule2 = tag.getByte("itemCraftingRule2");
if(tag.hasKey("itemCraftingRule3"))
tag.getByte("itemCraftingRule3");
if (tag.hasKey(ITEM_CRAFTING_RULE_1_TAG))
{
rule1 = tag.getByte(ITEM_CRAFTING_RULE_1_TAG);
}
if (tag.hasKey(ITEM_CRAFTING_RULE_2_TAG))
{
rule2 = tag.getByte(ITEM_CRAFTING_RULE_2_TAG);
}
if (tag.hasKey(ITEM_CRAFTING_RULE_3_TAG))
{
tag.getByte(ITEM_CRAFTING_RULE_3_TAG);
}

itemCraftingRules[2] = rule2;
itemCraftingRules[1] = rule1;
itemCraftingRules[0] = rule;

tag.setByte("itemCraftingRule1", (byte) itemCraftingRules[0]);
tag.setByte("itemCraftingRule2", (byte) itemCraftingRules[1]);
tag.setByte("itemCraftingRule3", (byte) itemCraftingRules[2]);
tag.setByte(ITEM_CRAFTING_RULE_1_TAG, (byte) itemCraftingRules[0]);
tag.setByte(ITEM_CRAFTING_RULE_2_TAG, (byte) itemCraftingRules[1]);
tag.setByte(ITEM_CRAFTING_RULE_3_TAG, (byte) itemCraftingRules[2]);

anvilItemStacks[slot].setTagCompound(tag);
}
}

public void removeRules(int slot)
{
if(anvilItemStacks[slot].hasTagCompound())
if (anvilItemStacks[slot].hasTagCompound())
{
NBTTagCompound tag = anvilItemStacks[slot].getTagCompound();
if(tag.hasKey("itemCraftingRule1"))
tag.removeTag("itemCraftingRule1");
if(tag.hasKey("itemCraftingRule2"))
tag.removeTag("itemCraftingRule2");
if(tag.hasKey("itemCraftingRule3"))
tag.removeTag("itemCraftingRule3");
if(tag.hasKey("itemCraftingValue"))
tag.removeTag("itemCraftingValue");

// No need to check if the tag has the key before removing it thanks to gagMap functionality
tag.removeTag(ITEM_CRAFTING_RULE_1_TAG);
tag.removeTag(ITEM_CRAFTING_RULE_2_TAG);
tag.removeTag(ITEM_CRAFTING_RULE_3_TAG);
tag.removeTag(ITEM_CRAFTING_VALUE_TAG);

anvilItemStacks[slot].setTagCompound(tag);
}
Expand All @@ -346,29 +353,45 @@ public void removeRules(int slot)
public int[] getItemRules()
{
int[] rules = new int[3];
if(anvilItemStacks[1] != null && anvilItemStacks[1].hasTagCompound())
ItemStack input = anvilItemStacks[INPUT1_SLOT];

if (input != null && input.hasTagCompound())
{
if(anvilItemStacks[1].stackTagCompound.hasKey("itemCraftingRule1"))
rules[0] = anvilItemStacks[1].stackTagCompound.getByte("itemCraftingRule1");
NBTTagCompound tag = input.getTagCompound();
if (tag.hasKey(ITEM_CRAFTING_RULE_1_TAG))
{
rules[0] = tag.getByte(ITEM_CRAFTING_RULE_1_TAG);
}
else
{
rules[0] = RuleEnum.ANY.Action;
}

if(anvilItemStacks[1].stackTagCompound.hasKey("itemCraftingRule2"))
rules[1] = anvilItemStacks[1].stackTagCompound.getByte("itemCraftingRule2");
if (tag.hasKey(ITEM_CRAFTING_RULE_2_TAG))
{
rules[1] = tag.getByte(ITEM_CRAFTING_RULE_2_TAG);
}
else
{
rules[1] = RuleEnum.ANY.Action;
}

if(anvilItemStacks[1].stackTagCompound.hasKey("itemCraftingRule3"))
rules[2] = anvilItemStacks[1].stackTagCompound.getByte("itemCraftingRule3");
if (tag.hasKey(ITEM_CRAFTING_RULE_3_TAG))
{
rules[2] = tag.getByte(ITEM_CRAFTING_RULE_3_TAG);
}
else
{
rules[2] = RuleEnum.ANY.Action;
}
}
else
{
rules[0] = RuleEnum.ANY.Action;
rules[1] = RuleEnum.ANY.Action;
rules[2] = RuleEnum.ANY.Action;
}

return rules;
}

Expand Down Expand Up @@ -640,64 +663,59 @@ public boolean isItemValidForSlot(int i, ItemStack itemstack)

public boolean setItemCraftingValue(int i)
{
if(anvilItemStacks[INPUT1_SLOT] != null && anvilItemStacks[INPUT1_SLOT].hasTagCompound() && anvilItemStacks[INPUT1_SLOT].getTagCompound().hasKey("itemCraftingValue"))
{
short icv = anvilItemStacks[INPUT1_SLOT].getTagCompound().getShort("itemCraftingValue");
if(icv+i >= 0)
anvilItemStacks[INPUT1_SLOT].getTagCompound().setShort("itemCraftingValue", (short) (icv + i));
return true;
}
else if(anvilItemStacks[INPUT1_SLOT] != null && anvilItemStacks[INPUT1_SLOT].hasTagCompound())
{
if(i >= 0)
anvilItemStacks[INPUT1_SLOT].getTagCompound().setShort("itemCraftingValue", (short)i);
return true;
}
else if(anvilItemStacks[INPUT1_SLOT] != null && !anvilItemStacks[INPUT1_SLOT].hasTagCompound())
ItemStack input = anvilItemStacks[INPUT1_SLOT];
if (input != null)
{
NBTTagCompound tag = new NBTTagCompound();
tag.setShort("itemCraftingValue", (short)i);
anvilItemStacks[INPUT1_SLOT].stackTagCompound = tag;
NBTTagCompound tag = null;
if (input.hasTagCompound())
{
tag = input.getTagCompound();
if (tag.hasKey(ITEM_CRAFTING_VALUE_TAG))
{
short craftingValue = tag.getShort(ITEM_CRAFTING_VALUE_TAG);
// Use Math.max to prevent negative values
tag.setShort(ITEM_CRAFTING_VALUE_TAG, (short) Math.max(0, craftingValue + i));
}
else
{
// Use Math.max to prevent negative values
tag.setShort(ITEM_CRAFTING_VALUE_TAG, (short) Math.max(0, i));
}
}
else
{
tag = new NBTTagCompound();
// Use Math.max to prevent negative values
tag.setShort(ITEM_CRAFTING_VALUE_TAG, (short) Math.max(0, i));
input.setTagCompound(tag);
}

return true;
}

return false;
}

public int getItemCraftingValue()
{
if(anvilItemStacks[INPUT1_SLOT] != null && anvilItemStacks[INPUT1_SLOT].hasTagCompound() && anvilItemStacks[INPUT1_SLOT].getTagCompound().hasKey("itemCraftingValue"))
return anvilItemStacks[INPUT1_SLOT].getTagCompound().getShort("itemCraftingValue");
else if(anvilItemStacks[INPUT1_SLOT] != null && !anvilItemStacks[INPUT1_SLOT].hasTagCompound() && craftingValue != 0)
{
NBTTagCompound tag = new NBTTagCompound();
tag.setShort("itemCraftingValue", (short) 0);
anvilItemStacks[INPUT1_SLOT].setTagCompound(tag);
return 0;
}
else if(anvilItemStacks[INPUT1_SLOT] != null && anvilItemStacks[INPUT1_SLOT].hasTagCompound() && !anvilItemStacks[INPUT1_SLOT].getTagCompound().hasKey("itemCraftingValue") && craftingValue != 0)
ItemStack input = anvilItemStacks[INPUT1_SLOT];
if (input != null && input.hasTagCompound() && input.getTagCompound().hasKey(ITEM_CRAFTING_VALUE_TAG))
{
NBTTagCompound tag = anvilItemStacks[1].getTagCompound();
tag.setShort("itemCraftingValue", (short) 0);
anvilItemStacks[INPUT1_SLOT].setTagCompound(tag);
return 0;
return input.getTagCompound().getShort(ITEM_CRAFTING_VALUE_TAG);
}
else
return 0;

return 0;
}

public int getItemCraftingValueNoSet(int i)
{
if(anvilItemStacks[i] != null && anvilItemStacks[i].hasTagCompound())
ItemStack input = anvilItemStacks[i];
if (input != null && input.hasTagCompound() && input.getTagCompound().hasKey(ITEM_CRAFTING_VALUE_TAG))
{
if(!anvilItemStacks[i].getTagCompound().hasKey("itemCraftingValue"))
return 0;
else
return anvilItemStacks[i].getTagCompound().getShort("itemCraftingValue");
return input.getTagCompound().getShort(ITEM_CRAFTING_VALUE_TAG);
}
else if(anvilItemStacks[i] != null && !anvilItemStacks[i].hasTagCompound())
return 0;
else
return 0;

return 0;
}

public Boolean isTemperatureWeldable(int i)
Expand Down

0 comments on commit cf7c6bf

Please sign in to comment.