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 |
private void setSurface(Player player, String[] split) { |
95 |
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} ; |
96 |
|
97 |
final int BLOCK_MAX = 86; |
98 |
|
99 |
if (split.length != 3) { |
100 |
player.sendMessage("Usage /setsurface <radius> <blockID>"); |
101 |
return; |
102 |
} |
103 |
|
104 |
|
105 |
int radius; |
106 |
try { |
107 |
radius = Integer.parseInt(split[1]); |
108 |
} catch (Exception e) { |
109 |
player.sendMessage("setsurface: radius must be an integer"); |
110 |
return; |
111 |
} |
112 |
|
113 |
if (radius > 20) { |
114 |
player.sendMessage("setsurface: radius may not exceed 20"); |
115 |
return; |
116 |
} |
117 |
|
118 |
|
119 |
int blockid; |
120 |
try { |
121 |
blockid = Integer.parseInt(split[2]); |
122 |
} catch (Exception e) { |
123 |
player.sendMessage("setsurface: blockid must be an integer"); |
124 |
return; |
125 |
} |
126 |
|
127 |
boolean validblock = false; |
128 |
for (int i=0; i<valid_block_array.length; i++) { |
129 |
if (valid_block_array[i] == blockid) { |
130 |
validblock = true; |
131 |
break; |
132 |
} |
133 |
} |
134 |
|
135 |
if ( !validblock ) { |
136 |
player.sendMessage("setsurface: block now allowed"); |
137 |
return; |
138 |
} |
139 |
|
140 |
int playerX = (int) player.getX(); |
141 |
int playerY = (int) player.getY(); |
142 |
int playerZ = (int) player.getZ(); |
143 |
|
144 |
if(playerY <= 2 && blockid != 7) { |
145 |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
146 |
return; |
147 |
} |
148 |
|
149 |
Server srv = etc.getServer(); |
150 |
|
151 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
152 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
153 |
int y = getGroundLevel(x,z) - 1; |
154 |
srv.setBlockAt(blockid, x, y, z); |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
private boolean validateLevelOrFill(Player player, String[] split) { |
160 |
if (split.length != 2) { |
161 |
player.sendMessage("Usage: " + split[0] + " <radius>"); |
162 |
return false; |
163 |
} |
164 |
|
165 |
int radius; |
166 |
try { |
167 |
radius = Integer.parseInt(split[1]); |
168 |
} catch (Exception e) { |
169 |
player.sendMessage(split[0] + ": radius must be an integer"); |
170 |
return false; |
171 |
} |
172 |
|
173 |
if (radius > 20) { |
174 |
player.sendMessage(split[0] + ": radius may not exceed 20"); |
175 |
return false; |
176 |
} |
177 |
|
178 |
return true; |
179 |
} |
180 |
|
181 |
private void fillArea(Player player, String[] split) { |
182 |
int radius = Integer.parseInt(split[1]); |
183 |
|
184 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
185 |
|
186 |
|
187 |
int playerX = (int) player.getX(); |
188 |
int playerY = (int) player.getY(); |
189 |
int playerZ = (int) player.getZ(); |
190 |
|
191 |
Server srv = etc.getServer(); |
192 |
|
193 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
194 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
195 |
|
196 |
for (int y=getGroundLevel(x,z); y<playerY; y++) { |
197 |
srv.setBlockAt(BLOCK_DIRT, x, y, z); |
198 |
} |
199 |
|
200 |
} |
201 |
|
202 |
} |
203 |
} |
204 |
|
205 |
private void levelArea(Player player, String[] split) { |
206 |
|
207 |
int radius = Integer.parseInt(split[1]); |
208 |
|
209 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
210 |
|
211 |
|
212 |
int playerX = (int) player.getX(); |
213 |
int playerY = (int) player.getY(); |
214 |
int playerZ = (int) player.getZ(); |
215 |
|
216 |
Server srv = etc.getServer(); |
217 |
|
218 |
int count = 0; |
219 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
220 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
221 |
|
222 |
//for (int y=playerY; y<=playerY+radius; y++) { |
223 |
for (int y=playerY; y<=128; y++) { |
224 |
count++; |
225 |
srv.setBlockAt(BLOCK_AIR, x, y, z); |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
} |
231 |
} |
232 |
|
233 |
|
234 |
|
235 |
double roundToPlaces(double value, int places) { |
236 |
double pow = Math.pow(10, places); |
237 |
double temp = Math.round( value*pow ); |
238 |
|
239 |
return temp / pow; |
240 |
} |
241 |
|
242 |
|
243 |
private String getBearingStr(int angle) { |
244 |
if (angle < 22) { |
245 |
return "N"; |
246 |
} else if (angle < 67) { |
247 |
return "NE"; |
248 |
} else if (angle < 112) { |
249 |
return "E"; |
250 |
} else if (angle < 157) { |
251 |
return "SE"; |
252 |
} else if (angle < 202) { |
253 |
return "S"; |
254 |
} else if (angle < 257) { |
255 |
return "SW"; |
256 |
} else if (angle < 292) { |
257 |
return "W"; |
258 |
} else if (angle < 337) { |
259 |
return "NW"; |
260 |
} else { |
261 |
return "N"; |
262 |
} |
263 |
} |
264 |
|
265 |
public void whereIs(Player p1, java.lang.String[] split) { |
266 |
if (split.length < 2 || split.length >3) { |
267 |
p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) [warpname]" ); |
268 |
return; |
269 |
} |
270 |
|
271 |
Location loc1 = p1.getLocation(); |
272 |
Location loc2; |
273 |
String name2; |
274 |
|
275 |
if (split[1].equals("home") ) { |
276 |
|
277 |
Warp home = etc.getDataSource().getHome( p1.getName() ); |
278 |
if (home == null) { |
279 |
p1.sendMessage(Colors.Rose + "you haven't set a home."); |
280 |
return; |
281 |
} |
282 |
loc2 = home.Location; |
283 |
name2 = "Your home"; |
284 |
} else if (split[1].equals("warp")) { |
285 |
if (split.length != 3) { |
286 |
p1.sendMessage("you have to enter the name of the warp point"); |
287 |
return; |
288 |
} |
289 |
Warp warp = etc.getDataSource().getWarp( split[2] ); |
290 |
if (warp == null) { |
291 |
p1.sendMessage("Found now warp with name " + split[2]); |
292 |
return; |
293 |
} |
294 |
loc2 = warp.Location; |
295 |
name2 = "Warppoint " + split[2]; |
296 |
|
297 |
|
298 |
} else { |
299 |
|
300 |
Player p2 = etc.getServer().getPlayer(split[1]); |
301 |
|
302 |
if (p2 == null) { |
303 |
p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] ); |
304 |
return; |
305 |
} |
306 |
|
307 |
loc2 = p2.getLocation(); |
308 |
name2 = p2.getName(); |
309 |
} |
310 |
//Location loc2 = new Location(); |
311 |
//loc2.x = loc2.y = loc2.z = 0; |
312 |
|
313 |
//System.out.println("p1: " + loc1.x + "," + loc1.z ); |
314 |
//System.out.println("p2: " + loc2.x + "," + loc2.z ); |
315 |
|
316 |
double distX = loc1.x - loc2.x ; |
317 |
double distZ = loc1.z - loc2.z ; |
318 |
|
319 |
int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) ); |
320 |
/*System.out.println("distX:" + distX ); |
321 |
System.out.println("distZ:" + distZ ); |
322 |
|
323 |
System.out.println(">> " + (distZ / distX ) );*/ |
324 |
|
325 |
double angle = Math.toDegrees( Math.atan( distZ / distX ) ); |
326 |
if (angle < 0.0) |
327 |
angle += 90.0; |
328 |
|
329 |
|
330 |
if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees |
331 |
//do nothing |
332 |
} else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees |
333 |
angle += 90.0; |
334 |
} else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees |
335 |
angle += 180.0; |
336 |
} else { |
337 |
angle += 270.0; |
338 |
} |
339 |
|
340 |
p1.sendMessage( Colors.Yellow + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) ); |
341 |
p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" ); |
342 |
p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" ); |
343 |
|
344 |
|
345 |
|
346 |
} |
347 |
|
348 |
|
349 |
public void setPos(Player player, java.lang.String[] split) { |
350 |
int x; |
351 |
int z; |
352 |
int y; |
353 |
|
354 |
int userY = 0; |
355 |
boolean hasY = false; |
356 |
|
357 |
if (split.length <3 || split.length > 4) { |
358 |
player.sendMessage(USAGE); |
359 |
return; |
360 |
} |
361 |
|
362 |
try { |
363 |
x = Integer.parseInt( split[1] ); |
364 |
z = Integer.parseInt( split[2] ); |
365 |
if (split.length == 4) { |
366 |
hasY = true; |
367 |
userY = Integer.parseInt( split[3] ); |
368 |
} |
369 |
} catch (Exception e) { |
370 |
player.sendMessage("/setpos error: non numeric argument"); |
371 |
return; |
372 |
} |
373 |
|
374 |
|
375 |
|
376 |
if (hasY) { |
377 |
if (isFree(x,userY,z)) { |
378 |
y = userY; |
379 |
} else { |
380 |
player.sendMessage("/setpos: that position is not free"); |
381 |
return; |
382 |
} |
383 |
} else { |
384 |
y = getGroundLevel(x,z); |
385 |
} |
386 |
|
387 |
log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" ); |
388 |
|
389 |
|
390 |
Location newLocation = new Location(); |
391 |
newLocation.rotY = player.getPitch(); |
392 |
newLocation.rotX = player.getRotation(); |
393 |
newLocation.x = x + 0.5; |
394 |
newLocation.z = z + 0.5; |
395 |
newLocation.y = y; |
396 |
|
397 |
player.teleportTo(newLocation); |
398 |
player.sendMessage("Whooooooosh"); |
399 |
} |
400 |
} |
401 |
} |