1 |
package dk.thoerup.bukkit.hoeruputils.creative; |
2 |
|
3 |
import org.bukkit.ChatColor; |
4 |
import org.bukkit.Location; |
5 |
import org.bukkit.Material; |
6 |
import org.bukkit.World; |
7 |
import org.bukkit.command.Command; |
8 |
import org.bukkit.command.CommandExecutor; |
9 |
import org.bukkit.command.CommandSender; |
10 |
import org.bukkit.entity.Player; |
11 |
|
12 |
import java.util.Arrays; |
13 |
import java.util.HashMap; |
14 |
import java.util.HashSet; |
15 |
import java.util.Set; |
16 |
|
17 |
public class GeneralContractorCommands implements CommandExecutor{ |
18 |
|
19 |
class StoredCommand { |
20 |
public StoredCommand(String l, String a[]) { |
21 |
this.label = l; |
22 |
this.args = a; |
23 |
} |
24 |
public String label; |
25 |
public String args[]; |
26 |
} |
27 |
|
28 |
final static int Y_MAX = 255; |
29 |
final Material valid_block_array[] = { |
30 |
Material.STONE, |
31 |
Material.GRASS, |
32 |
Material.DIRT, |
33 |
Material.COBBLESTONE, |
34 |
Material.WOOD, |
35 |
Material.BEDROCK, |
36 |
Material.WATER, |
37 |
Material.SAND, |
38 |
Material.GRAVEL, |
39 |
Material.GOLD_ORE, |
40 |
Material.IRON_ORE, |
41 |
Material.COAL_ORE, |
42 |
Material.LOG, |
43 |
Material.LEAVES, |
44 |
Material.GLASS, |
45 |
Material.LAPIS_ORE, |
46 |
Material.LAPIS_BLOCK, |
47 |
Material.SANDSTONE, |
48 |
Material.WOOL, |
49 |
Material.GOLD_BLOCK, |
50 |
Material.IRON_BLOCK, |
51 |
Material.BRICK, |
52 |
Material.MOSSY_COBBLESTONE, |
53 |
Material.OBSIDIAN, |
54 |
Material.DIAMOND_ORE, |
55 |
Material.DIAMOND_BLOCK, |
56 |
Material.ICE, |
57 |
Material.SNOW, |
58 |
Material.CLAY, |
59 |
Material.NETHERRACK, |
60 |
Material.SOUL_SAND |
61 |
}; |
62 |
//4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; |
63 |
|
64 |
final Set<Material> valid_materials = new HashSet<Material>(); |
65 |
|
66 |
HashMap<String,StoredCommand> commands = new HashMap<String,StoredCommand>(); |
67 |
|
68 |
|
69 |
public GeneralContractorCommands() { |
70 |
Arrays.sort(valid_block_array); |
71 |
|
72 |
for (Material mat : valid_block_array) { |
73 |
valid_materials.add( mat ); |
74 |
} |
75 |
} |
76 |
|
77 |
//@Override |
78 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
79 |
|
80 |
if (! (sender instanceof Player) ) { |
81 |
return false; |
82 |
} |
83 |
Player player = (Player) sender; |
84 |
|
85 |
Location loc = player.getLocation(); |
86 |
|
87 |
if ( ! loc.getWorld().getName().equals("creative")) { |
88 |
player.sendMessage( ChatColor.RED + "This command may only be used in creative world" ); |
89 |
return true; |
90 |
} |
91 |
|
92 |
|
93 |
if ( command.getLabel().equals("replay") ) { |
94 |
StoredCommand cmd = commands.get(player.getName() ); |
95 |
if (cmd != null) { |
96 |
label = cmd.label; |
97 |
args = cmd.args; |
98 |
} |
99 |
} else { |
100 |
commands.put(player.getName(), new StoredCommand(label,args) ); |
101 |
} |
102 |
|
103 |
//System.out.println( ">>" + label); //DEBUG |
104 |
|
105 |
if ( label.equals("levelarea") ) { |
106 |
if (validateLevelOrFill(player, label, args) ) { |
107 |
levelArea(player, loc, args); |
108 |
} |
109 |
return true; |
110 |
} |
111 |
|
112 |
if ( label.equals("createcave") ) { |
113 |
if (validateLevelOrFill(player, label, args) ) { |
114 |
createCave(player, loc, args); |
115 |
} |
116 |
return true; |
117 |
} |
118 |
|
119 |
if ( label.equals("fillarea") ) { |
120 |
if (validateLevelOrFill(player, label, args) ) { |
121 |
fillArea(player, loc, args); |
122 |
} |
123 |
return true; |
124 |
} |
125 |
|
126 |
if (label.equals("levelandfillarea") ) { |
127 |
if (validateLevelOrFill(player, label, args) ) { |
128 |
|
129 |
levelArea(player, loc, args); |
130 |
fillArea(player, loc, args); |
131 |
setSurface(player, loc, args); |
132 |
|
133 |
} |
134 |
} |
135 |
|
136 |
if ( label.equals("slopearea") ) { |
137 |
slopeArea(player, loc, args); |
138 |
return true; |
139 |
} |
140 |
|
141 |
|
142 |
if ( label.equals("setsurface") ) { |
143 |
setSurface(player, loc, args); |
144 |
return true; |
145 |
} |
146 |
|
147 |
if (label.equals("platform") ) { |
148 |
platform(player,loc,args); |
149 |
return true; |
150 |
} |
151 |
|
152 |
if (label.equals("cylinder") ) { |
153 |
if (validateLevelOrFill(player, label, args) ) { |
154 |
cylinder(player,loc,args); |
155 |
} |
156 |
return true; |
157 |
} |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
return false; |
163 |
} |
164 |
|
165 |
|
166 |
void slopeArea(Player player, Location loc, String[] split) { |
167 |
int radius; |
168 |
try { |
169 |
radius = Integer.parseInt(split[0]); |
170 |
} catch (Exception e) { |
171 |
player.sendMessage("setsurface: radius must be an integer"); |
172 |
return; |
173 |
} |
174 |
|
175 |
System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius); |
176 |
|
177 |
|
178 |
int playerX = loc.getBlockX(); |
179 |
int playerY = loc.getBlockY(); |
180 |
int playerZ = loc.getBlockZ(); |
181 |
|
182 |
World world = loc.getWorld(); |
183 |
|
184 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
185 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
186 |
|
187 |
|
188 |
int xdist = Math.abs(playerX-x); |
189 |
int zdist = Math.abs(playerZ-z); |
190 |
|
191 |
int dist = Math.max(xdist,zdist); |
192 |
|
193 |
//for (int y=playerY; y<=playerY+radius; y++) { |
194 |
for (int y=(playerY+dist); y<=Y_MAX; y++) { |
195 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
} |
201 |
} |
202 |
|
203 |
private void platform(Player player, Location loc, String[] split) { |
204 |
|
205 |
if (split.length != 2) { |
206 |
player.sendMessage("Usage /platform [radius] [material]"); |
207 |
return; |
208 |
} |
209 |
|
210 |
int radius; |
211 |
try { |
212 |
radius = Integer.parseInt(split[0]); |
213 |
} catch (Exception e) { |
214 |
player.sendMessage("platform: radius must be an integer"); |
215 |
return; |
216 |
} |
217 |
|
218 |
if (radius > 25) { |
219 |
player.sendMessage("platform: radius may not exceed 25"); |
220 |
return; |
221 |
} |
222 |
|
223 |
Material material = Material.matchMaterial( split[1] ); |
224 |
if (material == null) { |
225 |
player.sendMessage("platform: unknown material: " + split[1] ); |
226 |
return; |
227 |
} |
228 |
/* |
229 |
boolean validblock = false; |
230 |
for (int i=0; i<valid_block_array.length; i++) { |
231 |
if (valid_block_array[i] == blockid) { |
232 |
validblock = true; |
233 |
break; |
234 |
} |
235 |
} |
236 |
|
237 |
if ( !validblock ) { |
238 |
player.sendMessage("setsurface: block now allowed"); |
239 |
return; |
240 |
}*/ |
241 |
|
242 |
int playerX = loc.getBlockX(); |
243 |
int playerY = loc.getBlockY(); |
244 |
int playerZ = loc.getBlockZ(); |
245 |
|
246 |
|
247 |
World world = loc.getWorld(); |
248 |
|
249 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
250 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
251 |
int y = playerY - 1; |
252 |
|
253 |
world.getBlockAt(x, y, z).setType( material ); |
254 |
} |
255 |
} |
256 |
} |
257 |
|
258 |
private void setSurface(Player player, Location loc, String[] split) { |
259 |
|
260 |
if (split.length != 2) { |
261 |
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
262 |
return; |
263 |
} |
264 |
|
265 |
int radius; |
266 |
try { |
267 |
radius = Integer.parseInt(split[0]); |
268 |
} catch (Exception e) { |
269 |
player.sendMessage("setsurface: radius must be an integer"); |
270 |
return; |
271 |
} |
272 |
|
273 |
if (radius > 25) { |
274 |
player.sendMessage("setsurface: radius may not exceed 25"); |
275 |
return; |
276 |
} |
277 |
|
278 |
Material material = Material.matchMaterial( split[1] ); |
279 |
if (material == null) { |
280 |
player.sendMessage("setsurface: unknown material: " + split[1] ); |
281 |
return; |
282 |
} |
283 |
|
284 |
//check if the blockid is in the array of valid blocks |
285 |
if ( ! valid_materials.contains(material) ) { |
286 |
player.sendMessage("setsurface: block now allowed"); |
287 |
return; |
288 |
} |
289 |
|
290 |
int playerX = loc.getBlockX(); |
291 |
int playerY = loc.getBlockY(); |
292 |
int playerZ = loc.getBlockZ(); |
293 |
|
294 |
if(playerY <= 2 && material != Material.BEDROCK ) { |
295 |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
296 |
return; |
297 |
} |
298 |
|
299 |
World world = loc.getWorld(); |
300 |
|
301 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
302 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
303 |
//int y = getGroundLevel(x,z) - 1; |
304 |
int y = world.getHighestBlockYAt(x, z) ; |
305 |
y -= 1; |
306 |
|
307 |
world.getBlockAt(x, y, z).setType(material); |
308 |
} |
309 |
} |
310 |
} |
311 |
|
312 |
private boolean validateLevelOrFill(Player player, String label, String[] split) { |
313 |
if (split.length < 1 || split.length > 2) { |
314 |
player.sendMessage("Usage: " + label + " [radius]"); |
315 |
return false; |
316 |
} |
317 |
|
318 |
int radius; |
319 |
try { |
320 |
radius = Integer.parseInt(split[0]); |
321 |
} catch (Exception e) { |
322 |
player.sendMessage(label + ": radius must be an integer"); |
323 |
return false; |
324 |
} |
325 |
|
326 |
if (radius > 25) { |
327 |
player.sendMessage(label + ": radius may not exceed 25"); |
328 |
return false; |
329 |
} |
330 |
|
331 |
if (split.length == 2) { |
332 |
Material material = Material.matchMaterial( split[1] ); |
333 |
if (material == null) { |
334 |
player.sendMessage(label + ": unknown material: " + split[1] ); |
335 |
return false; |
336 |
} |
337 |
if ( material == Material.TNT) { |
338 |
player.sendMessage("Sorry dave, i can't do that"); |
339 |
return false; |
340 |
} |
341 |
|
342 |
} |
343 |
|
344 |
return true; |
345 |
} |
346 |
|
347 |
private void fillArea(Player player, Location loc, String[] split) { |
348 |
int radius = Integer.parseInt(split[0]); |
349 |
|
350 |
Material material = Material.DIRT; |
351 |
|
352 |
|
353 |
if (split.length == 2) { |
354 |
material = Material.matchMaterial( split[1] ); |
355 |
if (material == null) { |
356 |
player.sendMessage("Unknown material: " + split[1] ); |
357 |
return; |
358 |
} |
359 |
} |
360 |
|
361 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
362 |
|
363 |
|
364 |
int playerX = loc.getBlockX(); |
365 |
int playerY = loc.getBlockY(); |
366 |
int playerZ = loc.getBlockZ(); |
367 |
|
368 |
World world = loc.getWorld(); |
369 |
|
370 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
371 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
372 |
|
373 |
|
374 |
|
375 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
376 |
world.getBlockAt(x, y, z).setType( material ); |
377 |
} |
378 |
|
379 |
} |
380 |
|
381 |
} |
382 |
} |
383 |
|
384 |
private void createCave(Player player, Location loc, String[] split) { |
385 |
|
386 |
int radius = Integer.parseInt(split[0]); |
387 |
|
388 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
389 |
|
390 |
|
391 |
int playerX = loc.getBlockX(); |
392 |
int playerY = loc.getBlockY(); |
393 |
int playerZ = loc.getBlockZ(); |
394 |
|
395 |
|
396 |
World world = loc.getWorld(); |
397 |
|
398 |
|
399 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
400 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
401 |
|
402 |
int y_max = playerY+radius; |
403 |
//for (int y=playerY; y<=playerY+radius; y++) { |
404 |
for (int y=playerY; y<=y_max; y++) { |
405 |
|
406 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
407 |
|
408 |
} |
409 |
|
410 |
} |
411 |
|
412 |
} |
413 |
} |
414 |
|
415 |
private void levelArea(Player player, Location loc, String[] split) { |
416 |
|
417 |
int radius = Integer.parseInt(split[0]); |
418 |
|
419 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
420 |
|
421 |
|
422 |
int playerX = loc.getBlockX(); |
423 |
int playerY = loc.getBlockY(); |
424 |
int playerZ = loc.getBlockZ(); |
425 |
|
426 |
|
427 |
World world = loc.getWorld(); |
428 |
|
429 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
430 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
431 |
|
432 |
//for (int y=playerY; y<=playerY+radius; y++) { |
433 |
for (int y=playerY; y<=Y_MAX; y++) { |
434 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
435 |
|
436 |
} |
437 |
|
438 |
} |
439 |
|
440 |
} |
441 |
} |
442 |
|
443 |
|
444 |
|
445 |
private void resetMap(boolean[][] map) { |
446 |
for (int x=0; x<map.length; x++) { |
447 |
for (int z=0; z<map[0].length; z++) { |
448 |
map[x][z] = false; |
449 |
} |
450 |
} |
451 |
} |
452 |
|
453 |
private void cylinder(Player player, Location loc, String[] split) { |
454 |
if (split.length != 2) { |
455 |
player.sendMessage("Usage: /cylinder [radius] [material] "); |
456 |
return; |
457 |
} |
458 |
|
459 |
Material material = Material.matchMaterial( split[1] ); |
460 |
if ( material == null) { |
461 |
player.sendMessage("/cylinder: unknown material: " + split[1]); |
462 |
return; |
463 |
} |
464 |
|
465 |
int radius = Integer.parseInt(split[0]); |
466 |
int diameter = (radius*2) + 1; |
467 |
|
468 |
boolean map[][] = new boolean[diameter][diameter]; |
469 |
resetMap( map ); |
470 |
|
471 |
/* map[0][0] = true; |
472 |
map[0][diameter-1] = true; |
473 |
map[diameter-1][0] = true; |
474 |
map[diameter-1][diameter-1] = true;*/ |
475 |
|
476 |
for (int r=0; r<360; r++) { |
477 |
double rad = Math.toRadians( r ); |
478 |
|
479 |
double a = (double) radius * Math.sin( rad); |
480 |
double b = (double) radius * Math.cos( rad); |
481 |
|
482 |
int x = radius + (int)Math.round(a); |
483 |
int y = radius + (int)Math.round(b); |
484 |
|
485 |
map[x][y] = true; |
486 |
} |
487 |
|
488 |
|
489 |
drawMap( loc, map, material ); |
490 |
} |
491 |
|
492 |
private void drawMap(Location loc, boolean[][] map, Material material) { |
493 |
int playerX = loc.getBlockX(); |
494 |
int playerY = loc.getBlockY(); |
495 |
int playerZ = loc.getBlockZ(); |
496 |
|
497 |
World world = loc.getWorld(); |
498 |
|
499 |
for (int i=0; i<map.length; i++) { |
500 |
for (int j=0; j<map[0].length; j++) { |
501 |
|
502 |
int x = playerX + i + 1; |
503 |
int z = playerZ + j; |
504 |
|
505 |
if (map[i][j] == true) { |
506 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
507 |
world.getBlockAt(x, y, z).setType(material); |
508 |
} |
509 |
} |
510 |
|
511 |
} |
512 |
} |
513 |
} |
514 |
|
515 |
|
516 |
|
517 |
} |