From 93b8244115970a1686a3d4fa30c61f98f65dcbe7 Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 18 Nov 2016 11:07:02 +1100 Subject: [PATCH] SPIGOT-2787: Keep performing getter null conversion at call sites --- .../bukkit/craftbukkit/event/CraftEventFactory.java | 10 +++++++--- .../bukkit/craftbukkit/inventory/CraftInventory.java | 4 ++-- .../craftbukkit/inventory/CraftInventoryAnvil.java | 4 ++-- .../craftbukkit/inventory/CraftInventoryCrafting.java | 7 ++++--- .../bukkit/craftbukkit/inventory/CraftItemStack.java | 2 +- .../craftbukkit/inventory/NMSCraftItemStackTest.java | 8 ++++++++ 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index b84b376a32..3d3ea63118 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -197,7 +197,7 @@ public class CraftEventFactory { public static PlayerInteractEvent callPlayerInteractEvent(EntityHuman who, Action action, BlockPosition position, EnumDirection direction, ItemStack itemstack, EnumHand hand) { return callPlayerInteractEvent(who, action, position, direction, itemstack, false, hand); } - + public static PlayerInteractEvent callPlayerInteractEvent(EntityHuman who, Action action, BlockPosition position, EnumDirection direction, ItemStack itemstack, boolean cancelledBlock, EnumHand hand) { Player player = (who == null) ? null : (Player) who.getBukkitEntity(); CraftItemStack itemInHand = CraftItemStack.asCraftMirror(itemstack); @@ -220,6 +220,10 @@ public class CraftEventFactory { } BlockFace blockFace = CraftBlock.notchToBlockFace(direction); + if (itemInHand.getType() == Material.AIR || itemInHand.getAmount() == 0) { + itemInHand = null; + } + PlayerInteractEvent event = new PlayerInteractEvent(player, action, itemInHand, blockClicked, blockFace, (hand == null) ? null : ((hand == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND)); if (cancelledBlock) { event.setUseInteractedBlock(Event.Result.DENY); @@ -992,7 +996,7 @@ public class CraftEventFactory { } public static PrepareAnvilEvent callPrepareAnvilEvent(InventoryView view, ItemStack item) { - PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item)); + PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item).clone()); event.getView().getPlayer().getServer().getPluginManager().callEvent(event); event.getInventory().setItem(2, event.getResult()); return event; @@ -1012,7 +1016,7 @@ public class CraftEventFactory { public static EntityBreedEvent callEntityBreedEvent(EntityLiving child, EntityLiving mother, EntityLiving father, EntityLiving breeder, ItemStack bredWith, int experience) { org.bukkit.entity.LivingEntity breederEntity = (LivingEntity)(breeder == null ? null : breeder.getBukkitEntity()); - CraftItemStack bredWithStack = CraftItemStack.asCraftMirror(bredWith); + CraftItemStack bredWithStack = bredWith == null ? null : CraftItemStack.asCraftMirror(bredWith).clone(); EntityBreedEvent event = new EntityBreedEvent((LivingEntity) child.getBukkitEntity(), (LivingEntity) mother.getBukkitEntity(), (LivingEntity) father.getBukkitEntity(), breederEntity, bredWithStack, experience); child.world.getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java index bedc08540a..0033246bb3 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java @@ -46,7 +46,7 @@ public class CraftInventory implements Inventory { public ItemStack getItem(int index) { net.minecraft.server.ItemStack item = getInventory().getItem(index); - return CraftItemStack.asCraftMirror(item); + return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } @Override @@ -65,7 +65,7 @@ public class CraftInventory implements Inventory { int size = Math.min(items.length, mcItems.size()); for (int i = 0; i < size; i++) { - items[i] = CraftItemStack.asCraftMirror(mcItems.get(i)); + items[i] = (mcItems.get(i).isEmpty()) ? null : CraftItemStack.asCraftMirror(mcItems.get(i)); } return items; } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java index 997eaf66b3..6b415f234c 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java @@ -28,10 +28,10 @@ public class CraftInventoryAnvil extends CraftInventory implements AnvilInventor public ItemStack getItem(int slot) { if (slot < getIngredientsInventory().getSize()) { net.minecraft.server.ItemStack item = getIngredientsInventory().getItem(slot); - return CraftItemStack.asCraftMirror(item); + return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } else { net.minecraft.server.ItemStack item = getResultInventory().getItem(slot - getIngredientsInventory().getSize()); - return CraftItemStack.asCraftMirror(item); + return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java index f4e5ddf43c..2ae768faa2 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryCrafting.java @@ -70,10 +70,10 @@ public class CraftInventoryCrafting extends CraftInventory implements CraftingIn public CraftItemStack getItem(int index) { if (index < getResultInventory().getSize()) { net.minecraft.server.ItemStack item = getResultInventory().getItem(index); - return CraftItemStack.asCraftMirror(item); + return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } else { net.minecraft.server.ItemStack item = getMatrixInventory().getItem(index - getResultInventory().getSize()); - return CraftItemStack.asCraftMirror(item); + return item.isEmpty() ? null : CraftItemStack.asCraftMirror(item); } } @@ -99,7 +99,8 @@ public class CraftInventoryCrafting extends CraftInventory implements CraftingIn public ItemStack getResult() { net.minecraft.server.ItemStack item = getResultInventory().getItem(0); - return CraftItemStack.asCraftMirror(item); + if (!item.isEmpty()) return CraftItemStack.asCraftMirror(item); + return null; } public void setMatrix(ItemStack[] contents) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index 178a626dc0..0ce23f0fa1 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -68,7 +68,7 @@ public final class CraftItemStack extends ItemStack { } public static CraftItemStack asCraftMirror(net.minecraft.server.ItemStack original) { - return (original.isEmpty()) ? null : new CraftItemStack(original); + return new CraftItemStack(original); } public static CraftItemStack asCraftCopy(ItemStack original) { diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java index 10baff3919..1f5a6a32f1 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/NMSCraftItemStackTest.java @@ -25,4 +25,12 @@ public class NMSCraftItemStackTest extends AbstractTestingBase { assertThat(clone.getData(), is(itemStack.getData())); assertThat(clone, is(itemStack)); } + + @Test + public void testCloneNullItem() throws Exception { + net.minecraft.server.ItemStack nmsItemStack = null; + ItemStack itemStack = CraftItemStack.asCraftMirror(nmsItemStack); + ItemStack clone = itemStack.clone(); + assertThat(clone, is(itemStack)); + } }