1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.InputStreamReader; |
7 |
|
8 |
import org.bukkit.ChatColor; |
9 |
import org.bukkit.Location; |
10 |
import org.bukkit.Material; |
11 |
import org.bukkit.World; |
12 |
import org.bukkit.command.Command; |
13 |
import org.bukkit.command.CommandExecutor; |
14 |
import org.bukkit.command.CommandSender; |
15 |
import org.bukkit.entity.Player; |
16 |
import org.bukkit.plugin.Plugin; |
17 |
|
18 |
public class TemplateCommand implements CommandExecutor { |
19 |
|
20 |
|
21 |
|
22 |
Plugin plugin; |
23 |
|
24 |
public TemplateCommand(Plugin plugin) { |
25 |
this.plugin = plugin; |
26 |
} |
27 |
|
28 |
@Override |
29 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
30 |
if (! (sender instanceof Player) ) { |
31 |
sender.sendMessage( "this command can not be run from console" ); |
32 |
return true; |
33 |
} |
34 |
|
35 |
Player player = (Player) sender; |
36 |
|
37 |
if (! sender.isOp() ) { |
38 |
sender.sendMessage( "you need to be op to run this command" ); |
39 |
return true; |
40 |
} |
41 |
|
42 |
if ( args.length != 1) { |
43 |
sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>"); |
44 |
return true; |
45 |
} |
46 |
|
47 |
File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt"); |
48 |
if (! templateFile.exists() ) { |
49 |
sender.sendMessage( ChatColor.YELLOW + "Template not found" ); |
50 |
return true; |
51 |
} |
52 |
|
53 |
Material[][][] template; |
54 |
|
55 |
try { |
56 |
template = parseFile(templateFile); |
57 |
if( template == null) { |
58 |
sender.sendMessage( ChatColor.YELLOW + "Invalid template file" ); |
59 |
return true; |
60 |
} |
61 |
} catch (Exception e) { |
62 |
plugin.getLogger().info( e.getMessage() ); |
63 |
sender.sendMessage( ChatColor.YELLOW + e.getMessage() ); |
64 |
return true; |
65 |
} |
66 |
|
67 |
buildTemplate( player.getLocation(), template, 1); |
68 |
buildTemplate( player.getLocation(), template, 2); |
69 |
|
70 |
return false; |
71 |
} |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
Material[][][] parseFile(File templateFile) throws Exception { |
77 |
|
78 |
|
79 |
int lines = 0; |
80 |
|
81 |
try (FileInputStream fis = new FileInputStream( templateFile ); |
82 |
InputStreamReader isr = new InputStreamReader(fis); |
83 |
BufferedReader in = new BufferedReader( isr ) |
84 |
) { |
85 |
|
86 |
|
87 |
String line = in.readLine(); |
88 |
lines ++; |
89 |
|
90 |
if ( line.charAt(0) != '#') { |
91 |
throw new Exception( "Template: invalid start character" ); |
92 |
} |
93 |
|
94 |
line = line.replace('#', ' ').trim(); |
95 |
String dimensions[] = line.split(","); |
96 |
|
97 |
if ( dimensions.length != 3) { |
98 |
throw new Exception( "Template: invalid dimensions line" ); |
99 |
} |
100 |
|
101 |
int xsize = Integer.parseInt(dimensions[0]); |
102 |
int ysize = Integer.parseInt(dimensions[1]); |
103 |
int zsize = Integer.parseInt(dimensions[2]); |
104 |
|
105 |
Material[][][] array = new Material[xsize][ysize][zsize]; |
106 |
|
107 |
int x = 0; |
108 |
int y = 0; |
109 |
|
110 |
|
111 |
while ( (line = in.readLine() ) != null ) { |
112 |
lines++; |
113 |
|
114 |
line = line.trim(); |
115 |
if (line.length() == 0) |
116 |
continue; |
117 |
|
118 |
if (line.charAt(0) == '#') |
119 |
continue; |
120 |
|
121 |
if ( y >= ysize) { |
122 |
throw new Exception( "Template: found more levels in file than specified in header" ); |
123 |
} |
124 |
|
125 |
String elements[] = line.split(","); |
126 |
if (elements.length != zsize) { |
127 |
throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); |
128 |
} |
129 |
|
130 |
for (int i=0; i<elements.length; i++) { |
131 |
|
132 |
String element[] = elements[i].trim().split("\\."); |
133 |
|
134 |
|
135 |
Material m; |
136 |
try { |
137 |
m = Material.valueOf(element[0]); |
138 |
} catch (Exception e) { |
139 |
throw new Exception( "Template: invalid material on line " + lines + ": " + element[0] ); |
140 |
} |
141 |
|
142 |
|
143 |
array[x][y][i] = m; |
144 |
} |
145 |
|
146 |
x++; |
147 |
if (x >= xsize) { |
148 |
x = 0; |
149 |
y++; |
150 |
} |
151 |
} |
152 |
|
153 |
if (x != 0) { |
154 |
throw new Exception( "Template: not enough lines to complete the last level" ); |
155 |
} |
156 |
|
157 |
if (y != ysize) { |
158 |
throw new Exception( "Template: not enough levels"); |
159 |
} |
160 |
|
161 |
|
162 |
return array; |
163 |
|
164 |
} |
165 |
|
166 |
} |
167 |
|
168 |
void buildTemplate(Location loc, Material[][][] template, int pass) { |
169 |
|
170 |
|
171 |
World world = loc.getWorld(); |
172 |
for (int i=0; i<template.length; i++ ) { |
173 |
for (int j=0; j<template[0].length; j++) { |
174 |
for (int k=0; k<template[0][0].length; k++) { |
175 |
int x, y ,z; |
176 |
|
177 |
x = z = 0; |
178 |
|
179 |
y = loc.getBlockY() + j; |
180 |
if (y >= 255) { |
181 |
continue; |
182 |
} |
183 |
|
184 |
switch ( getFacing(loc) ) { |
185 |
case 0: //west |
186 |
x = loc.getBlockX() - k; |
187 |
z = loc.getBlockZ() + i + 1; |
188 |
break; |
189 |
case 1: //north |
190 |
x = loc.getBlockX() - i - 1; |
191 |
z = loc.getBlockZ() - k; |
192 |
break; |
193 |
case 2: //east |
194 |
x = loc.getBlockX() + k; |
195 |
z = loc.getBlockZ() - i - 1; |
196 |
break; |
197 |
case 3: //south |
198 |
x = loc.getBlockX() + i + 1; |
199 |
z = loc.getBlockZ() + k; |
200 |
break; |
201 |
} |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
Material mat = template[i][j][k]; |
207 |
|
208 |
//plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) ); |
209 |
|
210 |
//TODO: rewrite this to build a Set<Material> of skipped matterials |
211 |
if (pass == 1) { |
212 |
|
213 |
if ( mat == Material.TORCH || mat == Material.REDSTONE_TORCH || mat == Material.REDSTONE_WALL_TORCH) //torch / redstone torch |
214 |
continue; |
215 |
|
216 |
if (mat == Material.ACACIA_DOOR || mat == Material.BIRCH_DOOR || mat == Material.DARK_OAK_DOOR ) //door / irondoor |
217 |
continue; |
218 |
|
219 |
if (mat == Material.SIGN) // sign |
220 |
continue; |
221 |
if (mat == Material.LADDER) // ladder |
222 |
continue; |
223 |
if (mat == Material.OAK_STAIRS) //TODO: need to take all star variants into account |
224 |
continue; |
225 |
} |
226 |
|
227 |
|
228 |
world.getBlockAt(x, y, z).setType(mat); |
229 |
|
230 |
} |
231 |
} |
232 |
} |
233 |
|
234 |
} |
235 |
|
236 |
|
237 |
//yaw is left,right looking direction, 0=straight west |
238 |
int getFacing(Location loc) { |
239 |
int angle = (int) loc.getYaw(); |
240 |
|
241 |
if (angle < 45) { |
242 |
return 0; |
243 |
} else if (angle < 135) { |
244 |
return 1; |
245 |
} else if (angle < 225) { |
246 |
return 2; |
247 |
} else if (angle < 315) { |
248 |
return 3; |
249 |
} else { |
250 |
return 0; |
251 |
} |
252 |
} |
253 |
|
254 |
} |