Paper/src/main/java/org/bukkit/craftbukkit/CraftWorld.java

311 lines
9.6 KiB
Java
Raw Normal View History

2010-12-27 03:13:03 +01:00
package org.bukkit.craftbukkit;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.server.EntityMinecart;
import java.util.Random;
import net.minecraft.server.EntityBoat;
import net.minecraft.server.EntityEgg;
2011-01-08 21:48:45 +01:00
import net.minecraft.server.EntityItem;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayerMP;
import net.minecraft.server.EntitySnowball;
import net.minecraft.server.EntityArrow;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.TileEntity;
import net.minecraft.server.WorldGenBigTree;
2010-12-27 03:13:03 +01:00
import net.minecraft.server.WorldServer;
2011-01-03 16:13:29 +01:00
import net.minecraft.server.WorldGenTrees;
import org.bukkit.Arrow;
2010-12-27 03:13:03 +01:00
import org.bukkit.Block;
import org.bukkit.Boat;
2010-12-27 03:13:03 +01:00
import org.bukkit.Chunk;
2011-01-08 21:48:45 +01:00
import org.bukkit.ItemDrop;
import org.bukkit.ItemStack;
2011-01-02 09:40:27 +01:00
import org.bukkit.Location;
import org.bukkit.Minecart;
import org.bukkit.PoweredMinecart;
import org.bukkit.StorageMinecart;
2011-01-02 09:40:27 +01:00
import org.bukkit.Vector;
2010-12-27 03:13:03 +01:00
import org.bukkit.World;
public class CraftWorld implements World {
2011-01-04 16:54:41 +01:00
private final Map<ChunkCoordinate, CraftChunk> chunkCache = new HashMap<ChunkCoordinate, CraftChunk>();
private final Map<BlockCoordinate, CraftBlock> blockCache = new HashMap<BlockCoordinate, CraftBlock>();
2010-12-27 03:13:03 +01:00
private final WorldServer world;
private static final Random rand = new Random();
2010-12-27 03:13:03 +01:00
2010-12-30 05:37:32 +01:00
public CraftWorld(WorldServer world) {
2010-12-27 03:13:03 +01:00
this.world = world;
}
2010-12-27 03:13:03 +01:00
public Block getBlockAt(int x, int y, int z) {
BlockCoordinate loc = new BlockCoordinate(x, y, z);
2011-01-04 16:54:41 +01:00
CraftBlock block = blockCache.get(loc);
2010-12-27 03:13:03 +01:00
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;
}
2011-01-03 22:56:09 +01:00
public int getHighestBlockYAt(int x, int z) {
return world.d(x, z);
}
2010-12-27 03:13:03 +01:00
public Chunk getChunkAt(int x, int z) {
ChunkCoordinate loc = new ChunkCoordinate(x, z);
2011-01-04 16:54:41 +01:00
CraftChunk chunk = chunkCache.get(loc);
2010-12-27 03:13:03 +01:00
if (chunk == null) {
2011-01-04 15:17:05 +01:00
chunk = new CraftChunk(this, x, z);
2010-12-27 03:13:03 +01:00
chunkCache.put(loc, chunk);
}
return chunk;
}
public Chunk getChunkAt(Block block) {
return getChunkAt(block.getX() << 4, block.getZ() << 4);
}
2011-01-03 04:25:24 +01:00
public boolean isChunkLoaded(Chunk chunk) {
return world.A.a(chunk.getX(), chunk.getZ());
2010-12-27 03:13:03 +01:00
}
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;
}
2011-01-04 16:54:41 +01:00
public CraftChunk updateChunk(int x, int z) {
ChunkCoordinate loc = new ChunkCoordinate(x, z);
CraftChunk chunk = chunkCache.get(loc);
if (chunk == null) {
chunk = new CraftChunk(this, x, z);
chunkCache.put(loc, chunk);
} else {
// TODO: Chunk stuff
}
return chunk;
}
public WorldServer getHandle() {
return world;
}
2011-01-08 21:48:45 +01:00
public ItemDrop dropItem(Location loc, ItemStack item) {
net.minecraft.server.ItemStack stack =
new net.minecraft.server.ItemStack(
item.getTypeID(), item.getAmount(), item.getDamage());
EntityItem entity = new EntityItem(world, loc.getX(), loc.getY(),
loc.getZ(), stack);
entity.c = 10;
world.a(entity);
return new CraftItemDrop(world.getServer(), entity);
}
public ItemDrop dropItemNaturally(Location loc, ItemStack item) {
double xs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
double ys = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
double zs = world.l.nextFloat() * 0.7F + (1.0F - 0.7F) * 0.5D;
loc = loc.clone();
loc.setX(loc.getX() + xs);
loc.setX(loc.getY() + ys);
loc.setX(loc.getZ() + zs);
return dropItem(loc, item);
}
public Arrow spawnArrow(Location loc, Vector velocity, float speed,
2011-01-02 09:40:27 +01:00
float spread) {
EntityArrow arrow = new EntityArrow(world);
arrow.c(loc.getX(), loc.getY(), loc.getZ());
world.a(arrow);
arrow.a(velocity.getX(), velocity.getY(), velocity.getZ(), speed, spread);
return new CraftArrow(world.getServer(), arrow);
2011-01-02 09:40:27 +01:00
}
public Minecart spawnMinecart(Location loc) {
EntityMinecart minecart = new EntityMinecart(
world,
loc.getX(), loc.getY(), loc.getZ(),
CraftMinecart.Type.Minecart.getID());
world.a(minecart);
return new CraftMinecart(world.getServer(), minecart);
}
public StorageMinecart spawnStorageMinecart(Location loc) {
EntityMinecart minecart = new EntityMinecart(
world,
loc.getX(), loc.getY(), loc.getZ(),
CraftMinecart.Type.StorageMinecart.getID());
world.a(minecart);
return new CraftStorageMinecart(world.getServer(), minecart);
}
public PoweredMinecart spawnPoweredMinecart(Location loc) {
EntityMinecart minecart = new EntityMinecart(
world,
loc.getX(), loc.getY(), loc.getZ(),
CraftMinecart.Type.PoweredMinecart.getID());
world.a(minecart);
return new CraftPoweredMinecart(world.getServer(), minecart);
}
public Boat spawnBoat(Location loc) {
EntityBoat boat =
new EntityBoat(world, loc.getX(), loc.getY(), loc.getZ());
world.a(boat);
return new CraftBoat(world.getServer(), boat);
}
public boolean generateTree(Location loc) {
WorldGenTrees treeGen = new WorldGenTrees();
return treeGen.a(world, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
public boolean generateBigTree(Location loc) {
WorldGenBigTree treeGen = new WorldGenBigTree();
return treeGen.a(world, rand,
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
public CraftEntity toCraftEntity(net.minecraft.server.Entity entity) {
if (entity instanceof CraftMappable) {
return ((CraftMappable)entity).getCraftEntity();
2011-01-08 21:48:45 +01:00
} else if (entity instanceof EntityItem) {
return new CraftItemDrop(world.getServer(), (EntityItem)entity);
} else if (entity instanceof EntityArrow) {
return new CraftArrow(world.getServer(), (EntityArrow)entity);
} else if (entity instanceof EntityEgg) {
return new CraftEgg(world.getServer(), (EntityEgg)entity);
} else if (entity instanceof EntityPlayerMP) {
return new CraftPlayer(world.getServer(), (EntityPlayerMP)entity);
} else if (entity instanceof EntitySnowball) {
return new CraftSnowball(world.getServer(), (EntitySnowball)entity);
} else if (entity instanceof EntityPlayer) {
return new CraftHumanEntity(world.getServer(), (EntityPlayer)entity);
} else if (entity instanceof EntityLiving) {
return new CraftLivingEntity(world.getServer(), (EntityLiving)entity);
} else {
return null;
}
}
2011-01-02 09:40:27 +01:00
public TileEntity getTileEntityAt(final int x, final int y, final int z) {
return world.l(x, y, z);
}
2011-01-08 03:29:57 +01:00
public String getName() {
return world.w;
}
public long getId() {
return world.u;
}
@Override
public String toString() {
return "CraftWorld";
}
2010-12-27 03:13:03 +01:00
private final class ChunkCoordinate {
public final int x;
public final int z;
2010-12-27 03:13:03 +01:00
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;
2010-12-27 03:13:03 +01:00
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;
}
}
}