/[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 1195 - (show annotations) (download)
Sat Nov 20 08:42:06 2010 UTC (13 years, 6 months ago) by torben
File size: 12922 byte(s)
switch enable() and disable()
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 //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
22 public void enable() {
23 registerCommands();
24 }
25
26 @Override
27 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
38 public void initialize() {
39 PluginLoader loader = etc.getLoader();
40 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 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 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
155 //http://www.minecraftforum.net/viewtopic.php?f=35&t=14739
156 //Y is height
157
158 // air == blockID:0
159
160 private int getGroundLevel(int x, int z) {
161 Server srv = etc.getServer();
162
163 int level = srv.getHighestBlockY(x,z);
164
165 if (level == -1)
166 level = 128;
167 return level;
168
169 /*
170 int y;
171
172 for (y=128; y>=0 && srv.getBlockIdAt(x,y-1,z) == AIRBLOCK; y--)
173 ;
174
175 System.out.println("Groundlevel: " + y);
176 System.out.println("getHighestBlockY: " + srv.getHighestBlockY(x,z) );
177
178 return y;
179 */
180 }
181
182
183 private boolean isFree(int x, int y, int z) {
184 Server srv = etc.getServer();
185
186 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
204 public boolean onCommand(Player player, java.lang.String[] split) {
205 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);
210 return true;
211 } else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) {
212 whereIs(player, split);
213 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 {
228 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) {
398 double pow = Math.pow(10, places);
399 double temp = Math.round( value*pow );
400
401 return temp / pow;
402 }
403
404
405 public void whereIs(Player p1, java.lang.String[] split) {
406 if (split.length < 2 || split.length >3) {
407 p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) <warpname>" );
408 return;
409 }
410
411 Location loc1 = p1.getLocation();
412 Location loc2;
413 String name2;
414
415 if (split[1].equals("home") ) {
416
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 } else {
439
440 Player p2 = etc.getServer().getPlayer(split[1]);
441
442 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 int dist = calcDistance(loc1, loc2);
452 int angle = calcBearing(loc1, loc2);
453
454
455
456 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" );
458 p1.sendMessage( Colors.Yellow + "Bearing: " + angle + " (" + getBearingStr( angle) + ")" );
459
460
461
462 }
463
464
465 public void setPos(Player player, java.lang.String[] split) {
466 int x;
467 int z;
468 int y;
469
470 int userY = 0;
471 boolean hasY = false;
472
473 if (split.length <3 || split.length > 4) {
474 player.sendMessage(USAGE);
475 return;
476 }
477
478 try {
479 x = Integer.parseInt( split[1] );
480 z = Integer.parseInt( split[2] );
481 if (split.length == 4) {
482 hasY = true;
483 userY = Integer.parseInt( split[3] );
484 }
485 } catch (Exception e) {
486 player.sendMessage("/setpos error: non numeric argument");
487 return;
488 }
489
490
491
492 if (hasY) {
493 if (isFree(x,userY,z)) {
494 y = userY;
495 } else {
496 player.sendMessage("/setpos: that position is not free");
497 return;
498 }
499 } else {
500 y = getGroundLevel(x,z);
501 }
502
503 log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" );
504
505
506 Location newLocation = new Location();
507 newLocation.rotY = player.getPitch();
508 newLocation.rotX = player.getRotation();
509 newLocation.x = x + 0.5;
510 newLocation.z = z + 0.5;
511 newLocation.y = y;
512
513 player.teleportTo(newLocation);
514 player.sendMessage("Whooooooosh");
515 }
516 }
517 }

  ViewVC Help
Powered by ViewVC 1.1.20