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

  ViewVC Help
Powered by ViewVC 1.1.20