--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/creative/GeneralContractorCommands.java 2012/03/12 20:08:39 1724 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/creative/GeneralContractorCommands.java 2016/11/18 13:14:07 3133 @@ -9,10 +9,43 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; public class GeneralContractorCommands implements CommandExecutor{ + class StoredCommand { + public StoredCommand(String l, String a[]) { + this.label = l; + this.args = a; + } + public String label; + public String args[]; + } + final static int Y_MAX = 255; + final Material valid_block_array[] = { + Material.STONE, + Material.GRASS, + Material.DIRT, + Material.COBBLESTONE + }; +//4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; + + final Set valid_materials = new HashSet(); + + HashMap commands = new HashMap(); + + + public GeneralContractorCommands() { + Arrays.sort(valid_block_array); + + for (Material mat : valid_block_array) { + valid_materials.add( mat ); + } + } //@Override public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { @@ -28,19 +61,36 @@ player.sendMessage( ChatColor.RED + "This command may only be used in creative world" ); return true; } + + + if ( command.getLabel().equals("replay") ) { + StoredCommand cmd = commands.get(player.getName() ); + if (cmd != null) { + label = cmd.label; + args = cmd.args; + } + } else { + commands.put(player.getName(), new StoredCommand(label,args) ); + } //System.out.println( ">>" + label); //DEBUG if ( label.equals("levelarea") ) { - if (validateLevelOrFill(player, args) ) { + if (validateLevelOrFill(player, label, args) ) { levelArea(player, loc, args); } return true; } + if ( label.equals("createcave") ) { + if (validateLevelOrFill(player, label, args) ) { + createCave(player, loc, args); + } + return true; + } if ( label.equals("fillarea") ) { - if (validateLevelOrFill(player, args) ) { + if (validateLevelOrFill(player, label, args) ) { fillArea(player, loc, args); } return true; @@ -56,14 +106,32 @@ setSurface(player, loc, args); return true; } + + if (label.equals("platform") ) { + platform(player,loc,args); + return true; + } + + if (label.equals("cylinder") ) { + if (validateLevelOrFill(player, label, args) ) { + cylinder(player,loc,args); + } + return true; + } return false; } - void slopeArea(Player player, Location loc, String[] args) { - int radius = Integer.parseInt(args[1]); + void slopeArea(Player player, Location loc, String[] split) { + int radius; + try { + radius = Integer.parseInt(split[0]); + } catch (Exception e) { + player.sendMessage("setsurface: radius must be an integer"); + return; + } System.out.println("Player " + player.getName() + " used slopearea with radius=" + radius); @@ -93,39 +161,32 @@ } } - private void setSurface(Player player, Location loc, String[] split) { - int valid_block_array[] = {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 48, 49, 56, 57, 73, 74, 79, 80, 82} ; - - + private void platform(Player player, Location loc, String[] split) { - if (split.length != 3) { - player.sendMessage("Usage /setsurface [radius] [blockID]"); + if (split.length != 2) { + player.sendMessage("Usage /platform [radius] [material]"); return; } - int radius; try { - radius = Integer.parseInt(split[1]); + radius = Integer.parseInt(split[0]); } catch (Exception e) { - player.sendMessage("setsurface: radius must be an integer"); + player.sendMessage("platform: radius must be an integer"); return; } if (radius > 20) { - player.sendMessage("setsurface: radius may not exceed 20"); + player.sendMessage("platform: radius may not exceed 20"); return; } - - int blockid; - try { - blockid = Integer.parseInt(split[2]); - } catch (Exception e) { - player.sendMessage("setsurface: blockid must be an integer"); + Material material = Material.matchMaterial( split[1] ); + if (material == null) { + player.sendMessage("platform: blockid must be an integer"); return; } - +/* boolean validblock = false; for (int i=0; i 20) { + player.sendMessage("setsurface: radius may not exceed 20"); + return; + } + + Material material = Material.matchMaterial( split[1] ); + if (material == null) { + player.sendMessage("platform: blockid must be an integer"); + return; + } + + //check if the blockid is in the array of valid blocks + if ( ! valid_materials.contains(material) ) { + player.sendMessage("setsurface: block now allowed"); + return; } int playerX = loc.getBlockX(); int playerY = loc.getBlockY(); int playerZ = loc.getBlockZ(); - if(playerY <= 2 && blockid != 7) { + if(playerY <= 2 && material != Material.BEDROCK ) { player.sendMessage("setsurface: at this level you may only use bedrock(id=7)"); return; } World world = loc.getWorld(); - for (int x=(playerX-radius); x<=(playerX+radius); x++) { for (int z=(playerZ-radius); z<=(playerZ+radius); z++) { //int y = getGroundLevel(x,z) - 1; - int y = world.getHighestBlockYAt(x, z); + int y = world.getHighestBlockYAt(x, z) ; - world.getBlockAt(x, y, z).setTypeId(blockid); + world.getBlockAt(x, y, z).setType(material); } } } - private boolean validateLevelOrFill(Player player, String[] split) { - if (split.length < 2 || split.length > 3) { - player.sendMessage("Usage: " + split[0] + " [radius]"); + private boolean validateLevelOrFill(Player player, String label, String[] split) { + if (split.length < 1 || split.length > 2) { + player.sendMessage("Usage: " + label + " [radius]"); return false; } int radius; try { - radius = Integer.parseInt(split[1]); + radius = Integer.parseInt(split[0]); } catch (Exception e) { - player.sendMessage(split[0] + ": radius must be an integer"); + player.sendMessage(label + ": radius must be an integer"); return false; } if (radius > 20) { - player.sendMessage(split[0] + ": radius may not exceed 20"); + player.sendMessage(label + ": radius may not exceed 20"); return false; } - if (split.length == 3) { + if (split.length == 2) { int id; try { - id = Integer.parseInt( split[2] ); + id = Integer.parseInt( split[1] ); } catch (Exception e) { - player.sendMessage(split[0] + ": id must be an integer"); + player.sendMessage(label + ": id must be an integer"); return false; } if ( id == 46) { @@ -199,12 +307,17 @@ } private void fillArea(Player player, Location loc, String[] split) { - int radius = Integer.parseInt(split[1]); + int radius = Integer.parseInt(split[0]); - int material = Material.DIRT.getId(); + Material material = Material.DIRT; + - if (split.length == 3) { - material = Integer.parseInt( split[2] ); + if (split.length == 2) { + material = Material.matchMaterial( split[1] ); + if (material == null) { + player.sendMessage("Unknown material: " + split[1] ); + return; + } } System.out.println("Player " + player.getName() + " used fillarea with radius=" + radius); @@ -222,7 +335,7 @@ for (int y=world.getHighestBlockYAt(x, z); y