From f1f9802ded9b7bf273ccdbfc077c636a57d3942d Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Thu, 1 Mar 2012 18:02:16 +0000 Subject: [PATCH] Implemented new 1.2 entities - this fixes BUKKIT-872 and BUKKIT-885 --- .../org/bukkit/craftbukkit/CraftWorld.java | 19 +++- .../craftbukkit/entity/CraftEntity.java | 11 ++- .../bukkit/craftbukkit/entity/CraftGolem.java | 21 +++++ .../craftbukkit/entity/CraftIronGolem.java | 27 ++++++ .../craftbukkit/entity/CraftOcelot.java | 32 +++++++ .../craftbukkit/entity/CraftSnowman.java | 3 +- .../entity/CraftTameableAnimal.java | 81 ++++++++++++++++ .../entity/CraftThrownExpBottle.java | 26 ++++++ .../bukkit/craftbukkit/entity/CraftWolf.java | 93 +------------------ 9 files changed, 213 insertions(+), 100 deletions(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftGolem.java create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftIronGolem.java create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftOcelot.java create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftTameableAnimal.java create mode 100644 src/main/java/org/bukkit/craftbukkit/entity/CraftThrownExpBottle.java diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 7fd2ff2f89..1d53bd539e 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -781,6 +781,9 @@ public class CraftWorld implements World { } else if (Arrow.class.isAssignableFrom(clazz)) { entity = new EntityArrow(world); entity.setPositionRotation(x, y, z, 0, 0); + } else if (ThrownExpBottle.class.isAssignableFrom(clazz)) { + entity = new EntityThrownExpBottle(world); + entity.setPositionRotation(x, y, z, 0, 0); } else if (Fireball.class.isAssignableFrom(clazz)) { if (SmallFireball.class.isAssignableFrom(clazz)) { entity = new EntitySmallFireball(world); @@ -813,8 +816,12 @@ public class CraftWorld implements World { } else { entity = new EntityCow(world); } - } else if (Snowman.class.isAssignableFrom(clazz)) { - entity = new EntitySnowman(world); + } else if (Golem.class.isAssignableFrom(clazz)) { + if (Snowman.class.isAssignableFrom(clazz)) { + entity = new EntitySnowman(world); + } else if (IronGolem.class.isAssignableFrom(clazz)) { + entity = new EntityIronGolem(world); + } } else if (Creeper.class.isAssignableFrom(clazz)) { entity = new EntityCreeper(world); } else if (Ghast.class.isAssignableFrom(clazz)) { @@ -841,8 +848,12 @@ public class CraftWorld implements World { } } else if (Squid.class.isAssignableFrom(clazz)) { entity = new EntitySquid(world); - } else if (Wolf.class.isAssignableFrom(clazz)) { - entity = new EntityWolf(world); + } else if (Tameable.class.isAssignableFrom(clazz)) { + if (Wolf.class.isAssignableFrom(clazz)) { + entity = new EntityWolf(world); + } else if (Ocelot.class.isAssignableFrom(clazz)) { + entity = new EntityOcelot(world); + } } else if (PigZombie.class.isAssignableFrom(clazz)) { entity = new EntityPigZombie(world); } else if (Zombie.class.isAssignableFrom(clazz)) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java index 4b5814da2d..3e67ddecbb 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -51,7 +51,10 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { else { return new CraftCow(server, (EntityCow) entity); } } else if (entity instanceof EntityPig) { return new CraftPig(server, (EntityPig) entity); } - else if (entity instanceof EntityWolf) { return new CraftWolf(server, (EntityWolf) entity); } + else if (entity instanceof EntityTameableAnimal) { + if (entity instanceof EntityWolf) { return new CraftWolf(server, (EntityWolf) entity); } + else if (entity instanceof EntityOcelot) { return new CraftOcelot(server, (EntityOcelot) entity); } + } else if (entity instanceof EntitySheep) { return new CraftSheep(server, (EntitySheep) entity); } else { return new CraftAnimals(server, (EntityAnimal) entity); } } @@ -79,7 +82,10 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { if (entity instanceof EntitySquid) { return new CraftSquid(server, (EntitySquid) entity); } else { return new CraftWaterMob(server, (EntityWaterAnimal) entity); } } - else if (entity instanceof EntitySnowman) { return new CraftSnowman(server, (EntitySnowman) entity); } + else if (entity instanceof EntityGolem) { + if (entity instanceof EntitySnowman) { return new CraftSnowman(server, (EntitySnowman) entity); } + else if (entity instanceof EntityIronGolem) { return new CraftIronGolem(server, (EntityIronGolem) entity); } + } else if (entity instanceof EntityVillager) { return new CraftVillager(server, (EntityVillager) entity); } else { return new CraftCreature(server, (EntityCreature) entity); } } @@ -111,6 +117,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { else if (entity instanceof EntitySnowball) { return new CraftSnowball(server, (EntitySnowball) entity); } else if (entity instanceof EntityPotion) { return new CraftThrownPotion(server, (EntityPotion) entity); } else if (entity instanceof EntityEnderPearl) { return new CraftEnderPearl(server, (EntityEnderPearl) entity); } + else if (entity instanceof EntityThrownExpBottle) { return new CraftThrownExpBottle(server, (EntityThrownExpBottle) entity); } } else if (entity instanceof EntityFallingBlock) { return new CraftFallingSand(server, (EntityFallingBlock) entity); } else if (entity instanceof EntityFireball) { diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftGolem.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftGolem.java new file mode 100644 index 0000000000..1fef5e0291 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftGolem.java @@ -0,0 +1,21 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityGolem; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.Golem; + +public class CraftGolem extends CraftCreature implements Golem { + public CraftGolem(CraftServer server, EntityGolem entity) { + super(server, entity); + } + + @Override + public EntityGolem getHandle() { + return (EntityGolem) entity; + } + + @Override + public String toString() { + return "CraftGolem"; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftIronGolem.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftIronGolem.java new file mode 100644 index 0000000000..39f97e4745 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftIronGolem.java @@ -0,0 +1,27 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityIronGolem; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.IronGolem; + +public class CraftIronGolem extends CraftGolem implements IronGolem { + public CraftIronGolem(CraftServer server, EntityIronGolem entity) { + super(server, entity); + } + + @Override + public EntityIronGolem getHandle() { + return (EntityIronGolem) entity; + } + + @Override + public String toString() { + return "CraftIronGolem"; + } + + @Override + public EntityType getType() { + return EntityType.IRON_GOLEM; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftOcelot.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftOcelot.java new file mode 100644 index 0000000000..37cc315c6b --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftOcelot.java @@ -0,0 +1,32 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityOcelot; +import org.apache.commons.lang.Validate; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Ocelot; + +public class CraftOcelot extends CraftTameableAnimal implements Ocelot { + public CraftOcelot(CraftServer server, EntityOcelot wolf) { + super(server, wolf); + } + + @Override + public EntityOcelot getHandle() { + return (EntityOcelot) entity; + } + + public Type getCatType() { + return Type.getType(getHandle().getCatType()); + } + + public void setCatType(Type type) { + Validate.notNull(type, "Cat type cannot be null"); + getHandle().setCatType(type.getId()); + } + + @Override + public EntityType getType() { + return EntityType.OCELOT; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java index d725de330c..319a3f3ce9 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftSnowman.java @@ -1,12 +1,11 @@ package org.bukkit.craftbukkit.entity; import net.minecraft.server.EntitySnowman; - import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.EntityType; import org.bukkit.entity.Snowman; -public class CraftSnowman extends CraftCreature implements Snowman { +public class CraftSnowman extends CraftGolem implements Snowman { public CraftSnowman(CraftServer server, EntitySnowman entity) { super(server, entity); } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftTameableAnimal.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftTameableAnimal.java new file mode 100644 index 0000000000..5226c392f8 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftTameableAnimal.java @@ -0,0 +1,81 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityTameableAnimal; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.AnimalTamer; +import org.bukkit.entity.Creature; +import org.bukkit.entity.Player; +import org.bukkit.entity.Tameable; + +public class CraftTameableAnimal extends CraftAnimals implements Tameable, Creature { + private AnimalTamer owner; + + public CraftTameableAnimal(CraftServer server, EntityTameableAnimal entity) { + super(server, entity); + } + + @Override + public EntityTameableAnimal getHandle() { + return (EntityTameableAnimal)super.getHandle(); + } + + public AnimalTamer getOwner() { + if (owner == null && !("").equals(getOwnerName())) { + owner = getServer().getPlayer(getOwnerName()); + + if (owner == null) { + owner = getServer().getOfflinePlayer(getOwnerName()); + } + } + + return owner; + } + + public String getOwnerName() { + return getHandle().getOwnerName(); + } + + public boolean isTamed() { + return getHandle().isTamed(); + } + + public void setOwner(AnimalTamer tamer) { + owner = tamer; + + if (owner != null) { + setTamed(true); + getHandle().setPathEntity(null); + + if (owner instanceof Player) { + setOwnerName(((Player) owner).getName()); + } else { + setOwnerName(""); + } + } else { + setTamed(false); + setOwnerName(""); + } + } + + public void setOwnerName(String ownerName) { + getHandle().setOwnerName(ownerName); + } + + public void setTamed(boolean tame) { + getHandle().setTamed(tame); + } + + public boolean isSitting() { + return getHandle().isSitting(); + } + + public void setSitting(boolean sitting) { + getHandle().setSitting(sitting); + getHandle().setPathEntity(null); + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{owner=" + getOwner() + ",tamed=" + isTamed() + "}"; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftThrownExpBottle.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftThrownExpBottle.java new file mode 100644 index 0000000000..fb3416e658 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftThrownExpBottle.java @@ -0,0 +1,26 @@ +package org.bukkit.craftbukkit.entity; + +import net.minecraft.server.EntityThrownExpBottle; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.ThrownExpBottle; + +public class CraftThrownExpBottle extends CraftProjectile implements ThrownExpBottle { + public CraftThrownExpBottle(CraftServer server, EntityThrownExpBottle entity) { + super(server, entity); + } + + @Override + public EntityThrownExpBottle getHandle() { + return (EntityThrownExpBottle) entity; + } + + @Override + public String toString() { + return "EntityThrownExpBottle"; + } + + public EntityType getType() { + return EntityType.THROWN_EXP_BOTTLE; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftWolf.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftWolf.java index 86326b0a6d..d317801224 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftWolf.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftWolf.java @@ -1,17 +1,11 @@ package org.bukkit.craftbukkit.entity; import net.minecraft.server.EntityWolf; -import net.minecraft.server.PathEntity; - import org.bukkit.craftbukkit.CraftServer; -import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; import org.bukkit.entity.Wolf; -public class CraftWolf extends CraftAnimals implements Wolf { - private AnimalTamer owner; - +public class CraftWolf extends CraftTameableAnimal implements Wolf { public CraftWolf(CraftServer server, EntityWolf wolf) { super(server, wolf); } @@ -24,97 +18,12 @@ public class CraftWolf extends CraftAnimals implements Wolf { getHandle().setAngry(angry); } - public boolean isSitting() { - return getHandle().isSitting(); - } - - public void setSitting(boolean sitting) { - getHandle().setSitting(sitting); - // TODO determine what the following would do - it is affected every time a player makes their wolf sit or stand - // getHandle().ay = false; - setPath((PathEntity) null); - } - - public boolean isTamed() { - return getHandle().isTamed(); - } - - public void setTamed(boolean tame) { - getHandle().setTamed(tame); - } - - public AnimalTamer getOwner() { - // If the wolf has a previously set owner use that, otherwise try and find the player who owns it - if (owner == null && !("").equals(getOwnerName())) { - owner = getServer().getPlayer(getOwnerName()); - - if (owner == null) { - owner = getServer().getOfflinePlayer(getOwnerName()); - } - } - return owner; - } - - public void setOwner(AnimalTamer tamer) { - owner = tamer; - if (owner != null) { - setTamed(true); /* Make him tame */ - setPath((PathEntity) null); /* Clear path */ - /* Set owner */ - // TODO persist owner to the persistence store - if (owner instanceof Player) { - setOwnerName(((Player) owner).getName()); - } else { - setOwnerName(""); - } - } else { - setTamed(false); /* Make him not tame */ - setOwnerName(""); /* Clear owner */ - } - } - - /** - * The owner's name is how MC knows and persists the Wolf's owner. Since we choose to instead use an AnimalTamer, this functionality - * is used only as a backup. If the animal tamer is a player, we will store their name, otherwise we store an empty string. - * - * @return the owner's name, if they are a player; otherwise, the empty string or null. - */ - String getOwnerName() { - return getHandle().getOwnerName(); - } - - void setOwnerName(String ownerName) { - getHandle().setOwnerName(ownerName); - } - - /** - * Only used internally at the moment, and there to set the path to null (that is stop the thing from running around) - * TODO use this later to extend the API, when we have Path classes in Bukkit - * - * @param pathentity currently the MC defined PathEntity class. Should be replaced with an API interface at some point. - */ - private void setPath(PathEntity pathentity) { - getHandle().setPathEntity(pathentity); - } - - /* - * This method requires a(boolean) to be made visible. It will allow for hearts to be animated on a successful taming. - * TODO add this to the API, and make it visible - private void playTamingAnimation(boolean successful){ - getHandle().a(successful); - } - */ - @Override public EntityWolf getHandle() { return (EntityWolf) entity; } @Override - public String toString() { - return "CraftWolf{anger=" + isAngry() + ",owner=" + getOwner() + ",tame=" + isTamed() + ",sitting=" + isSitting() + "}"; - } - public EntityType getType() { return EntityType.WOLF; }