From 837fa8ac26a6ef069e3e4b70db43d39af899218c Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Thu, 30 Dec 2010 04:30:12 +0000 Subject: [PATCH] Implemented entities By: Dinnerbone --- .../org/bukkit/craftbukkit/CraftEntity.java | 68 +++++++++++++++++++ .../bukkit/craftbukkit/CraftHumanEntity.java | 36 ++++++++++ .../bukkit/craftbukkit/CraftLivingEntity.java | 36 ++++++++++ .../org/bukkit/craftbukkit/CraftPlayer.java | 30 +++----- .../org/bukkit/craftbukkit/CraftServer.java | 4 ++ 5 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 paper-server/src/org/bukkit/craftbukkit/CraftEntity.java create mode 100644 paper-server/src/org/bukkit/craftbukkit/CraftHumanEntity.java create mode 100644 paper-server/src/org/bukkit/craftbukkit/CraftLivingEntity.java diff --git a/paper-server/src/org/bukkit/craftbukkit/CraftEntity.java b/paper-server/src/org/bukkit/craftbukkit/CraftEntity.java new file mode 100644 index 0000000000..71aa013c1b --- /dev/null +++ b/paper-server/src/org/bukkit/craftbukkit/CraftEntity.java @@ -0,0 +1,68 @@ + +package org.bukkit.craftbukkit; + +import net.minecraft.server.Entity; +import net.minecraft.server.WorldServer; +import org.bukkit.Location; +import org.bukkit.World; + +public class CraftEntity implements org.bukkit.Entity { + protected final CraftServer server; + private final Entity entity; + + public CraftEntity(final CraftServer server, final Entity entity) { + this.server = server; + this.entity = entity; + } + + public Location getLocation() { + return new Location(getWorld(), entity.p, entity.q, entity.r, entity.v, entity.w); + } + + public World getWorld() { + return server.getWorld((WorldServer)entity.l); + } + + public void teleportTo(Location location) { + entity.b(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + } + + public int getEntityID() { + return entity.g; + } + + public Entity getHandle() { + return entity; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final CraftEntity other = (CraftEntity) obj; + if (this.server != other.server && (this.server == null || !this.server.equals(other.server))) { + return false; + } + if (this.entity != other.entity && (this.entity == null || !this.entity.equals(other.entity))) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 89 * hash + (this.server != null ? this.server.hashCode() : 0); + hash = 89 * hash + (this.entity != null ? this.entity.hashCode() : 0); + return hash; + } + + @Override + public String toString() { + return "CraftEntity{" + "id=" + getEntityID() + '}'; + } +} diff --git a/paper-server/src/org/bukkit/craftbukkit/CraftHumanEntity.java b/paper-server/src/org/bukkit/craftbukkit/CraftHumanEntity.java new file mode 100644 index 0000000000..1152bc62f4 --- /dev/null +++ b/paper-server/src/org/bukkit/craftbukkit/CraftHumanEntity.java @@ -0,0 +1,36 @@ + +package org.bukkit.craftbukkit; + +import net.minecraft.server.EntityPlayer; +import net.minecraft.server.InventoryPlayer; +import org.bukkit.HumanEntity; +import org.bukkit.ItemStack; + +public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { + private final EntityPlayer entity; + + public CraftHumanEntity(final CraftServer server, final EntityPlayer entity) { + super(server, entity); + this.entity = entity; + } + + public ItemStack getSelectedItem() { + // TODO: Implement inventories + final InventoryPlayer inventory = entity.an; + return new ItemStack(inventory.e().c, inventory.e().a); + } + + public String getName() { + return entity.aw; + } + + @Override + public EntityPlayer getHandle() { + return entity; + } + + @Override + public String toString() { + return "CraftHumanEntity{" + "id=" + getEntityID() + "name=" + getName() + '}'; + } +} diff --git a/paper-server/src/org/bukkit/craftbukkit/CraftLivingEntity.java b/paper-server/src/org/bukkit/craftbukkit/CraftLivingEntity.java new file mode 100644 index 0000000000..2c70d10a08 --- /dev/null +++ b/paper-server/src/org/bukkit/craftbukkit/CraftLivingEntity.java @@ -0,0 +1,36 @@ + +package org.bukkit.craftbukkit; + +import net.minecraft.server.EntityLiving; +import org.bukkit.LivingEntity; + +public class CraftLivingEntity extends CraftEntity implements LivingEntity { + private final EntityLiving entity; + + public CraftLivingEntity(final CraftServer server, final EntityLiving entity) { + super(server, entity); + this.entity = entity; + } + + public int getHealth() { + return entity.ba; + } + + public void setHealth(int health) { + if ((health < 0) || (health > 20)) { + throw new IllegalArgumentException("Health must be between 0 and 20"); + } + + entity.ba = health; + } + + @Override + public EntityLiving getHandle() { + return entity; + } + + @Override + public String toString() { + return "CraftLivingEntity{" + "id=" + getEntityID() + '}'; + } +} diff --git a/paper-server/src/org/bukkit/craftbukkit/CraftPlayer.java b/paper-server/src/org/bukkit/craftbukkit/CraftPlayer.java index f42bd63728..d69fd3b272 100644 --- a/paper-server/src/org/bukkit/craftbukkit/CraftPlayer.java +++ b/paper-server/src/org/bukkit/craftbukkit/CraftPlayer.java @@ -2,19 +2,16 @@ package org.bukkit.craftbukkit; import net.minecraft.server.EntityPlayerMP; -import org.bukkit.Location; import org.bukkit.Player; -import org.bukkit.World; -public class CraftPlayer implements Player { - private EntityPlayerMP player; +public class CraftPlayer extends CraftHumanEntity implements Player { + private final EntityPlayerMP entity; private final String name; - private final CraftServer server; - public CraftPlayer(CraftServer serv, EntityPlayerMP handle) { - player = handle; - name = player.aw; - server = serv; + public CraftPlayer(CraftServer server, EntityPlayerMP entity) { + super(server, entity); + this.entity = entity; + name = entity.aw; } public String getName() { @@ -22,19 +19,12 @@ public class CraftPlayer implements Player { } public boolean isOnline() { - return server.server.g(name); + return server.getHandle().g(name); } - public Location getLocation() { - return new Location(getWorld(), player.p, player.q, player.r, player.v, player.w); - } - - public World getWorld() { - return server.getWorld(player.b.e); - } - - public EntityPlayerMP getEntity() { - return player; + @Override + public EntityPlayerMP getHandle() { + return entity; } @Override diff --git a/paper-server/src/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/org/bukkit/craftbukkit/CraftServer.java index db3f119bd5..34aca81e79 100644 --- a/paper-server/src/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/org/bukkit/craftbukkit/CraftServer.java @@ -100,4 +100,8 @@ public final class CraftServer implements Server { return result; } + + public ServerConfigurationManager getHandle() { + return server; + } }