/[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 3141 - (hide annotations) (download)
Fri Nov 18 21:12:47 2016 UTC (7 years, 6 months ago) by torben
File size: 11586 byte(s)
creative add more materials
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    
126     if ( label.equals("slopearea") ) {
127     slopeArea(player, loc, args);
128     return true;
129     }
130    
131    
132     if ( label.equals("setsurface") ) {
133     setSurface(player, loc, args);
134     return true;
135     }
136 torben 1730
137     if (label.equals("platform") ) {
138     platform(player,loc,args);
139     return true;
140     }
141 torben 1733
142     if (label.equals("cylinder") ) {
143     if (validateLevelOrFill(player, label, args) ) {
144     cylinder(player,loc,args);
145     }
146     return true;
147     }
148 torben 1723
149    
150     return false;
151     }
152    
153    
154 torben 1725 void slopeArea(Player player, Location loc, String[] split) {
155     int radius;
156     try {
157     radius = Integer.parseInt(split[0]);
158     } catch (Exception e) {
159     player.sendMessage("setsurface: radius must be an integer");
160     return;
161     }
162 torben 1723
163     System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius);
164    
165    
166     int playerX = loc.getBlockX();
167     int playerY = loc.getBlockY();
168     int playerZ = loc.getBlockZ();
169    
170     World world = loc.getWorld();
171    
172     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
173     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
174    
175    
176     int xdist = Math.abs(playerX-x);
177     int zdist = Math.abs(playerZ-z);
178    
179     int dist = Math.max(xdist,zdist);
180    
181     //for (int y=playerY; y<=playerY+radius; y++) {
182     for (int y=(playerY+dist); y<=Y_MAX; y++) {
183     world.getBlockAt(x, y, z).setType(Material.AIR);
184     }
185    
186     }
187    
188     }
189     }
190    
191 torben 1730 private void platform(Player player, Location loc, String[] split) {
192 torben 1723
193 torben 1730 if (split.length != 2) {
194 torben 3133 player.sendMessage("Usage /platform [radius] [material]");
195 torben 1730 return;
196     }
197 torben 1723
198 torben 1730 int radius;
199     try {
200     radius = Integer.parseInt(split[0]);
201     } catch (Exception e) {
202     player.sendMessage("platform: radius must be an integer");
203     return;
204     }
205 torben 1723
206 torben 1730 if (radius > 20) {
207     player.sendMessage("platform: radius may not exceed 20");
208     return;
209     }
210    
211 torben 3133 Material material = Material.matchMaterial( split[1] );
212     if (material == null) {
213 torben 3140 player.sendMessage("platform: unknown material: " + split[1] );
214 torben 1730 return;
215     }
216     /*
217     boolean validblock = false;
218     for (int i=0; i<valid_block_array.length; i++) {
219     if (valid_block_array[i] == blockid) {
220     validblock = true;
221     break;
222     }
223     }
224    
225     if ( !validblock ) {
226     player.sendMessage("setsurface: block now allowed");
227     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 torben 3133 world.getBlockAt(x, y, z).setType( material );
242 torben 1730 }
243     }
244     }
245    
246     private void setSurface(Player player, Location loc, String[] split) {
247    
248 torben 1725 if (split.length != 2) {
249 torben 1723 player.sendMessage("Usage /setsurface [radius] [blockID]");
250     return;
251     }
252    
253     int radius;
254     try {
255 torben 1725 radius = Integer.parseInt(split[0]);
256 torben 1723 } 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 torben 3133 Material material = Material.matchMaterial( split[1] );
267     if (material == null) {
268 torben 3140 player.sendMessage("setsurface: unknown material: " + split[1] );
269 torben 1723 return;
270     }
271    
272 torben 1731 //check if the blockid is in the array of valid blocks
273 torben 3133 if ( ! valid_materials.contains(material) ) {
274 torben 1723 player.sendMessage("setsurface: block now allowed");
275     return;
276     }
277    
278     int playerX = loc.getBlockX();
279     int playerY = loc.getBlockY();
280     int playerZ = loc.getBlockZ();
281    
282 torben 3133 if(playerY <= 2 && material != Material.BEDROCK ) {
283 torben 1723 player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
284     return;
285     }
286    
287     World world = loc.getWorld();
288    
289     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
290     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
291     //int y = getGroundLevel(x,z) - 1;
292 torben 1729 int y = world.getHighestBlockYAt(x, z) ;
293 torben 3139 y -= 1;
294 torben 1723
295 torben 3133 world.getBlockAt(x, y, z).setType(material);
296 torben 1723 }
297     }
298     }
299    
300 torben 1725 private boolean validateLevelOrFill(Player player, String label, String[] split) {
301     if (split.length < 1 || split.length > 2) {
302     player.sendMessage("Usage: " + label + " [radius]");
303 torben 1723 return false;
304     }
305    
306     int radius;
307     try {
308 torben 1725 radius = Integer.parseInt(split[0]);
309 torben 1723 } catch (Exception e) {
310 torben 1725 player.sendMessage(label + ": radius must be an integer");
311 torben 1723 return false;
312     }
313    
314     if (radius > 20) {
315 torben 1725 player.sendMessage(label + ": radius may not exceed 20");
316 torben 1723 return false;
317     }
318    
319 torben 1725 if (split.length == 2) {
320 torben 3139 Material material = Material.matchMaterial( split[1] );
321     if (material == null) {
322 torben 3140 player.sendMessage(label + ": unknown material: " + split[1] );
323 torben 1723 return false;
324     }
325 torben 3139 if ( material == Material.TNT) {
326 torben 1723 player.sendMessage("Sorry dave, i can't do that");
327     return false;
328     }
329    
330     }
331    
332     return true;
333     }
334    
335     private void fillArea(Player player, Location loc, String[] split) {
336 torben 1725 int radius = Integer.parseInt(split[0]);
337 torben 1723
338 torben 3133 Material material = Material.DIRT;
339    
340 torben 1723
341 torben 1725 if (split.length == 2) {
342 torben 3133 material = Material.matchMaterial( split[1] );
343     if (material == null) {
344     player.sendMessage("Unknown material: " + split[1] );
345     return;
346     }
347 torben 1723 }
348    
349     System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
350    
351    
352     int playerX = loc.getBlockX();
353     int playerY = loc.getBlockY();
354     int playerZ = loc.getBlockZ();
355    
356     World world = loc.getWorld();
357    
358     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
359     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
360    
361    
362    
363     for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
364 torben 3133 world.getBlockAt(x, y, z).setType( material );
365 torben 1723 }
366    
367     }
368    
369     }
370     }
371    
372 torben 1751 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 torben 1723 private void levelArea(Player player, Location loc, String[] split) {
404    
405 torben 1725 int radius = Integer.parseInt(split[0]);
406 torben 1723
407     System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
408    
409    
410     int playerX = loc.getBlockX();
411     int playerY = loc.getBlockY();
412     int playerZ = loc.getBlockZ();
413    
414    
415     World world = loc.getWorld();
416    
417     int count = 0;
418     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
419     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
420    
421     //for (int y=playerY; y<=playerY+radius; y++) {
422     for (int y=playerY; y<=Y_MAX; y++) {
423     count++;
424     world.getBlockAt(x, y, z).setType(Material.AIR);
425    
426     }
427    
428     }
429    
430     }
431     }
432 torben 1751
433 torben 1733
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 torben 3133 Material material = Material.matchMaterial( split[1] );
450     if ( material == null) {
451     player.sendMessage("/cylinder: unknown material: " + split[1]);
452     return;
453     }
454    
455 torben 1733 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 torben 1723
479 torben 1733 drawMap( loc, map, material );
480     }
481    
482 torben 3133 private void drawMap(Location loc, boolean[][] map, Material material) {
483 torben 1733 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 torben 3133 world.getBlockAt(x, y, z).setType(material);
498 torben 1733 }
499     }
500    
501     }
502     }
503     }
504    
505 torben 1723
506 torben 1733
507 torben 1723 }

  ViewVC Help
Powered by ViewVC 1.1.20