--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2012/03/18 11:14:51 1746 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java 2012/03/19 10:01:12 1755 @@ -3,7 +3,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStreamReader; import org.bukkit.ChatColor; @@ -18,7 +17,7 @@ public class TemplateCommand implements CommandExecutor { - /*class Pair { + class Pair { public int id; public int subId; @@ -27,7 +26,7 @@ subId = s; } - }*/ + } Plugin plugin; @@ -54,13 +53,13 @@ return true; } - File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt"); + File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt"); if (! templateFile.exists() ) { sender.sendMessage( ChatColor.YELLOW + "Template not found" ); return true; } - int[][][] template; + Pair[][][] template; try { template = parseFile(templateFile); @@ -74,7 +73,8 @@ return true; } - buildTemplate( player.getLocation(), template); + buildTemplate( player.getLocation(), template, 1); + buildTemplate( player.getLocation(), template, 2); return false; } @@ -82,7 +82,7 @@ - int[][][] parseFile(File templateFile) throws Exception { + Pair[][][] parseFile(File templateFile) throws Exception { int lines = 0; @@ -107,7 +107,7 @@ int ysize = Integer.parseInt(dimensions[1]); int zsize = Integer.parseInt(dimensions[2]); - int[][][] array = new int[xsize][ysize][zsize]; + Pair[][][] array = new Pair[xsize][ysize][zsize]; int x = 0; int y = 0; @@ -122,6 +122,10 @@ if (line.charAt(0) == '-') continue; + + if ( y >= ysize) { + throw new Exception( "Template: found more levels in file than specified in header" ); + } String elements[] = line.split(","); if (elements.length != zsize) { @@ -129,18 +133,31 @@ } for (int i=0; i255) { - throw new Exception( "Template: invalid value on line " + lines + ": " + val ); - } - array[x][y][i] = val; - + int val = 0; + int subval = 0; + + String element[] = elements[i].trim().split("\\."); + + + try { + val = Integer.parseInt( element[0] ); } catch (Exception e) { - throw new Exception( "Template: invalid value on line " + lines + ": " + elements[i] ); + throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] ); + } + + if (val < 0 || val>255) { + 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++; @@ -163,25 +180,80 @@ } - void buildTemplate(Location loc, int[][][] template) { + void buildTemplate(Location loc, Pair[][][] template, int pass) { + + World world = loc.getWorld(); for (int i=0; i= 255) { + continue; + } + + switch ( getFacing(loc) ) { + case 0: //west + x = loc.getBlockX() - k; + z = loc.getBlockZ() + i + 1; + break; + case 1: //north + x = loc.getBlockX() - i - 1; + z = loc.getBlockZ() - k; + break; + case 2: //east + x = loc.getBlockX() + k; + z = loc.getBlockZ() - i - 1; + break; + case 3: //south + x = loc.getBlockX() + i + 1; + z = loc.getBlockZ() + k; + break; + } - int type = template[i][j][k]; + int type = template[i][j][k].id; + byte data = (byte) template[i][j][k].subId; //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) ); - world.getBlockAt(x, y, z).setTypeId( type ); + if (pass == 1) { + if ( type == 50 || type == 75 || type == 76) //torch / redstone torch + continue; + if (type == 64 || type==71) //door / irondoor + continue; + if (type == 68) // sign + continue; + } + + world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true); + } } } } + + + //yaw is left,right looking direction, 0=straight west + int getFacing(Location loc) { + int angle = (int) loc.getYaw(); + + if (angle < 45) { + return 0; + } else if (angle < 135) { + return 1; + } else if (angle < 225) { + return 2; + } else if (angle < 315) { + return 3; + } else { + return 0; + } + } }