From ce4a390b00fa6cec9b9d9941e2a2b09b8f9e8b6b Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Tue, 14 Feb 2012 23:52:38 -0500 Subject: [Bleeding] Added EntityType to replace CreatureType. --- src/main/java/org/bukkit/World.java | 10 ++ .../java/org/bukkit/block/CreatureSpawner.java | 37 ++++++ src/main/java/org/bukkit/entity/CreatureType.java | 14 +++ src/main/java/org/bukkit/entity/Entity.java | 6 + src/main/java/org/bukkit/entity/EntityType.java | 129 +++++++++++++++++++++ .../bukkit/event/entity/CreatureSpawnEvent.java | 31 +++-- .../bukkit/event/player/PlayerEggThrowEvent.java | 39 ++++++- 7 files changed, 253 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/bukkit/entity/EntityType.java (limited to 'src/main') diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java index 53a1c5a1..5212f961 100644 --- a/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java @@ -310,6 +310,16 @@ public interface World extends PluginMessageRecipient { * @param type The creature to spawn * @return Resulting LivingEntity of this method, or null if it was unsuccessful */ + public LivingEntity spawnCreature(Location loc, EntityType type); + + /** + * Creates a creature at the given {@link Location} + * + * @param loc The location to spawn the creature + * @param type The creature to spawn + * @return Resulting LivingEntity of this method, or null if it was unsuccessful + */ + @Deprecated public LivingEntity spawnCreature(Location loc, CreatureType type); /** diff --git a/src/main/java/org/bukkit/block/CreatureSpawner.java b/src/main/java/org/bukkit/block/CreatureSpawner.java index 2ea1eb4e..9e479a75 100644 --- a/src/main/java/org/bukkit/block/CreatureSpawner.java +++ b/src/main/java/org/bukkit/block/CreatureSpawner.java @@ -1,6 +1,7 @@ package org.bukkit.block; import org.bukkit.entity.CreatureType; +import org.bukkit.entity.EntityType; /** * Represents a creature spawner. @@ -11,21 +12,41 @@ public interface CreatureSpawner extends BlockState { * Get the spawner's creature type. * * @return The creature type. + * @deprecated In favour of {@link #getSpawnedType()}. */ + @Deprecated public CreatureType getCreatureType(); + /** + * Get the spawner's creature type. + * + * @return The creature type. + */ + public EntityType getSpawnedType(); + + /** + * Set the spawner's creature type. + * + * @param creatureType The creature type. + */ + public void setSpawnedType(EntityType creatureType); + /** * Set the spawner creature type. * * @param creatureType The creature type. + * @deprecated In favour of {@link #setSpawnedType(EntityType}. */ + @Deprecated public void setCreatureType(CreatureType creatureType); /** * Get the spawner's creature type. * * @return The creature type's name. + * @deprecated Use {@link #getCreatureTypeName()}. */ + @Deprecated public String getCreatureTypeId(); /** @@ -33,6 +54,22 @@ public interface CreatureSpawner extends BlockState { * * @param creatureType The creature type's name. */ + public void setCreatureTypeByName(String creatureType); + + /** + * Get the spawner's creature type. + * + * @return The creature type's name. + */ + public String getCreatureTypeName(); + + /** + * Set the spawner mob type. + * + * @param creatureType The creature type's name. + * @deprecated Use {@link #setCreatureTypeByName(String)}. + */ + @Deprecated public void setCreatureTypeId(String creatureType); /** diff --git a/src/main/java/org/bukkit/entity/CreatureType.java b/src/main/java/org/bukkit/entity/CreatureType.java index b6a27d86..e1b32470 100644 --- a/src/main/java/org/bukkit/entity/CreatureType.java +++ b/src/main/java/org/bukkit/entity/CreatureType.java @@ -4,6 +4,11 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.Map; +/** + * Represents a type of creature. + * @deprecated Use EntityType instead. + */ +@Deprecated public enum CreatureType { // These strings MUST match the strings in nms.EntityTypes and are case sensitive. CREEPER("Creeper", Creeper.class, 50), @@ -74,4 +79,13 @@ public enum CreatureType { } return ID_MAP.get((short) id); } + + @Deprecated + public EntityType toEntityType() { + return EntityType.fromName(getName()); + } + + public static CreatureType fromEntityType(EntityType creatureType) { + return fromName(creatureType.getName()); + } } \ No newline at end of file diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java index 7d7c5f5c..c8554734 100644 --- a/src/main/java/org/bukkit/entity/Entity.java +++ b/src/main/java/org/bukkit/entity/Entity.java @@ -226,4 +226,10 @@ public interface Entity { * @param type Effect to play. */ public void playEffect(EntityEffect type); + + /** + * Get the type of the entity. + * @return The entity type. + */ + public EntityType getType(); } diff --git a/src/main/java/org/bukkit/entity/EntityType.java b/src/main/java/org/bukkit/entity/EntityType.java new file mode 100644 index 00000000..db9f2869 --- /dev/null +++ b/src/main/java/org/bukkit/entity/EntityType.java @@ -0,0 +1,129 @@ +package org.bukkit.entity; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.World; + +public enum EntityType { + // These strings MUST match the strings in nms.EntityTypes and are case sensitive. + DROPPED_ITEM("Item", Item.class, 1, false), + EXPERIENCE_ORB("XPOrb", ExperienceOrb.class, 2), + PAINTING("Painting", Painting.class, 9), + ARROW("Arrow", Arrow.class, 10), + SNOWBALL("Snowball", Snowball.class, 11), + FIREBALL("Fireball", Fireball.class, 12), + SMALL_FIREBALL("SmallFireball", SmallFireball.class, 13), + ENDER_PEARL("ThrownEnderpearl", EnderPearl.class, 14), + ENDER_SIGNAL("EyeOfEnderSignal", EnderSignal.class, 15), + PRIMED_TNT("PrimedTnt", TNTPrimed.class, 20), + FALLING_BLOCK("FallingSand", FallingSand.class, 21, false), + MINECART("Minecart", Minecart.class, 40), + BOAT("Boat", Boat.class, 41), + CREEPER("Creeper", Creeper.class, 50), + SKELETON("Skeleton", Skeleton.class, 51), + SPIDER("Spider", Spider.class, 52), + GIANT("Giant", Giant.class, 53), + ZOMBIE("Zombie", Zombie.class, 54), + SLIME("Slime", Slime.class, 55), + GHAST("Ghast", Ghast.class, 56), + PIG_ZOMBIE("PigZombie", PigZombie.class, 57), + ENDERMAN("Enderman", Enderman.class, 58), + CAVE_SPIDER("CaveSpider", CaveSpider.class, 59), + SILVERFISH("Silverfish", Silverfish.class, 60), + BLAZE("Blaze", Blaze.class, 61), + MAGMA_CUBE("LavaSlime", MagmaCube.class, 62), + ENDER_DRAGON("EnderDragon", EnderDragon.class, 63), + PIG("Pig", Pig.class, 90), + SHEEP("Sheep", Sheep.class, 91), + COW("Cow", Cow.class, 92), + CHICKEN("Chicken", Chicken.class, 93), + SQUID("Squid", Squid.class, 94), + WOLF("Wolf", Wolf.class, 95), + MUSHROOM_COW("MushroomCow", MushroomCow.class, 96), + SNOWMAN("SnowMan", Snowman.class, 97), + VILLAGER("Villager", Villager.class, 120), + ENDER_CRYSTAL("EnderCrystal", EnderCrystal.class, 200), + // These don't have an entity ID in nms.EntityTypes. + SPLASH_POTION(null, ThrownPotion.class, -1, false), + EGG(null, Egg.class, -1, false), + FISHING_HOOK(null, Fish.class, -1, false), + /** + * Spawn with {@link World#strikeLightning(org.bukkit.Location)}. + */ + LIGHTNING(null, LightningStrike.class, -1, false), + WEATHER(null, Weather.class, -1, false), + PLAYER(null, Player.class, -1, false), + COMPLEX_PART(null, ComplexEntityPart.class, -1, false), + /** + * An unknown entity without an Entity Class + */ + UNKNOWN(null, null, -1, false); + + private String name; + private Class clazz; + private short typeId; + private boolean independent, living; + + private static final Map NAME_MAP = new HashMap(); + private static final Map ID_MAP = new HashMap(); + + static { + for (EntityType type : EnumSet.allOf(EntityType.class)) { + NAME_MAP.put(type.name, type); + NAME_MAP.put(type.name(), type); + if (type.typeId != 0) { + ID_MAP.put(type.typeId, type); + } + } + } + + private EntityType(String name, Class clazz, int typeId) { + this(name, clazz, typeId, true); + } + + private EntityType(String name, Class clazz, int typeId, boolean independent) { + this.name = name; + this.clazz = clazz; + this.typeId = (short) typeId; + this.independent = independent; + this.living = LivingEntity.class.isAssignableFrom(clazz); + } + + public String getName() { + return name; + } + + public Class getEntityClass() { + return clazz; + } + + public short getTypeId() { + return typeId; + } + + public static EntityType fromName(String name) { + return NAME_MAP.get(name); + } + + public static EntityType fromId(int id) { + if (id > Short.MAX_VALUE) { + return null; + } + return ID_MAP.get((short) id); + } + + /** + * Some entities cannot be spawned using {@link World#spawn(org.bukkit.Location, EntityType)}, usually + * because they require additional information in order to spawn. + * @return False if the entity type cannot be spawned + */ + public boolean isSpawnable() { + return independent; + } + + public boolean isAlive() { + return living; + } +} \ No newline at end of file diff --git a/src/main/java/org/bukkit/event/entity/CreatureSpawnEvent.java b/src/main/java/org/bukkit/event/entity/CreatureSpawnEvent.java index 8fd3211e..fec86e09 100644 --- a/src/main/java/org/bukkit/event/entity/CreatureSpawnEvent.java +++ b/src/main/java/org/bukkit/event/entity/CreatureSpawnEvent.java @@ -1,8 +1,10 @@ package org.bukkit.event.entity; import org.bukkit.entity.CreatureType; -import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -13,18 +15,20 @@ import org.bukkit.event.HandlerList; */ public class CreatureSpawnEvent extends EntityEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final Location location; private boolean canceled; - private final CreatureType creatureType; private final SpawnReason spawnReason; - public CreatureSpawnEvent(final Entity spawnee, final CreatureType mobtype, final Location loc, final SpawnReason spawnReason) { + public CreatureSpawnEvent(final LivingEntity spawnee, final SpawnReason spawnReason) { super(spawnee); - this.creatureType = mobtype; - this.location = loc; this.spawnReason = spawnReason; } + @Deprecated + public CreatureSpawnEvent(Entity spawnee, CreatureType type, Location loc, SpawnReason reason) { + super(spawnee); + spawnReason = reason; + } + public boolean isCancelled() { return canceled; } @@ -39,16 +43,27 @@ public class CreatureSpawnEvent extends EntityEvent implements Cancellable { * @return The location at which the creature is spawning */ public Location getLocation() { - return location; + return getEntity().getLocation(); } /** * Gets the type of creature being spawned. * * @return A CreatureType value detailing the type of creature being spawned + * @deprecated In favour of {@link #getSpawnedType()}. */ + @Deprecated public CreatureType getCreatureType() { - return creatureType; + return CreatureType.fromEntityType(getSpawnedType()); + } + + /** + * Gets the type of creature being spawned. + * + * @return A CreatureType value detailing the type of creature being spawned + */ + public EntityType getSpawnedType() { + return getEntity().getType(); } /** diff --git a/src/main/java/org/bukkit/event/player/PlayerEggThrowEvent.java b/src/main/java/org/bukkit/event/player/PlayerEggThrowEvent.java index fc0a77fb..010075c4 100644 --- a/src/main/java/org/bukkit/event/player/PlayerEggThrowEvent.java +++ b/src/main/java/org/bukkit/event/player/PlayerEggThrowEvent.java @@ -2,6 +2,7 @@ package org.bukkit.event.player; import org.bukkit.entity.CreatureType; import org.bukkit.entity.Egg; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; @@ -12,15 +13,20 @@ public class PlayerEggThrowEvent extends PlayerEvent { private static final HandlerList handlers = new HandlerList(); private final Egg egg; private boolean hatching; - private CreatureType hatchType; + private EntityType hatchType; private byte numHatches; - public PlayerEggThrowEvent(final Player player, final Egg egg, final boolean hatching, final byte numHatches, final CreatureType hatchType) { + public PlayerEggThrowEvent(final Player player, final Egg egg, final boolean hatching, final byte numHatches, final EntityType hatchingType) { super(player); this.egg = egg; this.hatching = hatching; this.numHatches = numHatches; - this.hatchType = hatchType; + this.hatchType = hatchingType; + } + + @Deprecated + public PlayerEggThrowEvent(Player player, Egg egg, boolean hatching, byte numHatches, CreatureType hatchingType) { + this(player, egg, hatching, numHatches, hatchingType.toEntityType()); } /** @@ -53,20 +59,43 @@ public class PlayerEggThrowEvent extends PlayerEvent { } /** - * Get the type of the mob being hatched (CreatureType.CHICKEN by default) + * Get the type of the mob being hatched (EntityType.CHICKEN by default) * * @return The type of the mob being hatched by the egg + * @deprecated In favour of {@link #getHatchingType()}. */ + @Deprecated public CreatureType getHatchType() { - return CreatureType.fromName(hatchType.getName()); + return CreatureType.fromEntityType(hatchType); + } + + /** + * Get the type of the mob being hatched (EntityType.CHICKEN by default) + * + * @return The type of the mob being hatched by the egg + */ + public EntityType getHatchingType() { + return hatchType; } /** * Change the type of mob being hatched by the egg * * @param hatchType The type of the mob being hatched by the egg + * @deprecated In favour of {@link #setHatchingType(EntityType)}. */ + @Deprecated public void setHatchType(CreatureType hatchType) { + this.hatchType = hatchType.toEntityType(); + } + + /** + * Change the type of mob being hatched by the egg + * + * @param hatchType The type of the mob being hatched by the egg + */ + public void setHatchingType(EntityType hatchType) { + if(!hatchType.isSpawnable()) throw new IllegalArgumentException("Can't spawn that entity type from an egg!"); this.hatchType = hatchType; } -- cgit v1.2.3