From 54cce5bf9210362b9dda0148b65267327a1c8b17 Mon Sep 17 00:00:00 2001 From: h31ix Date: Tue, 30 Oct 2012 23:21:53 -0400 Subject: [PATCH] [Bleeding] Implement new Hanging events. Adds BUKKIT-2754 --- .../net/minecraft/server/EntityHanging.java | 83 +++++++++++-------- .../net/minecraft/server/ItemHanging.java | 25 ++++-- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/main/java/net/minecraft/server/EntityHanging.java b/src/main/java/net/minecraft/server/EntityHanging.java index 97ae4f6f96..0427997e4e 100644 --- a/src/main/java/net/minecraft/server/EntityHanging.java +++ b/src/main/java/net/minecraft/server/EntityHanging.java @@ -4,8 +4,10 @@ import java.util.Iterator; import java.util.List; // CraftBukkit start +import org.bukkit.entity.Hanging; import org.bukkit.entity.Painting; -import org.bukkit.event.painting.PaintingBreakEvent.RemoveCause; +import org.bukkit.event.hanging.HangingBreakByEntityEvent; +import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.painting.PaintingBreakEvent; // CraftBukkit end @@ -104,25 +106,29 @@ public abstract class EntityHanging extends Entity { this.e = 0; if (!this.dead && !this.survives()) { // CraftBukkit start + Material material = this.world.getMaterial((int) this.locX, (int) this.locY, (int) this.locZ); + HangingBreakEvent.RemoveCause cause; + + if (!material.equals(Material.AIR)) { + // TODO: This feels insufficient to catch 100% of suffocation cases + cause = HangingBreakEvent.RemoveCause.OBSTRUCTION; + } else { + cause = HangingBreakEvent.RemoveCause.PHYSICS; + } + + HangingBreakEvent event = new HangingBreakEvent((Hanging) this.getBukkitEntity(), cause); + this.world.getServer().getPluginManager().callEvent(event); + + PaintingBreakEvent paintingEvent = null; if (this instanceof EntityPainting) { - Material material = this.world.getMaterial((int) this.locX, (int) this.locY, (int) this.locZ); - RemoveCause cause; + // Fire old painting event until it can be removed + paintingEvent = new PaintingBreakEvent((Painting) this.getBukkitEntity(), PaintingBreakEvent.RemoveCause.valueOf(cause.name())); + paintingEvent.setCancelled(event.isCancelled()); + this.world.getServer().getPluginManager().callEvent(paintingEvent); + } - if (material.equals(Material.WATER)) { - cause = RemoveCause.WATER; - } else if (!material.equals(Material.AIR)) { - // TODO: This feels insufficient to catch 100% of suffocation cases - cause = RemoveCause.OBSTRUCTION; - } else { - cause = RemoveCause.PHYSICS; - } - - PaintingBreakEvent event = new PaintingBreakEvent((Painting) this.getBukkitEntity(), cause); - this.world.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled() || dead) { - return; - } + if (dead || event.isCancelled() || (paintingEvent != null && paintingEvent.isCancelled())) { + return; } // CraftBukkit end @@ -200,28 +206,39 @@ public abstract class EntityHanging extends Entity { public boolean damageEntity(DamageSource damagesource, int i) { if (!this.dead && !this.world.isStatic) { // CraftBukkit start + HangingBreakEvent event = null; + PaintingBreakEvent paintingEvent = null; + if (damagesource.getEntity() != null) { + event = new HangingBreakByEntityEvent((Hanging) this.getBukkitEntity(), damagesource.getEntity() == null ? null : damagesource.getEntity().getBukkitEntity()); + } else { + if (damagesource == DamageSource.FIRE) { + event = new HangingBreakEvent((Hanging) this.getBukkitEntity(), HangingBreakEvent.RemoveCause.FIRE); + } + // TODO: Could put other stuff here? + } + if (this instanceof EntityPainting) { - PaintingBreakEvent event = null; + // Fire old painting event until it can be removed if (damagesource.getEntity() != null) { - event = new org.bukkit.event.painting.PaintingBreakByEntityEvent((Painting) this.getBukkitEntity(), damagesource.getEntity() == null ? null : damagesource.getEntity().getBukkitEntity()); + paintingEvent = new org.bukkit.event.painting.PaintingBreakByEntityEvent((Painting) this.getBukkitEntity(), damagesource.getEntity() == null ? null : damagesource.getEntity().getBukkitEntity()); } else { if (damagesource == DamageSource.FIRE) { - event = new PaintingBreakEvent((Painting) this.getBukkitEntity(), RemoveCause.FIRE); - } - // TODO: Could put other stuff here? - } - - if (event != null) { - this.world.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return true; + paintingEvent = new PaintingBreakEvent((Painting) this.getBukkitEntity(), PaintingBreakEvent.RemoveCause.valueOf(HangingBreakEvent.RemoveCause.FIRE.name())); } } + } - if (this.dead) { - return true; - } + if (event != null) { + this.world.getServer().getPluginManager().callEvent(event); + } + + if (paintingEvent != null) { + paintingEvent.setCancelled(event != null && event.isCancelled()); + this.world.getServer().getPluginManager().callEvent(paintingEvent); + } + + if (dead || event.isCancelled() || (paintingEvent != null && paintingEvent.isCancelled())) { + return true; } // CraftBukkit end diff --git a/src/main/java/net/minecraft/server/ItemHanging.java b/src/main/java/net/minecraft/server/ItemHanging.java index 85ccd00cd3..27d4c25932 100644 --- a/src/main/java/net/minecraft/server/ItemHanging.java +++ b/src/main/java/net/minecraft/server/ItemHanging.java @@ -2,6 +2,7 @@ package net.minecraft.server; // CraftBukkit start import org.bukkit.entity.Player; +import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.painting.PaintingPlaceEvent; // CraftBukkit end @@ -30,17 +31,23 @@ public class ItemHanging extends Item { if (entityhanging != null && entityhanging.survives()) { if (!world.isStatic) { // CraftBukkit start - if (entityhanging instanceof EntityPainting) { - Player who = (entityhuman == null) ? null : (Player) entityhuman.getBukkitEntity(); - org.bukkit.block.Block blockClicked = world.getWorld().getBlockAt(i, j, k); - org.bukkit.block.BlockFace blockFace = org.bukkit.craftbukkit.block.CraftBlock.notchToBlockFace(l); + Player who = (entityhuman == null) ? null : (Player) entityhuman.getBukkitEntity(); + org.bukkit.block.Block blockClicked = world.getWorld().getBlockAt(i, j, k); + org.bukkit.block.BlockFace blockFace = org.bukkit.craftbukkit.block.CraftBlock.notchToBlockFace(l); - PaintingPlaceEvent event = new PaintingPlaceEvent((org.bukkit.entity.Painting) entityhanging.getBukkitEntity(), who, blockClicked, blockFace); - world.getServer().getPluginManager().callEvent(event); + HangingPlaceEvent event = new HangingPlaceEvent((org.bukkit.entity.Hanging) entityhanging.getBukkitEntity(), who, blockClicked, blockFace); + world.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { - return false; - } + PaintingPlaceEvent paintingEvent = null; + if(entityhanging instanceof EntityPainting) { + // Fire old painting event until it can be removed + paintingEvent = new PaintingPlaceEvent((org.bukkit.entity.Painting) entityhanging.getBukkitEntity(), who, blockClicked, blockFace); + paintingEvent.setCancelled(event.isCancelled()); + world.getServer().getPluginManager().callEvent(paintingEvent); + } + + if (event.isCancelled() || (paintingEvent != null && paintingEvent.isCancelled())) { + return false; } // CraftBukkit end