Created pattern, mask, and block registries.

Deprecated getBlock, getBlockPattern, and so-on in WorldEdit.
This commit is contained in:
sk89q
2014-04-03 17:52:53 -07:00
parent 589c3e9629
commit 9d08f266bf
32 changed files with 2192 additions and 478 deletions

View File

@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.input;
/**
* Thrown when usage is disallowed.
*/
public class DisallowedUsageException extends InputParseException {
/**
* Create with a message.
*
* @param message the message
*/
public DisallowedUsageException(String message) {
super(message);
}
/**
* Create with a message and a cause.
*
* @param message the message
* @param cause the cause
*/
public DisallowedUsageException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,48 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.input;
import com.sk89q.worldedit.WorldEditException;
/**
* Thrown when parsed input results in an error.
*/
public class InputParseException extends WorldEditException {
/**
* Throw with a message.
*
* @param message the message
*/
public InputParseException(String message) {
super(message);
}
/**
* Throw with a message and a cause.
*
* @param message the message
* @param cause the cause
*/
public InputParseException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.input;
/**
* Thrown when a match fails when input is parsed.
*/
public class NoMatchException extends InputParseException {
/**
* Create with a message.
*
* @param message the message
*/
public NoMatchException(String message) {
super(message);
}
/**
* Create with a message and a cause.
*
* @param message the message
* @param cause the cause
*/
public NoMatchException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,211 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.input;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.extension.registry.MaskRegistry;
import com.sk89q.worldedit.extent.Extent;
import javax.annotation.Nullable;
/**
* Contains contextual information that may be useful when constructing
* objects from a registry (such as {@link MaskRegistry}).
* </p>
* By default, {@link #isRestricted()} will return true.
*/
public class ParserContext {
private @Nullable Extent extent;
private @Nullable LocalSession session;
private @Nullable LocalWorld world;
private @Nullable LocalPlayer player;
private boolean restricted = true;
private boolean preferringWildcard;
/**
* Get the {@link Extent} set on this context.
*
* @return an extent
*/
public @Nullable Extent getExtent() {
return extent;
}
/**
* Set the extent.
*
* @param extent an extent, or null if none is available
*/
public void setExtent(@Nullable Extent extent) {
this.extent = extent;
}
/**
* Get the {@link LocalSession}.
*
* @return a session
*/
public @Nullable LocalSession getSession() {
return session;
}
/**
* Set the session.
*
* @param session a session, or null if none is available
*/
public void setSession(@Nullable LocalSession session) {
this.session = session;
}
/**
* Get the {@link LocalWorld} set on this context.
*
* @return a world
*/
public @Nullable LocalWorld getWorld() {
return world;
}
/**
* Set the world.
*
* @param world a world, or null if none is available
*/
public void setWorld(@Nullable LocalWorld world) {
this.world = world;
}
/**
* Get the {@link LocalPlayer} set on this context.
*
* @return a player
*/
public @Nullable LocalPlayer getPlayer() {
return player;
}
/**
* Set the player.
*
* @param player a player, or null if none is available
*/
public void setPlayer(@Nullable LocalPlayer player) {
this.player = player;
}
/**
* Get the {@link Extent} set on this context.
*
* @return an extent
* @throws InputParseException thrown if no {@link Extent} is set
*/
public Extent requireExtent() throws InputParseException {
Extent extent = getExtent();
if (extent == null) {
throw new InputParseException("No Extent is known");
}
return extent;
}
/**
* Get the {@link LocalSession}.
*
* @return a session
* @throws InputParseException thrown if no {@link LocalSession} is set
*/
public LocalSession requireSession() throws InputParseException {
LocalSession session = getSession();
if (session == null) {
throw new InputParseException("No LocalSession is known");
}
return session;
}
/**
* Get the {@link LocalWorld} set on this context.
*
* @return a world
* @throws InputParseException thrown if no {@link LocalWorld} is set
*/
public LocalWorld requireWorld() throws InputParseException {
LocalWorld world = getWorld();
if (world == null) {
throw new InputParseException("No world is known");
}
return world;
}
/**
* Get the {@link LocalPlayer} set on this context.
*
* @return a player
* @throws InputParseException thrown if no {@link LocalPlayer} is set
*/
public LocalPlayer requirePlayer() throws InputParseException {
LocalPlayer player = getPlayer();
if (player == null) {
throw new InputParseException("No player is known");
}
return player;
}
/**
* Returns whether there should be restrictions (as a result of
* limits or permissions) considered when parsing the input.
*
* @return true if restricted
*/
public boolean isRestricted() {
return restricted;
}
/**
* Set whether there should be restrictions (as a result of
* limits or permissions) considered when parsing the input.
*
* @param restricted true if restricted
*/
public void setRestricted(boolean restricted) {
this.restricted = restricted;
}
/**
* Get whether wildcards are preferred.
*
* @return true if wildcards are preferred
*/
public boolean isPreferringWildcard() {
return preferringWildcard;
}
/**
* Set whether wildcards are preferred.
*
* @param preferringWildcard true if wildcards are preferred
*/
public void setPreferringWildcard(boolean preferringWildcard) {
this.preferringWildcard = preferringWildcard;
}
}

View File

@@ -0,0 +1,67 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.internal.registry.AbstractRegistry;
import java.util.HashSet;
import java.util.Set;
/**
* A registry of known {@link BaseBlock}s. Provides methods to instantiate
* new blocks from input.
* </p>
* Instances of this class can be taken from
* {@link WorldEdit#getBlockRegistry()}.
*/
public class BlockRegistry extends AbstractRegistry<BaseBlock> {
/**
* Create a new instance.
*
* @param worldEdit the WorldEdit instance.
*/
public BlockRegistry(WorldEdit worldEdit) {
super(worldEdit);
parsers.add(new DefaultBlockParser(worldEdit));
}
/**
* Return a set of blocks from a comma-delimited list of blocks.
*
* @param input the input
* @param context the context
* @return a set of blocks
* @throws InputParseException thrown in error with the input
*/
public Set<BaseBlock> parseFromListInput(String input, ParserContext context) throws InputParseException {
Set<BaseBlock> blocks = new HashSet<BaseBlock>();
for (String token : input.split(",")) {
blocks.add(parseFromInput(token, context));
}
return blocks;
}
}

View File

@@ -0,0 +1,307 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.internal.registry.InputParser;
/**
* Parses block input strings.
*/
class DefaultBlockParser extends InputParser<BaseBlock> {
protected DefaultBlockParser(WorldEdit worldEdit) {
super(worldEdit);
}
private static BaseBlock getBlockInHand(LocalPlayer player) throws InputParseException {
try {
return player.getBlockInHand();
} catch (NotABlockException e) {
throw new InputParseException("You're not holding a block!");
} catch (WorldEditException e) {
throw new InputParseException("Unknown error occurred: " + e.getMessage(), e);
}
}
@Override
public BaseBlock parseFromInput(String input, ParserContext context) throws InputParseException {
BlockType blockType;
input = input.replace("_", " ");
input = input.replace(";", "|");
String[] blockAndExtraData = input.split("\\|");
String[] typeAndData = blockAndExtraData[0].split(":", 2);
String testID = typeAndData[0];
int blockId = -1;
int data = -1;
boolean parseDataValue = true;
if ("hand".equalsIgnoreCase(testID)) {
// Get the block type from the item in the user's hand.
final BaseBlock blockInHand = getBlockInHand(context.requirePlayer());
if (blockInHand.getClass() != BaseBlock.class) {
return blockInHand;
}
blockId = blockInHand.getId();
blockType = BlockType.fromID(blockId);
data = blockInHand.getData();
} else if ("pos1".equalsIgnoreCase(testID)) {
// Get the block type from the "primary position"
final LocalWorld world = context.requireWorld();
final BlockVector primaryPosition;
try {
primaryPosition = context.requireSession().getRegionSelector(world).getPrimaryPosition();
} catch (IncompleteRegionException e) {
throw new InputParseException("Your selection is not complete.");
}
final BaseBlock blockInHand = world.getBlock(primaryPosition);
if (blockInHand.getClass() != BaseBlock.class) {
return blockInHand;
}
blockId = blockInHand.getId();
blockType = BlockType.fromID(blockId);
data = blockInHand.getData();
} else {
// Attempt to parse the item ID or otherwise resolve an item/block
// name to its numeric ID
try {
blockId = Integer.parseInt(testID);
blockType = BlockType.fromID(blockId);
} catch (NumberFormatException e) {
blockType = BlockType.lookup(testID);
if (blockType == null) {
int t = worldEdit.getServer().resolveItem(testID);
if (t > 0) {
blockType = BlockType.fromID(t); // Could be null
blockId = t;
}
}
}
if (blockId == -1 && blockType == null) {
// Maybe it's a cloth
ClothColor col = ClothColor.lookup(testID);
if (col == null) {
throw new NoMatchException("Unknown wool color '" + input + "'");
}
blockType = BlockType.CLOTH;
data = col.getID();
// Prevent overriding the data value
parseDataValue = false;
}
// Read block ID
if (blockId == -1) {
blockId = blockType.getID();
}
if (!context.requireWorld().isValidBlockType(blockId)) {
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
}
}
if (!context.isPreferringWildcard() && data == -1) {
// No wildcards allowed => eliminate them.
data = 0;
}
if (parseDataValue) { // Block data not yet detected
// Parse the block data (optional)
try {
if (typeAndData.length > 1 && typeAndData[1].length() > 0) {
data = Integer.parseInt(typeAndData[1]);
}
if (data > 15) {
throw new NoMatchException("Invalid data value '" + typeAndData[1] + "'");
}
if (data < 0 && (context.isRestricted() || data != -1)) {
data = 0;
}
} catch (NumberFormatException e) {
if (blockType == null) {
throw new NoMatchException("Unknown data value '" + typeAndData[1] + "'");
}
switch (blockType) {
case CLOTH:
case STAINED_CLAY:
case CARPET:
ClothColor col = ClothColor.lookup(typeAndData[1]);
if (col == null) {
throw new NoMatchException("Unknown wool color '" + typeAndData[1] + "'");
}
data = col.getID();
break;
case STEP:
case DOUBLE_STEP:
BlockType dataType = BlockType.lookup(typeAndData[1]);
if (dataType == null) {
throw new NoMatchException("Unknown step type '" + typeAndData[1] + "'");
}
switch (dataType) {
case STONE:
data = 0;
break;
case SANDSTONE:
data = 1;
break;
case WOOD:
data = 2;
break;
case COBBLESTONE:
data = 3;
break;
case BRICK:
data = 4;
break;
case STONE_BRICK:
data = 5;
break;
case NETHER_BRICK:
data = 6;
break;
case QUARTZ_BLOCK:
data = 7;
break;
default:
throw new NoMatchException("Invalid step type '" + typeAndData[1] + "'");
}
break;
default:
throw new NoMatchException("Unknown data value '" + typeAndData[1] + "'");
}
}
}
// Check if the item is allowed
LocalPlayer player = context.requirePlayer();
if (context.isRestricted() && player != null && !player.hasPermission("worldedit.anyblock")
&& worldEdit.getConfiguration().disallowedBlocks.contains(blockId)) {
throw new DisallowedUsageException("You are not allowed to use '" + input + "'");
}
if (blockType == null) {
return new BaseBlock(blockId, data);
}
switch (blockType) {
case SIGN_POST:
case WALL_SIGN:
// Allow special sign text syntax
String[] text = new String[4];
text[0] = blockAndExtraData.length > 1 ? blockAndExtraData[1] : "";
text[1] = blockAndExtraData.length > 2 ? blockAndExtraData[2] : "";
text[2] = blockAndExtraData.length > 3 ? blockAndExtraData[3] : "";
text[3] = blockAndExtraData.length > 4 ? blockAndExtraData[4] : "";
return new SignBlock(blockType.getID(), data, text);
case MOB_SPAWNER:
// Allow setting mob spawn type
if (blockAndExtraData.length > 1) {
String mobName = blockAndExtraData[1];
for (MobType mobType : MobType.values()) {
if (mobType.getName().toLowerCase().equals(mobName.toLowerCase())) {
mobName = mobType.getName();
break;
}
}
if (!worldEdit.getServer().isValidMobType(mobName)) {
throw new NoMatchException("Unknown mob type '" + mobName + "'");
}
return new MobSpawnerBlock(data, mobName);
} else {
return new MobSpawnerBlock(data, MobType.PIG.getName());
}
case NOTE_BLOCK:
// Allow setting note
if (blockAndExtraData.length <= 1) {
return new NoteBlock(data, (byte) 0);
}
byte note = Byte.parseByte(blockAndExtraData[1]);
if (note < 0 || note > 24) {
throw new InputParseException("Out of range note value: '" + blockAndExtraData[1] + "'");
}
return new NoteBlock(data, note);
case HEAD:
// allow setting type/player/rotation
if (blockAndExtraData.length <= 1) {
return new SkullBlock(data);
}
byte rot = 0;
String type = "";
try {
rot = Byte.parseByte(blockAndExtraData[1]);
} catch (NumberFormatException e) {
type = blockAndExtraData[1];
if (blockAndExtraData.length > 2) {
try {
rot = Byte.parseByte(blockAndExtraData[2]);
} catch (NumberFormatException e2) {
throw new InputParseException("Second part of skull metadata should be a number.");
}
}
}
byte skullType = 0;
// type is either the mob type or the player name
// sorry for the four minecraft accounts named "skeleton", "wither", "zombie", or "creeper"
if (!type.isEmpty()) {
if (type.equalsIgnoreCase("skeleton")) skullType = 0;
else if (type.equalsIgnoreCase("wither")) skullType = 1;
else if (type.equalsIgnoreCase("zombie")) skullType = 2;
else if (type.equalsIgnoreCase("creeper")) skullType = 4;
else skullType = 3;
}
if (skullType == 3) {
return new SkullBlock(data, rot, type.replace(" ", "_")); // valid MC usernames
} else {
return new SkullBlock(data, skullType, rot);
}
default:
return new BaseBlock(blockId, data);
}
}
}

View File

@@ -0,0 +1,139 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.*;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.masks.BiomeTypeMask;
import com.sk89q.worldedit.math.noise.RandomNoise;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.session.request.RequestSelection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Parses mask input strings.
*/
class DefaultMaskParser extends InputParser<Mask> {
protected DefaultMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
List<Mask> masks = new ArrayList<Mask>();
for (String component : input.split(" ")) {
if (component.length() == 0) {
continue;
}
Mask current = getBlockMaskComponent(masks, component, context);
masks.add(current);
}
switch (masks.size()) {
case 0:
return null;
case 1:
return masks.get(0);
default:
return new MaskIntersection(masks);
}
}
private Mask getBlockMaskComponent(List<Mask> masks, String component, ParserContext context) throws InputParseException {
Extent extent = Request.request().getEditSession();
final char firstChar = component.charAt(0);
switch (firstChar) {
case '#':
if (component.equalsIgnoreCase("#existing")) {
return new ExistingBlockMask(extent);
} else if (component.equalsIgnoreCase("#solid")) {
return new SolidBlockMask(extent);
} else if (component.equalsIgnoreCase("#dregion")
|| component.equalsIgnoreCase("#dselection")
|| component.equalsIgnoreCase("#dsel")) {
return new RegionMask(new RequestSelection());
} else if (component.equalsIgnoreCase("#selection")
|| component.equalsIgnoreCase("#region")
|| component.equalsIgnoreCase("#sel")) {
try {
return new RegionMask(context.requireSession().getSelection(context.requireWorld()).clone());
} catch (IncompleteRegionException e) {
throw new InputParseException("Please make a selection first.");
}
} else {
throw new NoMatchException("Unrecognized mask '" + component + "'");
}
case '>':
case '<':
Mask submask;
if (component.length() > 1) {
submask = getBlockMaskComponent(masks, component.substring(1), context);
} else {
submask = new ExistingBlockMask(extent);
}
OffsetMask offsetMask = new OffsetMask(submask, new Vector(0, firstChar == '>' ? -1 : 1, 0));
return new MaskIntersection(offsetMask, Masks.negate(submask));
case '$':
Set<BiomeType> biomes = new HashSet<BiomeType>();
String[] biomesList = component.substring(1).split(",");
for (String biomeName : biomesList) {
try {
BiomeType biome = worldEdit.getServer().getBiomes().get(biomeName);
biomes.add(biome);
} catch (UnknownBiomeTypeException e) {
throw new InputParseException("Unknown biome '" + biomeName + "'");
}
}
return Masks.wrap(new BiomeTypeMask(biomes));
case '%':
int i = Integer.parseInt(component.substring(1));
return new NoiseFilter(new RandomNoise(), ((double) i) / 100);
case '!':
if (component.length() > 1) {
return Masks.negate(getBlockMaskComponent(masks, component.substring(1), context));
}
default:
return new BlockMask(extent, worldEdit.getBlockRegistry().parseFromInput(component, context));
}
}
}

View File

@@ -0,0 +1,60 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.ClipboardPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.extension.input.InputParseException;
class HashTagPatternParser extends InputParser<Pattern> {
HashTagPatternParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.charAt(0) == '#') {
if (!input.equals("#clipboard") && !input.equals("#copy")) {
throw new InputParseException("#clipboard or #copy is acceptable for patterns starting with #");
}
LocalSession session = context.requireSession();
if (session != null) {
try {
return new ClipboardPattern(session.getClipboard());
} catch (EmptyClipboardException e) {
throw new InputParseException("To use #clipboard, please first copy something to your clipboard");
}
} else {
throw new InputParseException("No session is available, so no clipboard is available");
}
} else {
return null;
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.AbstractRegistry;
/**
* A registry of known {@link Mask}s. Provides methods to instantiate
* new masks from input.
* </p>
* Instances of this class can be taken from
* {@link WorldEdit#getMaskRegistry()}.
*/
public final class MaskRegistry extends AbstractRegistry<Mask> {
/**
* Create a new mask registry.
*
* @param worldEdit the WorldEdit instance
*/
public MaskRegistry(WorldEdit worldEdit) {
super(worldEdit);
parsers.add(new DefaultMaskParser(worldEdit));
}
}

View File

@@ -0,0 +1,48 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.AbstractRegistry;
/**
* A registry of known {@link Pattern}s. Provides methods to instantiate
* new patterns from input.
* </p>
* Instances of this class can be taken from
* {@link WorldEdit#getPatternRegistry()}.
*/
public final class PatternRegistry extends AbstractRegistry<Pattern> {
/**
* Create a new instance.
*
* @param worldEdit the WorldEdit instance
*/
public PatternRegistry(WorldEdit worldEdit) {
super(worldEdit);
parsers.add(new HashTagPatternParser(worldEdit));
parsers.add(new SingleBlockPatternParser(worldEdit));
parsers.add(new RandomPatternParser(worldEdit));
}
}

View File

@@ -0,0 +1,67 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.internal.registry.InputParser;
class RandomPatternParser extends InputParser<Pattern> {
RandomPatternParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
BlockRegistry blockRegistry = worldEdit.getBlockRegistry();
RandomPattern randomPattern = new RandomPattern();
for (String token : input.split(",")) {
BaseBlock block;
double chance;
// Parse special percentage syntax
if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) {
String[] p = token.split("%");
if (p.length < 2) {
throw new InputParseException("Missing the type after the % symbol for '" + input + "'");
} else {
chance = Double.parseDouble(p[0]);
block = blockRegistry.parseFromInput(p[1], context);
}
} else {
chance = 1;
block = blockRegistry.parseFromInput(token, context);
}
randomPattern.add(new BlockPattern(block), chance);
}
return randomPattern;
}
}

View File

@@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.registry;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.extension.input.InputParseException;
class SingleBlockPatternParser extends InputParser<Pattern> {
SingleBlockPatternParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
String[] items = input.split(",");
if (items.length == 1) {
return new BlockPattern(worldEdit.getBlockRegistry().parseFromInput(items[0], context));
} else {
return null;
}
}
}