More general-purpose vector code abstracted from craftbook and more convenience methods for conversion.

This commit is contained in:
hash@exultant.us 2011-05-10 23:55:07 -05:00
parent 0f4d331e7a
commit e6f3a1b5af
2 changed files with 54 additions and 5 deletions

View File

@ -44,6 +44,28 @@ public class BlockWorldVector extends WorldVector {
super(world, pt);
}
/**
* Construct the Vector object.
*
* @param world
* @param x
* @param y
* @param z
*/
public BlockWorldVector(WorldVector world, int x, int y, int z) {
super(world.getWorld(), x, y, z);
}
/**
* Construct the Vector object.
*
* @param world
* @param v
*/
public BlockWorldVector(WorldVector world, Vector v) {
super(world.getWorld(), v.x, v.y, v.z);
}
/**
* Construct the Vector object.
*

View File

@ -19,27 +19,42 @@
package com.sk89q.worldedit.bukkit;
import java.util.List;
import org.bukkit.block.Block;
import java.util.*;
import org.bukkit.block.*;
import org.bukkit.entity.Player;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.Vector;
public class BukkitUtil {
private BukkitUtil() {
}
public static Location toLocation(World world, Vector loc) {
return new Location(world, loc.getX(), loc.getY(), loc.getZ());
private static final Map<World,LocalWorld> wlw = new HashMap<World,LocalWorld>();
public static LocalWorld getLocalWorld(World w) {
LocalWorld lw = wlw.get(w);
if (lw == null) {
lw = new BukkitWorld(w);
wlw.put(w, lw);
}
return lw;
}
public static BlockVector toVector(Block block) {
return new BlockVector(block.getX(), block.getY(), block.getZ());
}
public static BlockVector toVector(BlockFace face) {
return new BlockVector(face.getModX(), face.getModY(), face.getModZ());
}
public static BlockWorldVector toWorldVector(Block block) {
return new BlockWorldVector(getLocalWorld(block.getWorld()), block.getX(), block.getY(), block.getZ());
}
public static Vector toVector(Location loc) {
return new Vector(loc.getX(), loc.getY(), loc.getZ());
}
@ -48,6 +63,10 @@ public class BukkitUtil {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public static Location toLocation(WorldVector pt) {
return new Location(toWorld(pt), pt.getX(), pt.getY(), pt.getZ());
}
public static Player matchSinglePlayer(Server server, String name) {
List<Player> players = server.matchPlayer(name);
if (players.size() == 0) {
@ -55,4 +74,12 @@ public class BukkitUtil {
}
return players.get(0);
}
public static Block toBlock(BlockWorldVector pt) {
return toWorld(pt).getBlockAt(toLocation(pt));
}
public static World toWorld(WorldVector pt) {
return ((BukkitWorld)pt.getWorld()).getWorld();
}
}