/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3201 - (show annotations) (download)
Wed May 31 08:56:00 2017 UTC (6 years, 11 months ago) by torben
File size: 6232 byte(s)
Code cleanup(still doesn't compile)
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.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
20 class Pair {
21 public int id;
22 public int subId;
23
24 public Pair (int i, int s) {
25 id = i;
26 subId = s;
27 }
28
29 }
30
31 Plugin plugin;
32
33 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
44 Player player = (Player) sender;
45
46 if (! sender.isOp() ) {
47 sender.sendMessage( "you need to be op to run this command" );
48 return true;
49 }
50
51 if ( args.length != 1) {
52 sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
53 return true;
54 }
55
56 File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt");
57 if (! templateFile.exists() ) {
58 sender.sendMessage( ChatColor.YELLOW + "Template not found" );
59 return true;
60 }
61
62 Pair[][][] template;
63
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 }
75
76 buildTemplate( player.getLocation(), template, 1);
77 buildTemplate( player.getLocation(), template, 2);
78
79 return false;
80 }
81
82
83
84
85 Pair[][][] parseFile(File templateFile) throws Exception {
86
87
88 int lines = 0;
89
90 try (FileInputStream fis = new FileInputStream( templateFile );
91 InputStreamReader isr = new InputStreamReader(fis);
92 BufferedReader in = new BufferedReader( isr )
93 ) {
94
95
96 String line = in.readLine();
97 lines ++;
98
99 if ( line.charAt(0) != '#') {
100 throw new Exception( "Template: invalid start character" );
101 }
102
103 line = line.replace('#', ' ').trim();
104 String dimensions[] = line.split(",");
105
106 if ( dimensions.length != 3) {
107 throw new Exception( "Template: invalid dimensions line" );
108 }
109
110 int xsize = Integer.parseInt(dimensions[0]);
111 int ysize = Integer.parseInt(dimensions[1]);
112 int zsize = Integer.parseInt(dimensions[2]);
113
114 Pair[][][] array = new Pair[xsize][ysize][zsize];
115
116 int x = 0;
117 int y = 0;
118
119
120 while ( (line = in.readLine() ) != null ) {
121 lines++;
122
123 line = line.trim();
124 if (line.length() == 0)
125 continue;
126
127 if (line.charAt(0) == '#')
128 continue;
129
130 if ( y >= ysize) {
131 throw new Exception( "Template: found more levels in file than specified in header" );
132 }
133
134 String elements[] = line.split(",");
135 if (elements.length != zsize) {
136 throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
137 }
138
139 for (int i=0; i<elements.length; i++) {
140 int val = 0;
141 int subval = 0;
142
143 String element[] = elements[i].trim().split("\\.");
144
145
146 try {
147 val = Integer.parseInt( element[0] );
148 } catch (Exception e) {
149 throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
150 }
151
152 if (val < -1 || val>255) {
153 throw new Exception( "Template: invalid value on line " + lines + ": " + val );
154 }
155
156 if (element.length == 2) {
157 try {
158 subval = Integer.parseInt( element[1] );
159 } catch (Exception e) {
160 throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] );
161 }
162 }
163
164 array[x][y][i] = new Pair(val,subval);
165 }
166
167 x++;
168 if (x >= xsize) {
169 x = 0;
170 y++;
171 }
172 }
173
174 if (x != 0) {
175 throw new Exception( "Template: not enough lines to complete the last level" );
176 }
177
178 if (y != ysize) {
179 throw new Exception( "Template: not enough levels");
180 }
181
182
183 return array;
184
185 }
186
187 }
188
189 void buildTemplate(Location loc, Pair[][][] template, int pass) {
190
191
192 World world = loc.getWorld();
193 for (int i=0; i<template.length; i++ ) {
194 for (int j=0; j<template[0].length; j++) {
195 for (int k=0; k<template[0][0].length; k++) {
196 int x, y ,z;
197
198 x = z = 0;
199
200 y = loc.getBlockY() + j;
201 if (y >= 255) {
202 continue;
203 }
204
205 switch ( getFacing(loc) ) {
206 case 0: //west
207 x = loc.getBlockX() - k;
208 z = loc.getBlockZ() + i + 1;
209 break;
210 case 1: //north
211 x = loc.getBlockX() - i - 1;
212 z = loc.getBlockZ() - k;
213 break;
214 case 2: //east
215 x = loc.getBlockX() + k;
216 z = loc.getBlockZ() - i - 1;
217 break;
218 case 3: //south
219 x = loc.getBlockX() + i + 1;
220 z = loc.getBlockZ() + k;
221 break;
222 }
223
224
225 int type = template[i][j][k].id;
226 byte data = (byte) template[i][j][k].subId;
227
228 if (type == -1) {
229 continue;
230 }
231
232 //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) );
233
234 if (pass == 1) {
235 if ( type == 50 || type == 75 || type == 76) //torch / redstone torch
236 continue;
237 if (type == 64 || type==71) //door / irondoor
238 continue;
239 if (type == 68) // sign
240 continue;
241 if (type == 65) // ladder
242 continue;
243 if (type == 67) // steps
244 continue;
245 }
246
247 world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
248
249 }
250 }
251 }
252
253 }
254
255
256 //yaw is left,right looking direction, 0=straight west
257 int getFacing(Location loc) {
258 int angle = (int) loc.getYaw();
259
260 if (angle < 45) {
261 return 0;
262 } else if (angle < 135) {
263 return 1;
264 } else if (angle < 225) {
265 return 2;
266 } else if (angle < 315) {
267 return 3;
268 } else {
269 return 0;
270 }
271 }
272
273 }

  ViewVC Help
Powered by ViewVC 1.1.20