Implement Horse API. Adds BUKKIT-4424

API has been added to interface with Horses and to modify their inventories. Horse entities will now be recognized with the type EntityType.HORSE, and will no longer be UNKNOWN.

HorseJumpEvent, EntityDamageEvent, and EntityTameEvent are all correctly fired for horses.

This commit fixes BUKKIT-4393.

By: h31ix <zero_gravity@outlook.com>
This commit is contained in:
CraftBukkit/Spigot 2013-07-10 11:58:18 -04:00
parent 69a37fc533
commit 8524394d06
5 changed files with 159 additions and 33 deletions

View file

@ -906,6 +906,8 @@ public class CraftWorld implements World {
// need a net server handler for this one
} else if (Sheep.class.isAssignableFrom(clazz)) {
entity = new EntitySheep(world);
} else if (Horse.class.isAssignableFrom(clazz)) {
entity = new EntityHorse(world);
} else if (Skeleton.class.isAssignableFrom(clazz)) {
entity = new EntitySkeleton(world);
} else if (Slime.class.isAssignableFrom(clazz)) {

View file

@ -51,6 +51,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
else if (entity instanceof EntityOcelot) { return new CraftOcelot(server, (EntityOcelot) entity); }
}
else if (entity instanceof EntitySheep) { return new CraftSheep(server, (EntitySheep) entity); }
else if (entity instanceof EntityHorse) { return new CraftHorse(server, (EntityHorse) entity); }
else { return new CraftAnimals(server, (EntityAnimal) entity); }
}
// Monsters

View file

@ -1,23 +1,136 @@
package org.bukkit.craftbukkit.entity;
import net.minecraft.server.EntityAnimal;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
public class CraftHorse extends CraftAnimals implements Horse {
public CraftHorse(CraftServer server, EntityAnimal entity) {
super(server, entity);
}
@Override
public String toString() {
return "CraftHorse";
}
public EntityType getType() {
return EntityType.HORSE;
}
}
package org.bukkit.craftbukkit.entity;
import net.minecraft.server.EntityHorse;
import org.apache.commons.lang.Validate;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.inventory.CraftInventoryHorse;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.inventory.HorseInventory;
public class CraftHorse extends CraftAnimals implements Horse {
public CraftHorse(CraftServer server, EntityHorse entity) {
super(server, entity);
}
@Override
public EntityHorse getHandle() {
return (EntityHorse) entity;
}
public Variant getVariant() {
return Variant.values()[getHandle().getType()];
}
public void setVariant(Variant variant) {
Validate.notNull(variant, "Variant cannot be null");
getHandle().setType(variant.ordinal());
}
public Color getColor() {
return Color.values()[getHandle().getVariant() & 0xFF];
}
public void setColor(Color color) {
Validate.notNull(color, "Color cannot be null");
getHandle().setVariant(color.ordinal() & 0xFF | getStyle().ordinal() << 8);
}
public Style getStyle() {
return Style.values()[getHandle().getVariant() >>> 8];
}
public void setStyle(Style style) {
Validate.notNull(style, "Style cannot be null");
getHandle().setVariant(getColor().ordinal() & 0xFF | style.ordinal() << 8);
}
public boolean isCarryingChest() {
return getHandle().hasChest();
}
public void setCarryingChest(boolean chest) {
if (chest == isCarryingChest()) return;
getHandle().setHasChest(chest);
getHandle().cH(); // Should be loadChest
}
public int getDomestication() {
return getHandle().getTemper();
}
public void setDomestication(int value) {
Validate.isTrue(value >= 0, "Domestication cannot be less than zero");
Validate.isTrue(value <= getMaxDomestication(), "Domestication cannot be greater than the max domestication");
getHandle().setTemper(value);
}
public int getMaxDomestication() {
return getHandle().cq(); // Should be getMaxDomestication
}
public void setMaxDomestication(int value) {
Validate.isTrue(value > 0, "Max domestication cannot be zero or less");
getHandle().maxDomestication = value;
}
public double getJumpStrength() {
return getHandle().getJumpStrength();
}
public void setJumpStrength(double strength) {
Validate.isTrue(strength >= 0, "Jump strength cannot be less than zero");
getHandle().getAttributeInstance(EntityHorse.attributeJumpStrength).setValue(strength);
}
@Override
public boolean isTamed() {
return getHandle().isTame();
}
@Override
public void setTamed(boolean tamed) {
getHandle().setTame(tamed);
}
@Override
public AnimalTamer getOwner() {
if (getOwnerName() == null || "".equals(getOwnerName())) return null;
return getServer().getOfflinePlayer(getOwnerName());
}
@Override
public void setOwner(AnimalTamer owner) {
if (owner != null && !"".equals(owner.getName())) {
setTamed(true);
getHandle().setPathEntity(null);
setOwnerName(owner.getName());
} else {
setTamed(false);
setOwnerName("");
}
}
public String getOwnerName() {
return getHandle().getOwnerName();
}
public void setOwnerName(String name) {
getHandle().setOwnerName(name);
}
public HorseInventory getInventory() {
return new CraftInventoryHorse(getHandle().inventoryChest);
}
@Override
public String toString() {
return "CraftHorse{variant=" + getVariant() + ", owner=" + getOwner() + '}';
}
public EntityType getType() {
return EntityType.HORSE;
}
}

View file

@ -48,6 +48,7 @@ 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;
import org.bukkit.entity.Pig;
@ -492,6 +493,12 @@ public class CraftEventFactory {
return event;
}
public static HorseJumpEvent callHorseJumpEvent(Entity horse, float power) {
HorseJumpEvent event = new HorseJumpEvent((Horse) horse.getBukkitEntity(), power);
horse.getBukkitEntity().getServer().getPluginManager().callEvent(event);
return event;
}
public static EntityChangeBlockEvent callEntityChangeBlockEvent(org.bukkit.entity.Entity entity, Block block, Material material) {
return callEntityChangeBlockEvent(entity, block, material, 0);
}

View file

@ -1,25 +1,28 @@
package org.bukkit.craftbukkit.inventory;
import net.minecraft.server.IInventory;
import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.ItemStack;
public class CraftInventoryHorse extends CraftInventory {
private final IInventory resultInventory = null;
public class CraftInventoryHorse extends CraftInventory implements HorseInventory {
public CraftInventoryHorse(IInventory inventory) {
super(inventory);
}
public IInventory getResultInventory() {
return resultInventory;
public ItemStack getSaddle() {
return getItem(0);
}
public IInventory getIngredientsInventory() {
return inventory;
public ItemStack getArmor() {
return getItem(1);
}
@Override
public int getSize() {
//return getResultInventory().getSize() + getIngredientsInventory().getSize();
return getIngredientsInventory().getSize();
public void setSaddle(ItemStack stack) {
setItem(0, stack);
}
public void setArmor(ItemStack stack) {
setItem(1, stack);
}
}