1 |
torben |
1738 |
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.World; |
11 |
|
|
import org.bukkit.command.Command; |
12 |
|
|
import org.bukkit.command.CommandExecutor; |
13 |
|
|
import org.bukkit.command.CommandSender; |
14 |
|
|
import org.bukkit.entity.Player; |
15 |
|
|
import org.bukkit.plugin.Plugin; |
16 |
|
|
|
17 |
|
|
public class TemplateCommand implements CommandExecutor { |
18 |
|
|
|
19 |
torben |
1746 |
|
20 |
torben |
1747 |
class Pair { |
21 |
torben |
1738 |
public int id; |
22 |
|
|
public int subId; |
23 |
torben |
1746 |
|
24 |
torben |
1738 |
public Pair (int i, int s) { |
25 |
|
|
id = i; |
26 |
|
|
subId = s; |
27 |
|
|
} |
28 |
torben |
1746 |
|
29 |
torben |
1747 |
} |
30 |
torben |
1746 |
|
31 |
torben |
1738 |
Plugin plugin; |
32 |
torben |
1746 |
|
33 |
torben |
1738 |
public TemplateCommand(Plugin plugin) { |
34 |
|
|
this.plugin = plugin; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
@Override |
38 |
|
|
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
39 |
|
|
if (! (sender instanceof Player) ) { |
40 |
|
|
sender.sendMessage( "this command can not be run from console" ); |
41 |
|
|
return true; |
42 |
|
|
} |
43 |
torben |
1746 |
|
44 |
torben |
1738 |
Player player = (Player) sender; |
45 |
torben |
1746 |
|
46 |
torben |
1738 |
if (! sender.isOp() ) { |
47 |
|
|
sender.sendMessage( "you need to be op to run this command" ); |
48 |
|
|
return true; |
49 |
|
|
} |
50 |
torben |
1746 |
|
51 |
torben |
1738 |
if ( args.length != 1) { |
52 |
|
|
sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>"); |
53 |
|
|
return true; |
54 |
|
|
} |
55 |
torben |
1746 |
|
56 |
torben |
1738 |
File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt"); |
57 |
|
|
if (! templateFile.exists() ) { |
58 |
|
|
sender.sendMessage( ChatColor.YELLOW + "Template not found" ); |
59 |
|
|
return true; |
60 |
|
|
} |
61 |
torben |
1746 |
|
62 |
torben |
1747 |
Pair[][][] template; |
63 |
torben |
1746 |
|
64 |
|
|
try { |
65 |
|
|
template = parseFile(templateFile); |
66 |
|
|
if( template == null) { |
67 |
|
|
sender.sendMessage( ChatColor.YELLOW + "Invalid template file" ); |
68 |
|
|
return true; |
69 |
|
|
} |
70 |
|
|
} catch (Exception e) { |
71 |
|
|
plugin.getLogger().info( e.getMessage() ); |
72 |
|
|
sender.sendMessage( ChatColor.YELLOW + e.getMessage() ); |
73 |
|
|
return true; |
74 |
torben |
1738 |
} |
75 |
torben |
1746 |
|
76 |
torben |
1750 |
buildTemplate( player.getLocation(), template, 1); |
77 |
|
|
buildTemplate( player.getLocation(), template, 2); |
78 |
torben |
1738 |
|
79 |
|
|
return false; |
80 |
|
|
} |
81 |
|
|
|
82 |
torben |
1746 |
|
83 |
|
|
|
84 |
|
|
|
85 |
torben |
1747 |
Pair[][][] parseFile(File templateFile) throws Exception { |
86 |
torben |
1746 |
|
87 |
|
|
|
88 |
|
|
int lines = 0; |
89 |
|
|
|
90 |
|
|
FileInputStream fis = new FileInputStream( templateFile ); |
91 |
|
|
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
92 |
|
|
String line = in.readLine(); |
93 |
|
|
lines ++; |
94 |
|
|
|
95 |
|
|
if ( line.charAt(0) != '#') { |
96 |
|
|
throw new Exception( "Template: invalid start character" ); |
97 |
|
|
} |
98 |
torben |
1738 |
|
99 |
torben |
1746 |
line = line.replace('#', ' ').trim(); |
100 |
|
|
String dimensions[] = line.split(","); |
101 |
|
|
|
102 |
|
|
if ( dimensions.length != 3) { |
103 |
|
|
throw new Exception( "Template: invalid dimensions line" ); |
104 |
|
|
} |
105 |
torben |
1738 |
|
106 |
torben |
1746 |
int xsize = Integer.parseInt(dimensions[0]); |
107 |
|
|
int ysize = Integer.parseInt(dimensions[1]); |
108 |
|
|
int zsize = Integer.parseInt(dimensions[2]); |
109 |
|
|
|
110 |
torben |
1747 |
Pair[][][] array = new Pair[xsize][ysize][zsize]; |
111 |
torben |
1746 |
|
112 |
|
|
int x = 0; |
113 |
|
|
int y = 0; |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
while ( (line = in.readLine() ) != null ) { |
117 |
|
|
lines++; |
118 |
|
|
|
119 |
|
|
line = line.trim(); |
120 |
|
|
if (line.length() == 0) |
121 |
|
|
continue; |
122 |
|
|
|
123 |
|
|
if (line.charAt(0) == '-') |
124 |
|
|
continue; |
125 |
|
|
|
126 |
|
|
String elements[] = line.split(","); |
127 |
|
|
if (elements.length != zsize) { |
128 |
|
|
throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); |
129 |
torben |
1738 |
} |
130 |
torben |
1746 |
|
131 |
|
|
for (int i=0; i<elements.length; i++) { |
132 |
torben |
1747 |
int val = 0; |
133 |
|
|
int subval = 0; |
134 |
|
|
|
135 |
|
|
String element[] = elements[i].trim().split("\\."); |
136 |
torben |
1746 |
|
137 |
torben |
1747 |
|
138 |
|
|
try { |
139 |
|
|
val = Integer.parseInt( element[0] ); |
140 |
torben |
1746 |
} catch (Exception e) { |
141 |
torben |
1747 |
throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] ); |
142 |
torben |
1738 |
} |
143 |
torben |
1747 |
|
144 |
|
|
if (val < 0 || val>255) { |
145 |
|
|
throw new Exception( "Template: invalid value on line " + lines + ": " + val ); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
if (element.length == 2) { |
149 |
|
|
try { |
150 |
|
|
subval = Integer.parseInt( element[1] ); |
151 |
|
|
} catch (Exception e) { |
152 |
|
|
throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] ); |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
array[x][y][i] = new Pair(val,subval); |
157 |
torben |
1738 |
} |
158 |
torben |
1746 |
|
159 |
|
|
x++; |
160 |
|
|
if (x >= xsize) { |
161 |
|
|
x = 0; |
162 |
|
|
y++; |
163 |
torben |
1738 |
} |
164 |
|
|
} |
165 |
torben |
1746 |
|
166 |
|
|
if (x != 0) { |
167 |
|
|
throw new Exception( "Template: not enough lines to complete the last level" ); |
168 |
torben |
1738 |
} |
169 |
torben |
1746 |
|
170 |
|
|
if (y != ysize) { |
171 |
|
|
throw new Exception( "Template: not enough levels"); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
return array; |
176 |
|
|
|
177 |
torben |
1738 |
} |
178 |
torben |
1746 |
|
179 |
torben |
1750 |
void buildTemplate(Location loc, Pair[][][] template, int pass) { |
180 |
torben |
1738 |
World world = loc.getWorld(); |
181 |
|
|
for (int i=0; i<template.length; i++ ) { |
182 |
|
|
for (int j=0; j<template[0].length; j++) { |
183 |
|
|
for (int k=0; k<template[0][0].length; k++) { |
184 |
|
|
int x = loc.getBlockX() + i + 1; |
185 |
|
|
int y = loc.getBlockY() + j; |
186 |
|
|
int z = loc.getBlockZ() + k; |
187 |
torben |
1748 |
|
188 |
torben |
1749 |
if (y >= 255) { |
189 |
torben |
1748 |
continue; |
190 |
|
|
} |
191 |
torben |
1746 |
|
192 |
torben |
1747 |
int type = template[i][j][k].id; |
193 |
|
|
byte data = (byte) template[i][j][k].subId; |
194 |
torben |
1746 |
|
195 |
torben |
1745 |
//plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) ); |
196 |
torben |
1746 |
|
197 |
torben |
1750 |
if (pass == 1) { |
198 |
|
|
if ( type == 50 || type == 75 || type == 76) //torch / redstone torch |
199 |
|
|
continue; |
200 |
|
|
if (type == 64 || type==71) //door / irondoor |
201 |
|
|
continue; |
202 |
|
|
if (type == 68) // sign |
203 |
|
|
continue; |
204 |
|
|
} |
205 |
torben |
1747 |
|
206 |
|
|
world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true); |
207 |
torben |
1750 |
|
208 |
torben |
1738 |
} |
209 |
|
|
} |
210 |
|
|
} |
211 |
torben |
1746 |
|
212 |
torben |
1738 |
} |
213 |
|
|
|
214 |
|
|
} |