2010-10-11 10:22:47 +02:00
|
|
|
// $Id$
|
|
|
|
/*
|
2010-11-04 00:46:47 +01:00
|
|
|
* WorldEditLibrary
|
2010-10-11 10:22:47 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 03:03:56 +02:00
|
|
|
import com.sk89q.worldedit.Vector;
|
2010-10-20 02:10:02 +02:00
|
|
|
import com.sk89q.worldedit.blocks.BlockType;
|
2010-10-11 10:22:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public class WorldEditPlayer {
|
2010-10-11 10:22:47 +02:00
|
|
|
/**
|
2010-11-06 00:42:16 +01:00
|
|
|
* Stores the player.
|
2010-10-11 10:22:47 +02:00
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
private Player player;
|
2010-10-14 20:59:45 +02:00
|
|
|
|
|
|
|
/**
|
2010-11-06 00:42:16 +01:00
|
|
|
* Construct the object.
|
|
|
|
*
|
|
|
|
* @param player
|
2010-10-14 20:59:45 +02:00
|
|
|
*/
|
2010-11-06 00:42:16 +01:00
|
|
|
public WorldEditPlayer(Player player) {
|
|
|
|
this.player = player;
|
2010-10-14 20:59:45 +02:00
|
|
|
}
|
2010-10-17 02:08:56 +02:00
|
|
|
|
2010-10-13 20:26:07 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the player is holding a pick axe.
|
|
|
|
*
|
|
|
|
* @return whether a pick axe is held
|
|
|
|
*/
|
|
|
|
public boolean isHoldingPickAxe() {
|
|
|
|
int item = getItemInHand();
|
2010-11-05 07:07:47 +01:00
|
|
|
return item == 257 || item == 270 || item == 274 || item == 278
|
2010-10-13 20:26:07 +02:00
|
|
|
|| item == 285;
|
|
|
|
}
|
|
|
|
|
2010-10-13 07:06:46 +02:00
|
|
|
/**
|
|
|
|
* Move the player.
|
|
|
|
*
|
|
|
|
* @param pos
|
|
|
|
*/
|
|
|
|
public void setPosition(Vector pos) {
|
2010-10-14 10:31:05 +02:00
|
|
|
setPosition(pos, (float)getPitch(), (float)getYaw());
|
2010-10-13 07:06:46 +02:00
|
|
|
}
|
|
|
|
|
2010-10-11 10:22:47 +02:00
|
|
|
/**
|
|
|
|
* Find a position for the player to stand that is not inside a block.
|
|
|
|
* Blocks above the player will be iteratively tested until there is
|
|
|
|
* a series of two free blocks. The player will be teleported to
|
|
|
|
* that free position.
|
2010-10-20 02:10:02 +02:00
|
|
|
*
|
|
|
|
* @param searchPos search position
|
2010-10-11 10:22:47 +02:00
|
|
|
*/
|
2010-10-20 02:10:02 +02:00
|
|
|
public void findFreePosition(Vector searchPos) {
|
|
|
|
int x = searchPos.getBlockX();
|
2010-11-06 09:28:45 +01:00
|
|
|
int y = Math.max(0, searchPos.getBlockY());
|
2010-10-11 10:22:47 +02:00
|
|
|
int origY = y;
|
2010-10-20 02:10:02 +02:00
|
|
|
int z = searchPos.getBlockZ();
|
2010-10-11 10:22:47 +02:00
|
|
|
|
|
|
|
byte free = 0;
|
|
|
|
|
|
|
|
while (y <= 129) {
|
|
|
|
if (etc.getServer().getBlockIdAt(x, y, z) == 0) {
|
|
|
|
free++;
|
|
|
|
} else {
|
|
|
|
free = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free == 2) {
|
|
|
|
if (y - 1 != origY) {
|
2010-10-14 10:31:05 +02:00
|
|
|
setPosition(new Vector(x + 0.5, y - 1, z + 0.5));
|
2010-10-11 10:22:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 02:10:02 +02:00
|
|
|
/**
|
|
|
|
* Find a position for the player to stand that is not inside a block.
|
|
|
|
* Blocks above the player will be iteratively tested until there is
|
|
|
|
* a series of two free blocks. The player will be teleported to
|
|
|
|
* that free position.
|
|
|
|
*/
|
|
|
|
public void findFreePosition() {
|
|
|
|
findFreePosition(getBlockIn());
|
|
|
|
}
|
|
|
|
|
2010-10-13 07:06:46 +02:00
|
|
|
/**
|
|
|
|
* Go up one level to the next free space above.
|
|
|
|
*
|
|
|
|
* @return true if a spot was found
|
|
|
|
*/
|
|
|
|
public boolean ascendLevel() {
|
2010-10-17 01:56:59 +02:00
|
|
|
Vector pos = getBlockIn();
|
2010-10-14 10:31:05 +02:00
|
|
|
int x = pos.getBlockX();
|
2010-11-06 09:28:45 +01:00
|
|
|
int y = Math.max(0, pos.getBlockY());
|
2010-10-14 10:31:05 +02:00
|
|
|
int z = pos.getBlockZ();
|
2010-10-13 07:06:46 +02:00
|
|
|
|
|
|
|
byte free = 0;
|
|
|
|
byte spots = 0;
|
|
|
|
|
|
|
|
while (y <= 129) {
|
2010-11-06 00:42:16 +01:00
|
|
|
if (ServerInterface.getBlockType(new Vector(x, y, z)) == 0) {
|
2010-10-13 07:06:46 +02:00
|
|
|
free++;
|
|
|
|
} else {
|
|
|
|
free = 0;
|
|
|
|
}
|
|
|
|
|
2010-10-17 01:56:59 +02:00
|
|
|
if (free == 2) {
|
2010-10-13 07:06:46 +02:00
|
|
|
spots++;
|
2010-10-17 01:56:59 +02:00
|
|
|
if (spots == 2) {
|
2010-11-06 00:42:16 +01:00
|
|
|
int type = ServerInterface.getBlockType(new Vector(x, y - 2, z));
|
2010-10-17 02:15:17 +02:00
|
|
|
|
|
|
|
// Don't get put in lava!
|
|
|
|
if (type == 10 || type == 11) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-13 07:06:46 +02:00
|
|
|
setPosition(new Vector(x + 0.5, y - 1, z + 0.5));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go up one level to the next free space above.
|
|
|
|
*
|
|
|
|
* @return true if a spot was found
|
|
|
|
*/
|
|
|
|
public boolean descendLevel() {
|
2010-10-17 02:08:56 +02:00
|
|
|
Vector pos = getBlockIn();
|
2010-10-14 10:31:05 +02:00
|
|
|
int x = pos.getBlockX();
|
2010-11-06 09:28:45 +01:00
|
|
|
int y = Math.max(0, pos.getBlockY() - 1);
|
2010-10-14 10:31:05 +02:00
|
|
|
int z = pos.getBlockZ();
|
2010-10-13 07:06:46 +02:00
|
|
|
|
|
|
|
byte free = 0;
|
|
|
|
|
2010-10-17 02:08:56 +02:00
|
|
|
while (y >= 1) {
|
2010-11-06 00:42:16 +01:00
|
|
|
if (ServerInterface.getBlockType(new Vector(x, y, z)) == 0) {
|
2010-10-13 07:06:46 +02:00
|
|
|
free++;
|
|
|
|
} else {
|
|
|
|
free = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free == 2) {
|
2010-10-17 02:08:56 +02:00
|
|
|
// So we've found a spot, but we have to drop the player
|
|
|
|
// lightly and also check to see if there's something to
|
|
|
|
// stand upon
|
|
|
|
while (y >= 0) {
|
2010-11-06 00:42:16 +01:00
|
|
|
int type = ServerInterface.getBlockType(new Vector(x, y, z));
|
2010-10-17 02:15:17 +02:00
|
|
|
|
|
|
|
// Don't want to end up in lava
|
|
|
|
if (type != 0 && type != 10 && type != 11) {
|
2010-10-17 02:08:56 +02:00
|
|
|
// Found a block!
|
|
|
|
setPosition(new Vector(x + 0.5, y + 1, z + 0.5));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
y--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-10-13 07:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
y--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-20 02:10:02 +02:00
|
|
|
/**
|
|
|
|
* Ascend to the ceiling above.
|
|
|
|
*
|
|
|
|
* @param clearance
|
|
|
|
* @return whether the player was moved
|
|
|
|
*/
|
|
|
|
public boolean ascendToCeiling(int clearance) {
|
|
|
|
Vector pos = getBlockIn();
|
|
|
|
int x = pos.getBlockX();
|
2010-11-06 09:28:45 +01:00
|
|
|
int initialY = Math.max(0, pos.getBlockY());
|
|
|
|
int y = Math.max(0, pos.getBlockY() + 2);
|
2010-10-20 02:10:02 +02:00
|
|
|
int z = pos.getBlockZ();
|
|
|
|
|
2010-10-20 05:36:57 +02:00
|
|
|
// No free space above
|
2010-11-06 00:42:16 +01:00
|
|
|
if (ServerInterface.getBlockType(new Vector(x, y, z)) != 0) {
|
2010-10-20 02:10:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (y <= 127) {
|
|
|
|
// Found a ceiling!
|
2010-11-06 00:42:16 +01:00
|
|
|
if (ServerInterface.getBlockType(new Vector(x, y, z)) != 0) {
|
2010-10-20 05:36:57 +02:00
|
|
|
int platformY = Math.max(initialY, y - 3 - clearance);
|
2010-11-06 00:42:16 +01:00
|
|
|
ServerInterface.setBlockType(new Vector(x, platformY, z),
|
2010-10-20 02:10:02 +02:00
|
|
|
BlockType.GLASS.getID());
|
2010-10-20 05:36:57 +02:00
|
|
|
setPosition(new Vector(x + 0.5, platformY + 1, z + 0.5));
|
2010-10-20 02:10:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-11 10:22:47 +02:00
|
|
|
/**
|
|
|
|
* Returns true if equal.
|
2010-10-17 02:08:56 +02:00
|
|
|
*
|
2010-10-11 10:22:47 +02:00
|
|
|
* @param other
|
2010-10-11 17:56:19 +02:00
|
|
|
* @return whether the other object is equivalent
|
2010-10-11 10:22:47 +02:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (!(other instanceof WorldEditPlayer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WorldEditPlayer other2 = (WorldEditPlayer)other;
|
2010-10-14 10:31:05 +02:00
|
|
|
return other2.getName().equals(getName());
|
2010-10-11 10:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the hash code.
|
|
|
|
*
|
2010-10-11 17:56:19 +02:00
|
|
|
* @return hash code
|
2010-10-11 10:22:47 +02:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getName().hashCode();
|
|
|
|
}
|
2010-11-06 00:42:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the point of the block that is being stood in.
|
|
|
|
*
|
|
|
|
* @return point
|
|
|
|
*/
|
|
|
|
public Vector getBlockIn() {
|
|
|
|
return Vector.toBlockPoint(player.getX(), player.getY(), player.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the point of the block that is being stood upon.
|
|
|
|
*
|
|
|
|
* @return point
|
|
|
|
*/
|
|
|
|
public Vector getBlockOn() {
|
|
|
|
return Vector.toBlockPoint(player.getX(), player.getY() - 1, player.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the point of the block being looked at. May return null.
|
|
|
|
*
|
|
|
|
* @param range
|
|
|
|
* @return point
|
|
|
|
*/
|
|
|
|
public Vector getBlockTrace(int range) {
|
|
|
|
HitBlox hitBlox = new HitBlox(player, range, 0.2);
|
|
|
|
Block block = hitBlox.getTargetBlock();
|
|
|
|
if (block == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Vector(block.getX(), block.getY(), block.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the player's cardinal direction (N, W, NW, etc.).
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getCardinalDirection() {
|
|
|
|
// From hey0's code
|
|
|
|
double rot = (getYaw() - 90) % 360;
|
|
|
|
if (rot < 0) {
|
|
|
|
rot += 360.0;
|
|
|
|
}
|
|
|
|
return etc.getCompassPointForDirection(rot).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the item that the player is holding.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Get the ID of the item that the player is holding.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getItemInHand() {
|
|
|
|
return player.getItemInHand();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the player.
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return player.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the player's view pitch.
|
|
|
|
*
|
|
|
|
* @return pitch
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Get the player's view pitch.
|
|
|
|
*
|
|
|
|
* @return pitch
|
|
|
|
*/
|
|
|
|
public double getPitch() {
|
|
|
|
return player.getPitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the player's position.
|
|
|
|
*
|
|
|
|
* @return point
|
|
|
|
*/
|
|
|
|
public Vector getPosition() {
|
|
|
|
return new Vector(player.getX(), player.getY(), player.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the player's view yaw.
|
|
|
|
*
|
|
|
|
* @return yaw
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Get the player's view yaw.
|
|
|
|
*
|
|
|
|
* @return yaw
|
|
|
|
*/
|
|
|
|
public double getYaw() {
|
|
|
|
return player.getRotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gives the player an item.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param amt
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Gives the player an item.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param amt
|
|
|
|
*/
|
|
|
|
public void giveItem(int type, int amt) {
|
|
|
|
player.giveItem(type, amt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass through the wall that you are looking at.
|
|
|
|
*
|
|
|
|
* @param range
|
|
|
|
* @return whether the player was pass through
|
|
|
|
*/
|
|
|
|
public boolean passThroughForwardWall(int range) {
|
|
|
|
boolean foundNext = false;
|
|
|
|
int searchDist = 0;
|
|
|
|
HitBlox hitBlox = new HitBlox(player, range, 0.2);
|
|
|
|
Block block;
|
|
|
|
while ((block = hitBlox.getNextBlock()) != null) {
|
|
|
|
searchDist++;
|
|
|
|
if (searchDist > 20) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (block.getType() == 0) {
|
|
|
|
if (foundNext) {
|
|
|
|
Vector v = new Vector(block.getX(), block.getY() - 1, block.getZ());
|
|
|
|
if (ServerInterface.getBlockType(v) == 0) {
|
|
|
|
setPosition(v.add(0.5, 0, 0.5));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundNext = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a WorldEdit message.
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
public void print(String msg) {
|
|
|
|
player.sendMessage(Colors.LightPurple + msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a WorldEdit error.
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
public void printError(String msg) {
|
|
|
|
player.sendMessage(Colors.Rose + msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the player.
|
|
|
|
*
|
|
|
|
* @param pos
|
|
|
|
* @param pitch
|
|
|
|
* @param yaw
|
|
|
|
*/
|
|
|
|
public void setPosition(Vector pos, float pitch, float yaw) {
|
|
|
|
Location loc = new Location();
|
|
|
|
loc.x = pos.getX();
|
|
|
|
loc.y = pos.getY();
|
|
|
|
loc.z = pos.getZ();
|
|
|
|
loc.rotX = (float) yaw;
|
|
|
|
loc.rotY = (float) pitch;
|
|
|
|
player.teleportTo(loc);
|
|
|
|
}
|
2010-11-06 08:51:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a player's list of groups.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String[] getGroups() {
|
|
|
|
return player.getGroups();
|
|
|
|
}
|
2010-10-11 10:22:47 +02:00
|
|
|
}
|