/[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 1751 - (show 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 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
15 public class GeneralContractorCommands implements CommandExecutor{
16
17 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 final static int Y_MAX = 255;
27 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
29 HashMap<String,StoredCommand> commands = new HashMap<String,StoredCommand>();
30
31
32 public GeneralContractorCommands() {
33 Arrays.sort(valid_block_array);
34 }
35
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
51
52 if ( command.getLabel().equals("replay") ) {
53 StoredCommand cmd = commands.get(player.getName() );
54 if (cmd != null) {
55 label = cmd.label;
56 args = cmd.args;
57 }
58 } else {
59 commands.put(player.getName(), new StoredCommand(label,args) );
60 }
61
62 //System.out.println( ">>" + label); //DEBUG
63
64 if ( label.equals("levelarea") ) {
65 if (validateLevelOrFill(player, label, args) ) {
66 levelArea(player, loc, args);
67 }
68 return true;
69 }
70
71 if ( label.equals("createcave") ) {
72 if (validateLevelOrFill(player, label, args) ) {
73 createCave(player, loc, args);
74 }
75 return true;
76 }
77
78 if ( label.equals("fillarea") ) {
79 if (validateLevelOrFill(player, label, args) ) {
80 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
96 if (label.equals("platform") ) {
97 platform(player,loc,args);
98 return true;
99 }
100
101 if (label.equals("cylinder") ) {
102 if (validateLevelOrFill(player, label, args) ) {
103 cylinder(player,loc,args);
104 }
105 return true;
106 }
107
108
109 return false;
110 }
111
112
113 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
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 private void platform(Player player, Location loc, String[] split) {
151
152 if (split.length != 2) {
153 player.sendMessage("Usage /platform [radius] [blockID]");
154 return;
155 }
156
157 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
165 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 if (split.length != 2) {
210 player.sendMessage("Usage /setsurface [radius] [blockID]");
211 return;
212 }
213
214 int radius;
215 try {
216 radius = Integer.parseInt(split[0]);
217 } 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 blockid = Integer.parseInt(split[1]);
230 } catch (Exception e) {
231 player.sendMessage("setsurface: blockid must be an integer");
232 return;
233 }
234
235 //check if the blockid is in the array of valid blocks
236 boolean validblock = ( Arrays.binarySearch(valid_block_array, blockid) >= 0);
237
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 int y = world.getHighestBlockYAt(x, z) ;
258
259 world.getBlockAt(x, y, z).setTypeId(blockid);
260 }
261 }
262 }
263
264 private boolean validateLevelOrFill(Player player, String label, String[] split) {
265 if (split.length < 1 || split.length > 2) {
266 player.sendMessage("Usage: " + label + " [radius]");
267 return false;
268 }
269
270 int radius;
271 try {
272 radius = Integer.parseInt(split[0]);
273 } catch (Exception e) {
274 player.sendMessage(label + ": radius must be an integer");
275 return false;
276 }
277
278 if (radius > 20) {
279 player.sendMessage(label + ": radius may not exceed 20");
280 return false;
281 }
282
283 if (split.length == 2) {
284 int id;
285 try {
286 id = Integer.parseInt( split[1] );
287 } catch (Exception e) {
288 player.sendMessage(label + ": id must be an integer");
289 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 int radius = Integer.parseInt(split[0]);
303
304 int material = Material.DIRT.getId();
305
306 if (split.length == 2) {
307 material = Integer.parseInt( split[1] );
308 }
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 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 private void levelArea(Player player, Location loc, String[] split) {
365
366 int radius = Integer.parseInt(split[0]);
367
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
394
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
435 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
462
463 }

  ViewVC Help
Powered by ViewVC 1.1.20