From 54672a75f60a69802e7080b526fea737608a2530 Mon Sep 17 00:00:00 2001 From: T00thpick1 Date: Wed, 31 Jul 2013 17:35:16 -0400 Subject: [PATCH] Implement Leash API. Adds BUKKIT-4459, BUKKIT-4583 --- .../net/minecraft/server/EntityCreature.java | 1 + .../minecraft/server/EntityInsentient.java | 25 ++++++++- .../net/minecraft/server/EntityLeash.java | 28 ++++++++-- .../java/net/minecraft/server/ItemLeash.java | 17 +++++++ .../org/bukkit/craftbukkit/CraftWorld.java | 4 +- .../craftbukkit/entity/CraftEntity.java | 1 + .../bukkit/craftbukkit/entity/CraftLeash.java | 27 ++++++++++ .../craftbukkit/entity/CraftLivingEntity.java | 51 ++++++++++++++++--- .../craftbukkit/event/CraftEventFactory.java | 16 ++++-- 9 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftLeash.java diff --git a/src/main/java/net/minecraft/server/EntityCreature.java b/src/main/java/net/minecraft/server/EntityCreature.java index 9642c23e16..c2f63c4c95 100644 --- a/src/main/java/net/minecraft/server/EntityCreature.java +++ b/src/main/java/net/minecraft/server/EntityCreature.java @@ -264,6 +264,7 @@ public abstract class EntityCreature extends EntityInsentient { if (this instanceof EntityTameableAnimal && ((EntityTameableAnimal) this).isSitting()) { if (f > 10.0F) { + this.world.getServer().getPluginManager().callEvent(new org.bukkit.event.entity.EntityUnleashEvent(this.getBukkitEntity(), org.bukkit.event.entity.EntityUnleashEvent.UnleashReason.DISTANCE)); // CraftBukkit this.a(true, true); } diff --git a/src/main/java/net/minecraft/server/EntityInsentient.java b/src/main/java/net/minecraft/server/EntityInsentient.java index 34eb87252e..b590c370c6 100644 --- a/src/main/java/net/minecraft/server/EntityInsentient.java +++ b/src/main/java/net/minecraft/server/EntityInsentient.java @@ -4,8 +4,11 @@ import java.util.Iterator; import java.util.List; import java.util.UUID; -//CraftBukkit start +// CraftBukkit start import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.event.entity.EntityUnleashEvent; +import org.bukkit.event.entity.EntityUnleashEvent.UnleashReason; +// CraftBukkit end public abstract class EntityInsentient extends EntityLiving { @@ -769,6 +772,12 @@ public abstract class EntityInsentient extends EntityLiving { public final boolean c(EntityHuman entityhuman) { if (this.bH() && this.bI() == entityhuman) { + // CraftBukkit start + if (CraftEventFactory.callPlayerUnleashEntityEvent(this, entityhuman).isCancelled()) { + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet39AttachEntity(1, this, this.bI())); + return false; + } + // CraftBukkit end this.a(true, !entityhuman.abilities.canInstantlyBuild); return true; } else { @@ -776,12 +785,24 @@ public abstract class EntityInsentient extends EntityLiving { if (itemstack != null && itemstack.id == Item.LEASH.id && this.bG()) { if (!(this instanceof EntityTameableAnimal) || !((EntityTameableAnimal) this).isTamed()) { + // CraftBukkit start + if (CraftEventFactory.callPlayerLeashEntityEvent(this, entityhuman, entityhuman).isCancelled()) { + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet39AttachEntity(1, this, this.bI())); + return false; + } + // CraftBukkit end this.b(entityhuman, true); --itemstack.count; return true; } if (entityhuman.getName().equalsIgnoreCase(((EntityTameableAnimal) this).getOwnerName())) { + // CraftBukkit start + if (CraftEventFactory.callPlayerLeashEntityEvent(this, entityhuman, entityhuman).isCancelled()) { + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet39AttachEntity(1, this, this.bI())); + return false; + } + // CraftBukkit end this.b(entityhuman, true); --itemstack.count; return true; @@ -803,6 +824,7 @@ public abstract class EntityInsentient extends EntityLiving { if (this.bv) { if (this.bw == null || this.bw.dead) { + this.world.getServer().getPluginManager().callEvent(new EntityUnleashEvent(this.getBukkitEntity(), UnleashReason.HOLDER_GONE)); // CraftBukkit this.a(true, true); } } @@ -869,6 +891,7 @@ public abstract class EntityInsentient extends EntityLiving { this.bw = entityleash; } else { + this.world.getServer().getPluginManager().callEvent(new EntityUnleashEvent(this.getBukkitEntity(), UnleashReason.UNKNOWN)); // CraftBukkit this.a(false, true); } } diff --git a/src/main/java/net/minecraft/server/EntityLeash.java b/src/main/java/net/minecraft/server/EntityLeash.java index d7f33d03f4..da2632d533 100644 --- a/src/main/java/net/minecraft/server/EntityLeash.java +++ b/src/main/java/net/minecraft/server/EntityLeash.java @@ -3,6 +3,8 @@ package net.minecraft.server; import java.util.Iterator; import java.util.List; +import org.bukkit.craftbukkit.event.CraftEventFactory; // CraftBukkit + public class EntityLeash extends EntityHanging { public EntityLeash(World world) { @@ -55,6 +57,12 @@ public class EntityLeash extends EntityHanging { while (iterator.hasNext()) { entityinsentient = (EntityInsentient) iterator.next(); if (entityinsentient.bH() && entityinsentient.bI() == entityhuman) { + // CraftBukkit start + if (CraftEventFactory.callPlayerLeashEntityEvent(entityinsentient, this, entityhuman).isCancelled()) { + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet39AttachEntity(1, entityinsentient, entityinsentient.bI())); + continue; + } + // CraftBukkit end entityinsentient.b(this, true); flag = true; } @@ -63,8 +71,11 @@ public class EntityLeash extends EntityHanging { } if (!this.world.isStatic && !flag) { - this.die(); - if (entityhuman.abilities.canInstantlyBuild) { + // CraftBukkit start - Move below + // this.die(); + boolean die = true; + // CraftBukkit end + if (true || entityhuman.abilities.canInstantlyBuild) { // CraftBukkit - Process for non-creative as well d0 = 7.0D; list = this.world.a(EntityInsentient.class, AxisAlignedBB.a().a(this.locX - d0, this.locY - d0, this.locZ - d0, this.locX + d0, this.locY + d0, this.locZ + d0)); if (list != null) { @@ -73,11 +84,22 @@ public class EntityLeash extends EntityHanging { while (iterator.hasNext()) { entityinsentient = (EntityInsentient) iterator.next(); if (entityinsentient.bH() && entityinsentient.bI() == this) { - entityinsentient.a(true, false); + // CraftBukkit start + if (CraftEventFactory.callPlayerUnleashEntityEvent(entityinsentient, entityhuman).isCancelled()) { + die = false; + continue; + } + entityinsentient.a(true, !entityhuman.abilities.canInstantlyBuild); // false -> survival mode boolean + // CraftBukkit end } } } } + // CraftBukkit start + if (die) { + this.die(); + } + // CraftBukkit end } return true; diff --git a/src/main/java/net/minecraft/server/ItemLeash.java b/src/main/java/net/minecraft/server/ItemLeash.java index 7be274318e..c0e1eb3a03 100644 --- a/src/main/java/net/minecraft/server/ItemLeash.java +++ b/src/main/java/net/minecraft/server/ItemLeash.java @@ -3,6 +3,8 @@ package net.minecraft.server; import java.util.Iterator; import java.util.List; +import org.bukkit.event.hanging.HangingPlaceEvent; // CraftBukkit + public class ItemLeash extends Item { public ItemLeash(int i) { @@ -40,8 +42,23 @@ public class ItemLeash extends Item { if (entityinsentient.bH() && entityinsentient.bI() == entityhuman) { if (entityleash == null) { entityleash = EntityLeash.a(world, i, j, k); + + // CraftBukkit start + HangingPlaceEvent event = new HangingPlaceEvent((org.bukkit.entity.Hanging) entityleash.getBukkitEntity(), entityhuman != null ? (org.bukkit.entity.Player) entityhuman.getBukkitEntity() : null, world.getWorld().getBlockAt(i, j, k), org.bukkit.block.BlockFace.SELF); + world.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + entityleash.die(); + return false; + } + // CraftBukkit end } + // CraftBukkit start + if (org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerLeashEntityEvent(entityinsentient, entityleash, entityhuman).isCancelled()) { + continue; + } + // CraftBukkit end entityinsentient.b(entityleash, true); flag = true; } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 75b1fb2ec8..e8315dd08a 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -4,7 +4,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Set; @@ -994,6 +993,9 @@ public class CraftWorld implements World { entity = new EntityPainting(world, (int) x, (int) y, (int) z, dir); } else if (ItemFrame.class.isAssignableFrom(clazz)) { entity = new EntityItemFrame(world, (int) x, (int) y, (int) z, dir); + } else if (LeashHitch.class.isAssignableFrom(clazz)) { + entity = new EntityLeash(world, (int) x, (int) y, (int) z); + entity.p = true; } if (entity != null && !((EntityHanging) entity).survives()) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java index 8e2e5cda2a..d9659fb679 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -147,6 +147,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { } else if (entity instanceof EntityHanging) { if (entity instanceof EntityPainting) { return new CraftPainting(server, (EntityPainting) entity); } else if (entity instanceof EntityItemFrame) { return new CraftItemFrame(server, (EntityItemFrame) entity); } + else if (entity instanceof EntityLeash) { return new CraftLeash(server, (EntityLeash) entity); } else { return new CraftHanging(server, (EntityHanging) entity); } } else if (entity instanceof EntityTNTPrimed) { return new CraftTNTPrimed(server, (EntityTNTPrimed) entity); } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLeash.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLeash.java new file mode 100644 index 0000000000..710ed7a646 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLeash.java @@ -0,0 +1,27 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityLeash; + +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LeashHitch; + +public class CraftLeash extends CraftHanging implements LeashHitch { + public CraftLeash(CraftServer server, EntityLeash entity) { + super(server, entity); + } + + @Override + public EntityLeash getHandle() { + return (EntityLeash) entity; + } + + @Override + public String toString() { + return "CraftLeash"; + } + + public EntityType getType() { + return EntityType.LEASH_HITCH; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 443dde9195..f20c664fd1 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -14,18 +14,17 @@ import net.minecraft.server.EntityEnderPearl; import net.minecraft.server.EntityInsentient; import net.minecraft.server.EntityLargeFireball; import net.minecraft.server.EntityLiving; -import net.minecraft.server.EntitySmallFireball; -import net.minecraft.server.EntitySnowball; import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPotion; +import net.minecraft.server.EntitySmallFireball; +import net.minecraft.server.EntitySnowball; +import net.minecraft.server.EntityWither; import net.minecraft.server.EntityWitherSkull; import net.minecraft.server.GenericAttributes; import net.minecraft.server.MobEffect; import net.minecraft.server.MobEffectList; -import net.minecraft.server.Packet42RemoveMobEffect; import org.apache.commons.lang.Validate; - import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -35,11 +34,11 @@ import org.bukkit.craftbukkit.inventory.CraftEntityEquipment; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.entity.Arrow; import org.bukkit.entity.Egg; +import org.bukkit.entity.EnderPearl; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.EnderPearl; import org.bukkit.entity.Fireball; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; @@ -407,6 +406,46 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { return getHandle() instanceof EntityInsentient && ((EntityInsentient) getHandle()).getCustomNameVisible(); } + public boolean isLeashed() { + if (!(getHandle() instanceof EntityInsentient)) { + return false; + } + return ((EntityInsentient) getHandle()).bI() != null; + } + + public Entity getLeashHolder() throws IllegalStateException { + if (!isLeashed()) { + throw new IllegalStateException("Entity not leashed"); + } + return ((EntityInsentient) getHandle()).bI().getBukkitEntity(); + } + + private boolean unleash() { + if (!isLeashed()) { + return false; + } + ((EntityInsentient) getHandle()).a(true, false); + return true; + } + + public boolean setLeashHolder(Entity holder) { + if ((getHandle() instanceof EntityWither) || !(getHandle() instanceof EntityInsentient)) { + return false; + } + + if (holder == null) { + return unleash(); + } + + if (holder.isDead()) { + return false; + } + + unleash(); + ((EntityInsentient) getHandle()).b(((CraftEntity) holder).getHandle(), true); + return true; + } + @Deprecated public int _INVALID_getLastDamage() { return NumberConversions.ceil(getLastDamage()); diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 6db7b9ba30..8dea190bf2 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -30,8 +30,8 @@ import net.minecraft.server.World; import net.minecraft.server.WorldServer; import org.bukkit.Bukkit; -import org.bukkit.Server; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -44,10 +44,8 @@ import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.inventory.CraftInventoryCrafting; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.util.CraftDamageSource; -import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.Arrow; import org.bukkit.entity.Creeper; -import org.bukkit.entity.EntityType; import org.bukkit.entity.Horse; import org.bukkit.entity.LightningStrike; import org.bukkit.entity.LivingEntity; @@ -706,4 +704,16 @@ public class CraftEventFactory { player.playerConnection.sendPacket(new Packet103SetSlot(player.activeContainer.windowId, slot.g, itemInHand)); } } + + public static PlayerUnleashEntityEvent callPlayerUnleashEntityEvent(EntityInsentient entity, EntityHuman player) { + PlayerUnleashEntityEvent event = new PlayerUnleashEntityEvent(entity.getBukkitEntity(), (Player) player.getBukkitEntity()); + entity.world.getServer().getPluginManager().callEvent(event); + return event; + } + + public static PlayerLeashEntityEvent callPlayerLeashEntityEvent(EntityInsentient entity, Entity leashHolder, EntityHuman player) { + PlayerLeashEntityEvent event = new PlayerLeashEntityEvent(entity.getBukkitEntity(), leashHolder.getBukkitEntity(), (Player) player.getBukkitEntity()); + entity.world.getServer().getPluginManager().callEvent(event); + return event; + } }