/[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 1751 - (hide annotations) (download)
Sun Mar 18 14:58:22 2012 UTC (12 years, 2 months ago) by torben
File size: 10505 byte(s)
add createCave
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 1724
15 torben 1723 public class GeneralContractorCommands implements CommandExecutor{
16    
17 torben 1729 class StoredCommand {
18     public StoredCommand(String l, String a[]) {
19     this.label = l;
20     this.args = a;
21     }
22     public String label;
23     public String args[];
24     }
25    
26 torben 1723 final static int Y_MAX = 255;
27 torben 1730 final 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} ;
28 torben 1729
29     HashMap<String,StoredCommand> commands = new HashMap<String,StoredCommand>();
30 torben 1730
31    
32 torben 1731 public GeneralContractorCommands() {
33     Arrays.sort(valid_block_array);
34     }
35 torben 1723
36     //@Override
37     public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
38    
39     if (! (sender instanceof Player) ) {
40     return false;
41     }
42     Player player = (Player) sender;
43    
44     Location loc = player.getLocation();
45    
46     if ( ! loc.getWorld().getName().equals("creative")) {
47     player.sendMessage( ChatColor.RED + "This command may only be used in creative world" );
48     return true;
49     }
50 torben 1729
51    
52 torben 1730 if ( command.getLabel().equals("replay") ) {
53 torben 1729 StoredCommand cmd = commands.get(player.getName() );
54     if (cmd != null) {
55     label = cmd.label;
56     args = cmd.args;
57     }
58 torben 1730 } else {
59     commands.put(player.getName(), new StoredCommand(label,args) );
60 torben 1729 }
61 torben 1723
62     //System.out.println( ">>" + label); //DEBUG
63    
64     if ( label.equals("levelarea") ) {
65 torben 1726 if (validateLevelOrFill(player, label, args) ) {
66 torben 1723 levelArea(player, loc, args);
67     }
68     return true;
69     }
70    
71 torben 1751 if ( label.equals("createcave") ) {
72     if (validateLevelOrFill(player, label, args) ) {
73     createCave(player, loc, args);
74     }
75     return true;
76     }
77 torben 1723
78     if ( label.equals("fillarea") ) {
79 torben 1726 if (validateLevelOrFill(player, label, args) ) {
80 torben 1723 fillArea(player, loc, args);
81     }
82     return true;
83     }
84    
85     if ( label.equals("slopearea") ) {
86     slopeArea(player, loc, args);
87     return true;
88     }
89    
90    
91     if ( label.equals("setsurface") ) {
92     setSurface(player, loc, args);
93     return true;
94     }
95 torben 1730
96     if (label.equals("platform") ) {
97     platform(player,loc,args);
98     return true;
99     }
100 torben 1733
101     if (label.equals("cylinder") ) {
102     if (validateLevelOrFill(player, label, args) ) {
103     cylinder(player,loc,args);
104     }
105     return true;
106     }
107 torben 1723
108    
109     return false;
110     }
111    
112    
113 torben 1725 void slopeArea(Player player, Location loc, String[] split) {
114     int radius;
115     try {
116     radius = Integer.parseInt(split[0]);
117     } catch (Exception e) {
118     player.sendMessage("setsurface: radius must be an integer");
119     return;
120     }
121 torben 1723
122     System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius);
123    
124    
125     int playerX = loc.getBlockX();
126     int playerY = loc.getBlockY();
127     int playerZ = loc.getBlockZ();
128    
129     World world = loc.getWorld();
130    
131     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
132     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
133    
134    
135     int xdist = Math.abs(playerX-x);
136     int zdist = Math.abs(playerZ-z);
137    
138     int dist = Math.max(xdist,zdist);
139    
140     //for (int y=playerY; y<=playerY+radius; y++) {
141     for (int y=(playerY+dist); y<=Y_MAX; y++) {
142     world.getBlockAt(x, y, z).setType(Material.AIR);
143     }
144    
145     }
146    
147     }
148     }
149    
150 torben 1730 private void platform(Player player, Location loc, String[] split) {
151 torben 1723
152 torben 1730 if (split.length != 2) {
153     player.sendMessage("Usage /platform [radius] [blockID]");
154     return;
155     }
156 torben 1723
157 torben 1730 int radius;
158     try {
159     radius = Integer.parseInt(split[0]);
160     } catch (Exception e) {
161     player.sendMessage("platform: radius must be an integer");
162     return;
163     }
164 torben 1723
165 torben 1730 if (radius > 20) {
166     player.sendMessage("platform: radius may not exceed 20");
167     return;
168     }
169    
170     int blockid;
171     try {
172     blockid = Integer.parseInt(split[1]);
173     } catch (Exception e) {
174     player.sendMessage("platform: blockid must be an integer");
175     return;
176     }
177     /*
178     boolean validblock = false;
179     for (int i=0; i<valid_block_array.length; i++) {
180     if (valid_block_array[i] == blockid) {
181     validblock = true;
182     break;
183     }
184     }
185    
186     if ( !validblock ) {
187     player.sendMessage("setsurface: block now allowed");
188     return;
189     }*/
190    
191     int playerX = loc.getBlockX();
192     int playerY = loc.getBlockY();
193     int playerZ = loc.getBlockZ();
194    
195    
196     World world = loc.getWorld();
197    
198     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
199     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
200     int y = playerY - 1;
201    
202     world.getBlockAt(x, y, z).setTypeId(blockid);
203     }
204     }
205     }
206    
207     private void setSurface(Player player, Location loc, String[] split) {
208    
209 torben 1725 if (split.length != 2) {
210 torben 1723 player.sendMessage("Usage /setsurface [radius] [blockID]");
211     return;
212     }
213    
214     int radius;
215     try {
216 torben 1725 radius = Integer.parseInt(split[0]);
217 torben 1723 } catch (Exception e) {
218     player.sendMessage("setsurface: radius must be an integer");
219     return;
220     }
221    
222     if (radius > 20) {
223     player.sendMessage("setsurface: radius may not exceed 20");
224     return;
225     }
226    
227     int blockid;
228     try {
229 torben 1725 blockid = Integer.parseInt(split[1]);
230 torben 1723 } catch (Exception e) {
231     player.sendMessage("setsurface: blockid must be an integer");
232     return;
233     }
234    
235 torben 1731 //check if the blockid is in the array of valid blocks
236     boolean validblock = ( Arrays.binarySearch(valid_block_array, blockid) >= 0);
237 torben 1723
238     if ( !validblock ) {
239     player.sendMessage("setsurface: block now allowed");
240     return;
241     }
242    
243     int playerX = loc.getBlockX();
244     int playerY = loc.getBlockY();
245     int playerZ = loc.getBlockZ();
246    
247     if(playerY <= 2 && blockid != 7) {
248     player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
249     return;
250     }
251    
252     World world = loc.getWorld();
253    
254     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
255     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
256     //int y = getGroundLevel(x,z) - 1;
257 torben 1729 int y = world.getHighestBlockYAt(x, z) ;
258 torben 1723
259     world.getBlockAt(x, y, z).setTypeId(blockid);
260     }
261     }
262     }
263    
264 torben 1725 private boolean validateLevelOrFill(Player player, String label, String[] split) {
265     if (split.length < 1 || split.length > 2) {
266     player.sendMessage("Usage: " + label + " [radius]");
267 torben 1723 return false;
268     }
269    
270     int radius;
271     try {
272 torben 1725 radius = Integer.parseInt(split[0]);
273 torben 1723 } catch (Exception e) {
274 torben 1725 player.sendMessage(label + ": radius must be an integer");
275 torben 1723 return false;
276     }
277    
278     if (radius > 20) {
279 torben 1725 player.sendMessage(label + ": radius may not exceed 20");
280 torben 1723 return false;
281     }
282    
283 torben 1725 if (split.length == 2) {
284 torben 1723 int id;
285     try {
286 torben 1725 id = Integer.parseInt( split[1] );
287 torben 1723 } catch (Exception e) {
288 torben 1725 player.sendMessage(label + ": id must be an integer");
289 torben 1723 return false;
290     }
291     if ( id == 46) {
292     player.sendMessage("Sorry dave, i can't do that");
293     return false;
294     }
295    
296     }
297    
298     return true;
299     }
300    
301     private void fillArea(Player player, Location loc, String[] split) {
302 torben 1725 int radius = Integer.parseInt(split[0]);
303 torben 1723
304     int material = Material.DIRT.getId();
305    
306 torben 1725 if (split.length == 2) {
307     material = Integer.parseInt( split[1] );
308 torben 1723 }
309    
310     System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
311    
312    
313     int playerX = loc.getBlockX();
314     int playerY = loc.getBlockY();
315     int playerZ = loc.getBlockZ();
316    
317     World world = loc.getWorld();
318    
319     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
320     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
321    
322    
323    
324     for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
325     world.getBlockAt(x, y, z).setTypeId(material);
326     }
327    
328     }
329    
330     }
331     }
332    
333 torben 1751 private void createCave(Player player, Location loc, String[] split) {
334    
335     int radius = Integer.parseInt(split[0]);
336    
337     System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
338    
339    
340     int playerX = loc.getBlockX();
341     int playerY = loc.getBlockY();
342     int playerZ = loc.getBlockZ();
343    
344    
345     World world = loc.getWorld();
346    
347     int count = 0;
348     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
349     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
350    
351     int y_max = playerY+radius;
352     //for (int y=playerY; y<=playerY+radius; y++) {
353     for (int y=playerY; y<=y_max; y++) {
354     count++;
355     world.getBlockAt(x, y, z).setType(Material.AIR);
356    
357     }
358    
359     }
360    
361     }
362     }
363    
364 torben 1723 private void levelArea(Player player, Location loc, String[] split) {
365    
366 torben 1725 int radius = Integer.parseInt(split[0]);
367 torben 1723
368     System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
369    
370    
371     int playerX = loc.getBlockX();
372     int playerY = loc.getBlockY();
373     int playerZ = loc.getBlockZ();
374    
375    
376     World world = loc.getWorld();
377    
378     int count = 0;
379     for (int x=(playerX-radius); x<=(playerX+radius); x++) {
380     for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
381    
382     //for (int y=playerY; y<=playerY+radius; y++) {
383     for (int y=playerY; y<=Y_MAX; y++) {
384     count++;
385     world.getBlockAt(x, y, z).setType(Material.AIR);
386    
387     }
388    
389     }
390    
391     }
392     }
393 torben 1751
394 torben 1733
395    
396     private void resetMap(boolean[][] map) {
397     for (int x=0; x<map.length; x++) {
398     for (int z=0; z<map[0].length; z++) {
399     map[x][z] = false;
400     }
401     }
402     }
403    
404     private void cylinder(Player player, Location loc, String[] split) {
405     if (split.length != 2) {
406     player.sendMessage("Usage: /cylinder [radius] [material] ");
407     return;
408     }
409     int material = Integer.parseInt( split[1] );
410    
411     int radius = Integer.parseInt(split[0]);
412     int diameter = (radius*2) + 1;
413    
414     boolean map[][] = new boolean[diameter][diameter];
415     resetMap( map );
416    
417     /* map[0][0] = true;
418     map[0][diameter-1] = true;
419     map[diameter-1][0] = true;
420     map[diameter-1][diameter-1] = true;*/
421    
422     for (int r=0; r<360; r++) {
423     double rad = Math.toRadians( r );
424    
425     double a = (double) radius * Math.sin( rad);
426     double b = (double) radius * Math.cos( rad);
427    
428     int x = radius + (int)Math.round(a);
429     int y = radius + (int)Math.round(b);
430    
431     map[x][y] = true;
432     }
433    
434 torben 1723
435 torben 1733 drawMap( loc, map, material );
436     }
437    
438     private void drawMap(Location loc, boolean[][] map, int material) {
439     int playerX = loc.getBlockX();
440     int playerY = loc.getBlockY();
441     int playerZ = loc.getBlockZ();
442    
443     World world = loc.getWorld();
444    
445     for (int i=0; i<map.length; i++) {
446     for (int j=0; j<map[0].length; j++) {
447    
448     int x = playerX + i + 1;
449     int z = playerZ + j;
450    
451     if (map[i][j] == true) {
452     for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
453     world.getBlockAt(x, y, z).setTypeId(material);
454     }
455     }
456    
457     }
458     }
459     }
460    
461 torben 1723
462 torben 1733
463 torben 1723 }

  ViewVC Help
Powered by ViewVC 1.1.20