Skip to content
Permalink
Browse files

fix potential item null error, for #292

  • Loading branch information...
mcmonkey4eva committed May 19, 2019
1 parent b4216b7 commit f627cfa8361fdfdd49ccb65ca437cf0a308b6862
Showing with 33 additions and 43 deletions.
  1. +33 −43 src/main/java/org/mcmonkey/sentinel/SentinelItemHelper.java
@@ -41,28 +41,21 @@ public ItemStack getArrow() {
* Reduces the durability of the NPC's held item.
*/
public void reduceDurability() {
if (SentinelTarget.v1_9) {
ItemStack item = getLivingEntity().getEquipment().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) {
if (item.getDurability() >= item.getType().getMaxDurability() - 1) {
getLivingEntity().getEquipment().setItemInMainHand(null);
}
else {
item.setDurability((short) (item.getDurability() + 1));
getLivingEntity().getEquipment().setItemInMainHand(item);
}
ItemStack toSet;
ItemStack item = getHeldItem();
if (item != null && item.getType() != Material.AIR) {
if (item.getDurability() >= item.getType().getMaxDurability() - 1) {
toSet = null;
}
}
else {
ItemStack item = getLivingEntity().getEquipment().getItemInHand();
if (item != null && item.getType() != Material.AIR) {
if (item.getDurability() >= item.getType().getMaxDurability() - 1) {
getLivingEntity().getEquipment().setItemInHand(null);
}
else {
item.setDurability((short) (item.getDurability() + 1));
getLivingEntity().getEquipment().setItemInHand(item);
}
else {
item.setDurability((short) (item.getDurability() + 1));
toSet = item;
}
if (SentinelTarget.v1_9) {
getLivingEntity().getEquipment().setItemInMainHand(toSet);
}
else {
getLivingEntity().getEquipment().setItemInHand(toSet);
}
}
}
@@ -101,28 +94,21 @@ public void takeArrow() {
* Takes one item from the NPC's held items (for consumables).
*/
public void takeOne() {
if (SentinelTarget.v1_9) {
ItemStack item = getLivingEntity().getEquipment().getItemInMainHand();
if (item != null && item.getType() != Material.AIR) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
getLivingEntity().getEquipment().setItemInMainHand(item);
}
else {
getLivingEntity().getEquipment().setItemInMainHand(null);
}
ItemStack toSet;
ItemStack item = getHeldItem();
if (item != null && item.getType() != Material.AIR) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
toSet = item;
}
}
else {
ItemStack item = getLivingEntity().getEquipment().getItemInHand();
if (item != null && item.getType() != Material.AIR) {
if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
getLivingEntity().getEquipment().setItemInHand(item);
}
else {
getLivingEntity().getEquipment().setItemInHand(null);
}
else {
toSet = null;
}
if (SentinelTarget.v1_9) {
getLivingEntity().getEquipment().setItemInMainHand(toSet);
}
else {
getLivingEntity().getEquipment().setItemInHand(toSet);
}
}
}
@@ -348,7 +334,11 @@ public boolean usesPotion() {
* Returns whether the NPC can take durability from the held item.
*/
public boolean shouldTakeDura() {
Material type = getHeldItem().getType();
ItemStack it = getHeldItem();
if (it == null) {
return false;
}
Material type = it.getType();
return SentinelTarget.BOW_MATERIALS.contains(type) || SentinelTarget.SWORD_MATERIALS.contains(type)
|| SentinelTarget.PICKAXE_MATERIALS.contains(type) || SentinelTarget.AXE_MATERIALS.contains(type);
}

0 comments on commit f627cfa

Please sign in to comment.
You can’t perform that action at this time.