2011-01-26 20:04:53 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.bukkit;
|
|
|
|
|
2011-05-11 04:55:07 +00:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.bukkit.block.*;
|
2011-01-26 20:04:53 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Server;
|
2011-01-30 22:54:42 +00:00
|
|
|
import org.bukkit.World;
|
2011-05-11 04:55:07 +00:00
|
|
|
import com.sk89q.worldedit.*;
|
2011-01-26 20:04:53 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
|
|
|
|
public class BukkitUtil {
|
|
|
|
private BukkitUtil() {
|
|
|
|
}
|
|
|
|
|
2011-05-11 04:55:07 +00:00
|
|
|
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;
|
2011-01-30 22:54:42 +00:00
|
|
|
}
|
|
|
|
|
2011-01-26 20:04:53 +00:00
|
|
|
public static BlockVector toVector(Block block) {
|
|
|
|
return new BlockVector(block.getX(), block.getY(), block.getZ());
|
|
|
|
}
|
|
|
|
|
2011-05-11 04:55:07 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-01-26 20:04:53 +00:00
|
|
|
public static Vector toVector(Location loc) {
|
|
|
|
return new Vector(loc.getX(), loc.getY(), loc.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector toVector(org.bukkit.util.Vector vector) {
|
|
|
|
return new Vector(vector.getX(), vector.getY(), vector.getZ());
|
|
|
|
}
|
|
|
|
|
2011-05-11 04:55:07 +00:00
|
|
|
public static Location toLocation(WorldVector pt) {
|
|
|
|
return new Location(toWorld(pt), pt.getX(), pt.getY(), pt.getZ());
|
|
|
|
}
|
2011-05-11 05:11:25 +00:00
|
|
|
|
|
|
|
public static Location toLocation(World world, Vector pt) {
|
|
|
|
return new Location(world, pt.getX(), pt.getY(), pt.getZ());
|
|
|
|
}
|
2011-05-11 04:55:07 +00:00
|
|
|
|
2011-01-26 20:04:53 +00:00
|
|
|
public static Player matchSinglePlayer(Server server, String name) {
|
|
|
|
List<Player> players = server.matchPlayer(name);
|
|
|
|
if (players.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return players.get(0);
|
|
|
|
}
|
2011-05-11 04:55:07 +00:00
|
|
|
|
|
|
|
public static Block toBlock(BlockWorldVector pt) {
|
|
|
|
return toWorld(pt).getBlockAt(toLocation(pt));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static World toWorld(WorldVector pt) {
|
|
|
|
return ((BukkitWorld)pt.getWorld()).getWorld();
|
|
|
|
}
|
2011-06-07 00:32:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bukkit's Location class has serious problems with floating point
|
|
|
|
* precision.
|
|
|
|
*/
|
|
|
|
public static boolean equals(Location a, Location b) {
|
|
|
|
if (Math.abs(a.getX()-b.getX()) > EQUALS_PRECISION) return false;
|
|
|
|
if (Math.abs(a.getY()-b.getY()) > EQUALS_PRECISION) return false;
|
|
|
|
if (Math.abs(a.getZ()-b.getZ()) > EQUALS_PRECISION) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public static final double EQUALS_PRECISION = 0.0001;
|
2011-01-26 20:04:53 +00:00
|
|
|
}
|