From 77ef0510958b7386b7f9b861aa7936f21a183a6b Mon Sep 17 00:00:00 2001 From: feildmaster Date: Fri, 9 Mar 2012 15:40:59 -0600 Subject: [PATCH] [Bleeding] Added new target events. Fixes BUKKIT-935 --- .../net/minecraft/server/EntityHuman.java | 16 ----------- .../net/minecraft/server/EntityMonster.java | 24 ++++++++--------- .../net/minecraft/server/EntitySlime.java | 6 ++--- .../server/PathfinderGoalArrowAttack.java | 6 +++++ .../server/PathfinderGoalMeleeAttack.java | 6 +++++ .../server/PathfinderGoalTarget.java | 27 +++++++++++++++++++ .../craftbukkit/event/CraftEventFactory.java | 9 +++++-- 7 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java index 24fc1292c3..a5da800d8a 100644 --- a/src/main/java/net/minecraft/server/EntityHuman.java +++ b/src/main/java/net/minecraft/server/EntityHuman.java @@ -671,22 +671,6 @@ public abstract class EntityHuman extends EntityLiving { EntityWolf entitywolf1 = (EntityWolf) entity; if (entitywolf1.isTamed() && entitywolf1.H() == null && this.name.equals(entitywolf1.getOwnerName()) && (!flag || !entitywolf1.isSitting())) { - // CraftBukkit start - org.bukkit.entity.Entity bukkitTarget = entity == null ? null : entityliving.getBukkitEntity(); - - EntityTargetEvent event; - if (flag) { - event = new EntityTargetEvent(entitywolf1.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.OWNER_ATTACKED_TARGET); - } else { - event = new EntityTargetEvent(entitywolf1.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.TARGET_ATTACKED_OWNER); - } - this.world.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - continue; - } - // CraftBukkit end - entitywolf1.setSitting(false); entitywolf1.setTarget(entityliving); } diff --git a/src/main/java/net/minecraft/server/EntityMonster.java b/src/main/java/net/minecraft/server/EntityMonster.java index a5881412be..5f6dcf65c2 100644 --- a/src/main/java/net/minecraft/server/EntityMonster.java +++ b/src/main/java/net/minecraft/server/EntityMonster.java @@ -2,7 +2,6 @@ package net.minecraft.server; // CraftBukkit start import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.entity.CraftEntity; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityTargetEvent; @@ -46,20 +45,19 @@ public abstract class EntityMonster extends EntityCreature implements IMonster { if (this.passenger != entity && this.vehicle != entity) { if (entity != this) { - // CraftBukkit start - org.bukkit.entity.Entity bukkitTarget = entity == null ? null : entity.getBukkitEntity(); + // CraftBukkit start - We still need to call events for entities without goals + if (entity != this.target && (this instanceof EntityBlaze || this instanceof EntityEnderman || this instanceof EntitySpider || this instanceof EntityGiantZombie || this instanceof EntitySilverfish)) { + EntityTargetEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetEvent(this, entity, EntityTargetEvent.TargetReason.TARGET_ATTACKED_ENTITY); - EntityTargetEvent event = new EntityTargetEvent(this.getBukkitEntity(), bukkitTarget, EntityTargetEvent.TargetReason.TARGET_ATTACKED_ENTITY); - Bukkit.getPluginManager().callEvent(event); - - if (!event.isCancelled()) { - if (event.getTarget() == null) { - this.target = null; - this.lastDamager = null; - } else { - this.target = ((CraftEntity) event.getTarget()).getHandle(); - this.lastDamager = this.target instanceof EntityLiving ? (EntityLiving) this.target : null; + if (!event.isCancelled()) { + if (event.getTarget() == null) { + this.target = null; + } else { + this.target = ((org.bukkit.craftbukkit.entity.CraftEntity) event.getTarget()).getHandle(); + } } + } else { + this.target = entity; } // CraftBukkit end } diff --git a/src/main/java/net/minecraft/server/EntitySlime.java b/src/main/java/net/minecraft/server/EntitySlime.java index 4ed80557f1..939edc4b85 100644 --- a/src/main/java/net/minecraft/server/EntitySlime.java +++ b/src/main/java/net/minecraft/server/EntitySlime.java @@ -1,7 +1,5 @@ package net.minecraft.server; -import org.bukkit.entity.Slime; - public class EntitySlime extends EntityLiving implements IMonster { public float a; @@ -94,7 +92,7 @@ public class EntitySlime extends EntityLiving implements IMonster { protected void d_() { this.aF(); - EntityHuman entityhuman = this.world.findNearbyVulnerablePlayer(this, 16.0D); + EntityHuman entityhuman = this.world.findNearbyVulnerablePlayer(this, 16.0D); // CraftBukkit TODO: EntityTargetEvent if (entityhuman != null) { this.a(entityhuman, 10.0F, 20.0F); @@ -141,7 +139,7 @@ public class EntitySlime extends EntityLiving implements IMonster { int j = 2 + this.random.nextInt(3); // CraftBukkit start - org.bukkit.event.entity.SlimeSplitEvent event = new org.bukkit.event.entity.SlimeSplitEvent((Slime) this.getBukkitEntity(), j); + org.bukkit.event.entity.SlimeSplitEvent event = new org.bukkit.event.entity.SlimeSplitEvent((org.bukkit.entity.Slime) this.getBukkitEntity(), j); this.world.getServer().getPluginManager().callEvent(event); if (!event.isCancelled() && event.getCount() > 0) { diff --git a/src/main/java/net/minecraft/server/PathfinderGoalArrowAttack.java b/src/main/java/net/minecraft/server/PathfinderGoalArrowAttack.java index 479cb1eb8f..dce0513a27 100644 --- a/src/main/java/net/minecraft/server/PathfinderGoalArrowAttack.java +++ b/src/main/java/net/minecraft/server/PathfinderGoalArrowAttack.java @@ -1,5 +1,7 @@ package net.minecraft.server; +import org.bukkit.event.entity.EntityTargetEvent; // CraftBukkit + public class PathfinderGoalArrowAttack extends PathfinderGoal { World a; @@ -36,6 +38,10 @@ public class PathfinderGoalArrowAttack extends PathfinderGoal { } public void d() { + // CraftBukkit start + EntityTargetEvent.TargetReason reason = this.c.isAlive() ? EntityTargetEvent.TargetReason.FORGOT_TARGET : EntityTargetEvent.TargetReason.TARGET_DIED; + org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetEvent(b, null, reason); + // CraftBukkit end this.c = null; } diff --git a/src/main/java/net/minecraft/server/PathfinderGoalMeleeAttack.java b/src/main/java/net/minecraft/server/PathfinderGoalMeleeAttack.java index bed2ba503d..283e5f3098 100644 --- a/src/main/java/net/minecraft/server/PathfinderGoalMeleeAttack.java +++ b/src/main/java/net/minecraft/server/PathfinderGoalMeleeAttack.java @@ -1,5 +1,7 @@ package net.minecraft.server; +import org.bukkit.event.entity.EntityTargetEvent; // CraftBukkit + public class PathfinderGoalMeleeAttack extends PathfinderGoal { World a; @@ -52,6 +54,10 @@ public class PathfinderGoalMeleeAttack extends PathfinderGoal { } public void d() { + // CraftBukkit start + EntityTargetEvent.TargetReason reason = this.c.isAlive() ? EntityTargetEvent.TargetReason.FORGOT_TARGET : EntityTargetEvent.TargetReason.TARGET_DIED; + org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetEvent(b, null, reason); + // CraftBukkit end this.c = null; this.b.ak().f(); } diff --git a/src/main/java/net/minecraft/server/PathfinderGoalTarget.java b/src/main/java/net/minecraft/server/PathfinderGoalTarget.java index be5371a033..327f235c50 100644 --- a/src/main/java/net/minecraft/server/PathfinderGoalTarget.java +++ b/src/main/java/net/minecraft/server/PathfinderGoalTarget.java @@ -1,5 +1,7 @@ package net.minecraft.server; +import org.bukkit.event.entity.EntityTargetEvent; // CraftBukkit + public abstract class PathfinderGoalTarget extends PathfinderGoal { protected EntityLiving c; @@ -100,6 +102,31 @@ public abstract class PathfinderGoalTarget extends PathfinderGoal { } } + // CraftBukkit start - Check all the different target goals for the reason, default to RANDOM_TARGET + EntityTargetEvent.TargetReason reason = EntityTargetEvent.TargetReason.RANDOM_TARGET; + + if (this instanceof PathfinderGoalDefendVillage) { + reason = EntityTargetEvent.TargetReason.DEFEND_VILLAGE; + } else if (this instanceof PathfinderGoalHurtByTarget) { + reason = EntityTargetEvent.TargetReason.TARGET_ATTACKED_ENTITY; + } else if (this instanceof PathfinderGoalNearestAttackableTarget) { + if (entityliving instanceof EntityHuman) { + reason = EntityTargetEvent.TargetReason.CLOSEST_PLAYER; + } + } else if (this instanceof PathfinderGoalOwnerHurtByTarget) { + reason = EntityTargetEvent.TargetReason.TARGET_ATTACKED_OWNER; + } else if (this instanceof PathfinderGoalOwnerHurtTarget) { + reason = EntityTargetEvent.TargetReason.OWNER_ATTACKED_TARGET; + } + + org.bukkit.event.entity.EntityTargetLivingEntityEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetLivingEvent(c, entityliving, reason); + if (event.isCancelled() || event.getTarget() == null) { + return false; + } else if (entityliving.getBukkitEntity() != event.getTarget()) { + this.c.b((EntityLiving) ((org.bukkit.craftbukkit.entity.CraftEntity) event.getTarget()).getHandle()); + } + // CraftBukkit end + return true; } } diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index abf81e09d2..9684591ac0 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -430,7 +430,13 @@ public class CraftEventFactory { } public static EntityTargetEvent callEntityTargetEvent(Entity entity, Entity target, EntityTargetEvent.TargetReason reason) { - EntityTargetEvent event = new EntityTargetEvent(entity.getBukkitEntity(), target.getBukkitEntity(), reason); + EntityTargetEvent event = new EntityTargetEvent(entity.getBukkitEntity(), target == null ? null : target.getBukkitEntity(), reason); + entity.getBukkitEntity().getServer().getPluginManager().callEvent(event); + return event; + } + + public static EntityTargetLivingEntityEvent callEntityTargetLivingEvent(Entity entity, EntityLiving target, EntityTargetEvent.TargetReason reason) { + EntityTargetLivingEntityEvent event = new EntityTargetLivingEntityEvent(entity.getBukkitEntity(), (LivingEntity) target.getBukkitEntity(), reason); entity.getBukkitEntity().getServer().getPluginManager().callEvent(event); return event; } @@ -458,7 +464,6 @@ public class CraftEventFactory { public static ItemStack callPreCraftEvent(InventoryCrafting matrix, ItemStack result, InventoryView lastCraftView, boolean isRepair) { CraftInventoryCrafting inventory = new CraftInventoryCrafting(matrix, matrix.resultInventory); inventory.setResult(new CraftItemStack(result)); - PrepareItemCraftEvent event = new PrepareItemCraftEvent(inventory, lastCraftView, isRepair); Bukkit.getPluginManager().callEvent(event);