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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1201 - (hide annotations) (download)
Sat Dec 4 11:38:36 2010 UTC (13 years, 6 months ago) by torben
File size: 13621 byte(s)
replay latest construction command with //
1 torben 1166
2     import java.util.logging.*;
3 torben 1184 import java.util.*;
4 torben 1166
5 torben 1174 public class HoerupUtils extends Plugin {
6 torben 1166
7 torben 1174 final static Logger log = Logger.getLogger("HoerupUtils");
8 torben 1166
9 torben 1183
10 torben 1178 private void registerCommands() {
11     etc e = etc.getInstance();
12     e.addCommand("/setpos", "[x] [z] <height> - Teleports you to the given coordinates");
13     e.addCommand("/whereis", "[player] - Tells you the position of another player");
14     e.addCommand("/fillarea", "");
15     e.addCommand("/levelarea", "");
16     e.addCommand("/setsurface", "");
17 torben 1199 e.addCommand("/slopearea", "");
18     e.addCommand("/admindestroy","");
19 torben 1178 }
20 torben 1166
21     @Override
22 torben 1195 public void enable() {
23 torben 1178 registerCommands();
24     }
25 torben 1166
26     @Override
27 torben 1195 public void disable() {
28 torben 1178 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 torben 1199 e.removeCommand("/slopearea");
35     e.removeCommand("/admindestroy");
36 torben 1178 }
37 torben 1193
38 torben 1166
39     @Override
40     public void initialize() {
41     PluginLoader loader = etc.getLoader();
42 torben 1174 loader.addListener( PluginLoader.Hook.COMMAND, new HoerupUtilsPlugin(), this, PluginListener.Priority.MEDIUM );
43 torben 1193 loader.addListener( PluginLoader.Hook.LOGIN, new ConnectedUsers(), this, PluginListener.Priority.MEDIUM );
44 torben 1178
45 torben 1193 Jail j = new Jail();
46     loader.addListener( PluginLoader.Hook.TELEPORT, j, this, PluginListener.Priority.MEDIUM);
47     loader.addListener( PluginLoader.Hook.COMMAND, j, this, PluginListener.Priority.MEDIUM );
48     loader.addListener( PluginLoader.Hook.PLAYER_MOVE, j, this, PluginListener.Priority.MEDIUM );
49 torben 1183
50 torben 1196 AdminDestroy adm = new AdminDestroy();
51     loader.addListener( PluginLoader.Hook.COMMAND, adm, this, PluginListener.Priority.MEDIUM);
52     loader.addListener( PluginLoader.Hook.BLOCK_DESTROYED, adm, this, PluginListener.Priority.MEDIUM );
53     loader.addListener( PluginLoader.Hook.DISCONNECT, adm, this, PluginListener.Priority.MEDIUM );
54 torben 1193
55    
56 torben 1178 registerCommands();
57 torben 1166 }
58    
59    
60 torben 1184
61 torben 1193 class ConnectedUsers extends PluginListener {
62     public void onLogin(Player player) {
63     List<Player> players = etc.getServer().getPlayerList();
64     int count = players.size();
65    
66     StringBuilder sb = new StringBuilder();
67     for (int i=0; i<count; i++) {
68     if (i>0)
69     sb.append(" ");
70     sb.append( players.get(i).getName() );
71     }
72    
73     player.sendMessage(Colors.Red + "Connected users " + count + ": " + Colors.White + sb.toString() );
74     }
75     }
76    
77    
78     public static String getBearingStr(int angle) {
79     if (angle < 22) {
80     return "N";
81     } else if (angle < 67) {
82     return "NE";
83     } else if (angle < 112) {
84     return "E";
85     } else if (angle < 157) {
86     return "SE";
87     } else if (angle < 202) {
88     return "S";
89     } else if (angle < 257) {
90     return "SW";
91     } else if (angle < 292) {
92     return "W";
93     } else if (angle < 337) {
94     return "NW";
95     } else {
96     return "N";
97     }
98     }
99    
100     public static int calcDistance(Location loc1, Location loc2) {
101     double distX = loc1.x - loc2.x ;
102     double distZ = loc1.z - loc2.z ;
103    
104     int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) );
105    
106     return dist;
107     }
108    
109     public static int calcBearing(Location loc1, Location loc2) {
110     double distX = loc1.x - loc2.x ;
111     double distZ = loc1.z - loc2.z ;
112    
113     double angle = Math.toDegrees( Math.atan( distZ / distX ) );
114     if (angle < 0.0)
115     angle += 90.0;
116    
117    
118     if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees
119     //do nothing
120     } else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees
121     angle += 90.0;
122     } else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees
123     angle += 180.0;
124     } else {
125     angle += 270.0;
126     }
127    
128     return (int) angle;
129     }
130    
131 torben 1183 public class HoerupUtilsPlugin extends PluginListener {
132 torben 1178 final static String USAGE = "Usage: /setpos [x] [z] <height>";
133 torben 1176 final static int BLOCK_AIR = 0; //block id = 0 is air
134     final static int BLOCK_GRASS = 2;
135     final static int BLOCK_DIRT = 3;
136 torben 1166
137 torben 1201 private HashMap<String, String[]> commands = new HashMap<String,String[]>();
138    
139 torben 1166
140    
141     //http://www.minecraftforum.net/viewtopic.php?f=35&t=14739
142     //Y is height
143    
144     // air == blockID:0
145    
146     private int getGroundLevel(int x, int z) {
147     Server srv = etc.getServer();
148    
149     int level = srv.getHighestBlockY(x,z);
150    
151     if (level == -1)
152     level = 128;
153     return level;
154    
155     /*
156     int y;
157    
158     for (y=128; y>=0 && srv.getBlockIdAt(x,y-1,z) == AIRBLOCK; y--)
159     ;
160    
161     System.out.println("Groundlevel: " + y);
162     System.out.println("getHighestBlockY: " + srv.getHighestBlockY(x,z) );
163    
164     return y;
165     */
166     }
167    
168    
169     private boolean isFree(int x, int y, int z) {
170     Server srv = etc.getServer();
171    
172 torben 1176 return (srv.getBlockIdAt(x,y,z) == BLOCK_AIR && srv.getBlockIdAt(x,y+1,z) == BLOCK_AIR);
173 torben 1166 }
174    
175 torben 1198 public void slopeArea(Player player, java.lang.String[] split) {
176     int radius = Integer.parseInt(split[1]);
177 torben 1166
178 torben 1198 System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius);
179    
180    
181     int playerX = roundPos( player.getX() );
182     int playerY = (int) player.getY();
183     int playerZ = roundPos( player.getZ() );
184    
185     Server srv = etc.getServer();
186    
187     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
188     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
189    
190    
191     int xdist = Math.abs(playerX-x);
192     int zdist = Math.abs(playerZ-z);
193    
194     int dist = Math.max(xdist,zdist);
195    
196     //for (int y=playerY; y<=playerY+radius; y++) {
197     for (int y=(playerY+dist); y<=128; y++) {
198     srv.setBlockAt(BLOCK_AIR, x, y, z);
199     }
200    
201     }
202    
203     }
204     }
205    
206 torben 1166 @Override
207     public boolean onCommand(Player player, java.lang.String[] split) {
208 torben 1198 if (! player.canUseCommand(split[0]) ) {
209     return false;
210     }
211 torben 1201
212     if ( split[0].equals("/levelarea") || split[0].equals("/la") || split[0].equals("/slopearea") || split[0].equals("/fillarea") || split[0].equals("/setsurface") ) {
213     commands.put(player.getName(), split);
214     }
215 torben 1198
216 torben 1201 if ( split[0].equals("//") ) {
217     String cmd[] = commands.get(player.getName() );
218     if (cmd != null) {
219     onCommand(player, commands.get(player.getName() ) );
220     } else {
221     player.sendMessage("//: no recorded command found");
222     }
223     return true;
224     } else if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) {
225 torben 1166 setPos(player, split);
226     return true;
227 torben 1170 } else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) {
228 torben 1166 whereIs(player, split);
229     return true;
230 torben 1197 } else if ( (split[0].equals("/levelarea") || split[0].equals("/la")) && player.canUseCommand("/levelarea")) {
231 torben 1176 if (validateLevelOrFill(player,split)) {
232     levelArea(player, split);
233     }
234 torben 1169 return true;
235 torben 1198 } else if (split[0].equals("/slopearea") ) {
236     if (validateLevelOrFill(player,split)) {
237     slopeArea(player,split);
238     }
239     return true;
240 torben 1176 } else if (split[0].equals("/fillarea") && player.canUseCommand("/fillarea")) {
241     if (validateLevelOrFill(player,split)) {
242     fillArea(player, split);
243     }
244     return true;
245 torben 1175 } else if (split[0].equals("/setsurface") && player.canUseCommand("/setsurface")) {
246     setSurface(player,split);
247     return true;
248 torben 1166 } else {
249     return false;
250     }
251    
252     }
253    
254 torben 1177 int roundPos(double input) {
255     int result = (int) input;
256     if (input < 0.0) {
257     result--;
258     }
259    
260     return result;
261     }
262    
263 torben 1175 private void setSurface(Player player, String[] split) {
264     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} ;
265    
266     final int BLOCK_MAX = 86;
267    
268     if (split.length != 3) {
269 torben 1178 player.sendMessage("Usage /setsurface [radius] [blockID]");
270 torben 1175 return;
271     }
272    
273    
274     int radius;
275     try {
276     radius = Integer.parseInt(split[1]);
277     } catch (Exception e) {
278     player.sendMessage("setsurface: radius must be an integer");
279     return;
280     }
281    
282     if (radius > 20) {
283     player.sendMessage("setsurface: radius may not exceed 20");
284     return;
285     }
286    
287    
288     int blockid;
289     try {
290     blockid = Integer.parseInt(split[2]);
291     } catch (Exception e) {
292     player.sendMessage("setsurface: blockid must be an integer");
293     return;
294     }
295    
296     boolean validblock = false;
297     for (int i=0; i<valid_block_array.length; i++) {
298     if (valid_block_array[i] == blockid) {
299     validblock = true;
300     break;
301     }
302     }
303    
304     if ( !validblock ) {
305     player.sendMessage("setsurface: block now allowed");
306     return;
307     }
308    
309 torben 1177 int playerX = roundPos( player.getX() );
310 torben 1175 int playerY = (int) player.getY();
311 torben 1177 int playerZ = roundPos( player.getZ() );
312 torben 1175
313     if(playerY <= 2 && blockid != 7) {
314     player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
315     return;
316     }
317    
318     Server srv = etc.getServer();
319    
320     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
321     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
322     int y = getGroundLevel(x,z) - 1;
323     srv.setBlockAt(blockid, x, y, z);
324     }
325     }
326     }
327    
328 torben 1176 private boolean validateLevelOrFill(Player player, String[] split) {
329 torben 1183 if (split.length < 2 || split.length > 3) {
330 torben 1178 player.sendMessage("Usage: " + split[0] + " [radius]");
331 torben 1176 return false;
332 torben 1169 }
333    
334     int radius;
335     try {
336     radius = Integer.parseInt(split[1]);
337     } catch (Exception e) {
338 torben 1176 player.sendMessage(split[0] + ": radius must be an integer");
339     return false;
340 torben 1169 }
341 torben 1166
342 torben 1169 if (radius > 20) {
343 torben 1176 player.sendMessage(split[0] + ": radius may not exceed 20");
344     return false;
345 torben 1169 }
346 torben 1183
347     if (split.length == 3) {
348     try {
349     Integer.parseInt( split[2] );
350     } catch (Exception e) {
351     player.sendMessage(split[0] + ": radius must be an integer");
352     return false;
353     }
354     }
355 torben 1166
356 torben 1176 return true;
357     }
358 torben 1169
359 torben 1176 private void fillArea(Player player, String[] split) {
360     int radius = Integer.parseInt(split[1]);
361 torben 1183
362     int material = BLOCK_DIRT;
363     if (split.length == 3) {
364     material = Integer.parseInt( split[2] );
365     }
366 torben 1175
367 torben 1176 System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
368    
369    
370 torben 1177 int playerX = roundPos( player.getX() );
371 torben 1176 int playerY = (int) player.getY();
372 torben 1177 int playerZ = roundPos( player.getZ() );
373 torben 1176
374     Server srv = etc.getServer();
375    
376     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
377     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
378    
379     for (int y=getGroundLevel(x,z); y<playerY; y++) {
380 torben 1183 srv.setBlockAt(material, x, y, z);
381 torben 1176 }
382    
383     }
384    
385     }
386     }
387    
388     private void levelArea(Player player, String[] split) {
389    
390     int radius = Integer.parseInt(split[1]);
391    
392 torben 1169 System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
393    
394    
395 torben 1177 int playerX = roundPos( player.getX() );
396 torben 1169 int playerY = (int) player.getY();
397 torben 1177 int playerZ = roundPos( player.getZ() );
398 torben 1169
399     Server srv = etc.getServer();
400    
401     int count = 0;
402     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
403     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
404    
405     //for (int y=playerY; y<=playerY+radius; y++) {
406     for (int y=playerY; y<=128; y++) {
407     count++;
408 torben 1176 srv.setBlockAt(BLOCK_AIR, x, y, z);
409 torben 1169 }
410    
411     }
412    
413     }
414     }
415    
416    
417    
418 torben 1166 double roundToPlaces(double value, int places) {
419     double pow = Math.pow(10, places);
420     double temp = Math.round( value*pow );
421    
422     return temp / pow;
423     }
424    
425    
426     public void whereIs(Player p1, java.lang.String[] split) {
427 torben 1167 if (split.length < 2 || split.length >3) {
428 torben 1178 p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) <warpname>" );
429 torben 1166 return;
430     }
431    
432 torben 1167 Location loc1 = p1.getLocation();
433     Location loc2;
434     String name2;
435    
436     if (split[1].equals("home") ) {
437    
438     Warp home = etc.getDataSource().getHome( p1.getName() );
439     if (home == null) {
440     p1.sendMessage(Colors.Rose + "you haven't set a home.");
441     return;
442     }
443     loc2 = home.Location;
444     name2 = "Your home";
445     } else if (split[1].equals("warp")) {
446     if (split.length != 3) {
447     p1.sendMessage("you have to enter the name of the warp point");
448     return;
449     }
450     Warp warp = etc.getDataSource().getWarp( split[2] );
451     if (warp == null) {
452     p1.sendMessage("Found now warp with name " + split[2]);
453     return;
454     }
455     loc2 = warp.Location;
456     name2 = "Warppoint " + split[2];
457    
458    
459     } else {
460    
461     Player p2 = etc.getServer().getPlayer(split[1]);
462    
463     if (p2 == null) {
464     p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );
465     return;
466     }
467    
468     loc2 = p2.getLocation();
469     name2 = p2.getName();
470 torben 1166 }
471    
472 torben 1193 int dist = calcDistance(loc1, loc2);
473     int angle = calcBearing(loc1, loc2);
474 torben 1166
475    
476    
477 torben 1167 p1.sendMessage( Colors.Yellow + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) );
478 torben 1166 p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );
479 torben 1193 p1.sendMessage( Colors.Yellow + "Bearing: " + angle + " (" + getBearingStr( angle) + ")" );
480 torben 1166
481    
482    
483     }
484    
485    
486     public void setPos(Player player, java.lang.String[] split) {
487     int x;
488     int z;
489     int y;
490    
491     int userY = 0;
492     boolean hasY = false;
493    
494     if (split.length <3 || split.length > 4) {
495     player.sendMessage(USAGE);
496     return;
497     }
498    
499     try {
500     x = Integer.parseInt( split[1] );
501     z = Integer.parseInt( split[2] );
502     if (split.length == 4) {
503     hasY = true;
504     userY = Integer.parseInt( split[3] );
505     }
506     } catch (Exception e) {
507     player.sendMessage("/setpos error: non numeric argument");
508     return;
509     }
510    
511    
512    
513     if (hasY) {
514     if (isFree(x,userY,z)) {
515     y = userY;
516     } else {
517     player.sendMessage("/setpos: that position is not free");
518     return;
519     }
520     } else {
521     y = getGroundLevel(x,z);
522     }
523    
524     log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" );
525    
526    
527     Location newLocation = new Location();
528     newLocation.rotY = player.getPitch();
529     newLocation.rotX = player.getRotation();
530     newLocation.x = x + 0.5;
531     newLocation.z = z + 0.5;
532     newLocation.y = y;
533    
534     player.teleportTo(newLocation);
535     player.sendMessage("Whooooooosh");
536     }
537     }
538     }

  ViewVC Help
Powered by ViewVC 1.1.20