/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/creative/GeneralContractorCommands.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/creative/GeneralContractorCommands.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3239 - (show annotations) (download)
Wed Jul 18 07:59:05 2018 UTC (5 years, 10 months ago) by torben
File size: 12291 byte(s)
Compile for spigot 1.13
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
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
104 public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
105
106 if (! (sender instanceof Player) ) {
107 return false;
108 }
109 Player player = (Player) sender;
110
111 Location loc = player.getLocation();
112
113 if ( ! loc.getWorld().getName().equals("creative")) {
114 player.sendMessage( ChatColor.RED + "This command may only be used in creative world" );
115 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
130
131 if ( label.equals("levelarea") ) {
132 if (validateLevelOrFill(player, label, args) ) {
133 levelArea(player, loc, args);
134 }
135 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") ) {
146 if (validateLevelOrFill(player, label, args) ) {
147 fillArea(player, loc, args);
148 }
149 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") ) {
163 slopeArea(player, loc, args);
164 return true;
165 }
166
167
168 if ( label.equals("setsurface") ) {
169 setSurface(player, loc, args);
170 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;
189 }
190
191
192 void slopeArea(Player player, Location loc, String[] split) {
193 int radius;
194 try {
195 radius = Integer.parseInt(split[0]);
196 } catch (Exception e) {
197 player.sendMessage("setsurface: radius must be an integer");
198 return;
199 }
200
201 System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius);
202
203
204 int playerX = loc.getBlockX();
205 int playerY = loc.getBlockY();
206 int playerZ = loc.getBlockZ();
207
208 World world = loc.getWorld();
209
210 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
211 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
212
213
214 int xdist = Math.abs(playerX-x);
215 int zdist = Math.abs(playerZ-z);
216
217 int dist = Math.max(xdist,zdist);
218
219 //for (int y=playerY; y<=playerY+radius; y++) {
220 for (int y=(playerY+dist); y<=Y_MAX; y++) {
221 world.getBlockAt(x, y, z).setType(Material.AIR);
222 }
223
224 }
225
226 }
227 }
228
229 private void platform(Player player, Location loc, String[] split) {
230
231 if (split.length != 2) {
232 player.sendMessage("Usage /platform [radius] [material]");
233 return;
234 }
235
236 int radius;
237 try {
238 radius = Integer.parseInt(split[0]);
239 } catch (Exception e) {
240 player.sendMessage("platform: radius must be an integer");
241 return;
242 }
243
244 if (radius > 25) {
245 player.sendMessage("platform: radius may not exceed 25");
246 return;
247 }
248
249 Material material = Material.matchMaterial( split[1] );
250 if (material == null) {
251 player.sendMessage("platform: unknown material: " + split[1] );
252 return;
253 }
254 /*
255 boolean validblock = false;
256 for (int i=0; i<valid_block_array.length; i++) {
257 if (valid_block_array[i] == blockid) {
258 validblock = true;
259 break;
260 }
261 }
262
263 if ( !validblock ) {
264 player.sendMessage("setsurface: block now allowed");
265 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();
317 int playerY = loc.getBlockY();
318 int playerZ = loc.getBlockZ();
319
320 if(playerY <= 2 && material != Material.BEDROCK ) {
321 player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
322 return;
323 }
324
325 World world = loc.getWorld();
326
327 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
328 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
329 //int y = getGroundLevel(x,z) - 1;
330 int y = world.getHighestBlockYAt(x, z) ;
331 y -= 1;
332
333 world.getBlockAt(x, y, z).setType(material);
334 }
335 }
336 }
337
338 private boolean validateLevelOrFill(Player player, String label, String[] split) {
339 if (split.length < 1 || split.length > 2) {
340 player.sendMessage("Usage: " + label + " [radius]");
341 return false;
342 }
343
344 int radius;
345 try {
346 radius = Integer.parseInt(split[0]);
347 } catch (Exception e) {
348 player.sendMessage(label + ": radius must be an integer");
349 return false;
350 }
351
352 if (radius > 25) {
353 player.sendMessage(label + ": radius may not exceed 25");
354 return false;
355 }
356
357 if (split.length == 2) {
358 Material material = Material.matchMaterial( split[1] );
359 if (material == null) {
360 player.sendMessage(label + ": unknown material: " + split[1] );
361 return false;
362 }
363 if ( material == Material.TNT) {
364 player.sendMessage("Sorry dave, i can't do that");
365 return false;
366 }
367
368 }
369
370 return true;
371 }
372
373 private void fillArea(Player player, Location loc, String[] split) {
374 int radius = Integer.parseInt(split[0]);
375
376 Material material = Material.DIRT;
377
378
379 if (split.length == 2) {
380 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);
388
389
390 int playerX = loc.getBlockX();
391 int playerY = loc.getBlockY();
392 int playerZ = loc.getBlockZ();
393
394 World world = loc.getWorld();
395
396 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
397 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
398
399
400
401 for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
402 world.getBlockAt(x, y, z).setType( material );
403 }
404
405 }
406
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) {
442
443 int radius = Integer.parseInt(split[0]);
444
445 System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
446
447
448 int playerX = loc.getBlockX();
449 int playerY = loc.getBlockY();
450 int playerZ = loc.getBlockZ();
451
452
453 World world = loc.getWorld();
454
455 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
456 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
457
458 //for (int y=playerY; y<=playerY+radius; y++) {
459 for (int y=playerY; y<=Y_MAX; y++) {
460 world.getBlockAt(x, y, z).setType(Material.AIR);
461
462 }
463
464 }
465
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 }

  ViewVC Help
Powered by ViewVC 1.1.20