From 7812d8f5f8c42a51103f57a97824ccd36390a19f Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 20 Nov 2011 06:02:54 +0100 Subject: [PATCH] Cleanup and refactorings --- .../java/com/sk89q/worldedit/WorldEdit.java | 132 ++++++++++-------- .../sk89q/worldedit/masks/CombinedMask.java | 19 +-- 2 files changed, 83 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index 2103fe433..ed32374d7 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -175,8 +175,8 @@ public class WorldEdit { commands.register(ToolCommands.class); commands.register(UtilityCommands.class); } - - /* + + /** * Gets the LocalSession for a player name if it exists * * @param player @@ -460,22 +460,23 @@ public class WorldEdit { } /** - * Get a list of blocks as a set. This returns a Pattern. + * Returns a Pattern corresponding to the specified pattern string, + * as given by the player on the command line. * * @param player - * @param list + * @param patternString * @return pattern * @throws UnknownItemException * @throws DisallowedItemException */ - public Pattern getBlockPattern(LocalPlayer player, String list) + public Pattern getBlockPattern(LocalPlayer player, String patternString) throws UnknownItemException, DisallowedItemException { - String[] items = list.split(","); + String[] items = patternString.split(","); // Handle special block pattern types - if (list.charAt(0) == '#') { - if (list.equals("#clipboard") || list.equals("#copy")) { + if (patternString.charAt(0) == '#') { + if (patternString.equals("#clipboard") || patternString.equals("#copy")) { LocalSession session = getSession(player); CuboidClipboard clipboard; @@ -488,7 +489,7 @@ public class WorldEdit { return new ClipboardPattern(clipboard); } else { - throw new UnknownItemException(list); + throw new UnknownItemException(patternString); } } @@ -532,71 +533,82 @@ public class WorldEdit { */ public Mask getBlockMask(LocalPlayer player, LocalSession session, String maskString) throws WorldEditException { - Mask mask = null; + List masks = new ArrayList(); for (String component : maskString.split(" ")) { - Mask current = null; if (component.length() == 0) { continue; } - if (component.charAt(0) == '#') { - if (component.equalsIgnoreCase("#existing")) { - current = new ExistingBlockMask(); - } else if (component.equalsIgnoreCase("#selection") - || component.equalsIgnoreCase("#region") - || component.equalsIgnoreCase("#sel")) { - current = new RegionMask(session.getSelection(player.getWorld())); - } else { - throw new UnknownItemException(component); - } - } else if (component.charAt(0) == '>' - || component.charAt(0) == '<') { - LocalWorld world = player.getWorld(); - boolean over = component.charAt(0) == '>'; - Set set = new HashSet(); - String ids = component.replaceAll(">", "").replaceAll("<", ""); + Mask current = getBlockMaskComponent(player, session, masks, component); - if (!(ids.equals("*") || ids.equals(""))) { - for (String sid : ids.split(",")) { - try { - int pid = Integer.parseInt(sid); - if (!world.isValidBlockType(pid)) { - throw new UnknownItemException(sid); - } - set.add(pid); - } catch (NumberFormatException e) { - BlockType type = BlockType.lookup(sid); - int id = type.getID(); - if (!world.isValidBlockType(id)) { - throw new UnknownItemException(sid); - } - set.add(id); + masks.add(current); + } + + switch (masks.size()) { + case 0: + return null; + + case 1: + return masks.get(0); + + default: + return new CombinedMask(masks); + } + } + + private Mask getBlockMaskComponent(LocalPlayer player, LocalSession session, List masks, String component) throws IncompleteRegionException, UnknownItemException, DisallowedItemException { + final char firstChar = component.charAt(0); + switch (firstChar) { + case '#': + if (component.equalsIgnoreCase("#existing")) { + return new ExistingBlockMask(); + } else if (component.equalsIgnoreCase("#selection") + || component.equalsIgnoreCase("#region") + || component.equalsIgnoreCase("#sel")) { + return new RegionMask(session.getSelection(player.getWorld())); + } else { + throw new UnknownItemException(component); + } + + case '>': + case '<': + final LocalWorld world = player.getWorld(); + final boolean over = firstChar == '>'; + final String idString = component.substring(1); + final Set ids = new HashSet(); + + if (!(idString.equals("*") || idString.equals(""))) { + for (String sid : idString.split(",")) { + try { + final int pid = Integer.parseInt(sid); + if (!world.isValidBlockType(pid)) { + throw new UnknownItemException(sid); } + ids.add(pid); + } catch (NumberFormatException e) { + final BlockType type = BlockType.lookup(sid); + final int id = type.getID(); + if (!world.isValidBlockType(id)) { + throw new UnknownItemException(sid); + } + ids.add(id); } } - current = new UnderOverlayMask(set, over); - } else { - if (component.charAt(0) == '!' && component.length() > 1) { - current = new InvertedBlockTypeMask( - getBlockIDs(player, component.substring(1), true)); - } else { - current = new BlockTypeMask(getBlockIDs(player, component, true)); - } } - - if (mask == null) { - mask = current; - } else if (mask instanceof CombinedMask) { - ((CombinedMask) mask).add(current); - } else { - mask = new CombinedMask(mask); - ((CombinedMask) mask).add(current); + + return new UnderOverlayMask(ids, over); + + case '!': + if (component.length() > 1) { + return new InvertedBlockTypeMask(getBlockIDs(player, component.substring(1), true)); } + + default: + return new BlockTypeMask(getBlockIDs(player, component, true)); } - - return mask; } + /** * Get a list of blocks as a set. * diff --git a/src/main/java/com/sk89q/worldedit/masks/CombinedMask.java b/src/main/java/com/sk89q/worldedit/masks/CombinedMask.java index 166a21251..5ed5c1fea 100644 --- a/src/main/java/com/sk89q/worldedit/masks/CombinedMask.java +++ b/src/main/java/com/sk89q/worldedit/masks/CombinedMask.java @@ -25,24 +25,28 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; public class CombinedMask implements Mask { - + private List masks = new ArrayList(); - + public CombinedMask() { } - + public CombinedMask(Mask mask) { masks.add(mask); } - + + public CombinedMask(List masks) { + masks.addAll(masks); + } + public void add(Mask mask) { masks.add(mask); } - + public boolean remove(Mask mask) { return masks.remove(mask); } - + public boolean has(Mask mask) { return masks.contains(mask); } @@ -53,8 +57,7 @@ public class CombinedMask implements Mask { return false; } } - + return true; } - }