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

  ViewVC Help
Powered by ViewVC 1.1.20