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

Annotation 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 - (hide annotations) (download)
Wed May 31 08:56:00 2017 UTC (7 years ago) by torben
File size: 6232 byte(s)
Code cleanup(still doesn't compile)
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 1753 File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt");
57 torben 1738 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 torben 3201 try (FileInputStream fis = new FileInputStream( templateFile );
91     InputStreamReader isr = new InputStreamReader(fis);
92     BufferedReader in = new BufferedReader( isr )
93     ) {
94 torben 1755
95 torben 3201
96     String line = in.readLine();
97     lines ++;
98    
99     if ( line.charAt(0) != '#') {
100     throw new Exception( "Template: invalid start character" );
101 torben 1755 }
102 torben 3201
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 torben 1738 }
109 torben 3201
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 torben 1747
130 torben 3201 if ( y >= ysize) {
131     throw new Exception( "Template: found more levels in file than specified in header" );
132 torben 1738 }
133 torben 3201
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 torben 1747 }
138 torben 3201
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 torben 1747 try {
147 torben 3201 val = Integer.parseInt( element[0] );
148 torben 1747 } catch (Exception e) {
149 torben 3201 throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
150 torben 1747 }
151 torben 3201
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 torben 1747 }
166 torben 3201
167     x++;
168     if (x >= xsize) {
169     x = 0;
170     y++;
171     }
172 torben 1738 }
173 torben 3201
174     if (x != 0) {
175     throw new Exception( "Template: not enough lines to complete the last level" );
176 torben 1738 }
177 torben 3201
178     if (y != ysize) {
179     throw new Exception( "Template: not enough levels");
180     }
181    
182    
183     return array;
184    
185 torben 1738 }
186 torben 1746
187 torben 1738 }
188 torben 1746
189 torben 1750 void buildTemplate(Location loc, Pair[][][] template, int pass) {
190 torben 1752
191    
192 torben 1738 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 torben 1752 int x, y ,z;
197 torben 1748
198 torben 1752 x = z = 0;
199    
200     y = loc.getBlockY() + j;
201 torben 1749 if (y >= 255) {
202 torben 1748 continue;
203 torben 1752 }
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 torben 1748 }
223 torben 1746
224 torben 1752
225 torben 1747 int type = template[i][j][k].id;
226     byte data = (byte) template[i][j][k].subId;
227 torben 1757
228     if (type == -1) {
229     continue;
230     }
231 torben 1746
232 torben 1745 //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) );
233 torben 1746
234 torben 1750 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 torben 1756 if (type == 65) // ladder
242     continue;
243     if (type == 67) // steps
244     continue;
245 torben 1750 }
246 torben 1747
247     world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
248 torben 1750
249 torben 1738 }
250     }
251     }
252 torben 1746
253 torben 1738 }
254 torben 1752
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 torben 1738
273     }

  ViewVC Help
Powered by ViewVC 1.1.20