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

Annotation 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 3196 - (hide annotations) (download)
Mon May 29 13:03:52 2017 UTC (7 years ago) by torben
File size: 11804 byte(s)
make eternalday work on multiple worlds. Add levelandfillarea
1 torben 1723 package dk.thoerup.bukkit.hoeruputils.creative;
2    
3 torben 1724 import org.bukkit.ChatColor;
4 torben 1723 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 torben 1731 import java.util.Arrays;
13 torben 1729 import java.util.HashMap;
14 torben 3133 import java.util.HashSet;
15     import java.util.Set;
16 torben 1724
17 torben 1723 public class GeneralContractorCommands implements CommandExecutor{
18    
19 torben 1729 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 torben 1723 final static int Y_MAX = 255;
29 torben 3133 final Material valid_block_array[] = {
30     Material.STONE,
31     Material.GRASS,
32     Material.DIRT,
33 torben 3141 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 torben 3133 };
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 torben 1729
64 torben 3133 final Set<Material> valid_materials = new HashSet<Material>();
65    
66 torben 1729 HashMap<String,StoredCommand> commands = new HashMap<String,StoredCommand>();
67 torben 1730
68    
69 torben 1731 public GeneralContractorCommands() {
70     Arrays.sort(valid_block_array);
71 torben 3133
72     for (Material mat : valid_block_array) {
73     valid_materials.add( mat );
74     }
75 torben 1731 }
76 torben 1723
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 torben 1729
92    
93 torben 1730 if ( command.getLabel().equals("replay") ) {
94 torben 1729 StoredCommand cmd = commands.get(player.getName() );
95     if (cmd != null) {
96     label = cmd.label;
97     args = cmd.args;
98     }
99 torben 1730 } else {
100     commands.put(player.getName(), new StoredCommand(label,args) );
101 torben 1729 }
102 torben 1723
103     //System.out.println( ">>" + label); //DEBUG
104    
105     if ( label.equals("levelarea") ) {
106 torben 1726 if (validateLevelOrFill(player, label, args) ) {
107 torben 1723 levelArea(player, loc, args);
108     }
109     return true;
110     }
111    
112 torben 1751 if ( label.equals("createcave") ) {
113     if (validateLevelOrFill(player, label, args) ) {
114     createCave(player, loc, args);
115     }
116     return true;
117     }
118 torben 1723
119     if ( label.equals("fillarea") ) {
120 torben 1726 if (validateLevelOrFill(player, label, args) ) {
121 torben 1723 fillArea(player, loc, args);
122     }
123     return true;
124     }
125 torben 3196
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 torben 1723
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 torben 1730
147     if (label.equals("platform") ) {
148     platform(player,loc,args);
149     return true;
150     }
151 torben 1733
152     if (label.equals("cylinder") ) {
153     if (validateLevelOrFill(player, label, args) ) {
154     cylinder(player,loc,args);
155     }
156     return true;
157     }
158 torben 3196
159    
160 torben 1723
161    
162     return false;
163     }
164    
165    
166 torben 1725 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 torben 1723
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 torben 1730 private void platform(Player player, Location loc, String[] split) {
204 torben 1723
205 torben 1730 if (split.length != 2) {
206 torben 3133 player.sendMessage("Usage /platform [radius] [material]");
207 torben 1730 return;
208     }
209 torben 1723
210 torben 1730 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 torben 1723
218 torben 3196 if (radius > 25) {
219     player.sendMessage("platform: radius may not exceed 25");
220 torben 1730 return;
221     }
222    
223 torben 3133 Material material = Material.matchMaterial( split[1] );
224     if (material == null) {
225 torben 3140 player.sendMessage("platform: unknown material: " + split[1] );
226 torben 1730 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 torben 3133 world.getBlockAt(x, y, z).setType( material );
254 torben 1730 }
255     }
256     }
257    
258     private void setSurface(Player player, Location loc, String[] split) {
259    
260 torben 1725 if (split.length != 2) {
261 torben 1723 player.sendMessage("Usage /setsurface [radius] [blockID]");
262     return;
263     }
264    
265     int radius;
266     try {
267 torben 1725 radius = Integer.parseInt(split[0]);
268 torben 1723 } catch (Exception e) {
269     player.sendMessage("setsurface: radius must be an integer");
270     return;
271     }
272    
273 torben 3196 if (radius > 25) {
274     player.sendMessage("setsurface: radius may not exceed 25");
275 torben 1723 return;
276     }
277    
278 torben 3133 Material material = Material.matchMaterial( split[1] );
279     if (material == null) {
280 torben 3140 player.sendMessage("setsurface: unknown material: " + split[1] );
281 torben 1723 return;
282     }
283    
284 torben 1731 //check if the blockid is in the array of valid blocks
285 torben 3133 if ( ! valid_materials.contains(material) ) {
286 torben 1723 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 torben 3133 if(playerY <= 2 && material != Material.BEDROCK ) {
295 torben 1723 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 torben 1729 int y = world.getHighestBlockYAt(x, z) ;
305 torben 3139 y -= 1;
306 torben 1723
307 torben 3133 world.getBlockAt(x, y, z).setType(material);
308 torben 1723 }
309     }
310     }
311    
312 torben 1725 private boolean validateLevelOrFill(Player player, String label, String[] split) {
313     if (split.length < 1 || split.length > 2) {
314     player.sendMessage("Usage: " + label + " [radius]");
315 torben 1723 return false;
316     }
317    
318     int radius;
319     try {
320 torben 1725 radius = Integer.parseInt(split[0]);
321 torben 1723 } catch (Exception e) {
322 torben 1725 player.sendMessage(label + ": radius must be an integer");
323 torben 1723 return false;
324     }
325    
326 torben 3196 if (radius > 25) {
327     player.sendMessage(label + ": radius may not exceed 25");
328 torben 1723 return false;
329     }
330    
331 torben 1725 if (split.length == 2) {
332 torben 3139 Material material = Material.matchMaterial( split[1] );
333     if (material == null) {
334 torben 3140 player.sendMessage(label + ": unknown material: " + split[1] );
335 torben 1723 return false;
336     }
337 torben 3139 if ( material == Material.TNT) {
338 torben 1723 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 torben 1725 int radius = Integer.parseInt(split[0]);
349 torben 1723
350 torben 3133 Material material = Material.DIRT;
351    
352 torben 1723
353 torben 1725 if (split.length == 2) {
354 torben 3133 material = Material.matchMaterial( split[1] );
355     if (material == null) {
356     player.sendMessage("Unknown material: " + split[1] );
357     return;
358     }
359 torben 1723 }
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 torben 3133 world.getBlockAt(x, y, z).setType( material );
377 torben 1723 }
378    
379     }
380    
381     }
382     }
383    
384 torben 1751 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     int count = 0;
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     count++;
406     world.getBlockAt(x, y, z).setType(Material.AIR);
407    
408     }
409    
410     }
411    
412     }
413     }
414    
415 torben 1723 private void levelArea(Player player, Location loc, String[] split) {
416    
417 torben 1725 int radius = Integer.parseInt(split[0]);
418 torben 1723
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     int count = 0;
430     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
431     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
432    
433     //for (int y=playerY; y<=playerY+radius; y++) {
434     for (int y=playerY; y<=Y_MAX; y++) {
435     count++;
436     world.getBlockAt(x, y, z).setType(Material.AIR);
437    
438     }
439    
440     }
441    
442     }
443     }
444 torben 1751
445 torben 1733
446    
447     private void resetMap(boolean[][] map) {
448     for (int x=0; x<map.length; x++) {
449     for (int z=0; z<map[0].length; z++) {
450     map[x][z] = false;
451     }
452     }
453     }
454    
455     private void cylinder(Player player, Location loc, String[] split) {
456     if (split.length != 2) {
457     player.sendMessage("Usage: /cylinder [radius] [material] ");
458     return;
459     }
460    
461 torben 3133 Material material = Material.matchMaterial( split[1] );
462     if ( material == null) {
463     player.sendMessage("/cylinder: unknown material: " + split[1]);
464     return;
465     }
466    
467 torben 1733 int radius = Integer.parseInt(split[0]);
468     int diameter = (radius*2) + 1;
469    
470     boolean map[][] = new boolean[diameter][diameter];
471     resetMap( map );
472    
473     /* map[0][0] = true;
474     map[0][diameter-1] = true;
475     map[diameter-1][0] = true;
476     map[diameter-1][diameter-1] = true;*/
477    
478     for (int r=0; r<360; r++) {
479     double rad = Math.toRadians( r );
480    
481     double a = (double) radius * Math.sin( rad);
482     double b = (double) radius * Math.cos( rad);
483    
484     int x = radius + (int)Math.round(a);
485     int y = radius + (int)Math.round(b);
486    
487     map[x][y] = true;
488     }
489    
490 torben 1723
491 torben 1733 drawMap( loc, map, material );
492     }
493    
494 torben 3133 private void drawMap(Location loc, boolean[][] map, Material material) {
495 torben 1733 int playerX = loc.getBlockX();
496     int playerY = loc.getBlockY();
497     int playerZ = loc.getBlockZ();
498    
499     World world = loc.getWorld();
500    
501     for (int i=0; i<map.length; i++) {
502     for (int j=0; j<map[0].length; j++) {
503    
504     int x = playerX + i + 1;
505     int z = playerZ + j;
506    
507     if (map[i][j] == true) {
508     for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
509 torben 3133 world.getBlockAt(x, y, z).setType(material);
510 torben 1733 }
511     }
512    
513     }
514     }
515     }
516    
517 torben 1723
518 torben 1733
519 torben 1723 }

  ViewVC Help
Powered by ViewVC 1.1.20