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

  ViewVC Help
Powered by ViewVC 1.1.20