/[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 1733 - (show annotations) (download)
Wed Mar 14 20:25:27 2012 UTC (12 years, 2 months ago) by torben
File size: 9644 byte(s)
basic version of /cylinder
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
72 if ( label.equals("fillarea") ) {
73 if (validateLevelOrFill(player, label, args) ) {
74 fillArea(player, loc, args);
75 }
76 return true;
77 }
78
79 if ( label.equals("slopearea") ) {
80 slopeArea(player, loc, args);
81 return true;
82 }
83
84
85 if ( label.equals("setsurface") ) {
86 setSurface(player, loc, args);
87 return true;
88 }
89
90 if (label.equals("platform") ) {
91 platform(player,loc,args);
92 return true;
93 }
94
95 if (label.equals("cylinder") ) {
96 if (validateLevelOrFill(player, label, args) ) {
97 cylinder(player,loc,args);
98 }
99 return true;
100 }
101
102
103 return false;
104 }
105
106
107 void slopeArea(Player player, Location loc, String[] split) {
108 int radius;
109 try {
110 radius = Integer.parseInt(split[0]);
111 } catch (Exception e) {
112 player.sendMessage("setsurface: radius must be an integer");
113 return;
114 }
115
116 System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius);
117
118
119 int playerX = loc.getBlockX();
120 int playerY = loc.getBlockY();
121 int playerZ = loc.getBlockZ();
122
123 World world = loc.getWorld();
124
125 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
126 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
127
128
129 int xdist = Math.abs(playerX-x);
130 int zdist = Math.abs(playerZ-z);
131
132 int dist = Math.max(xdist,zdist);
133
134 //for (int y=playerY; y<=playerY+radius; y++) {
135 for (int y=(playerY+dist); y<=Y_MAX; y++) {
136 world.getBlockAt(x, y, z).setType(Material.AIR);
137 }
138
139 }
140
141 }
142 }
143
144 private void platform(Player player, Location loc, String[] split) {
145
146 if (split.length != 2) {
147 player.sendMessage("Usage /platform [radius] [blockID]");
148 return;
149 }
150
151 int radius;
152 try {
153 radius = Integer.parseInt(split[0]);
154 } catch (Exception e) {
155 player.sendMessage("platform: radius must be an integer");
156 return;
157 }
158
159 if (radius > 20) {
160 player.sendMessage("platform: radius may not exceed 20");
161 return;
162 }
163
164 int blockid;
165 try {
166 blockid = Integer.parseInt(split[1]);
167 } catch (Exception e) {
168 player.sendMessage("platform: blockid must be an integer");
169 return;
170 }
171 /*
172 boolean validblock = false;
173 for (int i=0; i<valid_block_array.length; i++) {
174 if (valid_block_array[i] == blockid) {
175 validblock = true;
176 break;
177 }
178 }
179
180 if ( !validblock ) {
181 player.sendMessage("setsurface: block now allowed");
182 return;
183 }*/
184
185 int playerX = loc.getBlockX();
186 int playerY = loc.getBlockY();
187 int playerZ = loc.getBlockZ();
188
189
190 World world = loc.getWorld();
191
192 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
193 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
194 int y = playerY - 1;
195
196 world.getBlockAt(x, y, z).setTypeId(blockid);
197 }
198 }
199 }
200
201 private void setSurface(Player player, Location loc, String[] split) {
202
203 if (split.length != 2) {
204 player.sendMessage("Usage /setsurface [radius] [blockID]");
205 return;
206 }
207
208 int radius;
209 try {
210 radius = Integer.parseInt(split[0]);
211 } catch (Exception e) {
212 player.sendMessage("setsurface: radius must be an integer");
213 return;
214 }
215
216 if (radius > 20) {
217 player.sendMessage("setsurface: radius may not exceed 20");
218 return;
219 }
220
221 int blockid;
222 try {
223 blockid = Integer.parseInt(split[1]);
224 } catch (Exception e) {
225 player.sendMessage("setsurface: blockid must be an integer");
226 return;
227 }
228
229 //check if the blockid is in the array of valid blocks
230 boolean validblock = ( Arrays.binarySearch(valid_block_array, blockid) >= 0);
231
232 if ( !validblock ) {
233 player.sendMessage("setsurface: block now allowed");
234 return;
235 }
236
237 int playerX = loc.getBlockX();
238 int playerY = loc.getBlockY();
239 int playerZ = loc.getBlockZ();
240
241 if(playerY <= 2 && blockid != 7) {
242 player.sendMessage("setsurface: at this level you may only use bedrock(id=7)");
243 return;
244 }
245
246 World world = loc.getWorld();
247
248 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
249 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
250 //int y = getGroundLevel(x,z) - 1;
251 int y = world.getHighestBlockYAt(x, z) ;
252
253 world.getBlockAt(x, y, z).setTypeId(blockid);
254 }
255 }
256 }
257
258 private boolean validateLevelOrFill(Player player, String label, String[] split) {
259 if (split.length < 1 || split.length > 2) {
260 player.sendMessage("Usage: " + label + " [radius]");
261 return false;
262 }
263
264 int radius;
265 try {
266 radius = Integer.parseInt(split[0]);
267 } catch (Exception e) {
268 player.sendMessage(label + ": radius must be an integer");
269 return false;
270 }
271
272 if (radius > 20) {
273 player.sendMessage(label + ": radius may not exceed 20");
274 return false;
275 }
276
277 if (split.length == 2) {
278 int id;
279 try {
280 id = Integer.parseInt( split[1] );
281 } catch (Exception e) {
282 player.sendMessage(label + ": id must be an integer");
283 return false;
284 }
285 if ( id == 46) {
286 player.sendMessage("Sorry dave, i can't do that");
287 return false;
288 }
289
290 }
291
292 return true;
293 }
294
295 private void fillArea(Player player, Location loc, String[] split) {
296 int radius = Integer.parseInt(split[0]);
297
298 int material = Material.DIRT.getId();
299
300 if (split.length == 2) {
301 material = Integer.parseInt( split[1] );
302 }
303
304 System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius);
305
306
307 int playerX = loc.getBlockX();
308 int playerY = loc.getBlockY();
309 int playerZ = loc.getBlockZ();
310
311 World world = loc.getWorld();
312
313 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
314 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
315
316
317
318 for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
319 world.getBlockAt(x, y, z).setTypeId(material);
320 }
321
322 }
323
324 }
325 }
326
327 private void levelArea(Player player, Location loc, String[] split) {
328
329 int radius = Integer.parseInt(split[0]);
330
331 System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius);
332
333
334 int playerX = loc.getBlockX();
335 int playerY = loc.getBlockY();
336 int playerZ = loc.getBlockZ();
337
338
339 World world = loc.getWorld();
340
341 int count = 0;
342 for (int x=(playerX-radius); x<=(playerX+radius); x++) {
343 for (int z=(playerZ-radius); z<=(playerZ+radius); z++) {
344
345 //for (int y=playerY; y<=playerY+radius; y++) {
346 for (int y=playerY; y<=Y_MAX; y++) {
347 count++;
348 world.getBlockAt(x, y, z).setType(Material.AIR);
349
350 }
351
352 }
353
354 }
355 }
356
357
358 private void resetMap(boolean[][] map) {
359 for (int x=0; x<map.length; x++) {
360 for (int z=0; z<map[0].length; z++) {
361 map[x][z] = false;
362 }
363 }
364 }
365
366 private void cylinder(Player player, Location loc, String[] split) {
367 if (split.length != 2) {
368 player.sendMessage("Usage: /cylinder [radius] [material] ");
369 return;
370 }
371 int material = Integer.parseInt( split[1] );
372
373 int radius = Integer.parseInt(split[0]);
374 int diameter = (radius*2) + 1;
375
376 boolean map[][] = new boolean[diameter][diameter];
377 resetMap( map );
378
379 /* map[0][0] = true;
380 map[0][diameter-1] = true;
381 map[diameter-1][0] = true;
382 map[diameter-1][diameter-1] = true;*/
383
384 for (int r=0; r<360; r++) {
385 double rad = Math.toRadians( r );
386
387 double a = (double) radius * Math.sin( rad);
388 double b = (double) radius * Math.cos( rad);
389
390 int x = radius + (int)Math.round(a);
391 int y = radius + (int)Math.round(b);
392
393 map[x][y] = true;
394 }
395
396
397 drawMap( loc, map, material );
398 }
399
400 private void drawMap(Location loc, boolean[][] map, int material) {
401 int playerX = loc.getBlockX();
402 int playerY = loc.getBlockY();
403 int playerZ = loc.getBlockZ();
404
405 World world = loc.getWorld();
406
407 for (int i=0; i<map.length; i++) {
408 for (int j=0; j<map[0].length; j++) {
409
410 int x = playerX + i + 1;
411 int z = playerZ + j;
412
413 if (map[i][j] == true) {
414 for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) {
415 world.getBlockAt(x, y, z).setTypeId(material);
416 }
417 }
418
419 }
420 }
421 }
422
423
424
425 }

  ViewVC Help
Powered by ViewVC 1.1.20