Implement InvertedMask & UnderOverlayMask as decorators

This commit is contained in:
aumgn 2012-03-21 11:34:45 +01:00 committed by zml2008
parent eb14efd2df
commit 30ed049918
4 changed files with 53 additions and 34 deletions

View File

@ -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<Integer> ids = new HashSet<Integer>();
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<BiomeType> biomes = new HashSet<BiomeType>();
@ -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:

View File

@ -28,6 +28,7 @@ import com.sk89q.worldedit.Vector;
*
* @author sk89q
*/
@Deprecated
public class InvertedBlockTypeMask extends BlockTypeMask {
public InvertedBlockTypeMask() {
}

View File

@ -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);
}
}

View File

@ -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<Integer> ids = new HashSet<Integer>();
private int yMod;
private Mask mask;
@Deprecated
public UnderOverlayMask(Set<Integer> 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<Integer> 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));
}
}