Fixed a bunch of extends and removed slottableblockbag

This commit is contained in:
MattBDev
2019-09-21 21:00:45 -04:00
parent 8b96cdc9a5
commit 24a147465d
52 changed files with 384 additions and 572 deletions

View File

@ -19,23 +19,17 @@
package com.sk89q.worldedit.extension.factory.parser;
import com.boydti.fawe.command.SuggestInputParseException;
import com.boydti.fawe.config.BBC;
import com.boydti.fawe.jnbt.JSON2NBT;
import com.boydti.fawe.jnbt.NBTException;
import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.StringMan;
import com.google.common.collect.Maps;
import com.sk89q.jnbt.CompoundTag;
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.BaseItem;
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.command.util.SuggestionHelper;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
import com.sk89q.worldedit.extension.input.InputParseException;
@ -43,8 +37,6 @@ import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.SlottableBlockBag;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
@ -57,13 +49,12 @@ import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.FuzzyBlockState;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.entity.EntityTypes;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@ -166,6 +157,71 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
}
}
private static Map<Property<?>, Object> parseProperties(BlockType type, String[] stateProperties, ParserContext context) throws NoMatchException {
Map<Property<?>, Object> blockStates = new HashMap<>();
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);
}
@SuppressWarnings("unchecked")
Property<Object> propertyKey = (Property<Object>) type.getPropertyMap().get(parts[0]);
if (propertyKey == null) {
if (context.getActor() != null) {
throw new NoMatchException("Unknown property " + parts[0] + " for block " + type.getId());
} else {
WorldEdit.logger.warn("Unknown property " + parts[0] + " for block " + type.getId());
}
return Maps.newHashMap();
}
if (blockStates.containsKey(propertyKey)) {
throw new NoMatchException("Duplicate property " + parts[0]);
}
Object value;
try {
value = propertyKey.getValueFor(parts[1]);
} catch (IllegalArgumentException e) {
throw new NoMatchException("Unknown value " + parts[1] + " for state " + parts[0]);
}
blockStates.put(propertyKey, value);
} catch (NoMatchException e) {
throw e; // Pass-through
} catch (Exception e) {
WorldEdit.logger.warn("Unknown state '" + parseableData + "'", e);
throw new NoMatchException("Unknown state '" + parseableData + "'");
}
}
}
return blockStates;
}
@Override
public Stream<String> getSuggestions(String input) {
final int idx = input.lastIndexOf('[');
if (idx < 0) {
return SuggestionHelper.getNamespacedRegistrySuggestions(BlockType.REGISTRY, input);
}
String blockType = input.substring(0, idx);
BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));
if (type == null) {
return Stream.empty();
}
String props = input.substring(idx + 1);
if (props.isEmpty()) {
return type.getProperties().stream().map(p -> input + p.getName() + "=");
}
return SuggestionHelper.getBlockPropertySuggestions(blockType, props);
}
private BaseBlock parseLogic(String input, ParserContext context) throws InputParseException {
BlockType blockType = null;
Map<Property<?>, Object> blockStates = new HashMap<>();
@ -182,28 +238,16 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
throw new InputParseException("Invalid colon.");
} else if (split.length == 1) {
state = LegacyMapper.getInstance().getBlockFromLegacy(Integer.parseInt(split[0]));
} else if (MathMan.isInteger(split[0])) {
int id = Integer.parseInt(split[0]);
int data = Integer.parseInt(split[1]);
if (data < 0 || data >= 16) {
throw new InputParseException("Invalid data " + data);
}
state = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
} else {
BlockType type = BlockTypes.get(split[0].toLowerCase(Locale.ROOT));
if (type != null) {
int data = Integer.parseInt(split[1]);
if (data < 0 || data >= 16) {
throw new InputParseException("Invalid data " + data);
}
state = LegacyMapper.getInstance().getBlockFromLegacy(type.getLegacyCombinedId() >> 4, data);
}
state = LegacyMapper.getInstance().getBlockFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
if (state != null) {
blockType = state.getBlockType();
}
} catch (NumberFormatException ignored) {
}
}
CompoundTag nbt = null;
if (state == null) {
String typeString;
String stateString = null;
@ -247,8 +291,7 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
blockType = blockInHand.getBlockType();
blockStates.putAll(blockInHand.getStates());
} else if (typeString.matches("pos[0-9]+")) {
int index = Integer.parseInt(typeString.replaceAll("[a-z]+", ""));
} else if ("pos1".equalsIgnoreCase(typeString)) {
// Get the block type from the "primary position"
final World world = context.requireWorld();
final BlockVector3 primaryPosition;
@ -261,74 +304,53 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
blockType = blockInHand.getBlockType();
blockStates.putAll(blockInHand.getStates());
} else if (typeString.matches("slot[0-9]+")) {
int slot = Integer.parseInt(typeString.substring(4)) - 1;
Actor actor = context.requireActor();
if (!(actor instanceof Player)) {
throw new InputParseException("The user is not a player!");
}
Player player = (Player) actor;
BlockBag bag = player.getInventoryBlockBag();
if (true) {
throw new InputParseException("Unsupported!");
}
SlottableBlockBag slottable = (SlottableBlockBag) bag;
BaseItem item = slottable.getItem(slot);
if (!item.getType().hasBlockType()) {
throw new InputParseException("You're not holding a block!");
}
state = item.getType().getBlockType().getDefaultState();
nbt = item.getNbtData();
} else {
BlockType type = BlockTypes.parse(typeString.toLowerCase(Locale.ROOT));
if (type != null) {
state = type.getDefaultState();
}
if (state == null) {
throw new NoMatchException(
"Does not match a valid block type: '" + input + "'");
}
// Attempt to lookup a block from ID or name.
blockType = BlockTypes.get(typeString.toLowerCase(Locale.ROOT));
}
if (nbt == null) nbt = state.getNbtData();
if (stateString != null) {
state = BlockState.get(state.getBlockType(), "[" + stateString + "]", state);
if (context.isPreferringWildcard()) {
if (stateString.isEmpty()) {
state = new FuzzyBlockState(state);
} else {
BlockType type = state.getBlockType();
FuzzyBlockState.Builder fuzzyBuilder = FuzzyBlockState.builder();
fuzzyBuilder.type(type);
String[] entries = stateString.split(",");
for (String entry : entries) {
String[] split = entry.split("=");
String key = split[0];
String val = split[1];
Property<Object> prop = type.getProperty(key);
fuzzyBuilder.withProperty(prop, prop.getValueFor(val));
}
state = fuzzyBuilder.build();
}
if (blockType == null) {
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
}
blockStates.putAll(parseProperties(blockType, stateProperties, context));
if (context.isPreferringWildcard()) {
FuzzyBlockState.Builder fuzzyBuilder = FuzzyBlockState.builder();
fuzzyBuilder.type(blockType);
for (Map.Entry<Property<?>, Object> blockState : blockStates.entrySet()) {
@SuppressWarnings("unchecked")
Property<Object> objProp = (Property<Object>) blockState.getKey();
fuzzyBuilder.withProperty(objProp, blockState.getValue());
}
state = fuzzyBuilder.build();
} else {
// No wildcards allowed => eliminate them. (Start with default state)
state = blockType.getDefaultState();
for (Map.Entry<Property<?>, Object> blockState : blockStates.entrySet()) {
@SuppressWarnings("unchecked")
Property<Object> objProp = (Property<Object>) blockState.getKey();
state = state.with(objProp, blockState.getValue());
}
}
}
if (blockAndExtraData.length > 1 && blockAndExtraData[1].startsWith("{")) {
String joined = StringMan.join(Arrays.copyOfRange(blockAndExtraData, 1, blockAndExtraData.length), "|");
try {
nbt = JSON2NBT.getTagFromJson(joined);
} catch (NBTException e) {
throw new NoMatchException(e.getMessage());
}
// this should be impossible but IntelliJ isn't that smart
if (blockType == null) {
throw new NoMatchException("Does not match a valid block type: '" + input + "'");
}
// Check if the item is allowed
BlockType blockType = state.getBlockType();
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 + "'");
}
}
if (nbt != null) return validate(context, state.toBaseBlock(nbt));
if (!context.isTryingLegacy()) {
return state.toBaseBlock();
}
if (blockType == BlockTypes.SIGN || blockType == BlockTypes.WALL_SIGN
|| BlockCategories.SIGNS.contains(blockType)) {
@ -343,22 +365,17 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
// 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(Locale.ROOT))) {
mobName = mobType.getName();
break;
}
EntityType ent = EntityTypes.get(mobName.toLowerCase(Locale.ROOT));
if (ent == null) {
throw new NoMatchException("Unknown entity type '" + mobName + "'");
}
mobName = ent.getId();
if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) {
String finalMobName = mobName.toLowerCase(Locale.ROOT);
throw new SuggestInputParseException("Unknown mob type '" + mobName + "'", mobName, () -> Stream.of(MobType.values())
.map(m -> m.getName().toLowerCase(Locale.ROOT))
.filter(s -> s.startsWith(finalMobName))
.collect(Collectors.toList()));
throw new NoMatchException("Unknown mob type '" + mobName + "'");
}
return validate(context, new MobSpawnerBlock(state, mobName));
} else {
return validate(context, new MobSpawnerBlock(state, MobType.PIG.getName()));
return validate(context, new MobSpawnerBlock(state, EntityTypes.PIG.getId()));
}
} else if (blockType == BlockTypes.PLAYER_HEAD || blockType == BlockTypes.PLAYER_WALL_HEAD) {
// allow setting type/player/rotation