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

  ViewVC Help
Powered by ViewVC 1.1.20