--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2017/05/31 08:46:51 3200 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2017/05/31 08:56:00 3201 @@ -87,97 +87,103 @@ 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" ); - } - - int xsize = Integer.parseInt(dimensions[0]); - int ysize = Integer.parseInt(dimensions[1]); - int zsize = Integer.parseInt(dimensions[2]); - - Pair[][][] array = new Pair[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; + try (FileInputStream fis = new FileInputStream( templateFile ); + InputStreamReader isr = new InputStreamReader(fis); + BufferedReader in = new BufferedReader( isr ) + ) { - if ( y >= ysize) { - throw new Exception( "Template: found more levels in file than specified in header" ); + + String line = in.readLine(); + lines ++; + + if ( line.charAt(0) != '#') { + throw new Exception( "Template: invalid start character" ); } - - String elements[] = line.split(","); - if (elements.length != zsize) { - throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); + + line = line.replace('#', ' ').trim(); + String dimensions[] = line.split(","); + + if ( dimensions.length != 3) { + throw new Exception( "Template: invalid dimensions line" ); } - - for (int i=0; i= ysize) { + throw new Exception( "Template: found more levels in file than specified in header" ); } - - if (val < -1 || val>255) { - throw new Exception( "Template: invalid value on line " + lines + ": " + val ); + + String elements[] = line.split(","); + if (elements.length != zsize) { + throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); } - - if (element.length == 2) { + + for (int i=0; i255) { + throw new Exception( "Template: invalid value on line " + lines + ": " + val ); } + + if (element.length == 2) { + try { + subval = Integer.parseInt( element[1] ); + } catch (Exception e) { + throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] ); + } + } + + array[x][y][i] = new Pair(val,subval); + } + + x++; + if (x >= xsize) { + x = 0; + y++; } - - array[x][y][i] = new Pair(val,subval); } - - x++; - if (x >= xsize) { - x = 0; - y++; + + 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; + } - 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, Pair[][][] template, int pass) {