/[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 1743 by torben, Sun Mar 18 10:52:16 2012 UTC revision 1748 by torben, Sun Mar 18 11:43:52 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);
77    
78                  return false;                  return false;
79          }          }
           
80    
81            
82          // we use y /ver  
83            
84          int[][][] parseFile(File templateFile) {          Pair[][][] parseFile(File templateFile) throws Exception {
85    
86    
87                    int lines = 0;
88    
89                    FileInputStream fis = new FileInputStream( templateFile );
90                    BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
91                    String line = in.readLine();
92                    lines ++;
93    
94                    if ( line.charAt(0) != '#') {
95                            throw new Exception( "Template: invalid start character" );
96                    }
97                                    
98                    line = line.replace('#', ' ').trim();
99                    String dimensions[] = line.split(",");
100    
101                    if ( dimensions.length != 3) {
102                            throw new Exception( "Template: invalid dimensions line" );
103                    }
104                                    
105                  try {                  int xsize = Integer.parseInt(dimensions[0]);
106                          int lines = 0;                  int ysize = Integer.parseInt(dimensions[1]);
107                                            int zsize = Integer.parseInt(dimensions[2]);
108                          FileInputStream fis = new FileInputStream( templateFile );  
109                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                  Pair[][][] array = new Pair[xsize][ysize][zsize];
110                          String line = in.readLine();  
111                          lines ++;                  int x = 0;
112                                            int y = 0;
113                          if ( line.charAt(0) != '#') {  
114                                  plugin.getLogger().info( "Template: invalid start character" );  
115                                  return null;                  while ( (line = in.readLine() ) != null ) {
116                          }                          lines++;
117                          line = line.replace('#', ' ').trim();  
118                          String dimensions[] = line.split(",");                          line = line.trim();
119                                                    if (line.length() == 0)
120                          if ( dimensions.length != 3) {                                  continue;
121                                  plugin.getLogger().info( "Template: invalid dimensions line" );  
122                                  return null;                          if (line.charAt(0) == '-')
123                                    continue;
124    
125                            String elements[] = line.split(",");
126                            if (elements.length != zsize) {
127                                    throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
128                          }                          }
129                          int xsize = Integer.parseInt(dimensions[0]);  
130                          int ysize = Integer.parseInt(dimensions[1]);                          for (int i=0; i<elements.length; i++) {
131                          int zsize = Integer.parseInt(dimensions[2]);                                  int val = 0;
132                                                            int subval = 0;
                         int[][][] array = new int[xsize][ysize][zsize];  
                           
                         int x = 0;  
                         int y = 0;  
                           
                           
                         while ( (line = in.readLine() ) != null ) {  
                                 lines++;  
133                                                                    
134                                  line = line.trim();                                  String element[] = elements[i].trim().split("\\.");
135                                  if (line.length() == 0)  
                                         continue;  
136                                                                    
137                                  if (line.charAt(0) == '-')                                  try {                                  
138                                          continue;                                          val = Integer.parseInt( element[0] );
139                                    } catch (Exception e) {
140                                            throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
141                                    }
142                                                                    
143                                  String elements[] = line.split(",");                                  if (val < 0 || val>255) {
144                                  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;  
145                                  }                                  }
146                                                                    
147                                  for (int i=0; i<elements.length; i++) {                                  if (element.length == 2) {
148                                          try {                                          try {                                  
149                                                  String element = elements[i].trim();                                                  subval = Integer.parseInt( element[1] );
                                                 int val = Integer.parseInt( element );  
                                                   
                                                 array[x][y][i] = val;  
                                                   
150                                          } catch (Exception e) {                                          } catch (Exception e) {
151                                                  plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );                                                  throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] );
                                                 return null;  
152                                          }                                          }
153                                  }                                  }
154                                                                                                    
155                                  x++;                                  array[x][y][i] = new Pair(val,subval);
                                 if (x >= xsize) {  
                                         x = 0;  
                                         y++;  
                                 }  
156                          }                          }
157                            
158                          if (x != 0) {                          x++;
159                                  plugin.getLogger().info( "Template: not enough lines to complete the last level" );                          if (x >= xsize) {
160                                  return null;                                  x = 0;
161                          }                                  y++;
                           
                         if (y != ysize) {  
                                 plugin.getLogger().info( "Template: not enough levels");  
                                 return null;                              
162                          }                          }
                           
                           
                         return array;  
                 }  
                 catch (IOException e) {  
                         plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );  
                         return null;  
163                  }                  }
164    
165                    if (x != 0) {
166                            throw new Exception( "Template: not enough lines to complete the last level" );
167                    }
168    
169                    if (y != ysize) {
170                            throw new Exception( "Template: not enough levels");            
171                    }
172    
173    
174                    return array;
175    
176          }          }
177            
178          void buildTemplate(Location loc, int[][][] template) {          void buildTemplate(Location loc, Pair[][][] template) {
179                  World world = loc.getWorld();                  World world = loc.getWorld();
180                  for (int i=0; i<template.length; i++ ) {                  for (int i=0; i<template.length; i++ ) {
181                          for (int j=0; j<template[0].length; j++) {                          for (int j=0; j<template[0].length; j++) {
# Line 170  public class TemplateCommand implements Line 184  public class TemplateCommand implements
184                                          int y = loc.getBlockY() + j;                                          int y = loc.getBlockY() + j;
185                                          int z = loc.getBlockZ() + k;                                          int z = loc.getBlockZ() + k;
186                                                                                    
187                                          int type = template[i][j][k];                                          if (y >= 256) {
188                                                                                            continue;
189                                                                                    }
190                                          plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );  
191                                            int type = template[i][j][k].id;
192                                            byte data = (byte) template[i][j][k].subId;
193    
194                                            //plugin.getLogger().info(  String.format(  "Setting typeid at %d,%d,%d  to %d", x,y,z, type)  );
195    
196                                                                                    
197                                          world.getBlockAt(x, y, z).setTypeId( type  );                                          world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
198                                  }                                  }
199                          }                          }
200                  }                  }
201                    
202          }          }
203    
204  }  }

Legend:
Removed from v.1743  
changed lines
  Added in v.1748

  ViewVC Help
Powered by ViewVC 1.1.20