Cleanup and refactorings

This commit is contained in:
TomyLobo 2011-11-20 06:02:54 +01:00
parent feee529095
commit 7812d8f5f8
2 changed files with 83 additions and 68 deletions

View File

@ -175,8 +175,8 @@ public class WorldEdit {
commands.register(ToolCommands.class); commands.register(ToolCommands.class);
commands.register(UtilityCommands.class); commands.register(UtilityCommands.class);
} }
/* /**
* Gets the LocalSession for a player name if it exists * Gets the LocalSession for a player name if it exists
* *
* @param player * @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 player
* @param list * @param patternString
* @return pattern * @return pattern
* @throws UnknownItemException * @throws UnknownItemException
* @throws DisallowedItemException * @throws DisallowedItemException
*/ */
public Pattern getBlockPattern(LocalPlayer player, String list) public Pattern getBlockPattern(LocalPlayer player, String patternString)
throws UnknownItemException, DisallowedItemException { throws UnknownItemException, DisallowedItemException {
String[] items = list.split(","); String[] items = patternString.split(",");
// Handle special block pattern types // Handle special block pattern types
if (list.charAt(0) == '#') { if (patternString.charAt(0) == '#') {
if (list.equals("#clipboard") || list.equals("#copy")) { if (patternString.equals("#clipboard") || patternString.equals("#copy")) {
LocalSession session = getSession(player); LocalSession session = getSession(player);
CuboidClipboard clipboard; CuboidClipboard clipboard;
@ -488,7 +489,7 @@ public class WorldEdit {
return new ClipboardPattern(clipboard); return new ClipboardPattern(clipboard);
} else { } else {
throw new UnknownItemException(list); throw new UnknownItemException(patternString);
} }
} }
@ -532,71 +533,82 @@ public class WorldEdit {
*/ */
public Mask getBlockMask(LocalPlayer player, LocalSession session, public Mask getBlockMask(LocalPlayer player, LocalSession session,
String maskString) throws WorldEditException { String maskString) throws WorldEditException {
Mask mask = null; List<Mask> masks = new ArrayList<Mask>();
for (String component : maskString.split(" ")) { for (String component : maskString.split(" ")) {
Mask current = null;
if (component.length() == 0) { if (component.length() == 0) {
continue; continue;
} }
if (component.charAt(0) == '#') { Mask current = getBlockMaskComponent(player, session, masks, component);
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<Integer> set = new HashSet<Integer>();
String ids = component.replaceAll(">", "").replaceAll("<", "");
if (!(ids.equals("*") || ids.equals(""))) { masks.add(current);
for (String sid : ids.split(",")) { }
try {
int pid = Integer.parseInt(sid); switch (masks.size()) {
if (!world.isValidBlockType(pid)) { case 0:
throw new UnknownItemException(sid); return null;
}
set.add(pid); case 1:
} catch (NumberFormatException e) { return masks.get(0);
BlockType type = BlockType.lookup(sid);
int id = type.getID(); default:
if (!world.isValidBlockType(id)) { return new CombinedMask(masks);
throw new UnknownItemException(sid); }
} }
set.add(id);
private Mask getBlockMaskComponent(LocalPlayer player, LocalSession session, List<Mask> 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<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);
} }
} }
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) { return new UnderOverlayMask(ids, over);
mask = current;
} else if (mask instanceof CombinedMask) { case '!':
((CombinedMask) mask).add(current); if (component.length() > 1) {
} else { return new InvertedBlockTypeMask(getBlockIDs(player, component.substring(1), true));
mask = new CombinedMask(mask);
((CombinedMask) mask).add(current);
} }
default:
return new BlockTypeMask(getBlockIDs(player, component, true));
} }
return mask;
} }
/** /**
* Get a list of blocks as a set. * Get a list of blocks as a set.
* *

View File

@ -25,24 +25,28 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
public class CombinedMask implements Mask { public class CombinedMask implements Mask {
private List<Mask> masks = new ArrayList<Mask>(); private List<Mask> masks = new ArrayList<Mask>();
public CombinedMask() { public CombinedMask() {
} }
public CombinedMask(Mask mask) { public CombinedMask(Mask mask) {
masks.add(mask); masks.add(mask);
} }
public CombinedMask(List<Mask> masks) {
masks.addAll(masks);
}
public void add(Mask mask) { public void add(Mask mask) {
masks.add(mask); masks.add(mask);
} }
public boolean remove(Mask mask) { public boolean remove(Mask mask) {
return masks.remove(mask); return masks.remove(mask);
} }
public boolean has(Mask mask) { public boolean has(Mask mask) {
return masks.contains(mask); return masks.contains(mask);
} }
@ -53,8 +57,7 @@ public class CombinedMask implements Mask {
return false; return false;
} }
} }
return true; return true;
} }
} }