2014-04-04 00:52:53 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2014-04-04 00:52:53 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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 Lesser General Public License
|
|
|
|
* for more details.
|
2014-04-04 00:52:53 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-04 00:52:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-12-23 08:56:26 +00:00
|
|
|
package com.sk89q.worldedit.extension.factory.parser;
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
import com.sk89q.worldedit.IncompleteRegionException;
|
|
|
|
import com.sk89q.worldedit.NotABlockException;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.blocks.MobSpawnerBlock;
|
|
|
|
import com.sk89q.worldedit.blocks.SignBlock;
|
|
|
|
import com.sk89q.worldedit.blocks.SkullBlock;
|
2014-07-29 14:57:45 +00:00
|
|
|
import com.sk89q.worldedit.blocks.metadata.MobType;
|
2014-04-05 00:54:14 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
|
|
|
|
import com.sk89q.worldedit.extension.input.InputParseException;
|
|
|
|
import com.sk89q.worldedit.extension.input.NoMatchException;
|
2014-04-05 00:54:14 +00:00
|
|
|
import com.sk89q.worldedit.extension.input.ParserContext;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2018-06-17 12:04:35 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Capability;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.internal.registry.InputParser;
|
2018-10-14 10:40:53 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2018-07-17 07:31:07 +00:00
|
|
|
import com.sk89q.worldedit.registry.state.Property;
|
2018-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.util.HandSide;
|
2014-04-05 09:59:38 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2018-12-27 00:39:10 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-07-17 07:31:07 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockType;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2018-12-27 05:19:58 +00:00
|
|
|
import com.sk89q.worldedit.world.block.FuzzyBlockState;
|
2018-07-23 02:48:11 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.LegacyMapper;
|
2018-06-17 13:13:22 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2014-04-04 00:52:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses block input strings.
|
|
|
|
*/
|
2018-12-27 00:39:10 +00:00
|
|
|
public class DefaultBlockParser extends InputParser<BaseBlock> {
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-12-23 08:56:26 +00:00
|
|
|
public DefaultBlockParser(WorldEdit worldEdit) {
|
2014-04-04 00:52:53 +00:00
|
|
|
super(worldEdit);
|
|
|
|
}
|
|
|
|
|
2018-06-17 07:50:04 +00:00
|
|
|
private static BaseBlock getBlockInHand(Actor actor, HandSide handSide) throws InputParseException {
|
2014-04-05 00:54:14 +00:00
|
|
|
if (actor instanceof Player) {
|
|
|
|
try {
|
2018-06-17 07:50:04 +00:00
|
|
|
return ((Player) actor).getBlockInHand(handSide);
|
2014-04-05 00:54:14 +00:00
|
|
|
} catch (NotABlockException e) {
|
|
|
|
throw new InputParseException("You're not holding a block!");
|
|
|
|
} catch (WorldEditException e) {
|
|
|
|
throw new InputParseException("Unknown error occurred: " + e.getMessage(), e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new InputParseException("The user is not a player!");
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-27 00:39:10 +00:00
|
|
|
public BaseBlock parseFromInput(String input, ParserContext context)
|
2016-06-17 23:59:41 +00:00
|
|
|
throws InputParseException {
|
|
|
|
String originalInput = input;
|
2014-04-04 00:52:53 +00:00
|
|
|
input = input.replace(";", "|");
|
2016-06-17 23:59:41 +00:00
|
|
|
Exception suppressed = null;
|
|
|
|
try {
|
2018-12-27 00:39:10 +00:00
|
|
|
BaseBlock modified = parseLogic(input, context);
|
2016-06-17 23:59:41 +00:00
|
|
|
if (modified != null) {
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
suppressed = e;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return parseLogic(originalInput, context);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (suppressed != null) {
|
|
|
|
e.addSuppressed(suppressed);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
private static String[] EMPTY_STRING_ARRAY = new String[]{};
|
|
|
|
|
2018-06-19 07:26:22 +00:00
|
|
|
/**
|
|
|
|
* Backwards compatibility for wool colours in block syntax.
|
|
|
|
*
|
|
|
|
* @param string Input string
|
|
|
|
* @return Mapped string
|
|
|
|
*/
|
|
|
|
private String woolMapper(String string) {
|
|
|
|
switch (string.toLowerCase()) {
|
|
|
|
case "white":
|
|
|
|
return BlockTypes.WHITE_WOOL.getId();
|
|
|
|
case "black":
|
|
|
|
return BlockTypes.BLACK_WOOL.getId();
|
|
|
|
case "blue":
|
|
|
|
return BlockTypes.BLUE_WOOL.getId();
|
|
|
|
case "brown":
|
|
|
|
return BlockTypes.BROWN_WOOL.getId();
|
|
|
|
case "cyan":
|
|
|
|
return BlockTypes.CYAN_WOOL.getId();
|
|
|
|
case "gray":
|
|
|
|
case "grey":
|
|
|
|
return BlockTypes.GRAY_WOOL.getId();
|
|
|
|
case "green":
|
|
|
|
return BlockTypes.GREEN_WOOL.getId();
|
|
|
|
case "light_blue":
|
|
|
|
case "lightblue":
|
|
|
|
return BlockTypes.LIGHT_BLUE_WOOL.getId();
|
|
|
|
case "light_gray":
|
|
|
|
case "light_grey":
|
|
|
|
case "lightgray":
|
|
|
|
case "lightgrey":
|
|
|
|
return BlockTypes.LIGHT_GRAY_WOOL.getId();
|
|
|
|
case "lime":
|
|
|
|
return BlockTypes.LIME_WOOL.getId();
|
|
|
|
case "magenta":
|
|
|
|
return BlockTypes.MAGENTA_WOOL.getId();
|
|
|
|
case "orange":
|
|
|
|
return BlockTypes.ORANGE_WOOL.getId();
|
|
|
|
case "pink":
|
|
|
|
return BlockTypes.PINK_WOOL.getId();
|
|
|
|
case "purple":
|
|
|
|
return BlockTypes.PURPLE_WOOL.getId();
|
|
|
|
case "yellow":
|
|
|
|
return BlockTypes.YELLOW_WOOL.getId();
|
|
|
|
case "red":
|
|
|
|
return BlockTypes.RED_WOOL.getId();
|
|
|
|
default:
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 12:03:22 +00:00
|
|
|
private static BlockState applyProperties(BlockState state, String[] stateProperties) throws NoMatchException {
|
|
|
|
if (stateProperties.length > 0) { // Block data not yet detected
|
|
|
|
// Parse the block data (optional)
|
|
|
|
for (String parseableData : stateProperties) {
|
|
|
|
try {
|
|
|
|
String[] parts = parseableData.split("=");
|
|
|
|
if (parts.length != 2) {
|
|
|
|
throw new NoMatchException("Bad state format in " + parseableData);
|
|
|
|
}
|
|
|
|
|
2018-12-27 00:39:10 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Property<Object> propertyKey = (Property<Object>) state.getBlockType().getPropertyMap().get(parts[0]);
|
2018-07-05 23:16:52 +00:00
|
|
|
if (propertyKey == null) {
|
2018-07-01 12:03:22 +00:00
|
|
|
throw new NoMatchException("Unknown state " + parts[0] + " for block " + state.getBlockType().getName());
|
|
|
|
}
|
2018-07-18 07:39:25 +00:00
|
|
|
Object value;
|
|
|
|
try {
|
|
|
|
value = propertyKey.getValueFor(parts[1]);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2018-07-01 12:03:22 +00:00
|
|
|
throw new NoMatchException("Unknown value " + parts[1] + " for state " + parts[0]);
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:16:52 +00:00
|
|
|
state = state.with(propertyKey, value);
|
2018-07-01 12:03:22 +00:00
|
|
|
} catch (NoMatchException e) {
|
|
|
|
throw e; // Pass-through
|
|
|
|
} catch (Exception e) {
|
2018-07-17 14:42:09 +00:00
|
|
|
e.printStackTrace();
|
2018-07-01 12:03:22 +00:00
|
|
|
throw new NoMatchException("Unknown state '" + parseableData + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-12-27 00:39:10 +00:00
|
|
|
private BaseBlock parseLogic(String input, ParserContext context) throws InputParseException {
|
2018-07-23 02:48:11 +00:00
|
|
|
BlockType blockType = null;
|
2018-07-05 23:16:52 +00:00
|
|
|
Map<Property<?>, Object> blockStates = new HashMap<>();
|
2018-06-18 12:51:21 +00:00
|
|
|
String[] blockAndExtraData = input.trim().split("\\|");
|
2018-06-19 07:26:22 +00:00
|
|
|
blockAndExtraData[0] = woolMapper(blockAndExtraData[0]);
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
BlockState state = null;
|
2018-06-17 07:50:04 +00:00
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
// Legacy matcher
|
|
|
|
if (context.isTryingLegacy()) {
|
|
|
|
try {
|
|
|
|
String[] split = blockAndExtraData[0].split(":");
|
|
|
|
if (split.length == 1) {
|
|
|
|
state = LegacyMapper.getInstance().getBlockFromLegacy(Integer.parseInt(split[0]));
|
|
|
|
} else {
|
|
|
|
state = LegacyMapper.getInstance().getBlockFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
|
|
|
|
}
|
|
|
|
if (state != null) {
|
|
|
|
blockType = state.getBlockType();
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
2018-07-23 02:48:11 +00:00
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
if (state == null) {
|
2018-07-31 06:14:36 +00:00
|
|
|
String typeString;
|
|
|
|
String stateString = null;
|
|
|
|
int stateStart = blockAndExtraData[0].indexOf('[');
|
|
|
|
if (stateStart == -1) {
|
|
|
|
typeString = blockAndExtraData[0];
|
|
|
|
} else {
|
|
|
|
typeString = blockAndExtraData[0].substring(0, stateStart);
|
|
|
|
stateString = blockAndExtraData[0].substring(stateStart + 1, blockAndExtraData[0].length() - 1);
|
|
|
|
}
|
|
|
|
if (typeString == null || typeString.isEmpty()) {
|
2018-07-23 02:48:11 +00:00
|
|
|
throw new InputParseException("Invalid format");
|
|
|
|
}
|
|
|
|
String[] stateProperties = EMPTY_STRING_ARRAY;
|
2018-07-31 06:14:36 +00:00
|
|
|
if (stateString != null) {
|
|
|
|
stateProperties = stateString.split(",");
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
if ("hand".equalsIgnoreCase(typeString)) {
|
|
|
|
// Get the block type from the item in the user's hand.
|
|
|
|
final BaseBlock blockInHand = getBlockInHand(context.requireActor(), HandSide.MAIN_HAND);
|
|
|
|
if (blockInHand.getClass() != BaseBlock.class) {
|
|
|
|
return blockInHand;
|
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
blockType = blockInHand.getBlockType();
|
|
|
|
blockStates = blockInHand.getStates();
|
|
|
|
} else if ("offhand".equalsIgnoreCase(typeString)) {
|
|
|
|
// Get the block type from the item in the user's off hand.
|
|
|
|
final BaseBlock blockInHand = getBlockInHand(context.requireActor(), HandSide.OFF_HAND);
|
|
|
|
if (blockInHand.getClass() != BaseBlock.class) {
|
|
|
|
return blockInHand;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockType = blockInHand.getBlockType();
|
|
|
|
blockStates = blockInHand.getStates();
|
|
|
|
} else if ("pos1".equalsIgnoreCase(typeString)) {
|
|
|
|
// Get the block type from the "primary position"
|
|
|
|
final World world = context.requireWorld();
|
2018-10-14 10:40:53 +00:00
|
|
|
final BlockVector3 primaryPosition;
|
2018-07-23 02:48:11 +00:00
|
|
|
try {
|
|
|
|
primaryPosition = context.requireSession().getRegionSelector(world).getPrimaryPosition();
|
|
|
|
} catch (IncompleteRegionException e) {
|
|
|
|
throw new InputParseException("Your selection is not complete.");
|
|
|
|
}
|
|
|
|
final BlockState blockInHand = world.getBlock(primaryPosition);
|
|
|
|
|
|
|
|
blockType = blockInHand.getBlockType();
|
|
|
|
blockStates = blockInHand.getStates();
|
|
|
|
} else {
|
|
|
|
// Attempt to lookup a block from ID or name.
|
2018-07-24 10:46:35 +00:00
|
|
|
blockType = BlockTypes.get(typeString.toLowerCase());
|
2018-07-23 02:48:11 +00:00
|
|
|
|
|
|
|
if (blockType == null) {
|
|
|
|
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
|
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
if (!context.isPreferringWildcard()) {
|
|
|
|
// No wildcards allowed => eliminate them. (Start with default state)
|
|
|
|
state = blockType.getDefaultState();
|
|
|
|
} else {
|
2018-12-27 05:19:58 +00:00
|
|
|
FuzzyBlockState.Builder fuzzyBuilder = FuzzyBlockState.builder();
|
|
|
|
fuzzyBuilder.type(blockType);
|
2018-08-03 13:01:56 +00:00
|
|
|
for (Map.Entry<Property<?>, Object> blockState : blockStates.entrySet()) {
|
2018-12-27 00:39:10 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Property<Object> objProp = (Property<Object>) blockState.getKey();
|
2018-12-27 05:19:58 +00:00
|
|
|
fuzzyBuilder.withProperty(objProp, blockState.getValue());
|
2018-08-03 13:01:56 +00:00
|
|
|
}
|
2018-12-27 05:19:58 +00:00
|
|
|
state = fuzzyBuilder.build();
|
2018-07-23 02:48:11 +00:00
|
|
|
}
|
2018-06-17 13:13:22 +00:00
|
|
|
|
2018-07-23 02:48:11 +00:00
|
|
|
state = applyProperties(state, stateProperties);
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the item is allowed
|
2018-07-01 12:03:22 +00:00
|
|
|
if (context.isRestricted()) {
|
|
|
|
Actor actor = context.requireActor();
|
|
|
|
if (actor != null && !actor.hasPermission("worldedit.anyblock")
|
|
|
|
&& worldEdit.getConfiguration().disallowedBlocks.contains(blockType.getId())) {
|
|
|
|
throw new DisallowedUsageException("You are not allowed to use '" + input + "'");
|
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
if (blockType == BlockTypes.SIGN || blockType == BlockTypes.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(state, text);
|
2018-07-19 02:55:02 +00:00
|
|
|
} else if (blockType == BlockTypes.SPAWNER) {
|
2018-06-17 13:13:22 +00:00
|
|
|
// 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;
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 13:13:22 +00:00
|
|
|
if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) {
|
|
|
|
throw new NoMatchException("Unknown mob type '" + mobName + "'");
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
2018-06-17 13:13:22 +00:00
|
|
|
return new MobSpawnerBlock(state, mobName);
|
|
|
|
} else {
|
|
|
|
return new MobSpawnerBlock(state, MobType.PIG.getName());
|
|
|
|
}
|
|
|
|
} else if (blockType == BlockTypes.PLAYER_HEAD || blockType == BlockTypes.PLAYER_WALL_HEAD) {
|
|
|
|
// allow setting type/player/rotation
|
|
|
|
if (blockAndExtraData.length <= 1) {
|
|
|
|
return new SkullBlock(state);
|
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
String type = blockAndExtraData[1];
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-06-17 13:13:22 +00:00
|
|
|
return new SkullBlock(state, type.replace(" ", "_")); // valid MC usernames
|
|
|
|
} else {
|
2018-12-27 00:39:10 +00:00
|
|
|
return state.toBaseBlock();
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|