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 AIRBLOCK = 0; //block id = 0 is air |
26 |
|
27 |
|
28 |
|
29 |
//http://www.minecraftforum.net/viewtopic.php?f=35&t=14739 |
30 |
//Y is height |
31 |
|
32 |
// air == blockID:0 |
33 |
|
34 |
private int getGroundLevel(int x, int z) { |
35 |
Server srv = etc.getServer(); |
36 |
|
37 |
int level = srv.getHighestBlockY(x,z); |
38 |
|
39 |
if (level == -1) |
40 |
level = 128; |
41 |
return level; |
42 |
|
43 |
/* |
44 |
int y; |
45 |
|
46 |
for (y=128; y>=0 && srv.getBlockIdAt(x,y-1,z) == AIRBLOCK; y--) |
47 |
; |
48 |
|
49 |
System.out.println("Groundlevel: " + y); |
50 |
System.out.println("getHighestBlockY: " + srv.getHighestBlockY(x,z) ); |
51 |
|
52 |
return y; |
53 |
*/ |
54 |
} |
55 |
|
56 |
|
57 |
private boolean isFree(int x, int y, int z) { |
58 |
Server srv = etc.getServer(); |
59 |
|
60 |
return (srv.getBlockIdAt(x,y,z) == AIRBLOCK && srv.getBlockIdAt(x,y+1,z) == AIRBLOCK); |
61 |
} |
62 |
|
63 |
|
64 |
|
65 |
@Override |
66 |
public boolean onCommand(Player player, java.lang.String[] split) { |
67 |
if( split[0].equals("/setpos") && player.canUseCommand("/setpos") ) { |
68 |
setPos(player, split); |
69 |
return true; |
70 |
} else if ( split[0].equals("/whereis" ) && player.canUseCommand("/whereis")) { |
71 |
whereIs(player, split); |
72 |
return true; |
73 |
} else if (split[0].equals("/levelarea") && player.canUseCommand("/levelarea")) { |
74 |
levelArea(player, split); |
75 |
return true; |
76 |
} else { |
77 |
return false; |
78 |
} |
79 |
|
80 |
} |
81 |
|
82 |
private void levelArea(Player player, String[] split) { |
83 |
if (split.length != 2) { |
84 |
player.sendMessage("Usage: /levelarea <radius>"); |
85 |
return; |
86 |
} |
87 |
|
88 |
int radius; |
89 |
try { |
90 |
radius = Integer.parseInt(split[1]); |
91 |
} catch (Exception e) { |
92 |
player.sendMessage("levelarea: radius must be an integer"); |
93 |
return; |
94 |
} |
95 |
|
96 |
if (radius > 20) { |
97 |
player.sendMessage("levelarea: radius may not exceed 20"); |
98 |
return; |
99 |
} |
100 |
|
101 |
|
102 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
103 |
|
104 |
|
105 |
int playerX = (int) player.getX(); |
106 |
int playerY = (int) player.getY(); |
107 |
int playerZ = (int) player.getZ(); |
108 |
|
109 |
Server srv = etc.getServer(); |
110 |
|
111 |
int count = 0; |
112 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
113 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
114 |
|
115 |
//for (int y=playerY; y<=playerY+radius; y++) { |
116 |
for (int y=playerY; y<=128; y++) { |
117 |
count++; |
118 |
srv.setBlockAt(0, x, y, z); |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
} |
124 |
} |
125 |
|
126 |
|
127 |
|
128 |
double roundToPlaces(double value, int places) { |
129 |
double pow = Math.pow(10, places); |
130 |
double temp = Math.round( value*pow ); |
131 |
|
132 |
return temp / pow; |
133 |
} |
134 |
|
135 |
|
136 |
private String getBearingStr(int angle) { |
137 |
if (angle < 22) { |
138 |
return "N"; |
139 |
} else if (angle < 67) { |
140 |
return "NE"; |
141 |
} else if (angle < 112) { |
142 |
return "E"; |
143 |
} else if (angle < 157) { |
144 |
return "SE"; |
145 |
} else if (angle < 202) { |
146 |
return "S"; |
147 |
} else if (angle < 257) { |
148 |
return "SW"; |
149 |
} else if (angle < 292) { |
150 |
return "W"; |
151 |
} else if (angle < 337) { |
152 |
return "NW"; |
153 |
} else { |
154 |
return "N"; |
155 |
} |
156 |
} |
157 |
|
158 |
public void whereIs(Player p1, java.lang.String[] split) { |
159 |
if (split.length < 2 || split.length >3) { |
160 |
p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) [warpname]" ); |
161 |
return; |
162 |
} |
163 |
|
164 |
Location loc1 = p1.getLocation(); |
165 |
Location loc2; |
166 |
String name2; |
167 |
|
168 |
if (split[1].equals("home") ) { |
169 |
|
170 |
Warp home = etc.getDataSource().getHome( p1.getName() ); |
171 |
if (home == null) { |
172 |
p1.sendMessage(Colors.Rose + "you haven't set a home."); |
173 |
return; |
174 |
} |
175 |
loc2 = home.Location; |
176 |
name2 = "Your home"; |
177 |
} else if (split[1].equals("warp")) { |
178 |
if (split.length != 3) { |
179 |
p1.sendMessage("you have to enter the name of the warp point"); |
180 |
return; |
181 |
} |
182 |
Warp warp = etc.getDataSource().getWarp( split[2] ); |
183 |
if (warp == null) { |
184 |
p1.sendMessage("Found now warp with name " + split[2]); |
185 |
return; |
186 |
} |
187 |
loc2 = warp.Location; |
188 |
name2 = "Warppoint " + split[2]; |
189 |
|
190 |
|
191 |
} else { |
192 |
|
193 |
Player p2 = etc.getServer().getPlayer(split[1]); |
194 |
|
195 |
if (p2 == null) { |
196 |
p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] ); |
197 |
return; |
198 |
} |
199 |
|
200 |
loc2 = p2.getLocation(); |
201 |
name2 = p2.getName(); |
202 |
} |
203 |
//Location loc2 = new Location(); |
204 |
//loc2.x = loc2.y = loc2.z = 0; |
205 |
|
206 |
//System.out.println("p1: " + loc1.x + "," + loc1.z ); |
207 |
//System.out.println("p2: " + loc2.x + "," + loc2.z ); |
208 |
|
209 |
double distX = loc1.x - loc2.x ; |
210 |
double distZ = loc1.z - loc2.z ; |
211 |
|
212 |
int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) ); |
213 |
/*System.out.println("distX:" + distX ); |
214 |
System.out.println("distZ:" + distZ ); |
215 |
|
216 |
System.out.println(">> " + (distZ / distX ) );*/ |
217 |
|
218 |
double angle = Math.toDegrees( Math.atan( distZ / distX ) ); |
219 |
if (angle < 0.0) |
220 |
angle += 90.0; |
221 |
|
222 |
|
223 |
if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees |
224 |
//do nothing |
225 |
} else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees |
226 |
angle += 90.0; |
227 |
} else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees |
228 |
angle += 180.0; |
229 |
} else { |
230 |
angle += 270.0; |
231 |
} |
232 |
|
233 |
p1.sendMessage( Colors.Yellow + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) ); |
234 |
p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" ); |
235 |
p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" ); |
236 |
|
237 |
|
238 |
|
239 |
} |
240 |
|
241 |
|
242 |
public void setPos(Player player, java.lang.String[] split) { |
243 |
int x; |
244 |
int z; |
245 |
int y; |
246 |
|
247 |
int userY = 0; |
248 |
boolean hasY = false; |
249 |
|
250 |
if (split.length <3 || split.length > 4) { |
251 |
player.sendMessage(USAGE); |
252 |
return; |
253 |
} |
254 |
|
255 |
try { |
256 |
x = Integer.parseInt( split[1] ); |
257 |
z = Integer.parseInt( split[2] ); |
258 |
if (split.length == 4) { |
259 |
hasY = true; |
260 |
userY = Integer.parseInt( split[3] ); |
261 |
} |
262 |
} catch (Exception e) { |
263 |
player.sendMessage("/setpos error: non numeric argument"); |
264 |
return; |
265 |
} |
266 |
|
267 |
|
268 |
|
269 |
if (hasY) { |
270 |
if (isFree(x,userY,z)) { |
271 |
y = userY; |
272 |
} else { |
273 |
player.sendMessage("/setpos: that position is not free"); |
274 |
return; |
275 |
} |
276 |
} else { |
277 |
y = getGroundLevel(x,z); |
278 |
} |
279 |
|
280 |
log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" ); |
281 |
|
282 |
|
283 |
Location newLocation = new Location(); |
284 |
newLocation.rotY = player.getPitch(); |
285 |
newLocation.rotX = player.getRotation(); |
286 |
newLocation.x = x + 0.5; |
287 |
newLocation.z = z + 0.5; |
288 |
newLocation.y = y; |
289 |
|
290 |
player.teleportTo(newLocation); |
291 |
player.sendMessage("Whooooooosh"); |
292 |
} |
293 |
} |
294 |
} |