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 |
|
13 |
public class GeneralContractorCommands implements CommandExecutor{ |
14 |
|
15 |
final static int Y_MAX = 255; |
16 |
|
17 |
//@Override |
18 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
19 |
|
20 |
if (! (sender instanceof Player) ) { |
21 |
return false; |
22 |
} |
23 |
Player player = (Player) sender; |
24 |
|
25 |
Location loc = player.getLocation(); |
26 |
|
27 |
if ( ! loc.getWorld().getName().equals("creative")) { |
28 |
player.sendMessage( ChatColor.RED + "This command may only be used in creative world" ); |
29 |
return true; |
30 |
} |
31 |
|
32 |
//System.out.println( ">>" + label); //DEBUG |
33 |
|
34 |
if ( label.equals("levelarea") ) { |
35 |
if (validateLevelOrFill(player, label, args) ) { |
36 |
levelArea(player, loc, args); |
37 |
} |
38 |
return true; |
39 |
} |
40 |
|
41 |
|
42 |
if ( label.equals("fillarea") ) { |
43 |
if (validateLevelOrFill(player, label, args) ) { |
44 |
fillArea(player, loc, args); |
45 |
} |
46 |
return true; |
47 |
} |
48 |
|
49 |
if ( label.equals("slopearea") ) { |
50 |
slopeArea(player, loc, args); |
51 |
return true; |
52 |
} |
53 |
|
54 |
|
55 |
if ( label.equals("setsurface") ) { |
56 |
setSurface(player, loc, args); |
57 |
return true; |
58 |
} |
59 |
|
60 |
|
61 |
return false; |
62 |
} |
63 |
|
64 |
|
65 |
void slopeArea(Player player, Location loc, String[] split) { |
66 |
int radius; |
67 |
try { |
68 |
radius = Integer.parseInt(split[0]); |
69 |
} catch (Exception e) { |
70 |
player.sendMessage("setsurface: radius must be an integer"); |
71 |
return; |
72 |
} |
73 |
|
74 |
System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius); |
75 |
|
76 |
|
77 |
int playerX = loc.getBlockX(); |
78 |
int playerY = loc.getBlockY(); |
79 |
int playerZ = loc.getBlockZ(); |
80 |
|
81 |
World world = loc.getWorld(); |
82 |
|
83 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
84 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
85 |
|
86 |
|
87 |
int xdist = Math.abs(playerX-x); |
88 |
int zdist = Math.abs(playerZ-z); |
89 |
|
90 |
int dist = Math.max(xdist,zdist); |
91 |
|
92 |
//for (int y=playerY; y<=playerY+radius; y++) { |
93 |
for (int y=(playerY+dist); y<=Y_MAX; y++) { |
94 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
} |
100 |
} |
101 |
|
102 |
private void setSurface(Player player, Location loc, String[] split) { |
103 |
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} ; |
104 |
|
105 |
|
106 |
|
107 |
if (split.length != 2) { |
108 |
player.sendMessage("Usage /setsurface [radius] [blockID]"); |
109 |
return; |
110 |
} |
111 |
|
112 |
|
113 |
int radius; |
114 |
try { |
115 |
radius = Integer.parseInt(split[0]); |
116 |
} catch (Exception e) { |
117 |
player.sendMessage("setsurface: radius must be an integer"); |
118 |
return; |
119 |
} |
120 |
|
121 |
if (radius > 20) { |
122 |
player.sendMessage("setsurface: radius may not exceed 20"); |
123 |
return; |
124 |
} |
125 |
|
126 |
|
127 |
int blockid; |
128 |
try { |
129 |
blockid = Integer.parseInt(split[1]); |
130 |
} catch (Exception e) { |
131 |
player.sendMessage("setsurface: blockid must be an integer"); |
132 |
return; |
133 |
} |
134 |
|
135 |
boolean validblock = false; |
136 |
for (int i=0; i<valid_block_array.length; i++) { |
137 |
if (valid_block_array[i] == blockid) { |
138 |
validblock = true; |
139 |
break; |
140 |
} |
141 |
} |
142 |
|
143 |
if ( !validblock ) { |
144 |
player.sendMessage("setsurface: block now allowed"); |
145 |
return; |
146 |
} |
147 |
|
148 |
int playerX = loc.getBlockX(); |
149 |
int playerY = loc.getBlockY(); |
150 |
int playerZ = loc.getBlockZ(); |
151 |
|
152 |
if(playerY <= 2 && blockid != 7) { |
153 |
player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); |
154 |
return; |
155 |
} |
156 |
|
157 |
World world = loc.getWorld(); |
158 |
|
159 |
|
160 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
161 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
162 |
//int y = getGroundLevel(x,z) - 1; |
163 |
int y = world.getHighestBlockYAt(x, z) - 1; |
164 |
|
165 |
world.getBlockAt(x, y, z).setTypeId(blockid); |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
private boolean validateLevelOrFill(Player player, String label, String[] split) { |
171 |
if (split.length < 1 || split.length > 2) { |
172 |
player.sendMessage("Usage: " + label + " [radius]"); |
173 |
return false; |
174 |
} |
175 |
|
176 |
int radius; |
177 |
try { |
178 |
radius = Integer.parseInt(split[0]); |
179 |
} catch (Exception e) { |
180 |
player.sendMessage(label + ": radius must be an integer"); |
181 |
return false; |
182 |
} |
183 |
|
184 |
if (radius > 20) { |
185 |
player.sendMessage(label + ": radius may not exceed 20"); |
186 |
return false; |
187 |
} |
188 |
|
189 |
if (split.length == 2) { |
190 |
int id; |
191 |
try { |
192 |
id = Integer.parseInt( split[1] ); |
193 |
} catch (Exception e) { |
194 |
player.sendMessage(label + ": id must be an integer"); |
195 |
return false; |
196 |
} |
197 |
if ( id == 46) { |
198 |
player.sendMessage("Sorry dave, i can't do that"); |
199 |
return false; |
200 |
} |
201 |
|
202 |
} |
203 |
|
204 |
return true; |
205 |
} |
206 |
|
207 |
private void fillArea(Player player, Location loc, String[] split) { |
208 |
int radius = Integer.parseInt(split[0]); |
209 |
|
210 |
int material = Material.DIRT.getId(); |
211 |
|
212 |
if (split.length == 2) { |
213 |
material = Integer.parseInt( split[1] ); |
214 |
} |
215 |
|
216 |
System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); |
217 |
|
218 |
|
219 |
int playerX = loc.getBlockX(); |
220 |
int playerY = loc.getBlockY(); |
221 |
int playerZ = loc.getBlockZ(); |
222 |
|
223 |
World world = loc.getWorld(); |
224 |
|
225 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
226 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
227 |
|
228 |
|
229 |
|
230 |
for (int y=world.getHighestBlockYAt(x, z); y<playerY; y++) { |
231 |
world.getBlockAt(x, y, z).setTypeId(material); |
232 |
} |
233 |
|
234 |
} |
235 |
|
236 |
} |
237 |
} |
238 |
|
239 |
private void levelArea(Player player, Location loc, String[] split) { |
240 |
|
241 |
int radius = Integer.parseInt(split[0]); |
242 |
|
243 |
System.out.println("Player " + player.getName() + " used levelarea with radius=" + radius); |
244 |
|
245 |
|
246 |
int playerX = loc.getBlockX(); |
247 |
int playerY = loc.getBlockY(); |
248 |
int playerZ = loc.getBlockZ(); |
249 |
|
250 |
|
251 |
World world = loc.getWorld(); |
252 |
|
253 |
int count = 0; |
254 |
for (int x=(playerX-radius); x<=(playerX+radius); x++) { |
255 |
for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { |
256 |
|
257 |
//for (int y=playerY; y<=playerY+radius; y++) { |
258 |
for (int y=playerY; y<=Y_MAX; y++) { |
259 |
count++; |
260 |
world.getBlockAt(x, y, z).setType(Material.AIR); |
261 |
|
262 |
} |
263 |
|
264 |
} |
265 |
|
266 |
} |
267 |
} |
268 |
|
269 |
|
270 |
} |