import java.util.logging.*; public class Setpos extends Plugin { final static Logger log = Logger.getLogger("SetposPlugin"); @Override public void disable() {} @Override public void enable() {} @Override public void initialize() { PluginLoader loader = etc.getLoader(); loader.addListener( PluginLoader.Hook.COMMAND, new RealSetposPlugin(), this, PluginListener.Priority.MEDIUM ); } public static class RealSetposPlugin extends PluginListener { final static String USAGE = "Usage: /setpos [height]"; final static int AIRBLOCK = 0; //block id = 0 is air //http://www.minecraftforum.net/viewtopic.php?f=35&t=14739 //Y is height // air == blockID:0 private int getGroundLevel(int x, int z) { Server srv = etc.getServer(); int level = srv.getHighestBlockY(x,z); if (level == -1) level = 128; return level; /* int y; for (y=128; y>=0 && srv.getBlockIdAt(x,y-1,z) == AIRBLOCK; y--) ; System.out.println("Groundlevel: " + y); System.out.println("getHighestBlockY: " + srv.getHighestBlockY(x,z) ); return y; */ } private boolean isFree(int x, int y, int z) { Server srv = etc.getServer(); return (srv.getBlockIdAt(x,y,z) == AIRBLOCK && srv.getBlockIdAt(x,y+1,z) == AIRBLOCK); } @Override public boolean onCommand(Player player, java.lang.String[] split) { if( split[0].equals("/setpos") ) { setPos(player, split); return true; } else if ( split[0].equals("/whereis" ) ) { whereIs(player, split); return true; } else { return false; } } double roundToPlaces(double value, int places) { double pow = Math.pow(10, places); double temp = Math.round( value*pow ); return temp / pow; } private String getBearingStr(int angle) { if (angle < 22) { return "N"; } else if (angle < 67) { return "NE"; } else if (angle < 112) { return "E"; } else if (angle < 157) { return "SE"; } else if (angle < 202) { return "S"; } else if (angle < 257) { return "SW"; } else if (angle < 292) { return "W"; } else if (angle < 337) { return "NW"; } else { return "N"; } } public void whereIs(Player p1, java.lang.String[] split) { if (split.length != 2) { p1.sendMessage( Colors.Rose + "usage: /whereis "); return; } Player p2 = etc.getServer().getPlayer(split[1]); if (p2 == null) { p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] ); return; } Location loc1 = p1.getLocation(); Location loc2 = p2.getLocation(); //Location loc2 = new Location(); //loc2.x = loc2.y = loc2.z = 0; //System.out.println("p1: " + loc1.x + "," + loc1.z ); //System.out.println("p2: " + loc2.x + "," + loc2.z ); double distX = loc1.x - loc2.x ; double distZ = loc1.z - loc2.z ; int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) ); /*System.out.println("distX:" + distX ); System.out.println("distZ:" + distZ ); System.out.println(">> " + (distZ / distX ) );*/ double angle = Math.toDegrees( Math.atan( distZ / distX ) ); if (angle < 0.0) angle += 90.0; if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees //do nothing } else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees angle += 90.0; } else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees angle += 180.0; } else { angle += 270.0; } p1.sendMessage( Colors.Yellow + p2.getName() + " is at x:" + roundToPlaces(p2.getX(),2) + " y:" + roundToPlaces(p2.getY(),2) + " z: " + roundToPlaces(p2.getZ(),2) ); p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" ); p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" ); } public void setPos(Player player, java.lang.String[] split) { int x; int z; int y; int userY = 0; boolean hasY = false; if (split.length <3 || split.length > 4) { player.sendMessage(USAGE); return; } try { x = Integer.parseInt( split[1] ); z = Integer.parseInt( split[2] ); if (split.length == 4) { hasY = true; userY = Integer.parseInt( split[3] ); } } catch (Exception e) { player.sendMessage("/setpos error: non numeric argument"); return; } if (hasY) { if (isFree(x,userY,z)) { y = userY; } else { player.sendMessage("/setpos: that position is not free"); return; } } else { y = getGroundLevel(x,z); } log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" ); Location newLocation = new Location(); newLocation.rotY = player.getPitch(); newLocation.rotX = player.getRotation(); newLocation.x = x + 0.5; newLocation.z = z + 0.5; newLocation.y = y; player.teleportTo(newLocation); player.sendMessage("Whooooooosh"); } } }