/[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 1745 by torben, Sun Mar 18 11:05:18 2012 UTC revision 1746 by torben, Sun Mar 18 11:14:51 2012 UTC
# Line 17  import org.bukkit.plugin.Plugin; Line 17  import org.bukkit.plugin.Plugin;
17    
18  public class TemplateCommand implements CommandExecutor {  public class TemplateCommand implements CommandExecutor {
19    
20    
21          /*class Pair {          /*class Pair {
22                  public int id;                  public int id;
23                  public int subId;                  public int subId;
24                    
25                  public Pair (int i, int s) {                  public Pair (int i, int s) {
26                          id = i;                          id = i;
27                          subId = s;                          subId = s;
28                  }                  }
29                    
30          }*/          }*/
31            
32          Plugin plugin;          Plugin plugin;
33            
34          public TemplateCommand(Plugin plugin) {          public TemplateCommand(Plugin plugin) {
35                  this.plugin = plugin;                  this.plugin = plugin;
36          }          }
# Line 40  public class TemplateCommand implements Line 41  public class TemplateCommand implements
41                          sender.sendMessage( "this command can not be run from console" );                          sender.sendMessage( "this command can not be run from console" );
42                          return true;                          return true;
43                  }                  }
44                    
45                  Player player = (Player) sender;                  Player player = (Player) sender;
46                    
47                  if (! sender.isOp() ) {                  if (! sender.isOp() ) {
48                          sender.sendMessage( "you need to be op to run this command" );                          sender.sendMessage( "you need to be op to run this command" );
49                          return true;                          return true;
50                  }                  }
51                    
52                  if ( args.length != 1) {                  if ( args.length != 1) {
53                          sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");                          sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
54                          return true;                          return true;
55                  }                                }              
56                                    
57                  File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt");                            File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt");          
58                  if (! templateFile.exists()  ) {                  if (! templateFile.exists()  ) {
59                          sender.sendMessage( ChatColor.YELLOW + "Template not found" );                          sender.sendMessage( ChatColor.YELLOW + "Template not found" );
60                          return true;                          return true;
61                  }                        }      
62                    
63                  int[][][] template = parseFile(templateFile);                  int[][][] template;
64                  if( template == null) {  
65                          sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );                  try {
66                          return true;                                              template = parseFile(templateFile);
67                            if( template == null) {
68                                    sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );
69                                    return true;                    
70                            }
71                    } catch (Exception e) {
72                            plugin.getLogger().info( e.getMessage() );
73                            sender.sendMessage( ChatColor.YELLOW + e.getMessage() );
74                            return true;
75                  }                  }
76                    
77                  buildTemplate( player.getLocation(),  template);                  buildTemplate( player.getLocation(),  template);
78    
79                  return false;                  return false;
80          }          }
           
81    
82            
83          // we use y /ver  
84            
85          int[][][] parseFile(File templateFile) {          int[][][] 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) );                  int[][][] array = new int[xsize][ysize][zsize];
111                          String line = in.readLine();  
112                          lines ++;                  int x = 0;
113                                            int y = 0;
114                          if ( line.charAt(0) != '#') {  
115                                  plugin.getLogger().info( "Template: invalid start character" );  
116                                  return null;                  while ( (line = in.readLine() ) != null ) {
117                          }                          lines++;
118                          line = line.replace('#', ' ').trim();  
119                          String dimensions[] = line.split(",");                          line = line.trim();
120                                                    if (line.length() == 0)
121                          if ( dimensions.length != 3) {                                  continue;
122                                  plugin.getLogger().info( "Template: invalid dimensions line" );  
123                                  return null;                          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                          }                          }
130                          int xsize = Integer.parseInt(dimensions[0]);  
131                          int ysize = Integer.parseInt(dimensions[1]);                          for (int i=0; i<elements.length; i++) {
132                          int zsize = Integer.parseInt(dimensions[2]);                                  try {
133                                                                    String element = elements[i].trim();
134                          int[][][] array = new int[xsize][ysize][zsize];                                          int val = Integer.parseInt( element );
135                            
136                          int x = 0;                                          if (val < 0 || val>255) {
137                          int y = 0;                                                  throw new Exception( "Template: invalid value on line " + lines + ": " + val );
                           
                           
                         while ( (line = in.readLine() ) != null ) {  
                                 lines++;  
                                   
                                 line = line.trim();  
                                 if (line.length() == 0)  
                                         continue;  
                                   
                                 if (line.charAt(0) == '-')  
                                         continue;  
                                   
                                 String elements[] = line.split(",");  
                                 if (elements.length != zsize) {  
                                         plugin.getLogger().info( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );  
                                         return null;  
                                 }  
                                   
                                 for (int i=0; i<elements.length; i++) {  
                                         try {  
                                                 String element = elements[i].trim();  
                                                 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;  
                                                   
                                         } catch (Exception e) {  
                                                 plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );  
                                                 return null;  
138                                          }                                          }
139                                            array[x][y][i] = val;
140    
141                                    } catch (Exception e) {
142                                            throw new Exception( "Template: invalid value on line " + lines + ": " + elements[i] );
143                                  }                                  }
                                   
                                 x++;  
                                 if (x >= xsize) {  
                                         x = 0;  
                                         y++;  
                                 }  
                         }  
                           
                         if (x != 0) {  
                                 plugin.getLogger().info( "Template: not enough lines to complete the last level" );  
                                 return null;  
144                          }                          }
145                            
146                          if (y != ysize) {                          x++;
147                                  plugin.getLogger().info( "Template: not enough levels");                          if (x >= xsize) {
148                                  return null;                                                              x = 0;
149                                    y++;
150                          }                          }
                           
                           
                         return array;  
                 }  
                 catch (IOException e) {  
                         plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );  
                         return null;  
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          void buildTemplate(Location loc, int[][][] template) {          void buildTemplate(Location loc, int[][][] template) {
167                  World world = loc.getWorld();                  World world = loc.getWorld();
168                  for (int i=0; i<template.length; i++ ) {                  for (int i=0; i<template.length; i++ ) {
# Line 173  public class TemplateCommand implements Line 171  public class TemplateCommand implements
171                                          int x = loc.getBlockX() + i + 1;                                          int x = loc.getBlockX() + i + 1;
172                                          int y = loc.getBlockY() + j;                                          int y = loc.getBlockY() + j;
173                                          int z = loc.getBlockZ() + k;                                          int z = loc.getBlockZ() + k;
174                                            
175                                          int type = template[i][j][k];                                          int type = template[i][j][k];
176                                            
177                                            
178                                          //plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );                                          //plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );
179                                            
180                                          world.getBlockAt(x, y, z).setTypeId( type  );                                          world.getBlockAt(x, y, z).setTypeId( type  );
181                                  }                                  }
182                          }                          }
183                  }                  }
184                    
185          }          }
186    
187  }  }

Legend:
Removed from v.1745  
changed lines
  Added in v.1746

  ViewVC Help
Powered by ViewVC 1.1.20