mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-12 04:38:35 +00:00
Rewrote block parsing, and further switch to BlockState
This commit is contained in:
@ -19,9 +19,19 @@
|
||||
|
||||
package com.sk89q.worldedit.extension.factory;
|
||||
|
||||
import com.sk89q.worldedit.*;
|
||||
import com.sk89q.worldedit.blocks.*;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
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.BaseBlock;
|
||||
import com.sk89q.worldedit.blocks.MobSpawnerBlock;
|
||||
import com.sk89q.worldedit.blocks.SignBlock;
|
||||
import com.sk89q.worldedit.blocks.SkullBlock;
|
||||
import com.sk89q.worldedit.blocks.metadata.MobType;
|
||||
import com.sk89q.worldedit.blocks.type.BlockState;
|
||||
import com.sk89q.worldedit.blocks.type.BlockType;
|
||||
import com.sk89q.worldedit.blocks.type.BlockTypes;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
@ -32,6 +42,14 @@ import com.sk89q.worldedit.extension.platform.Capability;
|
||||
import com.sk89q.worldedit.internal.registry.InputParser;
|
||||
import com.sk89q.worldedit.util.HandSide;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import com.sk89q.worldedit.world.registry.BundledBlockData;
|
||||
import com.sk89q.worldedit.world.registry.state.State;
|
||||
import com.sk89q.worldedit.world.registry.state.value.StateValue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Parses block input strings.
|
||||
@ -59,9 +77,6 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
|
||||
@Override
|
||||
public BaseBlock parseFromInput(String input, ParserContext context)
|
||||
throws InputParseException {
|
||||
// TODO: Rewrite this entire method to use BaseBlocks and ignore
|
||||
// BlockType, as well as to properly handle mod:name IDs
|
||||
|
||||
String originalInput = input;
|
||||
input = input.replace("_", " ");
|
||||
input = input.replace(";", "|");
|
||||
@ -84,51 +99,44 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
|
||||
}
|
||||
}
|
||||
|
||||
private static Pattern blockStatePattern = Pattern.compile("([a-z:]+)(?:\\[([a-zA-Z0-9=, ]+)\\])?", Pattern.CASE_INSENSITIVE);
|
||||
private static String[] EMPTY_STRING_ARRAY = new String[]{};
|
||||
|
||||
private BaseBlock parseLogic(String input, ParserContext context)
|
||||
throws InputParseException, NoMatchException,
|
||||
DisallowedUsageException {
|
||||
BlockType blockType;
|
||||
Map<State, StateValue> blockStates = new HashMap<>();
|
||||
String[] blockAndExtraData = input.split("\\|");
|
||||
String[] blockLocator = blockAndExtraData[0].split(":", 3);
|
||||
String[] typeAndData;
|
||||
switch (blockLocator.length) {
|
||||
case 3:
|
||||
typeAndData = new String[] {
|
||||
blockLocator[0] + ":" + blockLocator[1],
|
||||
blockLocator[2] };
|
||||
break;
|
||||
default:
|
||||
typeAndData = blockLocator;
|
||||
Matcher matcher = blockStatePattern.matcher(blockAndExtraData[0]);
|
||||
if (matcher.groupCount() < 1 || matcher.groupCount() > 2) {
|
||||
throw new InputParseException("Invalid format");
|
||||
}
|
||||
String typeString = matcher.group(1);
|
||||
String[] stateProperties = EMPTY_STRING_ARRAY;
|
||||
if (matcher.groupCount() == 2) {
|
||||
stateProperties = matcher.group(2).split(",");
|
||||
}
|
||||
String testId = typeAndData[0];
|
||||
|
||||
int blockId = -1;
|
||||
|
||||
int data = -1;
|
||||
|
||||
boolean parseDataValue = true;
|
||||
|
||||
if ("hand".equalsIgnoreCase(testId)) {
|
||||
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;
|
||||
}
|
||||
|
||||
blockId = blockInHand.getId();
|
||||
blockType = BlockType.fromID(blockId);
|
||||
data = blockInHand.getData();
|
||||
} else if ("offhand".equalsIgnoreCase(testId)) {
|
||||
blockType = blockInHand.getType();
|
||||
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;
|
||||
}
|
||||
|
||||
blockId = blockInHand.getId();
|
||||
blockType = BlockType.fromID(blockId);
|
||||
data = blockInHand.getData();
|
||||
} else if ("pos1".equalsIgnoreCase(testId)) {
|
||||
blockType = blockInHand.getType();
|
||||
blockStates = blockInHand.getStates();
|
||||
} else if ("pos1".equalsIgnoreCase(typeString)) {
|
||||
// Get the block type from the "primary position"
|
||||
final World world = context.requireWorld();
|
||||
final BlockVector primaryPosition;
|
||||
@ -142,135 +150,49 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
|
||||
return blockInHand;
|
||||
}
|
||||
|
||||
blockId = blockInHand.getId();
|
||||
blockType = BlockType.fromID(blockId);
|
||||
data = blockInHand.getData();
|
||||
blockType = blockInHand.getType();
|
||||
blockStates = blockInHand.getStates();
|
||||
} 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.getPlatformManager().queryCapability(Capability.USER_COMMANDS).resolveItem(testId);
|
||||
if (t >= 0) {
|
||||
blockType = BlockType.fromID(t); // Could be null
|
||||
blockId = t;
|
||||
} else if (blockLocator.length == 2) { // Block IDs in MC 1.7 and above use mod:name
|
||||
t = worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).resolveItem(blockAndExtraData[0]);
|
||||
if (t >= 0) {
|
||||
blockType = BlockType.fromID(t); // Could be null
|
||||
blockId = t;
|
||||
typeAndData = new String[] { blockAndExtraData[0] };
|
||||
testId = blockAndExtraData[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Attempt to lookup a block from ID or name.
|
||||
blockType = BlockTypes.getBlockType(typeString);
|
||||
|
||||
if (blockId == -1 && blockType == null) {
|
||||
// Maybe it's a cloth
|
||||
ClothColor col = ClothColor.lookup(testId);
|
||||
if (col == null) {
|
||||
throw new NoMatchException("Can't figure out what block '" + input + "' refers to");
|
||||
}
|
||||
|
||||
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)) {
|
||||
if (blockType == null) {
|
||||
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
|
||||
}
|
||||
}
|
||||
|
||||
if (!context.isPreferringWildcard() && data == -1) {
|
||||
// No wildcards allowed => eliminate them.
|
||||
data = 0;
|
||||
BlockState state;
|
||||
|
||||
if (!context.isPreferringWildcard()) {
|
||||
// No wildcards allowed => eliminate them. (Start with default state)
|
||||
state = blockType.getDefaultState();
|
||||
} else {
|
||||
state = new BlockState(blockType, blockStates);
|
||||
}
|
||||
|
||||
if (parseDataValue) { // Block data not yet detected
|
||||
if (stateProperties.length > 0) { // Block data not yet detected
|
||||
// Parse the block data (optional)
|
||||
try {
|
||||
if (typeAndData.length > 1 && !typeAndData[1].isEmpty()) {
|
||||
data = Integer.parseInt(typeAndData[1]);
|
||||
}
|
||||
for (String parseableData : stateProperties) {
|
||||
try {
|
||||
String[] parts = parseableData.split("=");
|
||||
if (parts.length != 2) {
|
||||
throw new NoMatchException("Bad state format in " + parseableData);
|
||||
}
|
||||
|
||||
if (data > 15) {
|
||||
throw new NoMatchException("Invalid data value '" + typeAndData[1] + "'");
|
||||
}
|
||||
State stateKey = BundledBlockData.getInstance().findById(blockType.getId()).states.get(parts[0]);
|
||||
if (stateKey == null) {
|
||||
throw new NoMatchException("Unknown state " + parts[0] + " for block " + blockType.getName());
|
||||
}
|
||||
StateValue value = stateKey.getValueFor(parts[1]);
|
||||
if (value == null) {
|
||||
throw new NoMatchException("Unknown value " + parts[1] + " for state " + parts[0]);
|
||||
}
|
||||
|
||||
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] + "'");
|
||||
state = state.with(stateKey, value);
|
||||
} catch (NoMatchException e) {
|
||||
throw e; // Pass-through
|
||||
} catch (Exception e) {
|
||||
throw new NoMatchException("Unknown state '" + parseableData + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -278,94 +200,46 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
|
||||
// Check if the item is allowed
|
||||
Actor actor = context.requireActor();
|
||||
if (context.isRestricted() && actor != null && !actor.hasPermission("worldedit.anyblock")
|
||||
&& worldEdit.getConfiguration().disallowedBlocks.contains(blockId)) {
|
||||
&& worldEdit.getConfiguration().disallowedBlocks.contains(blockType.getId())) {
|
||||
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.getPlatformManager().queryCapability(Capability.USER_COMMANDS).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.");
|
||||
}
|
||||
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);
|
||||
} else if (blockType == BlockTypes.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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) {
|
||||
throw new NoMatchException("Unknown mob type '" + mobName + "'");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
default:
|
||||
return new BaseBlock(blockId, data);
|
||||
String type = blockAndExtraData[1];
|
||||
|
||||
return new SkullBlock(state, type.replace(" ", "_")); // valid MC usernames
|
||||
} else {
|
||||
return new BaseBlock(state);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class DefaultItemParser extends InputParser<BaseItem> {
|
||||
try {
|
||||
damage = Short.parseShort(tokens[2]);
|
||||
} catch (NumberFormatException ignored) {
|
||||
throw new InputParseException("Expected '" + tokens[2] + "' to be a metadata value but it's not a number");
|
||||
throw new InputParseException("Expected '" + tokens[2] + "' to be a damage value but it's not a number");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user