From 30ed0499189ccc36cd8b4c3b287b8c48af98d855 Mon Sep 17 00:00:00 2001 From: aumgn Date: Wed, 21 Mar 2012 11:34:45 +0100 Subject: [PATCH] Implement InvertedMask & UnderOverlayMask as decorators --- .../java/com/sk89q/worldedit/WorldEdit.java | 32 ++++--------------- .../masks/InvertedBlockTypeMask.java | 1 + .../sk89q/worldedit/masks/InvertedMask.java | 25 +++++++++++++++ .../worldedit/masks/UnderOverlayMask.java | 29 +++++++++++------ 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/masks/InvertedMask.java diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index da4de3b79..1d6a87df1 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -614,31 +614,13 @@ public class WorldEdit { 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); - } - } + Mask submask; + if (component.length() > 1) { + submask = getBlockMaskComponent(player, session, masks, component.substring(1)); + } else { + submask = new ExistingBlockMask(); } - - return new UnderOverlayMask(ids, over); + return new UnderOverlayMask(submask, firstChar == '>'); case '$': Set biomes = new HashSet(); @@ -651,7 +633,7 @@ public class WorldEdit { case '!': if (component.length() > 1) { - return new InvertedBlockTypeMask(getBlockIDs(player, component.substring(1), true)); + return new InvertedMask(getBlockMaskComponent(player, session, masks, component.substring(1))); } default: diff --git a/src/main/java/com/sk89q/worldedit/masks/InvertedBlockTypeMask.java b/src/main/java/com/sk89q/worldedit/masks/InvertedBlockTypeMask.java index 897d06340..1df18ca58 100644 --- a/src/main/java/com/sk89q/worldedit/masks/InvertedBlockTypeMask.java +++ b/src/main/java/com/sk89q/worldedit/masks/InvertedBlockTypeMask.java @@ -28,6 +28,7 @@ import com.sk89q.worldedit.Vector; * * @author sk89q */ +@Deprecated public class InvertedBlockTypeMask extends BlockTypeMask { public InvertedBlockTypeMask() { } diff --git a/src/main/java/com/sk89q/worldedit/masks/InvertedMask.java b/src/main/java/com/sk89q/worldedit/masks/InvertedMask.java new file mode 100644 index 000000000..6f4c6c99c --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/masks/InvertedMask.java @@ -0,0 +1,25 @@ +package com.sk89q.worldedit.masks; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.LocalPlayer; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.Vector; + +public class InvertedMask implements Mask { + + private Mask mask; + + public InvertedMask(Mask mask) { + this.mask = mask; + } + + public void prepare(LocalSession session, LocalPlayer player, Vector target) { + mask.prepare(session, player, target); + } + + @Override + public boolean matches(EditSession editSession, Vector pos) { + return !mask.matches(editSession, pos); + } + +} diff --git a/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java b/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java index f8a3a3815..5c5dff6c9 100644 --- a/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java +++ b/src/main/java/com/sk89q/worldedit/masks/UnderOverlayMask.java @@ -19,14 +19,12 @@ package com.sk89q.worldedit.masks; -import java.util.HashSet; import java.util.Set; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.blocks.BlockID; /** * @@ -34,24 +32,37 @@ import com.sk89q.worldedit.blocks.BlockID; */ public class UnderOverlayMask implements Mask { - boolean overlay; - Set ids = new HashSet(); + private int yMod; + private Mask mask; + @Deprecated public UnderOverlayMask(Set ids, boolean overlay) { - addAll(ids); - this.overlay = overlay; + this(new BlockTypeMask(ids), overlay); + } + + public UnderOverlayMask(Mask mask, boolean overlay) { + this.yMod = overlay ? -1 : 1; + this.mask = mask; } + @Deprecated public void addAll(Set ids) { - this.ids.addAll(ids); + if (mask instanceof BlockTypeMask) { + BlockTypeMask blockTypeMask = (BlockTypeMask) mask; + for (Integer id : ids) { + blockTypeMask.add(id); + } + } else if (mask instanceof ExistingBlockMask) { + mask = new BlockTypeMask(ids); + } } public void prepare(LocalSession session, LocalPlayer player, Vector target) { + mask.prepare(session, player, target); } 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); + return mask.matches(editSession, pos.add(0, yMod, 0)); } }