/[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 1738 by torben, Sun Mar 18 10:19:19 2012 UTC revision 1750 by torben, Sun Mar 18 14:36:49 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(), "template_" + 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                          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]);                                  int val = 0;
133                                                            int subval = 0;
                         int[][][] array = new int[xsize][ysize][zsize];  
                           
                         int x = 0;  
                         int y = 0;  
                           
                           
                         while ( (line = in.readLine() ) != null ) {  
                                 lines++;  
134                                                                    
135                                  line = line.trim();                                  String element[] = elements[i].trim().split("\\.");
136                                  if (line.charAt(0) == '-')  
                                         continue;  
137                                                                    
138                                  String elements[] = line.split(",");                                  try {                                  
139                                  if (elements.length != zsize) {                                          val = Integer.parseInt( element[0] );
140                                          plugin.getLogger().info( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );                                  } catch (Exception e) {
141                                          return null;                                          throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
142                                  }                                  }
143                                                                    
144                                  for (int i=0; i<elements.length; i++) {                                  if (val < 0 || val>255) {
145                                          try {                                          throw new Exception( "Template: invalid value on line " + lines + ": " + val );
                                                 int val = Integer.parseInt( elements[i] );  
                                                   
                                                 array[x][y][i] = val;  
                                                   
                                         } catch (Exception e) {  
                                                 plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );  
                                                 return null;  
                                         }  
146                                  }                                  }
147                                                                    
148                                  x++;                                  if (element.length == 2) {
149                                  if (x >= xsize) {                                          try {                                  
150                                          x = 0;                                                  subval = Integer.parseInt( element[1] );
151                                          y++;                                          } 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                          }                          }
158                            
159                          if (x != 0) {                          x++;
160                                  plugin.getLogger().info( "Template: not enough lines to complete the last level" );                          if (x >= xsize) {
161                                  return null;                                  x = 0;
162                          }                                  y++;
                           
                         if (y != ysize) {  
                                 plugin.getLogger().info( "Template: not enough levels");  
                                 return null;                              
163                          }                          }
                           
                           
                         return array;  
                 }  
                 catch (IOException e) {  
                         plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );  
                         return null;  
164                  }                  }
165    
166                    if (x != 0) {
167                            throw new Exception( "Template: not enough lines to complete the last level" );
168                    }
169    
170                    if (y != ysize) {
171                            throw new Exception( "Template: not enough levels");            
172                    }
173    
174    
175                    return array;
176    
177          }          }
178            
179          void buildTemplate(Location loc, int[][][] template) {          void buildTemplate(Location loc, Pair[][][] template, int pass) {
180                  World world = loc.getWorld();                  World world = loc.getWorld();
181                  for (int i=0; i<template.length; i++ ) {                  for (int i=0; i<template.length; i++ ) {
182                          for (int j=0; j<template[0].length; j++) {                          for (int j=0; j<template[0].length; j++) {
# Line 166  public class TemplateCommand implements Line 185  public class TemplateCommand implements
185                                          int y = loc.getBlockY() + j;                                          int y = loc.getBlockY() + j;
186                                          int z = loc.getBlockZ() + k;                                          int z = loc.getBlockZ() + k;
187                                                                                    
188                                          world.getBlockAt(x, y, z).setTypeId(  template[x][y][z] );                                          if (y >= 255) {
189                                                    continue;
190                                            }
191    
192                                            int type = template[i][j][k].id;
193                                            byte data = (byte) template[i][j][k].subId;
194    
195                                            //plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );
196    
197                                            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                                            
206                                            world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
207                                            
208                                  }                                  }
209                          }                          }
210                  }                  }
211                    
212          }          }
213    
214  }  }

Legend:
Removed from v.1738  
changed lines
  Added in v.1750

  ViewVC Help
Powered by ViewVC 1.1.20