summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/bukkit')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftBlock.java152
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftChunk.java37
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftEntity.java72
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftHumanEntity.java41
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftLivingEntity.java42
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftPlayer.java67
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftServer.java80
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftWorld.java155
-rw-r--r--src/main/java/org/bukkit/craftbukkit/Main.java16
9 files changed, 662 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
new file mode 100644
index 00000000..f67d14bf
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
@@ -0,0 +1,152 @@
+
+package org.bukkit.craftbukkit;
+
+import org.bukkit.*;
+
+public class CraftBlock implements Block {
+ private final CraftWorld world;
+ private final CraftChunk chunk;
+ private final int x;
+ private final int y;
+ private final int z;
+ protected int type;
+ protected byte data;
+
+ protected CraftBlock(final CraftWorld world, final int x, final int y, final int z, final int type, final byte data) {
+ this.world = world;
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.type = type;
+ this.data = data;
+ this.chunk = (CraftChunk)world.getChunkAt(x << 4, z << 4);
+ }
+
+ /**
+ * Gets the world which contains this Block
+ *
+ * @return World containing this block
+ */
+ public World getWorld() {
+ return world;
+ }
+
+ /**
+ * Gets the x-coordinate of this block
+ *
+ * @return x-coordinate
+ */
+ public int getX() {
+ return x;
+ }
+
+ /**
+ * Gets the y-coordinate of this block
+ *
+ * @return y-coordinate
+ */
+ public int getY() {
+ return y;
+ }
+
+ /**
+ * Gets the z-coordinate of this block
+ *
+ * @return z-coordinate
+ */
+ public int getZ() {
+ return z;
+ }
+
+ /**
+ * Gets the chunk which contains this block
+ *
+ * @return Containing Chunk
+ */
+ public Chunk getChunk() {
+ return chunk;
+ }
+
+ /**
+ * Sets the metadata for this block
+ *
+ * @param data New block specific metadata
+ */
+ public void setData(final byte data) {
+ this.data = data;
+ world.getHandle().c(x, y, z, data);
+ }
+
+ /**
+ * Gets the metadata for this block
+ *
+ * @return block specific metadata
+ */
+ public byte getData() {
+ return data;
+ }
+
+ /**
+ * Sets the type of this block
+ *
+ * @param type Material to change this block to
+ */
+ public void setType(final Material type) {
+ setTypeID(type.getID());
+ }
+
+ /**
+ * Sets the type-ID of this block
+ *
+ * @param type Type-ID to change this block to
+ */
+ public void setTypeID(final int type) {
+ this.type = type;
+ world.getHandle().d(x, y, z, type);
+ }
+
+ /**
+ * Gets the type of this block
+ *
+ * @return block type
+ */
+ public Material getType() {
+ return Material.getMaterial(getTypeID());
+ }
+
+ /**
+ * Gets the type-ID of this block
+ *
+ * @return block type-ID
+ */
+ public int getTypeID() {
+ return type;
+ }
+
+ /**
+ * Gets the block at the given face
+ *
+ * @param face Face of this block to return
+ * @return Block at the given face
+ */
+ public Block getFace(final BlockFace face) {
+ return getRelative(face.getModX(), face.getModY(), face.getModZ());
+ }
+
+ /**
+ * Gets the block at the given offsets
+ *
+ * @param modX X-coordinate offset
+ * @param modY Y-coordinate offset
+ * @param modZ Z-coordinate offset
+ * @return Block at the given offsets
+ */
+ public Block getRelative(final int modX, final int modY, final int modZ) {
+ return getWorld().getBlockAt(getX() + modX, getY() + modY, getZ() + modZ);
+ }
+
+ @Override
+ public String toString() {
+ return "CraftBlock{" + "world=" + world + "x=" + x + "y=" + y + "z=" + z + "type=" + type + "data=" + data + '}';
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java
new file mode 100644
index 00000000..003ee0f4
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java
@@ -0,0 +1,37 @@
+
+package org.bukkit.craftbukkit;
+
+import org.bukkit.Chunk;
+
+public class CraftChunk implements Chunk {
+ private final int x;
+ private final int z;
+
+ protected CraftChunk(final int x, final int z) {
+ this.x = x;
+ this.z = z;
+ }
+
+ /**
+ * Gets the X-coordinate of this chunk
+ *
+ * @return X-coordinate
+ */
+ public int getX() {
+ return x;
+ }
+
+ /**
+ * Gets the Z-coordinate of this chunk
+ *
+ * @return Z-coordinate
+ */
+ public int getZ() {
+ return z;
+ }
+
+ @Override
+ public String toString() {
+ return "CraftChunk{" + "x=" + x + "z=" + z + '}';
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/CraftEntity.java
new file mode 100644
index 00000000..efbb7f28
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftEntity.java
@@ -0,0 +1,72 @@
+
+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 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 ((WorldServer)entity.l).getWorld();
+ }
+
+ 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;
+ }
+
+ public void setHandle(final Entity entity) {
+ this.entity = 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/src/main/java/org/bukkit/craftbukkit/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/CraftHumanEntity.java
new file mode 100644
index 00000000..b03f0967
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftHumanEntity.java
@@ -0,0 +1,41 @@
+
+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 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;
+ }
+
+ public void setHandle(final EntityPlayer entity) {
+ super.setHandle((EntityPlayer)entity);
+ this.entity = entity;
+ }
+
+ @Override
+ public String toString() {
+ return "CraftHumanEntity{" + "id=" + getEntityID() + "name=" + getName() + '}';
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/CraftLivingEntity.java
new file mode 100644
index 00000000..8f753386
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftLivingEntity.java
@@ -0,0 +1,42 @@
+
+package org.bukkit.craftbukkit;
+
+import net.minecraft.server.Entity;
+import net.minecraft.server.EntityLiving;
+import org.bukkit.LivingEntity;
+
+public class CraftLivingEntity extends CraftEntity implements LivingEntity {
+ private 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;
+ }
+
+ public void setHandle(final EntityLiving entity) {
+ super.setHandle((Entity)entity);
+ this.entity = entity;
+ }
+
+ @Override
+ public String toString() {
+ return "CraftLivingEntity{" + "id=" + getEntityID() + '}';
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftPlayer.java
new file mode 100644
index 00000000..a8a368dc
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftPlayer.java
@@ -0,0 +1,67 @@
+
+package org.bukkit.craftbukkit;
+
+import net.minecraft.server.EntityPlayer;
+import net.minecraft.server.EntityPlayerMP;
+import net.minecraft.server.Packet3Chat;
+import org.bukkit.Location;
+import org.bukkit.Player;
+
+public class CraftPlayer extends CraftHumanEntity implements Player {
+ private EntityPlayerMP entity;
+
+ public CraftPlayer(CraftServer server, EntityPlayerMP entity) {
+ super(server, entity);
+ this.entity = entity;
+ }
+
+ public boolean isOnline() {
+ return server.getHandle().g(getName());
+ }
+
+ @Override
+ public EntityPlayerMP getHandle() {
+ return entity;
+ }
+
+ public void setHandle(final EntityPlayerMP entity) {
+ super.setHandle((EntityPlayer)entity);
+ this.entity = entity;
+ }
+
+ public void sendMessage(String message) {
+ entity.a.b(new Packet3Chat(message));
+ }
+
+ @Override
+ public void teleportTo(Location location) {
+ entity.a.a(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
+ }
+
+ @Override
+ public String toString() {
+ return "CraftPlayer{" + "name=" + getName() + '}';
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final CraftPlayer other = (CraftPlayer) obj;
+ if ((this.getName() == null) ? (other.getName() != null) : !this.getName().equals(other.getName())) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 5;
+ hash = 97 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
+ return hash;
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
new file mode 100644
index 00000000..1fe1ee44
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -0,0 +1,80 @@
+
+package org.bukkit.craftbukkit;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import net.minecraft.server.EntityPlayerMP;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.ServerConfigurationManager;
+import net.minecraft.server.WorldServer;
+import org.bukkit.*;
+import org.bukkit.plugin.PluginManager;
+import org.bukkit.plugin.SimplePluginManager;
+import org.bukkit.plugin.java.JavaPluginLoader;
+
+public final class CraftServer implements Server {
+ private final String serverName = "Craftbukkit";
+ private final String serverVersion;
+ private final PluginManager pluginManager = new SimplePluginManager(this);
+
+ protected final MinecraftServer console;
+ protected final ServerConfigurationManager server;
+
+ public CraftServer(MinecraftServer instance, String ver) {
+ serverVersion = ver;
+
+ console = instance;
+ server = console.f;
+
+ pluginManager.RegisterInterface(JavaPluginLoader.class);
+
+ File pluginFolder = new File("plugins");
+ if (pluginFolder.exists()) {
+ try {
+ pluginManager.loadPlugins(pluginFolder);
+ } catch (Throwable ex) {
+ Logger.getLogger(CraftServer.class.getName()).log(Level.SEVERE, "(Did you extract the lib folder?)", ex);
+ }
+ } else {
+ pluginFolder.mkdir();
+ }
+ }
+
+ public String getName() {
+ return serverName;
+ }
+
+ public String getVersion() {
+ return serverVersion;
+ }
+
+ public Player[] getOnlinePlayers() {
+ List<EntityPlayerMP> online = server.b;
+ Player[] players = new Player[online.size()];
+
+ for (int i = 0; i < players.length; i++) {
+ players[i] = online.get(i).a.getPlayer();
+ }
+
+ return players;
+ }
+
+ public Player getPlayer(final EntityPlayerMP entity) {
+ return entity.a.getPlayer();
+ }
+
+ public PluginManager getPluginManager() {
+ return pluginManager;
+ }
+
+ public World[] getWorlds() {
+ return new World[] { console.e.getWorld() };
+ }
+
+ public ServerConfigurationManager getHandle() {
+ return server;
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
new file mode 100644
index 00000000..5e6d20d7
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -0,0 +1,155 @@
+
+package org.bukkit.craftbukkit;
+
+import java.util.HashMap;
+import java.util.Map;
+import net.minecraft.server.WorldServer;
+import org.bukkit.Block;
+import org.bukkit.Chunk;
+import org.bukkit.World;
+
+public class CraftWorld implements World {
+ private final Map<ChunkCoordinate, Chunk> chunkCache = new HashMap<ChunkCoordinate, Chunk>();
+ private final Map<BlockCoordinate, Block> blockCache = new HashMap<BlockCoordinate, Block>();
+ private final WorldServer world;
+
+ public CraftWorld(WorldServer world) {
+ this.world = world;
+ }
+
+ public Block getBlockAt(int x, int y, int z) {
+ BlockCoordinate loc = new BlockCoordinate(x, y, z);
+ Block block = blockCache.get(loc);
+
+ if (block == null) {
+ block = new CraftBlock(this, x, y, z, world.a(x, y, z), (byte)world.b(x, y, z));
+ blockCache.put(loc, block);
+ }
+
+ return block;
+ }
+
+ public Chunk getChunkAt(int x, int z) {
+ ChunkCoordinate loc = new ChunkCoordinate(x, z);
+ Chunk chunk = chunkCache.get(loc);
+
+ if (chunk == null) {
+ chunk = new CraftChunk(x, z);
+ chunkCache.put(loc, chunk);
+ }
+
+ return chunk;
+ }
+
+ public Chunk getChunkAt(Block block) {
+ return getChunkAt(block.getX() << 4, block.getZ() << 4);
+ }
+
+ public boolean isChunkLoaded() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public Block updateBlock(int x, int y, int z) {
+ BlockCoordinate loc = new BlockCoordinate(x, y, z);
+ CraftBlock block = (CraftBlock)blockCache.get(loc);
+ final int type = world.a(x, y, z);
+ final byte data = (byte)world.b(x, y, z);
+
+ if (block == null) {
+ block = new CraftBlock(this, x, y, z, type, data);
+ blockCache.put(loc, block);
+ } else {
+ block.type = type;
+ block.data = data;
+ }
+
+ return block;
+ }
+
+ public WorldServer getHandle() {
+ return world;
+ }
+
+ @Override
+ public String toString() {
+ return "CraftWorld";
+ }
+
+ private final class ChunkCoordinate {
+ public final int x;
+ public final int z;
+
+ public ChunkCoordinate(final int x, final int z) {
+ this.x = x;
+ this.z = z;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final ChunkCoordinate other = (ChunkCoordinate) obj;
+ if (this.x != other.x) {
+ return false;
+ }
+ if (this.z != other.z) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 5;
+ hash = 53 * hash + this.x;
+ hash = 53 * hash + this.z;
+ return hash;
+ }
+ }
+
+ private final class BlockCoordinate {
+ public final int x;
+ public final int y;
+ public final int z;
+
+ public BlockCoordinate(final int x, final int y, final int z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final BlockCoordinate other = (BlockCoordinate) obj;
+ if (this.x != other.x) {
+ return false;
+ }
+ if (this.y != other.y) {
+ return false;
+ }
+ if (this.z != other.z) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 7;
+ hash = 37 * hash + this.x;
+ hash = 37 * hash + this.y;
+ hash = 37 * hash + this.z;
+ return hash;
+ }
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
new file mode 100644
index 00000000..d2543b5e
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
@@ -0,0 +1,16 @@
+
+package org.bukkit.craftbukkit;
+
+import net.minecraft.server.MinecraftServer;
+
+public class Main {
+ public static void main(String[] args) {
+ // Todo: Installation script
+
+ try {
+ MinecraftServer.main(args);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+}