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 |
|
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 |
//@Override |
78 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
88 |
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" ); |
89 |
return true; |
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 |
//System.out.println( ">>" + label); //DEBUG |
104 |
|
|
109 |
return true; |
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") ) { |
if ( label.equals("fillarea") ) { |
120 |
if (validateLevelOrFill(player, label, args) ) { |
if (validateLevelOrFill(player, label, args) ) { |
133 |
setSurface(player, loc, args); |
setSurface(player, loc, args); |
134 |
return true; |
return true; |
135 |
} |
} |
136 |
|
|
137 |
|
if (label.equals("platform") ) { |
138 |
|
platform(player,loc,args); |
139 |
|
return true; |
140 |
|
} |
141 |
|
|
142 |
|
if (label.equals("cylinder") ) { |
143 |
|
if (validateLevelOrFill(player, label, args) ) { |
144 |
|
cylinder(player,loc,args); |
145 |
|
} |
146 |
|
return true; |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
return false; |
return false; |
188 |
} |
} |
189 |
} |
} |
190 |
|
|
191 |
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} ; |
|
|
|
|
|
|
|
192 |
|
|
193 |
if (split.length != 2) { |
if (split.length != 2) { |
194 |
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
player.sendMessage("Usage /platform [radius] [material]"); |
195 |
return; |
return; |
196 |
} |
} |
197 |
|
|
|
|
|
198 |
int radius; |
int radius; |
199 |
try { |
try { |
200 |
radius = Integer.parseInt(split[0]); |
radius = Integer.parseInt(split[0]); |
201 |
} catch (Exception e) { |
} catch (Exception e) { |
202 |
player.sendMessage("setsurface: radius must be an integer"); |
player.sendMessage("platform: radius must be an integer"); |
203 |
return; |
return; |
204 |
} |
} |
205 |
|
|
206 |
if (radius > 20) { |
if (radius > 20) { |
207 |
player.sendMessage("setsurface: radius may not exceed 20"); |
player.sendMessage("platform: radius may not exceed 20"); |
208 |
return; |
return; |
209 |
} |
} |
210 |
|
|
211 |
|
Material material = Material.matchMaterial( split[1] ); |
212 |
int blockid; |
if (material == null) { |
213 |
try { |
player.sendMessage("platform: unknown material: " + split[1] ); |
|
blockid = Integer.parseInt(split[1]); |
|
|
} catch (Exception e) { |
|
|
player.sendMessage("setsurface: blockid must be an integer"); |
|
214 |
return; |
return; |
215 |
} |
} |
216 |
|
/* |
217 |
boolean validblock = false; |
boolean validblock = false; |
218 |
for (int i=0; i<valid_block_array.length; i++) { |
for (int i=0; i<valid_block_array.length; i++) { |
219 |
if (valid_block_array[i] == blockid) { |
if (valid_block_array[i] == blockid) { |
225 |
if ( !validblock ) { |
if ( !validblock ) { |
226 |
player.sendMessage("setsurface: block now allowed"); |
player.sendMessage("setsurface: block now allowed"); |
227 |
return; |
return; |
228 |
|
}*/ |
229 |
|
|
230 |
|
int playerX = loc.getBlockX(); |
231 |
|
int playerY = loc.getBlockY(); |
232 |
|
int playerZ = loc.getBlockZ(); |
233 |
|
|
234 |
|
|
235 |
|
World world = loc.getWorld(); |
236 |
|
|
237 |
|
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
238 |
|
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
239 |
|
int y = playerY - 1; |
240 |
|
|
241 |
|
world.getBlockAt(x, y, z).setType( material ); |
242 |
|
} |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
|
private void setSurface(Player player, Location loc, String[] split) { |
247 |
|
|
248 |
|
if (split.length != 2) { |
249 |
|
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
250 |
|
return; |
251 |
|
} |
252 |
|
|
253 |
|
int radius; |
254 |
|
try { |
255 |
|
radius = Integer.parseInt(split[0]); |
256 |
|
} catch (Exception e) { |
257 |
|
player.sendMessage("setsurface: radius must be an integer"); |
258 |
|
return; |
259 |
|
} |
260 |
|
|
261 |
|
if (radius > 20) { |
262 |
|
player.sendMessage("setsurface: radius may not exceed 20"); |
263 |
|
return; |
264 |
|
} |
265 |
|
|
266 |
|
Material material = Material.matchMaterial( split[1] ); |
267 |
|
if (material == null) { |
268 |
|
player.sendMessage("setsurface: unknown material: " + split[1] ); |
269 |
|
return; |
270 |
|
} |
271 |
|
|
272 |
|
//check if the blockid is in the array of valid blocks |
273 |
|
if ( ! valid_materials.contains(material) ) { |
274 |
|
player.sendMessage("setsurface: block now allowed"); |
275 |
|
return; |
276 |
} |
} |
277 |
|
|
278 |
int playerX = loc.getBlockX(); |
int playerX = loc.getBlockX(); |
279 |
int playerY = loc.getBlockY(); |
int playerY = loc.getBlockY(); |
280 |
int playerZ = loc.getBlockZ(); |
int playerZ = loc.getBlockZ(); |
281 |
|
|
282 |
if(playerY <= 2 && blockid != 7) { |
if(playerY <= 2 && material != Material.BEDROCK ) { |
283 |
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)"); |
284 |
return; |
return; |
285 |
} |
} |
286 |
|
|
287 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
|
|
|
288 |
|
|
289 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
290 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
291 |
//int y = getGroundLevel(x,z) - 1; |
//int y = getGroundLevel(x,z) - 1; |
292 |
int y = world.getHighestBlockYAt(x, z); |
int y = world.getHighestBlockYAt(x, z) ; |
293 |
|
y -= 1; |
294 |
|
|
295 |
world.getBlockAt(x, y, z).setTypeId(blockid); |
world.getBlockAt(x, y, z).setType(material); |
296 |
} |
} |
297 |
} |
} |
298 |
} |
} |
317 |
} |
} |
318 |
|
|
319 |
if (split.length == 2) { |
if (split.length == 2) { |
320 |
int id; |
Material material = Material.matchMaterial( split[1] ); |
321 |
try { |
if (material == null) { |
322 |
id = Integer.parseInt( split[1] ); |
player.sendMessage(label + ": unknown material: " + split[1] ); |
|
} catch (Exception e) { |
|
|
player.sendMessage(label + ": id must be an integer"); |
|
323 |
return false; |
return false; |
324 |
} |
} |
325 |
if ( id == 46) { |
if ( material == Material.TNT) { |
326 |
player.sendMessage("Sorry dave, i can't do that"); |
player.sendMessage("Sorry dave, i can't do that"); |
327 |
return false; |
return false; |
328 |
} |
} |
335 |
private void fillArea(Player player, Location loc, String[] split) { |
private void fillArea(Player player, Location loc, String[] split) { |
336 |
int radius = Integer.parseInt(split[0]); |
int radius = Integer.parseInt(split[0]); |
337 |
|
|
338 |
int material = Material.DIRT.getId(); |
Material material = Material.DIRT; |
339 |
|
|
340 |
|
|
341 |
if (split.length == 2) { |
if (split.length == 2) { |
342 |
material = Integer.parseInt( split[1] ); |
material = Material.matchMaterial( split[1] ); |
343 |
|
if (material == null) { |
344 |
|
player.sendMessage("Unknown material: " + split[1] ); |
345 |
|
return; |
346 |
|
} |
347 |
} |
} |
348 |
|
|
349 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
361 |
|
|
362 |
|
|
363 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
364 |
world.getBlockAt(x, y, z).setTypeId(material); |
world.getBlockAt(x, y, z).setType( material ); |
365 |
} |
} |
366 |
|
|
367 |
} |
} |
369 |
} |
} |
370 |
} |
} |
371 |
|
|
372 |
|
private void createCave(Player player, Location loc, String[] split) { |
373 |
|
|
374 |
|
int radius = Integer.parseInt(split[0]); |
375 |
|
|
376 |
|
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
377 |
|
|
378 |
|
|
379 |
|
int playerX = loc.getBlockX(); |
380 |
|
int playerY = loc.getBlockY(); |
381 |
|
int playerZ = loc.getBlockZ(); |
382 |
|
|
383 |
|
|
384 |
|
World world = loc.getWorld(); |
385 |
|
|
386 |
|
int count = 0; |
387 |
|
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
388 |
|
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
389 |
|
|
390 |
|
int y_max = playerY+radius; |
391 |
|
//for (int y=playerY; y<=playerY+radius; y++) { |
392 |
|
for (int y=playerY; y<=y_max; y++) { |
393 |
|
count++; |
394 |
|
world.getBlockAt(x, y, z).setType(Material.AIR); |
395 |
|
|
396 |
|
} |
397 |
|
|
398 |
|
} |
399 |
|
|
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
private void levelArea(Player player, Location loc, String[] split) { |
private void levelArea(Player player, Location loc, String[] split) { |
404 |
|
|
405 |
int radius = Integer.parseInt(split[0]); |
int radius = Integer.parseInt(split[0]); |
430 |
} |
} |
431 |
} |
} |
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
private void resetMap(boolean[][] map) { |
436 |
|
for (int x=0; x<map.length; x++) { |
437 |
|
for (int z=0; z<map[0].length; z++) { |
438 |
|
map[x][z] = false; |
439 |
|
} |
440 |
|
} |
441 |
|
} |
442 |
|
|
443 |
|
private void cylinder(Player player, Location loc, String[] split) { |
444 |
|
if (split.length != 2) { |
445 |
|
player.sendMessage("Usage: /cylinder [radius] [material] "); |
446 |
|
return; |
447 |
|
} |
448 |
|
|
449 |
|
Material material = Material.matchMaterial( split[1] ); |
450 |
|
if ( material == null) { |
451 |
|
player.sendMessage("/cylinder: unknown material: " + split[1]); |
452 |
|
return; |
453 |
|
} |
454 |
|
|
455 |
|
int radius = Integer.parseInt(split[0]); |
456 |
|
int diameter = (radius*2) + 1; |
457 |
|
|
458 |
|
boolean map[][] = new boolean[diameter][diameter]; |
459 |
|
resetMap( map ); |
460 |
|
|
461 |
|
/* map[0][0] = true; |
462 |
|
map[0][diameter-1] = true; |
463 |
|
map[diameter-1][0] = true; |
464 |
|
map[diameter-1][diameter-1] = true;*/ |
465 |
|
|
466 |
|
for (int r=0; r<360; r++) { |
467 |
|
double rad = Math.toRadians( r ); |
468 |
|
|
469 |
|
double a = (double) radius * Math.sin( rad); |
470 |
|
double b = (double) radius * Math.cos( rad); |
471 |
|
|
472 |
|
int x = radius + (int)Math.round(a); |
473 |
|
int y = radius + (int)Math.round(b); |
474 |
|
|
475 |
|
map[x][y] = true; |
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
|
drawMap( loc, map, material ); |
480 |
|
} |
481 |
|
|
482 |
|
private void drawMap(Location loc, boolean[][] map, Material material) { |
483 |
|
int playerX = loc.getBlockX(); |
484 |
|
int playerY = loc.getBlockY(); |
485 |
|
int playerZ = loc.getBlockZ(); |
486 |
|
|
487 |
|
World world = loc.getWorld(); |
488 |
|
|
489 |
|
for (int i=0; i<map.length; i++) { |
490 |
|
for (int j=0; j<map[0].length; j++) { |
491 |
|
|
492 |
|
int x = playerX + i + 1; |
493 |
|
int z = playerZ + j; |
494 |
|
|
495 |
|
if (map[i][j] == true) { |
496 |
|
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
497 |
|
world.getBlockAt(x, y, z).setType(material); |
498 |
|
} |
499 |
|
} |
500 |
|
|
501 |
|
} |
502 |
|
} |
503 |
|
} |
504 |
|
|
505 |
|
|
506 |
|
|
507 |
} |
} |