From e6f3a1b5af0b19010e3883b9bf13914bb21acf93 Mon Sep 17 00:00:00 2001 From: "hash@exultant.us" Date: Tue, 10 May 2011 23:55:07 -0500 Subject: [PATCH] More general-purpose vector code abstracted from craftbook and more convenience methods for conversion. --- .../com/sk89q/worldedit/BlockWorldVector.java | 22 +++++++++++ .../sk89q/worldedit/bukkit/BukkitUtil.java | 37 ++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/BlockWorldVector.java b/src/main/java/com/sk89q/worldedit/BlockWorldVector.java index 561a98028..056aea7df 100644 --- a/src/main/java/com/sk89q/worldedit/BlockWorldVector.java +++ b/src/main/java/com/sk89q/worldedit/BlockWorldVector.java @@ -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. * diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java index a05f34d54..b9c6ce6b5 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java @@ -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 wlw = new HashMap(); + 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 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(); + } }