/[projects]/miscJava/minecraft-plugins/hoeruputils/src/HoerupUtils.java
ViewVC logotype

Diff of /miscJava/minecraft-plugins/hoeruputils/src/HoerupUtils.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

miscJava/minecraft-plugins/setposplugin/src/Setpos.java revision 1166 by torben, Fri Oct 15 12:34:50 2010 UTC miscJava/minecraft-plugins/hoeruputils/src/HoerupUtils.java revision 1195 by torben, Sat Nov 20 08:42:06 2010 UTC
# Line 1  Line 1 
1    
2  import java.util.logging.*;  import java.util.logging.*;
3    import java.util.*;
4    
5  public class Setpos extends Plugin {  public class HoerupUtils extends Plugin {
6    
7          final static Logger log = Logger.getLogger("SetposPlugin");          final static Logger log = Logger.getLogger("HoerupUtils");
8    
9            //private boolean adminDestroy = false;
10            private Set<String> adminDestroyers = new HashSet<String>();
11    
12            private void registerCommands() {
13                    etc e = etc.getInstance();
14                    e.addCommand("/setpos", "[x] [z] <height> - Teleports you to the given coordinates");
15                    e.addCommand("/whereis", "[player] - Tells you the position of another player");
16                    e.addCommand("/fillarea", "");
17                    e.addCommand("/levelarea", "");
18                    e.addCommand("/setsurface", "");
19            }
20                    
21          @Override          @Override
22          public void disable() {}          public void enable() {
23                    registerCommands();
24            }
25    
26          @Override          @Override
27          public void enable() {}          public void disable() {
28                    etc e = etc.getInstance();
29                    e.removeCommand("/setpos");
30                    e.removeCommand("/whereis");
31                    e.removeCommand("/fillarea");
32                    e.removeCommand("/levelarea");
33                    e.removeCommand("/setsurface");
34            }
35    
36                    
37          @Override          @Override
38          public void initialize() {          public void initialize() {
39                  PluginLoader loader = etc.getLoader();                  PluginLoader loader = etc.getLoader();
40                  loader.addListener( PluginLoader.Hook.COMMAND, new RealSetposPlugin(), this, PluginListener.Priority.MEDIUM );                  loader.addListener( PluginLoader.Hook.COMMAND, new HoerupUtilsPlugin(), this, PluginListener.Priority.MEDIUM );
41                    loader.addListener( PluginLoader.Hook.BLOCK_DESTROYED, new AdminDestroy(), this, PluginListener.Priority.MEDIUM );
42                    loader.addListener( PluginLoader.Hook.LOGIN, new ConnectedUsers(), this, PluginListener.Priority.MEDIUM );
43    
44                    Jail j = new Jail();
45                    loader.addListener( PluginLoader.Hook.TELEPORT, j, this, PluginListener.Priority.MEDIUM);
46                    loader.addListener( PluginLoader.Hook.COMMAND, j, this, PluginListener.Priority.MEDIUM );
47                    loader.addListener( PluginLoader.Hook.PLAYER_MOVE, j, this, PluginListener.Priority.MEDIUM );
48    
49    
50    
51                    registerCommands();
52          }          }
53    
54            final static int HAND_EMPTY = -1;
55    
56    
57          public static class RealSetposPlugin extends PluginListener {          class ConnectedUsers extends PluginListener {
58                  final static String USAGE = "Usage: /setpos <x> <z> [height]";                  public void onLogin(Player player) {
59                  final static int AIRBLOCK = 0; //block id = 0 is air                          List<Player> players = etc.getServer().getPlayerList();
60                            int count = players.size();
61    
62                            StringBuilder sb = new StringBuilder();
63                            for (int i=0; i<count; i++) {
64                                    if (i>0)
65                                            sb.append(" ");
66                                    sb.append( players.get(i).getName() );
67                            }
68    
69                            player.sendMessage(Colors.Red + "Connected users " + count + ": " + Colors.White + sb.toString() );
70                    }
71            }
72    
73            public class AdminDestroy  extends PluginListener {    
74                    public boolean onBlockDestroy(Player player, Block block) {
75                            if (player.isAdmin() && adminDestroyers.contains(player.getName() ) ) {
76                                    if (player.getItemInHand() == HAND_EMPTY) {
77    
78                                            int oldType = block.getType();
79    
80                                            block.setType(0);
81                                            etc.getServer().setBlock(block);
82    
83                                            if (oldType > 4 && oldType != 13) {  //dont drop stone or dirt, gravel
84                                                    etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), oldType); // diamond resource block  should drop diamonds and not a new diamond resource block
85                                            }
86    
87                                            return true;
88                                    }
89                            }
90                            return false;
91                    }
92            }
93    
94            public static String getBearingStr(int angle) {
95                    if (angle < 22) {
96                            return "N";
97                    } else if (angle < 67) {
98                            return "NE";
99                    } else if (angle < 112) {
100                            return "E";
101                    } else if (angle < 157) {
102                            return "SE";
103                    } else if (angle < 202) {
104                            return "S";
105                    } else if (angle < 257) {
106                            return "SW";
107                    } else if (angle < 292) {
108                            return "W";
109                    } else if (angle < 337) {
110                            return "NW";
111                    } else {
112                            return "N";
113                    }
114            }
115    
116            public static int calcDistance(Location loc1, Location loc2) {
117                    double distX =   loc1.x - loc2.x  ;
118                    double distZ =   loc1.z - loc2.z  ;
119    
120                    int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) );
121    
122                    return dist;
123            }
124    
125            public static int calcBearing(Location loc1, Location loc2) {
126                    double distX =   loc1.x - loc2.x  ;
127                    double distZ =   loc1.z - loc2.z  ;
128    
129                    double angle = Math.toDegrees( Math.atan( distZ / distX ) );
130                    if (angle < 0.0)
131                            angle += 90.0;
132    
133    
134                    if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees
135                            //do nothing
136                    } else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees
137                            angle += 90.0;
138                    } else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees
139                            angle += 180.0;
140                    } else {
141                            angle += 270.0;
142                    }
143    
144                    return (int) angle;
145            }
146    
147            public class HoerupUtilsPlugin extends PluginListener {
148                    final static String USAGE = "Usage: /setpos [x] [z] <height>";
149                    final static int BLOCK_AIR = 0; //block id = 0 is air
150                    final static int BLOCK_GRASS = 2;
151                    final static int BLOCK_DIRT = 3;
152    
153                    
154    
# Line 57  public class Setpos extends Plugin { Line 183  public class Setpos extends Plugin {
183                  private boolean isFree(int x, int y, int z) {                  private boolean isFree(int x, int y, int z) {
184                          Server srv = etc.getServer();                          Server srv = etc.getServer();
185    
186                          return  (srv.getBlockIdAt(x,y,z) == AIRBLOCK && srv.getBlockIdAt(x,y+1,z) == AIRBLOCK);                          return  (srv.getBlockIdAt(x,y,z) == BLOCK_AIR && srv.getBlockIdAt(x,y+1,z) == BLOCK_AIR);
187                  }                  }
188    
189                    
190    
191                    public void adminDestroy(Player player, String[] split) {
192                            String name = player.getName();
193                            if (adminDestroyers.contains(name) ) {
194                                    adminDestroyers.remove(name);
195                                    player.sendMessage("Admindestroy disabled");
196                            } else {
197                                    adminDestroyers.add(name);
198                                    player.sendMessage("Admindestroy enabled");
199                            }
200                    }
201    
202    
203                  @Override                  @Override
204                  public boolean onCommand(Player player, java.lang.String[] split) {                  public boolean onCommand(Player player, java.lang.String[] split) {
205                          if( split[0].equals("/setpos") ) {                          if ( split[0].equals("/admindestroy") && player.canUseCommand("/admindestroy") ) {
206                                    adminDestroy(player,split);
207                                    return true;
208                            } else if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) {
209                                  setPos(player, split);                                  setPos(player, split);
210                                  return true;                                  return true;
211                          } else if ( split[0].equals("/whereis" ) ) {                          } else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) {
212                                  whereIs(player, split);                                  whereIs(player, split);
213                                  return true;                                  return true;
214                            } else if (split[0].equals("/levelarea") && player.canUseCommand("/levelarea")) {
215                                    if (validateLevelOrFill(player,split)) {
216                                            levelArea(player, split);
217                                    }
218                                    return true;
219                            } else if (split[0].equals("/fillarea") && player.canUseCommand("/fillarea")) {
220                                    if (validateLevelOrFill(player,split)) {
221                                            fillArea(player, split);
222                                    }
223                                    return true;
224                            } else if (split[0].equals("/setsurface") && player.canUseCommand("/setsurface")) {
225                                    setSurface(player,split);
226                                    return true;
227                          } else {                          } else {
228                                  return false;                                  return false;
229                          }                          }
230                    
231                  }                  }
232    
233                    int roundPos(double input) {
234                            int result = (int) input;
235                            if (input < 0.0) {
236                                    result--;
237                            }
238    
239                            return result;
240                    }
241    
242                    private void setSurface(Player player, String[] split) {
243                            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} ;
244    
245                            final int BLOCK_MAX = 86;
246    
247                            if (split.length != 3) {
248                                    player.sendMessage("Usage /setsurface [radius] [blockID]");
249                                    return;
250                            }
251    
252    
253                            int radius;
254                            try {
255                                    radius = Integer.parseInt(split[1]);
256                            } catch (Exception e) {
257                                    player.sendMessage("setsurface: radius must be an integer");
258                                    return;
259                            }
260    
261                            if (radius > 20) {
262                                    player.sendMessage("setsurface: radius may not exceed 20");
263                                    return;
264                            }
265    
266    
267                            int blockid;
268                            try {
269                                    blockid = Integer.parseInt(split[2]);
270                            } catch (Exception e) {
271                                    player.sendMessage("setsurface: blockid must be an integer");
272                                    return;
273                            }
274    
275                            boolean validblock = false;
276                            for (int i=0; i<valid_block_array.length; i++) {
277                                    if (valid_block_array[i] == blockid) {
278                                            validblock = true;
279                                            break;
280                                    }
281                            }
282    
283                            if ( !validblock ) {
284                                    player.sendMessage("setsurface: block now allowed");
285                                    return;
286                            }
287    
288                            int playerX = roundPos( player.getX() );
289                            int playerY = (int) player.getY();
290                            int playerZ = roundPos( player.getZ() );
291    
292                            if(playerY <= 2 && blockid != 7) {
293                                    player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
294                                    return;
295                            }
296                            
297                            Server srv = etc.getServer();
298    
299                            for (int x=(playerX-radius); x<=(playerX+radius); x++) {
300                                    for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
301                                            int y = getGroundLevel(x,z) - 1;
302                                            srv.setBlockAt(blockid, x, y, z);
303                                    }
304                            }
305                    }
306    
307                    private boolean validateLevelOrFill(Player player, String[] split) {
308                            if (split.length < 2 || split.length > 3) {
309                                    player.sendMessage("Usage: " + split[0] + " [radius]");
310                                    return false;
311                            }
312                            
313                            int radius;
314                            try {
315                                    radius = Integer.parseInt(split[1]);
316                            } catch (Exception e) {
317                                    player.sendMessage(split[0] + ": radius must be an integer");
318                                    return false;
319                            }
320    
321                            if (radius > 20) {
322                                    player.sendMessage(split[0] + ": radius may not exceed 20");
323                                    return false;
324                            }
325                            
326                            if (split.length == 3) {
327                                    try {
328                                            Integer.parseInt( split[2] );
329                                    } catch (Exception e) {
330                                            player.sendMessage(split[0] + ": radius must be an integer");
331                                            return false;
332                                    }
333                            }
334    
335                            return true;
336                    }
337    
338                    private void fillArea(Player player, String[] split) {
339                            int radius = Integer.parseInt(split[1]);
340                            
341                            int material = BLOCK_DIRT;
342                            if (split.length == 3) {
343                                    material = Integer.parseInt( split[2] );
344                            }
345    
346                            System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
347                            
348    
349                            int playerX = roundPos( player.getX() );
350                            int playerY = (int) player.getY();
351                            int playerZ = roundPos( player.getZ() );
352                            
353                            Server srv = etc.getServer();
354    
355                            for (int x=(playerX-radius); x<=(playerX+radius); x++) {
356                                    for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
357    
358                                            for (int y=getGroundLevel(x,z); y<playerY; y++) {
359                                                    srv.setBlockAt(material, x, y, z);
360                                            }
361    
362                                    }
363    
364                            }
365                    }
366    
367                    private void levelArea(Player player, String[] split) {
368    
369                            int radius = Integer.parseInt(split[1]);
370    
371                            System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
372                            
373    
374                            int playerX = roundPos( player.getX() );
375                            int playerY = (int) player.getY();
376                            int playerZ = roundPos( player.getZ() );
377                            
378                            Server srv = etc.getServer();
379    
380                            int count = 0;
381                            for (int x=(playerX-radius); x<=(playerX+radius); x++) {
382                                    for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
383    
384                                            //for (int y=playerY; y<=playerY+radius; y++) {
385                                            for (int y=playerY; y<=128; y++) {
386                                                    count++;
387                                                    srv.setBlockAt(BLOCK_AIR, x, y, z);
388                                            }
389    
390                                    }
391    
392                            }
393                    }
394    
395    
396    
397           double roundToPlaces(double value, int places) {           double roundToPlaces(double value, int places) {
# Line 86  public class Setpos extends Plugin { Line 402  public class Setpos extends Plugin {
402          }          }
403    
404    
         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";  
                 }  
         }  
   
405          public void whereIs(Player p1, java.lang.String[] split) {          public void whereIs(Player p1, java.lang.String[] split) {
406                  if (split.length != 2) {                  if (split.length < 2 || split.length >3) {
407                          p1.sendMessage( Colors.Rose + "usage: /whereis <playername>");                          p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) <warpname>" );
408                          return;                          return;
409                  }                  }
                 Player p2 = etc.getServer().getPlayer(split[1]);  
410    
                 if (p2 == null) {  
                         p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );  
                         return;  
                 }  
411                  Location loc1 = p1.getLocation();                  Location loc1 = p1.getLocation();
412                  Location loc2 = p2.getLocation();                  Location loc2;
413                  //Location loc2 = new Location();                  String name2;
                 //loc2.x = loc2.y = loc2.z = 0;  
414    
415  //System.out.println("p1: " + loc1.x + "," + loc1.z );                  if (split[1].equals("home") ) {
416  //System.out.println("p2: " + loc2.x + "," + loc2.z );                          
417                            Warp home = etc.getDataSource().getHome( p1.getName() );
418                            if (home == null) {
419                                    p1.sendMessage(Colors.Rose + "you haven't set a home.");
420                                    return;
421                            }
422                            loc2 = home.Location;
423                            name2 = "Your home";
424                    } else if (split[1].equals("warp")) {
425                            if (split.length != 3) {
426                                    p1.sendMessage("you have to enter the name of the warp point");
427                                    return;
428                            }
429                            Warp warp = etc.getDataSource().getWarp( split[2] );
430                            if (warp == null) {
431                                    p1.sendMessage("Found now warp with name " + split[2]);
432                                    return;
433                            }
434                            loc2 = warp.Location;
435                            name2 = "Warppoint " + split[2];
436                            
437    
438                  double distX =   loc1.x - loc2.x  ;                  } else {
                 double distZ =   loc1.z - loc2.z  ;  
439    
440                  int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) );                          Player p2 = etc.getServer().getPlayer(split[1]);
 /*System.out.println("distX:" + distX );  
 System.out.println("distZ:" + distZ );  
441    
442  System.out.println(">> " + (distZ / distX ) );*/                          if (p2 == null) {
443                                    p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );
444                                    return;
445                            }
446                            
447                            loc2 = p2.getLocation();
448                            name2 = p2.getName();
449                    }
450    
451                  double angle = Math.toDegrees( Math.atan( distZ / distX ) );                  int dist = calcDistance(loc1, loc2);
452                  if (angle < 0.0)                  int angle = calcBearing(loc1, loc2);
                         angle += 90.0;  
453    
454    
                 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;  
                 }  
455    
456                  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 + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) );
457                  p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );                  p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );
458                  p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle  + " (" + getBearingStr( (int) angle) + ")" );                  p1.sendMessage( Colors.Yellow + "Bearing: " + angle  + " (" + getBearingStr( angle) + ")" );
459    
460                                    
461    

Legend:
Removed from v.1166  
changed lines
  Added in v.1195

  ViewVC Help
Powered by ViewVC 1.1.20