/[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 1197 - (hide annotations) (download)
Sun Nov 28 18:58:26 2010 UTC (13 years, 6 months ago) by torben
File size: 11919 byte(s)
add /la as a shortcut to /levelarea
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     }
18 torben 1166
19     @Override
20 torben 1195 public void enable() {
21 torben 1178 registerCommands();
22     }
23 torben 1166
24     @Override
25 torben 1195 public void disable() {
26 torben 1178 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 torben 1193
34 torben 1166
35     @Override
36     public void initialize() {
37     PluginLoader loader = etc.getLoader();
38 torben 1174 loader.addListener( PluginLoader.Hook.COMMAND, new HoerupUtilsPlugin(), this, PluginListener.Priority.MEDIUM );
39 torben 1193 loader.addListener( PluginLoader.Hook.LOGIN, new ConnectedUsers(), this, PluginListener.Priority.MEDIUM );
40 torben 1178
41 torben 1193 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 torben 1183
46 torben 1196 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 torben 1193
51    
52 torben 1178 registerCommands();
53 torben 1166 }
54    
55    
56 torben 1184
57 torben 1193 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 torben 1183 public class HoerupUtilsPlugin extends PluginListener {
128 torben 1178 final static String USAGE = "Usage: /setpos [x] [z] <height>";
129 torben 1176 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 torben 1166
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 torben 1176 return (srv.getBlockIdAt(x,y,z) == BLOCK_AIR && srv.getBlockIdAt(x,y+1,z) == BLOCK_AIR);
167 torben 1166 }
168    
169    
170     @Override
171     public boolean onCommand(Player player, java.lang.String[] split) {
172 torben 1196 if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) {
173 torben 1166 setPos(player, split);
174     return true;
175 torben 1170 } else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) {
176 torben 1166 whereIs(player, split);
177     return true;
178 torben 1197 } else if ( (split[0].equals("/levelarea") || split[0].equals("/la")) && player.canUseCommand("/levelarea")) {
179 torben 1176 if (validateLevelOrFill(player,split)) {
180     levelArea(player, split);
181     }
182 torben 1169 return true;
183 torben 1176 } else if (split[0].equals("/fillarea") && player.canUseCommand("/fillarea")) {
184     if (validateLevelOrFill(player,split)) {
185     fillArea(player, split);
186     }
187     return true;
188 torben 1175 } else if (split[0].equals("/setsurface") && player.canUseCommand("/setsurface")) {
189     setSurface(player,split);
190     return true;
191 torben 1166 } else {
192     return false;
193     }
194    
195     }
196    
197 torben 1177 int roundPos(double input) {
198     int result = (int) input;
199     if (input < 0.0) {
200     result--;
201     }
202    
203     return result;
204     }
205    
206 torben 1175 private void setSurface(Player player, String[] split) {
207     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} ;
208    
209     final int BLOCK_MAX = 86;
210    
211     if (split.length != 3) {
212 torben 1178 player.sendMessage("Usage /setsurface [radius] [blockID]");
213 torben 1175 return;
214     }
215    
216    
217     int radius;
218     try {
219     radius = Integer.parseInt(split[1]);
220     } catch (Exception e) {
221     player.sendMessage("setsurface: radius must be an integer");
222     return;
223     }
224    
225     if (radius > 20) {
226     player.sendMessage("setsurface: radius may not exceed 20");
227     return;
228     }
229    
230    
231     int blockid;
232     try {
233     blockid = Integer.parseInt(split[2]);
234     } catch (Exception e) {
235     player.sendMessage("setsurface: blockid must be an integer");
236     return;
237     }
238    
239     boolean validblock = false;
240     for (int i=0; i<valid_block_array.length; i++) {
241     if (valid_block_array[i] == blockid) {
242     validblock = true;
243     break;
244     }
245     }
246    
247     if ( !validblock ) {
248     player.sendMessage("setsurface: block now allowed");
249     return;
250     }
251    
252 torben 1177 int playerX = roundPos( player.getX() );
253 torben 1175 int playerY = (int) player.getY();
254 torben 1177 int playerZ = roundPos( player.getZ() );
255 torben 1175
256     if(playerY <= 2 && blockid != 7) {
257     player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
258     return;
259     }
260    
261     Server srv = etc.getServer();
262    
263     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
264     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
265     int y = getGroundLevel(x,z) - 1;
266     srv.setBlockAt(blockid, x, y, z);
267     }
268     }
269     }
270    
271 torben 1176 private boolean validateLevelOrFill(Player player, String[] split) {
272 torben 1183 if (split.length < 2 || split.length > 3) {
273 torben 1178 player.sendMessage("Usage: " + split[0] + " [radius]");
274 torben 1176 return false;
275 torben 1169 }
276    
277     int radius;
278     try {
279     radius = Integer.parseInt(split[1]);
280     } catch (Exception e) {
281 torben 1176 player.sendMessage(split[0] + ": radius must be an integer");
282     return false;
283 torben 1169 }
284 torben 1166
285 torben 1169 if (radius > 20) {
286 torben 1176 player.sendMessage(split[0] + ": radius may not exceed 20");
287     return false;
288 torben 1169 }
289 torben 1183
290     if (split.length == 3) {
291     try {
292     Integer.parseInt( split[2] );
293     } catch (Exception e) {
294     player.sendMessage(split[0] + ": radius must be an integer");
295     return false;
296     }
297     }
298 torben 1166
299 torben 1176 return true;
300     }
301 torben 1169
302 torben 1176 private void fillArea(Player player, String[] split) {
303     int radius = Integer.parseInt(split[1]);
304 torben 1183
305     int material = BLOCK_DIRT;
306     if (split.length == 3) {
307     material = Integer.parseInt( split[2] );
308     }
309 torben 1175
310 torben 1176 System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
311    
312    
313 torben 1177 int playerX = roundPos( player.getX() );
314 torben 1176 int playerY = (int) player.getY();
315 torben 1177 int playerZ = roundPos( player.getZ() );
316 torben 1176
317     Server srv = etc.getServer();
318    
319     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
320     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
321    
322     for (int y=getGroundLevel(x,z); y<playerY; y++) {
323 torben 1183 srv.setBlockAt(material, x, y, z);
324 torben 1176 }
325    
326     }
327    
328     }
329     }
330    
331     private void levelArea(Player player, String[] split) {
332    
333     int radius = Integer.parseInt(split[1]);
334    
335 torben 1169 System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
336    
337    
338 torben 1177 int playerX = roundPos( player.getX() );
339 torben 1169 int playerY = (int) player.getY();
340 torben 1177 int playerZ = roundPos( player.getZ() );
341 torben 1169
342     Server srv = etc.getServer();
343    
344     int count = 0;
345     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
346     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
347    
348     //for (int y=playerY; y<=playerY+radius; y++) {
349     for (int y=playerY; y<=128; y++) {
350     count++;
351 torben 1176 srv.setBlockAt(BLOCK_AIR, x, y, z);
352 torben 1169 }
353    
354     }
355    
356     }
357     }
358    
359    
360    
361 torben 1166 double roundToPlaces(double value, int places) {
362     double pow = Math.pow(10, places);
363     double temp = Math.round( value*pow );
364    
365     return temp / pow;
366     }
367    
368    
369     public void whereIs(Player p1, java.lang.String[] split) {
370 torben 1167 if (split.length < 2 || split.length >3) {
371 torben 1178 p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) <warpname>" );
372 torben 1166 return;
373     }
374    
375 torben 1167 Location loc1 = p1.getLocation();
376     Location loc2;
377     String name2;
378    
379     if (split[1].equals("home") ) {
380    
381     Warp home = etc.getDataSource().getHome( p1.getName() );
382     if (home == null) {
383     p1.sendMessage(Colors.Rose + "you haven't set a home.");
384     return;
385     }
386     loc2 = home.Location;
387     name2 = "Your home";
388     } else if (split[1].equals("warp")) {
389     if (split.length != 3) {
390     p1.sendMessage("you have to enter the name of the warp point");
391     return;
392     }
393     Warp warp = etc.getDataSource().getWarp( split[2] );
394     if (warp == null) {
395     p1.sendMessage("Found now warp with name " + split[2]);
396     return;
397     }
398     loc2 = warp.Location;
399     name2 = "Warppoint " + split[2];
400    
401    
402     } else {
403    
404     Player p2 = etc.getServer().getPlayer(split[1]);
405    
406     if (p2 == null) {
407     p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );
408     return;
409     }
410    
411     loc2 = p2.getLocation();
412     name2 = p2.getName();
413 torben 1166 }
414    
415 torben 1193 int dist = calcDistance(loc1, loc2);
416     int angle = calcBearing(loc1, loc2);
417 torben 1166
418    
419    
420 torben 1167 p1.sendMessage( Colors.Yellow + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) );
421 torben 1166 p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );
422 torben 1193 p1.sendMessage( Colors.Yellow + "Bearing: " + angle + " (" + getBearingStr( angle) + ")" );
423 torben 1166
424    
425    
426     }
427    
428    
429     public void setPos(Player player, java.lang.String[] split) {
430     int x;
431     int z;
432     int y;
433    
434     int userY = 0;
435     boolean hasY = false;
436    
437     if (split.length <3 || split.length > 4) {
438     player.sendMessage(USAGE);
439     return;
440     }
441    
442     try {
443     x = Integer.parseInt( split[1] );
444     z = Integer.parseInt( split[2] );
445     if (split.length == 4) {
446     hasY = true;
447     userY = Integer.parseInt( split[3] );
448     }
449     } catch (Exception e) {
450     player.sendMessage("/setpos error: non numeric argument");
451     return;
452     }
453    
454    
455    
456     if (hasY) {
457     if (isFree(x,userY,z)) {
458     y = userY;
459     } else {
460     player.sendMessage("/setpos: that position is not free");
461     return;
462     }
463     } else {
464     y = getGroundLevel(x,z);
465     }
466    
467     log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" );
468    
469    
470     Location newLocation = new Location();
471     newLocation.rotY = player.getPitch();
472     newLocation.rotX = player.getRotation();
473     newLocation.x = x + 0.5;
474     newLocation.z = z + 0.5;
475     newLocation.y = y;
476    
477     player.teleportTo(newLocation);
478     player.sendMessage("Whooooooosh");
479     }
480     }
481     }

  ViewVC Help
Powered by ViewVC 1.1.20