package dk.thoerup.bukkit.hoeruputils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; 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; } @Override public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { if (! (sender instanceof Player) ) { 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(), "templates/" + args[0] + ".txt"); if (! templateFile.exists() ) { sender.sendMessage( ChatColor.YELLOW + "Template not found" ); return true; } Pair[][][] 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, 1); buildTemplate( player.getLocation(), template, 2); return false; } Pair[][][] 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" ); } 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; if ( y >= ysize) { throw new Exception( "Template: found more levels in file than specified in header" ); } String elements[] = line.split(","); if (elements.length != zsize) { throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize ); } 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++; } } 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) { 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].id; byte data = (byte) template[i][j][k].subId; if (type == -1) { continue; } //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, 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; if (type == 65) // ladder continue; if (type == 67) // steps 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; } } }