From 9d753c869286426c55f42de5fefb37bec371f921 Mon Sep 17 00:00:00 2001 From: Wizjany Date: Sat, 13 Aug 2011 00:29:28 -0400 Subject: [PATCH] Cleanup overlay/underlay mask. --- .../java/com/sk89q/worldedit/WorldEdit.java | 102 +++++------------- .../worldedit/masks/UnderOverlayMask.java | 38 +++---- 2 files changed, 41 insertions(+), 99 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index 819cd26ea..c99b826db 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -473,13 +473,13 @@ public class WorldEdit { public Mask getBlockMask(LocalPlayer player, LocalSession session, String maskString) throws WorldEditException { Mask mask = null; - + for (String component : maskString.split(" ")) { Mask current = null; - if (component.length() == 0) { + if (component.length() == 0) { continue; } - + if (component.charAt(0) == '#') { if (component.equalsIgnoreCase("#existing")) { current = new ExistingBlockMask(); @@ -487,82 +487,36 @@ public class WorldEdit { || component.equalsIgnoreCase("#region") || component.equalsIgnoreCase("#sel")) { current = new RegionMask(session.getSelection(player.getWorld())); - } - else { + } else { throw new UnknownItemException(component); } - } - else if (component.charAt(0) == '>') { + } else if (component.charAt(0) == '>' + || component.charAt(0) == '<') { LocalWorld world = player.getWorld(); - Set set = new HashSet(); - String ids = component.replaceAll(">", ""); - if(ids.equalsIgnoreCase("*")){ - current = new UnderOverlayMask(set,true,true); - } - else{ - String[] split = ids.split(","); - for(String sid :split){ - try{ - int pid = Integer.parseInt(sid); - if(!world.isValidBlockType(pid)){ - throw new UnknownItemException(sid); - } - else{ - set.add(pid); - } - }catch(NumberFormatException e){ - BlockType type = BlockType.lookup(sid); - int id = type.getID(); - if(!world.isValidBlockType(id)){ - throw new UnknownItemException(sid); - } - else{ - set.add(id); - } - } - current = new UnderOverlayMask(set, true, false); - } - } - } - else if (component.charAt(0) == '<') { - LocalWorld world = player.getWorld(); - Set set = new HashSet(); - - - String ids = component.replaceAll("<", ""); - if(ids.equalsIgnoreCase("*")){ - current = new UnderOverlayMask(set,false,true); + boolean over = component.charAt(0) == '>'; + Set set = new HashSet(); + String ids = component.replaceAll(">", "").replaceAll("<", ""); + + 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); } - else{ - String[] split = ids.split(","); - for(String sid :split){ - try{ - int pid = Integer.parseInt(sid); - if(!world.isValidBlockType(pid)){ - throw new UnknownItemException(sid); - } - else{ - set.add(pid); - } - }catch(NumberFormatException e){ - BlockType type = BlockType.lookup(sid); - int id = type.getID(); - if(!world.isValidBlockType(id)){ - throw new UnknownItemException(sid); - } - else{ - set.add(id); - - } - } - current = new UnderOverlayMask(set, false, false); - } - } - } - - else { - + } + current = new UnderOverlayMask(set, over); + } else { if (component.charAt(0) == '!' && component.length() > 1) { current = new InvertedBlockTypeMask( getBlockIDs(player, component.substring(1), true)); diff --git a/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java b/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java index 73c84bad0..aa4099477 100644 --- a/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java +++ b/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java @@ -1,4 +1,3 @@ - package com.sk89q.worldedit.masks; import java.util.HashSet; @@ -6,41 +5,30 @@ import java.util.Set; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.blocks.BlockID; /** * * @author 1337 */ -public class UnderOverlayMask implements Mask{ +public class UnderOverlayMask implements Mask { + boolean overlay; - boolean wildcard; Set ids = new HashSet(); - public UnderOverlayMask(Set ids,boolean overlay,boolean wildcard){ + + public UnderOverlayMask(Set ids, boolean overlay) { addAll(ids); this.overlay = overlay; - this.wildcard = wildcard; - } + public void addAll(Set ids){ this.ids.addAll(ids); } - public boolean matches(EditSession editSession, Vector pos) { - if(!wildcard){ - int id = editSession.getBlock(pos.setY(pos.getBlockY() + (overlay ? -1 :1))).getType(); - if(overlay){ -return ids.contains(id); - } - else if(!overlay){ - return ids.contains(id); - } - } - else{ - return (overlay ? editSession.getBlock(pos.setY(pos.getBlockY() + 1)).getType() != 0: editSession.getBlock(pos.setY(pos.getBlockY() - 1)).getType() != 0); - } - return false; - } - - - } - + + public boolean matches(EditSession editSession, Vector pos) { + int id = editSession.getBlock(pos.setY(pos.getBlockY() + (overlay ? -1 : 1))).getType(); + return ids.isEmpty() ? id != BlockID.AIR : ids.contains(id); + } + +}