--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2012/03/18 11:05:18 1745 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2012/03/18 11:14:51 1746 @@ -17,19 +17,20 @@ public class TemplateCommand implements CommandExecutor { + /*class Pair { public int id; public int subId; - + public Pair (int i, int s) { id = i; subId = s; } - + }*/ - + Plugin plugin; - + public TemplateCommand(Plugin plugin) { this.plugin = plugin; } @@ -40,131 +41,128 @@ sender.sendMessage( "this command can not be run from console" ); return true; } - + Player player = (Player) sender; - + if (! sender.isOp() ) { sender.sendMessage( "you need to be op to run this command" ); return true; } - + if ( args.length != 1) { sender.sendMessage( ChatColor.YELLOW + "Usage: /template "); return true; } - + File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt"); if (! templateFile.exists() ) { sender.sendMessage( ChatColor.YELLOW + "Template not found" ); return true; } - - int[][][] template = parseFile(templateFile); - if( template == null) { - sender.sendMessage( ChatColor.YELLOW + "Invalid template file" ); - return true; + + int[][][] template; + + try { + template = parseFile(templateFile); + if( template == null) { + sender.sendMessage( ChatColor.YELLOW + "Invalid template file" ); + return true; + } + } catch (Exception e) { + plugin.getLogger().info( e.getMessage() ); + sender.sendMessage( ChatColor.YELLOW + e.getMessage() ); + return true; } - + buildTemplate( player.getLocation(), template); return false; } - - - // we use y /ver - - int[][][] parseFile(File templateFile) { + + + + int[][][] parseFile(File templateFile) throws Exception { + + + int lines = 0; + + FileInputStream fis = new FileInputStream( templateFile ); + BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); + String line = in.readLine(); + lines ++; + + if ( line.charAt(0) != '#') { + throw new Exception( "Template: invalid start character" ); + } + line = line.replace('#', ' ').trim(); + String dimensions[] = line.split(","); + + if ( dimensions.length != 3) { + throw new Exception( "Template: invalid dimensions line" ); + } - try { - int lines = 0; - - FileInputStream fis = new FileInputStream( templateFile ); - BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); - String line = in.readLine(); - lines ++; - - if ( line.charAt(0) != '#') { - plugin.getLogger().info( "Template: invalid start character" ); - return null; - } - line = line.replace('#', ' ').trim(); - String dimensions[] = line.split(","); - - if ( dimensions.length != 3) { - plugin.getLogger().info( "Template: invalid dimensions line" ); - return null; + int xsize = Integer.parseInt(dimensions[0]); + int ysize = Integer.parseInt(dimensions[1]); + 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++; + + line = line.trim(); + if (line.length() == 0) + continue; + + if (line.charAt(0) == '-') + continue; + + String elements[] = line.split(","); + if (elements.length != zsize) { + throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); } - int xsize = Integer.parseInt(dimensions[0]); - int ysize = Integer.parseInt(dimensions[1]); - 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++; - - 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; i255) { - 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; + + for (int i=0; i255) { + throw new Exception( "Template: invalid value on line " + lines + ": " + val ); } + array[x][y][i] = val; + + } catch (Exception e) { + throw new Exception( "Template: invalid value on line " + lines + ": " + elements[i] ); } - - x++; - if (x >= xsize) { - x = 0; - y++; - } - } - - if (x != 0) { - plugin.getLogger().info( "Template: not enough lines to complete the last level" ); - return null; } - - if (y != ysize) { - plugin.getLogger().info( "Template: not enough levels"); - return null; + + x++; + if (x >= xsize) { + x = 0; + y++; } - - - return array; - } - catch (IOException e) { - plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() ); - return null; } + + if (x != 0) { + throw new Exception( "Template: not enough lines to complete the last level" ); + } + + if (y != ysize) { + throw new Exception( "Template: not enough levels"); + } + + + return array; + } - + void buildTemplate(Location loc, int[][][] template) { World world = loc.getWorld(); for (int i=0; i