--- miscJava/minecraft-plugins/hoeruputils/src/HoerupUtils.java 2010/10/22 13:49:02 1177 +++ miscJava/minecraft-plugins/hoeruputils/src/HoerupUtils.java 2010/12/04 21:32:52 1202 @@ -1,31 +1,141 @@ import java.util.logging.*; +import java.util.*; public class HoerupUtils extends Plugin { final static Logger log = Logger.getLogger("HoerupUtils"); + + private void registerCommands() { + etc e = etc.getInstance(); + e.addCommand("/setpos", "[x] [z] - Teleports you to the given coordinates"); + e.addCommand("/whereis", "[player] - Tells you the position of another player"); + e.addCommand("/fillarea", ""); + e.addCommand("/levelarea", ""); + e.addCommand("/setsurface", ""); + e.addCommand("/slopearea", ""); + e.addCommand("/admindestroy",""); + } @Override - public void disable() {} + public void enable() { + registerCommands(); + } @Override - public void enable() {} + public void disable() { + etc e = etc.getInstance(); + e.removeCommand("/setpos"); + e.removeCommand("/whereis"); + e.removeCommand("/fillarea"); + e.removeCommand("/levelarea"); + e.removeCommand("/setsurface"); + e.removeCommand("/slopearea"); + e.removeCommand("/admindestroy"); + } + @Override public void initialize() { PluginLoader loader = etc.getLoader(); loader.addListener( PluginLoader.Hook.COMMAND, new HoerupUtilsPlugin(), this, PluginListener.Priority.MEDIUM ); + loader.addListener( PluginLoader.Hook.LOGIN, new ConnectedUsers(), this, PluginListener.Priority.MEDIUM ); + + Jail j = new Jail(); + loader.addListener( PluginLoader.Hook.TELEPORT, j, this, PluginListener.Priority.MEDIUM); + loader.addListener( PluginLoader.Hook.COMMAND, j, this, PluginListener.Priority.MEDIUM ); + loader.addListener( PluginLoader.Hook.PLAYER_MOVE, j, this, PluginListener.Priority.MEDIUM ); + + AdminDestroy adm = new AdminDestroy(); + loader.addListener( PluginLoader.Hook.COMMAND, adm, this, PluginListener.Priority.MEDIUM); + loader.addListener( PluginLoader.Hook.BLOCK_DESTROYED, adm, this, PluginListener.Priority.MEDIUM ); + loader.addListener( PluginLoader.Hook.DISCONNECT, adm, this, PluginListener.Priority.MEDIUM ); + + + registerCommands(); + } + + + + class ConnectedUsers extends PluginListener { + public void onLogin(Player player) { + List players = etc.getServer().getPlayerList(); + int count = players.size(); + + StringBuilder sb = new StringBuilder(); + for (int i=0; i0) + sb.append(" "); + sb.append( players.get(i).getName() ); + } + + player.sendMessage(Colors.Red + "Connected users " + count + ": " + Colors.White + sb.toString() ); + } + } + + + public static 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 static int calcDistance(Location loc1, Location loc2) { + double distX = loc1.x - loc2.x ; + double distZ = loc1.z - loc2.z ; + + int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) ); + + return dist; } + public static int calcBearing(Location loc1, Location loc2) { + double distX = loc1.x - loc2.x ; + double distZ = loc1.z - loc2.z ; + + 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; + } + return (int) angle; + } - public static class HoerupUtilsPlugin extends PluginListener { - final static String USAGE = "Usage: /setpos [height]"; + public class HoerupUtilsPlugin extends PluginListener { + final static String USAGE = "Usage: /setpos [x] [z] "; final static int BLOCK_AIR = 0; //block id = 0 is air final static int BLOCK_GRASS = 2; final static int BLOCK_DIRT = 3; + private HashMap commands = new HashMap(); + //http://www.minecraftforum.net/viewtopic.php?f=35&t=14739 @@ -62,21 +172,71 @@ return (srv.getBlockIdAt(x,y,z) == BLOCK_AIR && srv.getBlockIdAt(x,y+1,z) == BLOCK_AIR); } + public void slopeArea(Player player, java.lang.String[] split) { + int radius = Integer.parseInt(split[1]); + System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius); + + + int playerX = roundPos( player.getX() ); + int playerY = (int) player.getY(); + int playerZ = roundPos( player.getZ() ); + + Server srv = etc.getServer(); + + for (int x=(playerX-radius); x<=(playerX+radius); x++) { + for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { + + + int xdist = Math.abs(playerX-x); + int zdist = Math.abs(playerZ-z); + + int dist = Math.max(xdist,zdist); + + //for (int y=playerY; y<=playerY+radius; y++) { + for (int y=(playerY+dist); y<=128; y++) { + srv.setBlockAt(BLOCK_AIR, x, y, z); + } + + } + + } + } @Override public boolean onCommand(Player player, java.lang.String[] split) { - if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) { + if (! player.canUseCommand(split[0]) ) { + return false; + } + + if ( split[0].equals("/levelarea") || split[0].equals("/la") || split[0].equals("/slopearea") || split[0].equals("/fillarea") || split[0].equals("/setsurface") ) { + commands.put(player.getName(), split); + } + + if ( split[0].equals("//") ) { + String cmd[] = commands.get(player.getName() ); + if (cmd != null) { + onCommand(player, commands.get(player.getName() ) ); + } else { + player.sendMessage("//: no recorded command found"); + } + return true; + } else if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) { setPos(player, split); return true; } else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) { whereIs(player, split); return true; - } else if (split[0].equals("/levelarea") && player.canUseCommand("/levelarea")) { + } else if ( (split[0].equals("/levelarea") || split[0].equals("/la")) && player.canUseCommand("/levelarea")) { if (validateLevelOrFill(player,split)) { levelArea(player, split); } return true; + } else if (split[0].equals("/slopearea") ) { + if (validateLevelOrFill(player,split)) { + slopeArea(player,split); + } + return true; } else if (split[0].equals("/fillarea") && player.canUseCommand("/fillarea")) { if (validateLevelOrFill(player,split)) { fillArea(player, split); @@ -101,12 +261,12 @@ } private void setSurface(Player player, String[] split) { - int valid_block_array[] = {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 46, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; + int valid_block_array[] = {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; final int BLOCK_MAX = 86; if (split.length != 3) { - player.sendMessage("Usage /setsurface "); + player.sendMessage("Usage /setsurface [radius] [blockID]"); return; } @@ -166,8 +326,8 @@ } private boolean validateLevelOrFill(Player player, String[] split) { - if (split.length != 2) { - player.sendMessage("Usage: " + split[0] + " "); + if (split.length < 2 || split.length > 3) { + player.sendMessage("Usage: " + split[0] + " [radius]"); return false; } @@ -183,12 +343,32 @@ player.sendMessage(split[0] + ": radius may not exceed 20"); return false; } + + if (split.length == 3) { + int id; + try { + id = Integer.parseInt( split[2] ); + } catch (Exception e) { + player.sendMessage(split[0] + ": id must be an integer"); + return false; + } + if ( id == 46) { + player.sendMessage("Sorry dave, i can't do that"); + return false; + } + + } return true; } private void fillArea(Player player, String[] split) { int radius = Integer.parseInt(split[1]); + + int material = BLOCK_DIRT; + if (split.length == 3) { + material = Integer.parseInt( split[2] ); + } System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); @@ -203,7 +383,7 @@ for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { for (int y=getGroundLevel(x,z); y3) { - p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) [warpname]" ); + p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) " ); return; } @@ -316,39 +474,15 @@ loc2 = p2.getLocation(); name2 = p2.getName(); } - //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 ); + int dist = calcDistance(loc1, loc2); + int angle = calcBearing(loc1, loc2); - 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 + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) ); p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" ); - p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" ); + p1.sendMessage( Colors.Yellow + "Bearing: " + angle + " (" + getBearingStr( angle) + ")" );