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 |
|
96 |
return false; |
97 |
} |
98 |
|
99 |
|
100 |
void slopeArea(Player player, Location loc, String[] split) { |
101 |
int radius; |
102 |
try { |
103 |
radius = Integer.parseInt(split[0]); |
104 |
} catch (Exception e) { |
105 |
player.sendMessage("setsurface: radius must be an integer"); |
106 |
return; |
107 |
} |
108 |
|
109 |
System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius); |
110 |
|
111 |
|
112 |
int playerX = loc.getBlockX(); |
113 |
int playerY = loc.getBlockY(); |
114 |
int playerZ = loc.getBlockZ(); |
115 |
|
116 |
World world = loc.getWorld(); |
117 |
|
118 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
119 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
120 |
|
121 |
|
122 |
int xdist = Math.abs(playerX-x); |
123 |
int zdist = Math.abs(playerZ-z); |
124 |
|
125 |
int dist = Math.max(xdist,zdist); |
126 |
|
127 |
//for (int y=playerY; y<=playerY+radius; y++) { |
128 |
for (int y=(playerY+dist); y<=Y_MAX; y++) { |
129 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
130 |
} |
131 |
|
132 |
} |
133 |
|
134 |
} |
135 |
} |
136 |
|
137 |
private void platform(Player player, Location loc, String[] split) { |
138 |
|
139 |
if (split.length != 2) { |
140 |
player.sendMessage("Usage /platform [radius] [blockID]"); |
141 |
return; |
142 |
} |
143 |
|
144 |
int radius; |
145 |
try { |
146 |
radius = Integer.parseInt(split[0]); |
147 |
} catch (Exception e) { |
148 |
player.sendMessage("platform: radius must be an integer"); |
149 |
return; |
150 |
} |
151 |
|
152 |
if (radius > 20) { |
153 |
player.sendMessage("platform: radius may not exceed 20"); |
154 |
return; |
155 |
} |
156 |
|
157 |
int blockid; |
158 |
try { |
159 |
blockid = Integer.parseInt(split[1]); |
160 |
} catch (Exception e) { |
161 |
player.sendMessage("platform: blockid must be an integer"); |
162 |
return; |
163 |
} |
164 |
/* |
165 |
boolean validblock = false; |
166 |
for (int i=0; i<valid_block_array.length; i++) { |
167 |
if (valid_block_array[i] == blockid) { |
168 |
validblock = true; |
169 |
break; |
170 |
} |
171 |
} |
172 |
|
173 |
if ( !validblock ) { |
174 |
player.sendMessage("setsurface: block now allowed"); |
175 |
return; |
176 |
}*/ |
177 |
|
178 |
int playerX = loc.getBlockX(); |
179 |
int playerY = loc.getBlockY(); |
180 |
int playerZ = loc.getBlockZ(); |
181 |
|
182 |
|
183 |
World world = loc.getWorld(); |
184 |
|
185 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
186 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
187 |
int y = playerY - 1; |
188 |
|
189 |
world.getBlockAt(x, y, z).setTypeId(blockid); |
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
private void setSurface(Player player, Location loc, String[] split) { |
195 |
|
196 |
if (split.length != 2) { |
197 |
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
198 |
return; |
199 |
} |
200 |
|
201 |
int radius; |
202 |
try { |
203 |
radius = Integer.parseInt(split[0]); |
204 |
} catch (Exception e) { |
205 |
player.sendMessage("setsurface: radius must be an integer"); |
206 |
return; |
207 |
} |
208 |
|
209 |
if (radius > 20) { |
210 |
player.sendMessage("setsurface: radius may not exceed 20"); |
211 |
return; |
212 |
} |
213 |
|
214 |
int blockid; |
215 |
try { |
216 |
blockid = Integer.parseInt(split[1]); |
217 |
} catch (Exception e) { |
218 |
player.sendMessage("setsurface: blockid must be an integer"); |
219 |
return; |
220 |
} |
221 |
|
222 |
//check if the blockid is in the array of valid blocks |
223 |
boolean validblock = ( Arrays.binarySearch(valid_block_array, blockid) >= 0); |
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 |
if(playerY <= 2 && blockid != 7) { |
235 |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
236 |
return; |
237 |
} |
238 |
|
239 |
World world = loc.getWorld(); |
240 |
|
241 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
242 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
243 |
//int y = getGroundLevel(x,z) - 1; |
244 |
int y = world.getHighestBlockYAt(x, z) ; |
245 |
|
246 |
world.getBlockAt(x, y, z).setTypeId(blockid); |
247 |
} |
248 |
} |
249 |
} |
250 |
|
251 |
private boolean validateLevelOrFill(Player player, String label, String[] split) { |
252 |
if (split.length < 1 || split.length > 2) { |
253 |
player.sendMessage("Usage: " + label + " [radius]"); |
254 |
return false; |
255 |
} |
256 |
|
257 |
int radius; |
258 |
try { |
259 |
radius = Integer.parseInt(split[0]); |
260 |
} catch (Exception e) { |
261 |
player.sendMessage(label + ": radius must be an integer"); |
262 |
return false; |
263 |
} |
264 |
|
265 |
if (radius > 20) { |
266 |
player.sendMessage(label + ": radius may not exceed 20"); |
267 |
return false; |
268 |
} |
269 |
|
270 |
if (split.length == 2) { |
271 |
int id; |
272 |
try { |
273 |
id = Integer.parseInt( split[1] ); |
274 |
} catch (Exception e) { |
275 |
player.sendMessage(label + ": id must be an integer"); |
276 |
return false; |
277 |
} |
278 |
if ( id == 46) { |
279 |
player.sendMessage("Sorry dave, i can't do that"); |
280 |
return false; |
281 |
} |
282 |
|
283 |
} |
284 |
|
285 |
return true; |
286 |
} |
287 |
|
288 |
private void fillArea(Player player, Location loc, String[] split) { |
289 |
int radius = Integer.parseInt(split[0]); |
290 |
|
291 |
int material = Material.DIRT.getId(); |
292 |
|
293 |
if (split.length == 2) { |
294 |
material = Integer.parseInt( split[1] ); |
295 |
} |
296 |
|
297 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
298 |
|
299 |
|
300 |
int playerX = loc.getBlockX(); |
301 |
int playerY = loc.getBlockY(); |
302 |
int playerZ = loc.getBlockZ(); |
303 |
|
304 |
World world = loc.getWorld(); |
305 |
|
306 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
307 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
308 |
|
309 |
|
310 |
|
311 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
312 |
world.getBlockAt(x, y, z).setTypeId(material); |
313 |
} |
314 |
|
315 |
} |
316 |
|
317 |
} |
318 |
} |
319 |
|
320 |
private void levelArea(Player player, Location loc, String[] split) { |
321 |
|
322 |
int radius = Integer.parseInt(split[0]); |
323 |
|
324 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
325 |
|
326 |
|
327 |
int playerX = loc.getBlockX(); |
328 |
int playerY = loc.getBlockY(); |
329 |
int playerZ = loc.getBlockZ(); |
330 |
|
331 |
|
332 |
World world = loc.getWorld(); |
333 |
|
334 |
int count = 0; |
335 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
336 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
337 |
|
338 |
//for (int y=playerY; y<=playerY+radius; y++) { |
339 |
for (int y=playerY; y<=Y_MAX; y++) { |
340 |
count++; |
341 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
342 |
|
343 |
} |
344 |
|
345 |
} |
346 |
|
347 |
} |
348 |
} |
349 |
|
350 |
|
351 |
} |