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

  ViewVC Help
Powered by ViewVC 1.1.20