9 |
import org.bukkit.command.CommandSender; |
import org.bukkit.command.CommandSender; |
10 |
import org.bukkit.entity.Player; |
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{ |
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; |
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 |
|
|
35 |
|
//Material.WOOD, |
36 |
|
Material.ACACIA_WOOD, |
37 |
|
Material.BIRCH_WOOD, |
38 |
|
Material.DARK_OAK_WOOD, |
39 |
|
Material.JUNGLE_WOOD, |
40 |
|
Material.OAK_WOOD, |
41 |
|
Material.SPRUCE_WOOD, |
42 |
|
|
43 |
|
Material.BEDROCK, |
44 |
|
Material.WATER, |
45 |
|
Material.SAND, |
46 |
|
Material.GRAVEL, |
47 |
|
Material.GOLD_ORE, |
48 |
|
Material.IRON_ORE, |
49 |
|
Material.COAL_ORE, |
50 |
|
|
51 |
|
//Material.LOG, |
52 |
|
Material.ACACIA_LOG, |
53 |
|
Material.BIRCH_LOG, |
54 |
|
Material.DARK_OAK_LOG, |
55 |
|
Material.JUNGLE_LOG, |
56 |
|
Material.OAK_LOG, |
57 |
|
Material.SPRUCE_LOG, |
58 |
|
//Material.LEAVES, |
59 |
|
Material.ACACIA_LEAVES, |
60 |
|
Material.BIRCH_LEAVES, |
61 |
|
Material.DARK_OAK_LEAVES, |
62 |
|
Material.JUNGLE_LEAVES, |
63 |
|
Material.OAK_LEAVES, |
64 |
|
Material.SPRUCE_LEAVES, |
65 |
|
|
66 |
|
Material.GLASS, |
67 |
|
Material.LAPIS_ORE, |
68 |
|
Material.LAPIS_BLOCK, |
69 |
|
Material.SANDSTONE, |
70 |
|
//Material.WOOL, |
71 |
|
Material.BLACK_WOOL, |
72 |
|
Material.WHITE_WOOL, |
73 |
|
Material.GRAY_WOOL, |
74 |
|
|
75 |
|
Material.GOLD_BLOCK, |
76 |
|
Material.IRON_BLOCK, |
77 |
|
Material.BRICK, |
78 |
|
Material.MOSSY_COBBLESTONE, |
79 |
|
Material.OBSIDIAN, |
80 |
|
Material.DIAMOND_ORE, |
81 |
|
Material.DIAMOND_BLOCK, |
82 |
|
Material.ICE, |
83 |
|
Material.SNOW, |
84 |
|
Material.CLAY, |
85 |
|
Material.NETHERRACK, |
86 |
|
Material.SOUL_SAND |
87 |
|
}; |
88 |
|
//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} ; |
89 |
|
|
90 |
|
final Set<Material> valid_materials = new HashSet<Material>(); |
91 |
|
|
92 |
|
HashMap<String,StoredCommand> commands = new HashMap<String,StoredCommand>(); |
93 |
|
|
94 |
|
|
95 |
|
public GeneralContractorCommands() { |
96 |
|
Arrays.sort(valid_block_array); |
97 |
|
|
98 |
|
for (Material mat : valid_block_array) { |
99 |
|
valid_materials.add( mat ); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
//@Override |
//@Override |
104 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
114 |
player.sendMessage( ChatColor.RED + "This command may only be used in creative world" ); |
player.sendMessage( ChatColor.RED + "This command may only be used in creative world" ); |
115 |
return true; |
return true; |
116 |
} |
} |
117 |
|
|
118 |
|
|
119 |
|
if ( command.getLabel().equals("replay") ) { |
120 |
|
StoredCommand cmd = commands.get(player.getName() ); |
121 |
|
if (cmd != null) { |
122 |
|
label = cmd.label; |
123 |
|
args = cmd.args; |
124 |
|
} |
125 |
|
} else { |
126 |
|
commands.put(player.getName(), new StoredCommand(label,args) ); |
127 |
|
} |
128 |
|
|
129 |
//System.out.println( ">>" + label); //DEBUG |
//System.out.println( ">>" + label); //DEBUG |
130 |
|
|
131 |
if ( label.equals("levelarea") ) { |
if ( label.equals("levelarea") ) { |
132 |
if (validateLevelOrFill(player, args) ) { |
if (validateLevelOrFill(player, label, args) ) { |
133 |
levelArea(player, loc, args); |
levelArea(player, loc, args); |
134 |
} |
} |
135 |
return true; |
return true; |
136 |
} |
} |
137 |
|
|
138 |
|
if ( label.equals("createcave") ) { |
139 |
|
if (validateLevelOrFill(player, label, args) ) { |
140 |
|
createCave(player, loc, args); |
141 |
|
} |
142 |
|
return true; |
143 |
|
} |
144 |
|
|
145 |
if ( label.equals("fillarea") ) { |
if ( label.equals("fillarea") ) { |
146 |
if (validateLevelOrFill(player, args) ) { |
if (validateLevelOrFill(player, label, args) ) { |
147 |
fillArea(player, loc, args); |
fillArea(player, loc, args); |
148 |
} |
} |
149 |
return true; |
return true; |
150 |
} |
} |
151 |
|
|
152 |
|
if (label.equals("levelandfillarea") ) { |
153 |
|
if (validateLevelOrFill(player, label, args) ) { |
154 |
|
|
155 |
|
levelArea(player, loc, args); |
156 |
|
fillArea(player, loc, args); |
157 |
|
setSurface(player, loc, args); |
158 |
|
|
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
if ( label.equals("slopearea") ) { |
if ( label.equals("slopearea") ) { |
163 |
slopeArea(player, loc, args); |
slopeArea(player, loc, args); |
169 |
setSurface(player, loc, args); |
setSurface(player, loc, args); |
170 |
return true; |
return true; |
171 |
} |
} |
172 |
|
|
173 |
|
if (label.equals("platform") ) { |
174 |
|
platform(player,loc,args); |
175 |
|
return true; |
176 |
|
} |
177 |
|
|
178 |
|
if (label.equals("cylinder") ) { |
179 |
|
if (validateLevelOrFill(player, label, args) ) { |
180 |
|
cylinder(player,loc,args); |
181 |
|
} |
182 |
|
return true; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
return false; |
return false; |
226 |
} |
} |
227 |
} |
} |
228 |
|
|
229 |
private void setSurface(Player player, Location loc, String[] split) { |
private void platform(Player player, Location loc, String[] split) { |
|
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, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; |
|
|
|
|
|
|
|
230 |
|
|
231 |
if (split.length != 2) { |
if (split.length != 2) { |
232 |
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
player.sendMessage("Usage /platform [radius] [material]"); |
233 |
return; |
return; |
234 |
} |
} |
235 |
|
|
|
|
|
236 |
int radius; |
int radius; |
237 |
try { |
try { |
238 |
radius = Integer.parseInt(split[0]); |
radius = Integer.parseInt(split[0]); |
239 |
} catch (Exception e) { |
} catch (Exception e) { |
240 |
player.sendMessage("setsurface: radius must be an integer"); |
player.sendMessage("platform: radius must be an integer"); |
241 |
return; |
return; |
242 |
} |
} |
243 |
|
|
244 |
if (radius > 20) { |
if (radius > 25) { |
245 |
player.sendMessage("setsurface: radius may not exceed 20"); |
player.sendMessage("platform: radius may not exceed 25"); |
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
249 |
|
Material material = Material.matchMaterial( split[1] ); |
250 |
int blockid; |
if (material == null) { |
251 |
try { |
player.sendMessage("platform: unknown material: " + split[1] ); |
|
blockid = Integer.parseInt(split[1]); |
|
|
} catch (Exception e) { |
|
|
player.sendMessage("setsurface: blockid must be an integer"); |
|
252 |
return; |
return; |
253 |
} |
} |
254 |
|
/* |
255 |
boolean validblock = false; |
boolean validblock = false; |
256 |
for (int i=0; i<valid_block_array.length; i++) { |
for (int i=0; i<valid_block_array.length; i++) { |
257 |
if (valid_block_array[i] == blockid) { |
if (valid_block_array[i] == blockid) { |
263 |
if ( !validblock ) { |
if ( !validblock ) { |
264 |
player.sendMessage("setsurface: block now allowed"); |
player.sendMessage("setsurface: block now allowed"); |
265 |
return; |
return; |
266 |
|
}*/ |
267 |
|
|
268 |
|
int playerX = loc.getBlockX(); |
269 |
|
int playerY = loc.getBlockY(); |
270 |
|
int playerZ = loc.getBlockZ(); |
271 |
|
|
272 |
|
|
273 |
|
World world = loc.getWorld(); |
274 |
|
|
275 |
|
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
276 |
|
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
277 |
|
int y = playerY - 1; |
278 |
|
|
279 |
|
world.getBlockAt(x, y, z).setType( material ); |
280 |
|
} |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
|
private void setSurface(Player player, Location loc, String[] split) { |
285 |
|
|
286 |
|
if (split.length != 2) { |
287 |
|
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
288 |
|
return; |
289 |
|
} |
290 |
|
|
291 |
|
int radius; |
292 |
|
try { |
293 |
|
radius = Integer.parseInt(split[0]); |
294 |
|
} catch (Exception e) { |
295 |
|
player.sendMessage("setsurface: radius must be an integer"); |
296 |
|
return; |
297 |
|
} |
298 |
|
|
299 |
|
if (radius > 25) { |
300 |
|
player.sendMessage("setsurface: radius may not exceed 25"); |
301 |
|
return; |
302 |
|
} |
303 |
|
|
304 |
|
Material material = Material.matchMaterial( split[1] ); |
305 |
|
if (material == null) { |
306 |
|
player.sendMessage("setsurface: unknown material: " + split[1] ); |
307 |
|
return; |
308 |
|
} |
309 |
|
|
310 |
|
//check if the blockid is in the array of valid blocks |
311 |
|
if ( ! valid_materials.contains(material) ) { |
312 |
|
player.sendMessage("setsurface: block now allowed"); |
313 |
|
return; |
314 |
} |
} |
315 |
|
|
316 |
int playerX = loc.getBlockX(); |
int playerX = loc.getBlockX(); |
317 |
int playerY = loc.getBlockY(); |
int playerY = loc.getBlockY(); |
318 |
int playerZ = loc.getBlockZ(); |
int playerZ = loc.getBlockZ(); |
319 |
|
|
320 |
if(playerY <= 2 && blockid != 7) { |
if(playerY <= 2 && material != Material.BEDROCK ) { |
321 |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
322 |
return; |
return; |
323 |
} |
} |
324 |
|
|
325 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
|
|
|
326 |
|
|
327 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
328 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
329 |
//int y = getGroundLevel(x,z) - 1; |
//int y = getGroundLevel(x,z) - 1; |
330 |
int y = world.getHighestBlockYAt(x, z); |
int y = world.getHighestBlockYAt(x, z) ; |
331 |
|
y -= 1; |
332 |
|
|
333 |
world.getBlockAt(x, y, z).setTypeId(blockid); |
world.getBlockAt(x, y, z).setType(material); |
334 |
} |
} |
335 |
} |
} |
336 |
} |
} |
349 |
return false; |
return false; |
350 |
} |
} |
351 |
|
|
352 |
if (radius > 20) { |
if (radius > 25) { |
353 |
player.sendMessage(label + ": radius may not exceed 20"); |
player.sendMessage(label + ": radius may not exceed 25"); |
354 |
return false; |
return false; |
355 |
} |
} |
356 |
|
|
357 |
if (split.length == 2) { |
if (split.length == 2) { |
358 |
int id; |
Material material = Material.matchMaterial( split[1] ); |
359 |
try { |
if (material == null) { |
360 |
id = Integer.parseInt( split[1] ); |
player.sendMessage(label + ": unknown material: " + split[1] ); |
|
} catch (Exception e) { |
|
|
player.sendMessage(label + ": id must be an integer"); |
|
361 |
return false; |
return false; |
362 |
} |
} |
363 |
if ( id == 46) { |
if ( material == Material.TNT) { |
364 |
player.sendMessage("Sorry dave, i can't do that"); |
player.sendMessage("Sorry dave, i can't do that"); |
365 |
return false; |
return false; |
366 |
} |
} |
373 |
private void fillArea(Player player, Location loc, String[] split) { |
private void fillArea(Player player, Location loc, String[] split) { |
374 |
int radius = Integer.parseInt(split[0]); |
int radius = Integer.parseInt(split[0]); |
375 |
|
|
376 |
int material = Material.DIRT.getId(); |
Material material = Material.DIRT; |
377 |
|
|
378 |
|
|
379 |
if (split.length == 2) { |
if (split.length == 2) { |
380 |
material = Integer.parseInt( split[1] ); |
material = Material.matchMaterial( split[1] ); |
381 |
|
if (material == null) { |
382 |
|
player.sendMessage("Unknown material: " + split[1] ); |
383 |
|
return; |
384 |
|
} |
385 |
} |
} |
386 |
|
|
387 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
399 |
|
|
400 |
|
|
401 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
402 |
world.getBlockAt(x, y, z).setTypeId(material); |
world.getBlockAt(x, y, z).setType( material ); |
403 |
} |
} |
404 |
|
|
405 |
} |
} |
407 |
} |
} |
408 |
} |
} |
409 |
|
|
410 |
|
private void createCave(Player player, Location loc, String[] split) { |
411 |
|
|
412 |
|
int radius = Integer.parseInt(split[0]); |
413 |
|
|
414 |
|
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
415 |
|
|
416 |
|
|
417 |
|
int playerX = loc.getBlockX(); |
418 |
|
int playerY = loc.getBlockY(); |
419 |
|
int playerZ = loc.getBlockZ(); |
420 |
|
|
421 |
|
|
422 |
|
World world = loc.getWorld(); |
423 |
|
|
424 |
|
|
425 |
|
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
426 |
|
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
427 |
|
|
428 |
|
int y_max = playerY+radius; |
429 |
|
//for (int y=playerY; y<=playerY+radius; y++) { |
430 |
|
for (int y=playerY; y<=y_max; y++) { |
431 |
|
|
432 |
|
world.getBlockAt(x, y, z).setType(Material.AIR); |
433 |
|
|
434 |
|
} |
435 |
|
|
436 |
|
} |
437 |
|
|
438 |
|
} |
439 |
|
} |
440 |
|
|
441 |
private void levelArea(Player player, Location loc, String[] split) { |
private void levelArea(Player player, Location loc, String[] split) { |
442 |
|
|
443 |
int radius = Integer.parseInt(split[0]); |
int radius = Integer.parseInt(split[0]); |
452 |
|
|
453 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
454 |
|
|
|
int count = 0; |
|
455 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
456 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
457 |
|
|
458 |
//for (int y=playerY; y<=playerY+radius; y++) { |
//for (int y=playerY; y<=playerY+radius; y++) { |
459 |
for (int y=playerY; y<=Y_MAX; y++) { |
for (int y=playerY; y<=Y_MAX; y++) { |
|
count++; |
|
460 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
world.getBlockAt(x, y, z).setType(Material.AIR); |
461 |
|
|
462 |
} |
} |
466 |
} |
} |
467 |
} |
} |
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
private void resetMap(boolean[][] map) { |
472 |
|
for (int x=0; x<map.length; x++) { |
473 |
|
for (int z=0; z<map[0].length; z++) { |
474 |
|
map[x][z] = false; |
475 |
|
} |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
|
private void cylinder(Player player, Location loc, String[] split) { |
480 |
|
if (split.length != 2) { |
481 |
|
player.sendMessage("Usage: /cylinder [radius] [material] "); |
482 |
|
return; |
483 |
|
} |
484 |
|
|
485 |
|
Material material = Material.matchMaterial( split[1] ); |
486 |
|
if ( material == null) { |
487 |
|
player.sendMessage("/cylinder: unknown material: " + split[1]); |
488 |
|
return; |
489 |
|
} |
490 |
|
|
491 |
|
int radius = Integer.parseInt(split[0]); |
492 |
|
int diameter = (radius*2) + 1; |
493 |
|
|
494 |
|
boolean map[][] = new boolean[diameter][diameter]; |
495 |
|
resetMap( map ); |
496 |
|
|
497 |
|
/* map[0][0] = true; |
498 |
|
map[0][diameter-1] = true; |
499 |
|
map[diameter-1][0] = true; |
500 |
|
map[diameter-1][diameter-1] = true;*/ |
501 |
|
|
502 |
|
for (int r=0; r<360; r++) { |
503 |
|
double rad = Math.toRadians( r ); |
504 |
|
|
505 |
|
double a = (double) radius * Math.sin( rad); |
506 |
|
double b = (double) radius * Math.cos( rad); |
507 |
|
|
508 |
|
int x = radius + (int)Math.round(a); |
509 |
|
int y = radius + (int)Math.round(b); |
510 |
|
|
511 |
|
map[x][y] = true; |
512 |
|
} |
513 |
|
|
514 |
|
|
515 |
|
drawMap( loc, map, material ); |
516 |
|
} |
517 |
|
|
518 |
|
private void drawMap(Location loc, boolean[][] map, Material material) { |
519 |
|
int playerX = loc.getBlockX(); |
520 |
|
int playerY = loc.getBlockY(); |
521 |
|
int playerZ = loc.getBlockZ(); |
522 |
|
|
523 |
|
World world = loc.getWorld(); |
524 |
|
|
525 |
|
for (int i=0; i<map.length; i++) { |
526 |
|
for (int j=0; j<map[0].length; j++) { |
527 |
|
|
528 |
|
int x = playerX + i + 1; |
529 |
|
int z = playerZ + j; |
530 |
|
|
531 |
|
if (map[i][j] == true) { |
532 |
|
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
533 |
|
world.getBlockAt(x, y, z).setType(material); |
534 |
|
} |
535 |
|
} |
536 |
|
|
537 |
|
} |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
|
|
543 |
} |
} |