mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2024-12-27 05:00:08 +08:00
Added a Location class and conversion utilities.
This is required for the latest WorldGuard version
This commit is contained in:
parent
bc94f8f973
commit
255b04e13d
120
src/main/java/com/sk89q/worldedit/Location.java
Normal file
120
src/main/java/com/sk89q/worldedit/Location.java
Normal file
@ -0,0 +1,120 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
||||
*
|
||||
* 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;
|
||||
|
||||
public class Location {
|
||||
private final LocalWorld world;
|
||||
private final Vector position;
|
||||
private final float yaw;
|
||||
private final float pitch;
|
||||
|
||||
public Location(LocalWorld world, Vector position) {
|
||||
this(world, position, 0, 0);
|
||||
}
|
||||
|
||||
public Location(LocalWorld world, Vector position, float yaw, float pitch) {
|
||||
this.world = world;
|
||||
this.position = position;
|
||||
this.yaw = yaw;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public LocalWorld getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
public Vector getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return yaw;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public Location setAngles(float yaw, float pitch) {
|
||||
return new Location(world, position, yaw, pitch);
|
||||
}
|
||||
|
||||
public Location setPosition(Vector position) {
|
||||
return new Location(world, position, yaw, pitch);
|
||||
}
|
||||
|
||||
public Location add(Vector other) {
|
||||
return setPosition(position.add(other));
|
||||
}
|
||||
|
||||
public Location add(double x, double y, double z) {
|
||||
return setPosition(position.add(x, y, z));
|
||||
}
|
||||
|
||||
public Vector getDirection() {
|
||||
final double yawRadians = Math.toRadians(yaw);
|
||||
final double pitchRadians = Math.toRadians(pitch);
|
||||
final double y = -Math.sin(pitchRadians);
|
||||
|
||||
final double h = Math.cos(pitchRadians);
|
||||
|
||||
final double x = -h * Math.sin(yawRadians);
|
||||
final double z = h * Math.cos(yawRadians);
|
||||
|
||||
return new Vector(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Location))
|
||||
return false;
|
||||
|
||||
Location location = (Location) obj;
|
||||
if (!world.equals(location.world))
|
||||
return false;
|
||||
|
||||
if (!position.equals(location.position))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return position.hashCode() + 19 * world.hashCode();
|
||||
}
|
||||
|
||||
|
||||
public static Location fromLookAt(LocalWorld world, Vector start, Vector lookAt) {
|
||||
final Vector diff = lookAt.subtract(start);
|
||||
|
||||
return fromEye(world, start, diff);
|
||||
}
|
||||
|
||||
public static Location fromEye(LocalWorld world, Vector start, Vector eye) {
|
||||
final double eyeX = eye.getX();
|
||||
final double eyeZ = eye.getZ();
|
||||
final float yaw = (float) Math.toDegrees(Math.atan2(-eyeX, eyeZ));
|
||||
final double length = Math.sqrt(eyeX * eyeX + eyeZ * eyeZ);
|
||||
final float pitch = (float) Math.toDegrees(Math.atan2(-eye.getY(), length));
|
||||
|
||||
return new Location(world, start, yaw, pitch);
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import com.sk89q.worldedit.*;
|
||||
@ -58,24 +57,32 @@ public static BlockWorldVector toWorldVector(Block block) {
|
||||
return new BlockWorldVector(getLocalWorld(block.getWorld()), block.getX(), block.getY(), block.getZ());
|
||||
}
|
||||
|
||||
public static Vector toVector(Location loc) {
|
||||
public static Vector toVector(org.bukkit.Location loc) {
|
||||
return new Vector(loc.getX(), loc.getY(), loc.getZ());
|
||||
}
|
||||
|
||||
public static Location toLocation(org.bukkit.Location loc) {
|
||||
return new Location(
|
||||
getLocalWorld(loc.getWorld()),
|
||||
new Vector(loc.getX(), loc.getY(), loc.getZ()),
|
||||
loc.getYaw(), loc.getPitch()
|
||||
);
|
||||
}
|
||||
|
||||
public static Vector toVector(org.bukkit.util.Vector vector) {
|
||||
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 org.bukkit.Location toLocation(WorldVector pt) {
|
||||
return new org.bukkit.Location(toWorld(pt), pt.getX(), pt.getY(), pt.getZ());
|
||||
}
|
||||
|
||||
public static Location toLocation(World world, Vector pt) {
|
||||
return new Location(world, pt.getX(), pt.getY(), pt.getZ());
|
||||
public static org.bukkit.Location toLocation(World world, Vector pt) {
|
||||
return new org.bukkit.Location(world, pt.getX(), pt.getY(), pt.getZ());
|
||||
}
|
||||
|
||||
public static Location center(Location loc) {
|
||||
return new Location(
|
||||
public static org.bukkit.Location center(org.bukkit.Location loc) {
|
||||
return new org.bukkit.Location(
|
||||
loc.getWorld(),
|
||||
loc.getBlockX() + 0.5,
|
||||
loc.getBlockY() + 0.5,
|
||||
@ -105,7 +112,7 @@ public static World toWorld(WorldVector pt) {
|
||||
* Bukkit's Location class has serious problems with floating point
|
||||
* precision.
|
||||
*/
|
||||
public static boolean equals(Location a, Location b) {
|
||||
public static boolean equals(org.bukkit.Location a, org.bukkit.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;
|
||||
@ -113,4 +120,13 @@ public static boolean equals(Location a, Location b) {
|
||||
}
|
||||
|
||||
public static final double EQUALS_PRECISION = 0.0001;
|
||||
|
||||
public static org.bukkit.Location toLocation(Location teleportLocation) {
|
||||
final LocalWorld world = teleportLocation.getWorld();
|
||||
return toLocation(toWorld(world), teleportLocation.getPosition());
|
||||
}
|
||||
|
||||
public static World toWorld(final LocalWorld world) {
|
||||
return ((BukkitWorld) world).getWorld();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user