From 0e60f1f7b8049a886b86be0522e6cd8f02e7b5c8 Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sat, 16 Mar 2013 16:38:19 -0500 Subject: [PATCH] Fire BlockDispenseEvent for new dispenser behaviors. Fixes BUKKIT-3774 --- .../server/DispenseBehaviorArmor.java | 36 +++++++++++++++++- .../server/DispenseBehaviorBonemeal.java | 34 ++++++++++++++++- .../server/DispenseBehaviorFlintAndSteel.java | 29 +++++++++++++++ .../minecraft/server/DispenseBehaviorTNT.java | 37 ++++++++++++++++++- 4 files changed, 131 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minecraft/server/DispenseBehaviorArmor.java b/src/main/java/net/minecraft/server/DispenseBehaviorArmor.java index 06e77b62a1..1c50ad4f75 100644 --- a/src/main/java/net/minecraft/server/DispenseBehaviorArmor.java +++ b/src/main/java/net/minecraft/server/DispenseBehaviorArmor.java @@ -2,6 +2,11 @@ package net.minecraft.server; import java.util.List; +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.event.block.BlockDispenseEvent; +// CraftBukkit end + final class DispenseBehaviorArmor extends DispenseBehaviorItem { DispenseBehaviorArmor() {} @@ -18,12 +23,39 @@ final class DispenseBehaviorArmor extends DispenseBehaviorItem { EntityLiving entityliving = (EntityLiving) list.get(0); int l = entityliving instanceof EntityHuman ? 1 : 0; int i1 = EntityLiving.b(itemstack); - ItemStack itemstack1 = itemstack.cloneItemStack(); + + // CraftBukkit start + ItemStack itemstack1 = itemstack.a(1); + World world = isourceblock.k(); + org.bukkit.block.Block block = world.getWorld().getBlockAt(isourceblock.getBlockX(), isourceblock.getBlockY(), isourceblock.getBlockZ()); + CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1); + + BlockDispenseEvent event = new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(0, 0, 0)); + if (!BlockDispenser.eventFired) { + world.getServer().getPluginManager().callEvent(event); + } + + if (event.isCancelled()) { + itemstack.count++; + return itemstack; + } + + if (!event.getItem().equals(craftItem)) { + itemstack.count++; + // Chain to handler for new item + ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem()); + IDispenseBehavior idispensebehavior = (IDispenseBehavior) BlockDispenser.a.a(eventStack.getItem()); + if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) { + idispensebehavior.a(isourceblock, eventStack); + return itemstack; + } + } + // CraftBukkit end itemstack1.count = 1; entityliving.setEquipment(i1 - l, itemstack1); entityliving.a(i1, 2.0F); - --itemstack.count; + // --itemstack.count; // CraftBukkit - handled above return itemstack; } else { return super.b(isourceblock, itemstack); diff --git a/src/main/java/net/minecraft/server/DispenseBehaviorBonemeal.java b/src/main/java/net/minecraft/server/DispenseBehaviorBonemeal.java index 4f18217138..54c590b687 100644 --- a/src/main/java/net/minecraft/server/DispenseBehaviorBonemeal.java +++ b/src/main/java/net/minecraft/server/DispenseBehaviorBonemeal.java @@ -1,5 +1,10 @@ package net.minecraft.server; +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.event.block.BlockDispenseEvent; +// CraftBukkit end + final class DispenseBehaviorBonemeal extends DispenseBehaviorItem { private boolean b = true; @@ -14,7 +19,34 @@ final class DispenseBehaviorBonemeal extends DispenseBehaviorItem { int j = isourceblock.getBlockY() + enumfacing.d(); int k = isourceblock.getBlockZ() + enumfacing.e(); - if (ItemDye.a(itemstack, world, i, j, k)) { + // CraftBukkit start + ItemStack itemstack1 = itemstack.a(1); + org.bukkit.block.Block block = world.getWorld().getBlockAt(isourceblock.getBlockX(), isourceblock.getBlockY(), isourceblock.getBlockZ()); + CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1); + + BlockDispenseEvent event = new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(0, 0, 0)); + if (!BlockDispenser.eventFired) { + world.getServer().getPluginManager().callEvent(event); + } + + if (event.isCancelled()) { + itemstack.count++; + return itemstack; + } + + if (!event.getItem().equals(craftItem)) { + itemstack.count++; + // Chain to handler for new item + ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem()); + IDispenseBehavior idispensebehavior = (IDispenseBehavior) BlockDispenser.a.a(eventStack.getItem()); + if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) { + idispensebehavior.a(isourceblock, eventStack); + return itemstack; + } + } + // CraftBukkit end + + if (ItemDye.a(itemstack1, world, i, j, k)) { // CraftBukkit - itemstack -> itemstack1 if (!world.isStatic) { world.triggerEffect(2005, i, j, k, 0); } diff --git a/src/main/java/net/minecraft/server/DispenseBehaviorFlintAndSteel.java b/src/main/java/net/minecraft/server/DispenseBehaviorFlintAndSteel.java index b143f6198c..834e93a27e 100644 --- a/src/main/java/net/minecraft/server/DispenseBehaviorFlintAndSteel.java +++ b/src/main/java/net/minecraft/server/DispenseBehaviorFlintAndSteel.java @@ -1,5 +1,10 @@ package net.minecraft.server; +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.event.block.BlockDispenseEvent; +// CraftBukkit end + final class DispenseBehaviorFlintAndSteel extends DispenseBehaviorItem { private boolean b = true; @@ -13,6 +18,30 @@ final class DispenseBehaviorFlintAndSteel extends DispenseBehaviorItem { int j = isourceblock.getBlockY() + enumfacing.d(); int k = isourceblock.getBlockZ() + enumfacing.e(); + // CraftBukkit start + org.bukkit.block.Block block = world.getWorld().getBlockAt(isourceblock.getBlockX(), isourceblock.getBlockY(), isourceblock.getBlockZ()); + CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack); + + BlockDispenseEvent event = new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(0, 0, 0)); + if (!BlockDispenser.eventFired) { + world.getServer().getPluginManager().callEvent(event); + } + + if (event.isCancelled()) { + return itemstack; + } + + if (!event.getItem().equals(craftItem)) { + // Chain to handler for new item + ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem()); + IDispenseBehavior idispensebehavior = (IDispenseBehavior) BlockDispenser.a.a(eventStack.getItem()); + if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) { + idispensebehavior.a(isourceblock, eventStack); + return itemstack; + } + } + // CraftBukkit end + if (world.isEmpty(i, j, k)) { world.setTypeIdUpdate(i, j, k, Block.FIRE.id); if (itemstack.isDamaged(1, world.random)) { diff --git a/src/main/java/net/minecraft/server/DispenseBehaviorTNT.java b/src/main/java/net/minecraft/server/DispenseBehaviorTNT.java index 724a2eef47..950d9e9d4b 100644 --- a/src/main/java/net/minecraft/server/DispenseBehaviorTNT.java +++ b/src/main/java/net/minecraft/server/DispenseBehaviorTNT.java @@ -1,5 +1,10 @@ package net.minecraft.server; +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.event.block.BlockDispenseEvent; +// CraftBukkit end + final class DispenseBehaviorTNT extends DispenseBehaviorItem { DispenseBehaviorTNT() {} @@ -10,10 +15,38 @@ final class DispenseBehaviorTNT extends DispenseBehaviorItem { int i = isourceblock.getBlockX() + enumfacing.c(); int j = isourceblock.getBlockY() + enumfacing.d(); int k = isourceblock.getBlockZ() + enumfacing.e(); - EntityTNTPrimed entitytntprimed = new EntityTNTPrimed(world, (double) ((float) i + 0.5F), (double) ((float) j + 0.5F), (double) ((float) k + 0.5F), (EntityLiving) null); + + // CraftBukkit start + ItemStack itemstack1 = itemstack.a(1); + org.bukkit.block.Block block = world.getWorld().getBlockAt(isourceblock.getBlockX(), isourceblock.getBlockY(), isourceblock.getBlockZ()); + CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1); + + BlockDispenseEvent event = new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(i + 0.5, j + 0.5, k + 0.5)); + if (!BlockDispenser.eventFired) { + world.getServer().getPluginManager().callEvent(event); + } + + if (event.isCancelled()) { + itemstack.count++; + return itemstack; + } + + if (!event.getItem().equals(craftItem)) { + itemstack.count++; + // Chain to handler for new item + ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem()); + IDispenseBehavior idispensebehavior = (IDispenseBehavior) BlockDispenser.a.a(eventStack.getItem()); + if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) { + idispensebehavior.a(isourceblock, eventStack); + return itemstack; + } + } + + EntityTNTPrimed entitytntprimed = new EntityTNTPrimed(world, event.getVelocity().getX(), event.getVelocity().getY(), event.getVelocity().getZ(), (EntityLiving) null); + // CraftBukkit end world.addEntity(entitytntprimed); - --itemstack.count; + // --itemstack.count; // CraftBukkit - handled above return itemstack; } }