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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20