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