SPIGOT-2787: Keep performing getter null conversion at call sites

This commit is contained in:
md_5 2016-11-18 11:07:02 +11:00
parent 0c3bb76487
commit 93b8244115
6 changed files with 24 additions and 11 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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));
}
}