Add NPE protection to ItemStack delegates. Closes #601

Also cleaned up the names of said delegates, we do not have to follow MCP's crowdsourced names as they are very bad.
Redirected damage through setItemDamage to allow items to have finder control of breaking.
This commit is contained in:
LexManos 2013-06-08 14:02:53 -07:00
parent 2bbb3ee0ca
commit 9034862024
2 changed files with 69 additions and 27 deletions

View file

@ -60,7 +60,7 @@
Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
return par1World.rayTraceBlocks_do_do(vec3, vec31, par3, !par3);
}
@@ -720,4 +736,508 @@
@@ -720,4 +736,534 @@
{
StatList.initStats();
}
@ -507,6 +507,11 @@
+ * @param stack The itemstack that is damaged
+ * @return the damage value
+ */
+ public int getDamage(ItemStack stack)
+ {
+ return getItemDamageFromStack(stack);
+ }
+ @Deprecated
+ public int getItemDamageFromStack(ItemStack stack)
+ {
+ return stack.itemDamage;
@ -517,20 +522,31 @@
+ * @param stack the stack
+ * @return the damage value
+ */
+ public int getDisplayDamage(ItemStack stack)
+ {
+ return getItemDamageFromStackForDisplay(stack);
+ }
+ @Deprecated
+ public int getItemDamageFromStackForDisplay(ItemStack stack)
+ {
+ return stack.itemDamage;
+ }
+
+ /**
+ * Return the maxDamage for this ItemStack. Defaults to the maxDamage field in this item, but can be overridden here for other sources such as NBT.
+ * Return the maxDamage for this ItemStack. Defaults to the maxDamage field in this item,
+ * but can be overridden here for other sources such as NBT.
+ *
+ * @param stack The itemstack that is damaged
+ * @return the damage value
+ */
+ public int getMaxDamage(ItemStack stack)
+ {
+ return getItemMaxDamageFromStack(stack);
+ }
+ @Deprecated
+ public int getItemMaxDamageFromStack(ItemStack stack)
+ {
+ return maxDamage;
+ return getMaxDamage();
+ }
+
+ /**
@ -538,6 +554,11 @@
+ * @param stack the stack
+ * @return if the stack is damaged
+ */
+ public boolean isDamaged(ItemStack stack)
+ {
+ return isItemStackDamaged(stack);
+ }
+ @Deprecated
+ public boolean isItemStackDamaged(ItemStack stack)
+ {
+ return stack.itemDamage > 0;
@ -548,6 +569,11 @@
+ * @param stack the stack
+ * @param damage the new damage value
+ */
+ public void setDamage(ItemStack stack, int damage)
+ {
+ setItemDamageForStack(stack, damage);
+ }
+ @Deprecated
+ public void setItemDamageForStack(ItemStack stack, int damage)
+ {
+ stack.itemDamage = damage;

View file

@ -1,56 +1,72 @@
--- ../src_base/minecraft/net/minecraft/item/ItemStack.java
+++ ../src_work/minecraft/net/minecraft/item/ItemStack.java
@@ -249,7 +249,7 @@
@@ -249,7 +249,9 @@
*/
public boolean isItemDamaged()
{
- return this.isItemStackDamageable() && this.itemDamage > 0;
+ return this.isItemStackDamageable() && this.getItem().isItemStackDamaged(this);
+ boolean damaged = itemDamage > 0;
+ if (getItem() != null) damaged = getItem().isDamaged(this);
+ return this.isItemStackDamageable() && damaged;
}
/**
@@ -257,7 +257,7 @@
@@ -257,6 +259,10 @@
*/
public int getItemDamageForDisplay()
{
- return this.itemDamage;
+ return this.getItem().getItemDamageFromStackForDisplay(this);
+ if (getItem() != null)
+ {
+ return getItem().getDisplayDamage(this);
+ }
return this.itemDamage;
}
/**
@@ -265,7 +265,7 @@
@@ -265,6 +271,10 @@
*/
public int getItemDamage()
{
- return this.itemDamage;
+ return this.getItem().getItemDamageFromStack(this);
+ if (getItem() != null)
+ {
+ return getItem().getDamage(this);
+ }
return this.itemDamage;
}
/**
@@ -273,12 +273,7 @@
@@ -273,6 +283,12 @@
*/
public void setItemDamage(int par1)
{
- this.itemDamage = par1;
-
- if (this.itemDamage < 0)
- {
- this.itemDamage = 0;
- }
+ this.getItem().setItemDamageForStack(this, par1);
}
+ if (getItem() != null)
+ {
+ getItem().setDamage(this, par1);
+ return;
+ }
+
this.itemDamage = par1;
/**
@@ -286,7 +281,7 @@
if (this.itemDamage < 0)
@@ -286,7 +302,7 @@
*/
public int getMaxDamage()
{
- return Item.itemsList[this.itemID].getMaxDamage();
+ return this.getItem().getItemMaxDamageFromStack(this);
+ return this.getItem().getMaxDamage(this);
}
/**
@@ -388,7 +383,7 @@
@@ -324,8 +340,8 @@
}
}
- this.itemDamage += par1;
- return this.itemDamage > this.getMaxDamage();
+ setItemDamage(getItemDamage() + par1); //Redirect through Item's callback if applicable.
+ return getItemDamage() > getMaxDamage();
}
}
@@ -388,7 +404,7 @@
*/
public int getDamageVsEntity(Entity par1Entity)
{
@ -59,7 +75,7 @@
}
/**
@@ -396,7 +391,7 @@
@@ -396,7 +412,7 @@
*/
public boolean canHarvestBlock(Block par1Block)
{