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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1744 by torben, Sun Mar 18 11:00:09 2012 UTC revision 1756 by torben, Mon Mar 19 10:18:44 2012 UTC
# Line 3  package dk.thoerup.bukkit.hoeruputils; Line 3  package dk.thoerup.bukkit.hoeruputils;
3  import java.io.BufferedReader;  import java.io.BufferedReader;
4  import java.io.File;  import java.io.File;
5  import java.io.FileInputStream;  import java.io.FileInputStream;
 import java.io.IOException;  
6  import java.io.InputStreamReader;  import java.io.InputStreamReader;
7    
8  import org.bukkit.ChatColor;  import org.bukkit.ChatColor;
# Line 17  import org.bukkit.plugin.Plugin; Line 16  import org.bukkit.plugin.Plugin;
16    
17  public class TemplateCommand implements CommandExecutor {  public class TemplateCommand implements CommandExecutor {
18    
19          /*class Pair {  
20            class Pair {
21                  public int id;                  public int id;
22                  public int subId;                  public int subId;
23                    
24                  public Pair (int i, int s) {                  public Pair (int i, int s) {
25                          id = i;                          id = i;
26                          subId = s;                          subId = s;
27                  }                  }
28                    
29          }*/          }
30            
31          Plugin plugin;          Plugin plugin;
32            
33          public TemplateCommand(Plugin plugin) {          public TemplateCommand(Plugin plugin) {
34                  this.plugin = plugin;                  this.plugin = plugin;
35          }          }
# Line 40  public class TemplateCommand implements Line 40  public class TemplateCommand implements
40                          sender.sendMessage( "this command can not be run from console" );                          sender.sendMessage( "this command can not be run from console" );
41                          return true;                          return true;
42                  }                  }
43                    
44                  Player player = (Player) sender;                  Player player = (Player) sender;
45                    
46                  if (! sender.isOp() ) {                  if (! sender.isOp() ) {
47                          sender.sendMessage( "you need to be op to run this command" );                          sender.sendMessage( "you need to be op to run this command" );
48                          return true;                          return true;
49                  }                  }
50                    
51                  if ( args.length != 1) {                  if ( args.length != 1) {
52                          sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");                          sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
53                          return true;                          return true;
54                  }                                }              
55                                    
56                  File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt");                            File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt");        
57                  if (! templateFile.exists()  ) {                  if (! templateFile.exists()  ) {
58                          sender.sendMessage( ChatColor.YELLOW + "Template not found" );                          sender.sendMessage( ChatColor.YELLOW + "Template not found" );
59                          return true;                          return true;
60                  }                        }      
61                    
62                  int[][][] template = parseFile(templateFile);                  Pair[][][] template;
63                  if( template == null) {  
64                          sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );                  try {
65                          return true;                                              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);                  buildTemplate( player.getLocation(),  template, 1);
77                    buildTemplate( player.getLocation(),  template, 2);
78    
79                  return false;                  return false;
80          }          }
           
81    
82            
83          // we use y /ver  
84            
85          int[][][] parseFile(File templateFile) {          Pair[][][] parseFile(File templateFile) throws Exception {
86    
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                                    
99                    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                                    
106                  try {                  int xsize = Integer.parseInt(dimensions[0]);
107                          int lines = 0;                  int ysize = Integer.parseInt(dimensions[1]);
108                                            int zsize = Integer.parseInt(dimensions[2]);
109                          FileInputStream fis = new FileInputStream( templateFile );  
110                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                  Pair[][][] array = new Pair[xsize][ysize][zsize];
111                          String line = in.readLine();  
112                          lines ++;                  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                          if ( line.charAt(0) != '#') {                          if ( y >= ysize) {
127                                  plugin.getLogger().info( "Template: invalid start character" );                                  throw new Exception( "Template: found more levels in file than specified in header" );
                                 return null;  
128                          }                          }
129                          line = line.replace('#', ' ').trim();  
130                          String dimensions[] = line.split(",");                          String elements[] = line.split(",");
131                                                    if (elements.length != zsize) {
132                          if ( dimensions.length != 3) {                                  throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
133                                  plugin.getLogger().info( "Template: invalid dimensions line" );                          }
134                                  return null;  
135                          }                          for (int i=0; i<elements.length; i++) {
136                          int xsize = Integer.parseInt(dimensions[0]);                                  int val = 0;
137                          int ysize = Integer.parseInt(dimensions[1]);                                  int subval = 0;
                         int zsize = Integer.parseInt(dimensions[2]);  
                           
                         int[][][] array = new int[xsize][ysize][zsize];  
                           
                         int x = 0;  
                         int y = 0;  
                           
                           
                         while ( (line = in.readLine() ) != null ) {  
                                 lines++;  
138                                                                    
139                                  line = line.trim();                                  String element[] = elements[i].trim().split("\\.");
140                                  if (line.length() == 0)  
                                         continue;  
141                                                                    
142                                  if (line.charAt(0) == '-')                                  try {                                  
143                                          continue;                                          val = Integer.parseInt( element[0] );
144                                    } catch (Exception e) {
145                                            throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
146                                    }
147                                                                    
148                                  String elements[] = line.split(",");                                  if (val < 0 || val>255) {
149                                  if (elements.length != zsize) {                                          throw new Exception( "Template: invalid value on line " + lines + ": " + val );
                                         plugin.getLogger().info( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );  
                                         return null;  
150                                  }                                  }
151                                                                    
152                                  for (int i=0; i<elements.length; i++) {                                  if (element.length == 2) {
153                                          try {                                          try {                                  
154                                                  String element = elements[i].trim();                                                  subval = Integer.parseInt( element[1] );
                                                 int val = Integer.parseInt( element );  
                                                   
                                                 if (val < 0 || val>255) {  
                                                         plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + val );  
                                                         return null;  
                                                 }  
                                                 array[x][y][i] = val;  
                                                   
155                                          } catch (Exception e) {                                          } catch (Exception e) {
156                                                  plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );                                                  throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] );
                                                 return null;  
157                                          }                                          }
158                                  }                                  }
159                                                                                                    
160                                  x++;                                  array[x][y][i] = new Pair(val,subval);
                                 if (x >= xsize) {  
                                         x = 0;  
                                         y++;  
                                 }  
                         }  
                           
                         if (x != 0) {  
                                 plugin.getLogger().info( "Template: not enough lines to complete the last level" );  
                                 return null;  
161                          }                          }
162                            
163                          if (y != ysize) {                          x++;
164                                  plugin.getLogger().info( "Template: not enough levels");                          if (x >= xsize) {
165                                  return null;                                                              x = 0;
166                                    y++;
167                          }                          }
                           
                           
                         return array;  
168                  }                  }
169                  catch (IOException e) {  
170                          plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );                  if (x != 0) {
171                          return null;                          throw new Exception( "Template: not enough lines to complete the last level" );
172                  }                  }
173    
174                    if (y != ysize) {
175                            throw new Exception( "Template: not enough levels");            
176                    }
177    
178    
179                    return array;
180    
181          }          }
182            
183          void buildTemplate(Location loc, int[][][] template) {          void buildTemplate(Location loc, Pair[][][] template, int pass) {
184    
185                    
186                  World world = loc.getWorld();                  World world = loc.getWorld();
187                  for (int i=0; i<template.length; i++ ) {                  for (int i=0; i<template.length; i++ ) {
188                          for (int j=0; j<template[0].length; j++) {                          for (int j=0; j<template[0].length; j++) {
189                                  for (int k=0; k<template[0][0].length; k++) {                                  for (int k=0; k<template[0][0].length; k++) {
190                                          int x = loc.getBlockX() + i + 1;                                          int x, y ,z;
                                         int y = loc.getBlockY() + j;  
                                         int z = loc.getBlockZ() + k;  
191                                                                                    
192                                          int type = template[i][j][k];                                          x = z = 0;
193                                                                                    
194                                            y = loc.getBlockY() + j;
195                                            if (y >= 255) {
196                                                    continue;
197                                            }                                                                              
198                                                                                    
199                                          plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );                                          switch ( getFacing(loc) ) {
200                                            case 0: //west                                          
201                                                    x = loc.getBlockX() - k;                                        
202                                                    z = loc.getBlockZ() + i + 1;                                                    
203                                                    break;
204                                            case 1: //north
205                                                    x = loc.getBlockX() - i - 1;                                    
206                                                    z = loc.getBlockZ() - k;                                                
207                                                    break;                                          
208                                            case 2: //east
209                                                    x = loc.getBlockX() + k;                                        
210                                                    z = loc.getBlockZ() - i - 1;
211                                                    break;
212                                            case 3: //south
213                                                    x = loc.getBlockX() + i + 1;                                    
214                                                    z = loc.getBlockZ() + k;                                                
215                                                    break;                                          
216                                            }
217    
218    
219                                            int type = template[i][j][k].id;
220                                            byte data = (byte) template[i][j][k].subId;
221    
222                                            //plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );
223    
224                                            if (pass == 1) {
225                                                    if ( type == 50 || type == 75 || type == 76) //torch / redstone torch                                                  
226                                                            continue;
227                                                    if (type == 64 || type==71) //door / irondoor
228                                                            continue;
229                                                    if (type == 68) // sign
230                                                            continue;
231                                                    if (type == 65) // ladder
232                                                            continue;
233                                                    if (type == 67) // steps
234                                                            continue;                                              
235                                            }
236                                            
237                                            world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
238                                                                                    
                                         world.getBlockAt(x, y, z).setTypeId( type  );  
239                                  }                                  }
240                          }                          }
241                  }                  }
242    
243            }
244            
245            
246            //yaw is left,right looking direction, 0=straight west
247            int getFacing(Location loc) {
248                    int angle = (int) loc.getYaw();
249                                    
250                    if (angle < 45) {
251                            return 0;
252                    } else if (angle < 135) {
253                            return 1;
254                    } else if (angle < 225) {
255                            return 2;
256                    } else if (angle < 315) {
257                            return 3;
258                    } else {
259                            return 0;
260                    }
261          }          }
262    
263  }  }

Legend:
Removed from v.1744  
changed lines
  Added in v.1756

  ViewVC Help
Powered by ViewVC 1.1.20