From 113aeb1689d954e59d44de1bf142cb1b5b448f2e Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Tue, 7 May 2019 16:40:37 +1000 Subject: [PATCH] Use fuzzy blocks in default block parser --- .../factory/parser/DefaultBlockParser.java | 23 +++++++++++++- .../parser/mask/DefaultMaskParser.java | 2 +- .../world/block/FuzzyBlockState.java | 31 ++++++++++++------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java index 82f9792c6..09d4ce435 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java @@ -47,6 +47,7 @@ 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; import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; @@ -54,9 +55,11 @@ import com.sk89q.worldedit.world.block.BlockState; 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.registry.LegacyMapper; import java.util.Arrays; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -249,10 +252,28 @@ public class DefaultBlockParser extends InputParser { throw new NoMatchException(BBC.getPrefix() + "Does not match a valid block type: '" + input + "'"); } } -// if (nbt == null) nbt = state.getNbtData(); + 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 prop = type.getProperty(key); + fuzzyBuilder.withProperty(prop, prop.getValueFor(val)); + } + state = fuzzyBuilder.build(); + } + } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java index 93127b01a..e24602ee9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java @@ -174,7 +174,7 @@ public class DefaultMaskParser extends FaweParser { } catch (InputParseException ignore) {} } if (mask == null) { - context.setPreferringWildcard(true); + context.setPreferringWildcard(false); context.setRestricted(false); BlockStateHolder block = worldEdit.getBlockFactory().parseFromInput(full, context); builder.add(block); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java index 28a8b7bb6..230b479ad 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java @@ -37,20 +37,24 @@ import java.util.Objects; */ public class FuzzyBlockState extends BlockState { - private final Map state; + private final Map props; - FuzzyBlockState(BlockType blockType) { - this(blockType, null); + public FuzzyBlockState(BlockType blockType) { + this(blockType.getDefaultState(), null); } - private FuzzyBlockState(BlockType blockType, Map, Object> values) { - super(blockType, blockType.getDefaultState().getInternalId(), blockType.getDefaultState().getOrdinal()); + public FuzzyBlockState(BlockState state) { + this(state, null); + } + + private FuzzyBlockState(BlockState state, Map, Object> values) { + super(state.getBlockType(), state.getInternalId(), state.getOrdinal()); if (values == null || values.isEmpty()) { - state = Collections.emptyMap(); + props = Collections.emptyMap(); } else { - state = new HashMap<>(values.size()); + props = new HashMap<>(values.size()); for (Map.Entry, Object> entry : values.entrySet()) { - state.put(entry.getKey().getKey(), entry.getValue()); + props.put(entry.getKey().getKey(), entry.getValue()); } } } @@ -76,8 +80,8 @@ public class FuzzyBlockState extends BlockState { if (!getBlockType().equals(o.getBlockType())) { return false; } - if (!state.isEmpty()) { - for (Map.Entry entry : state.entrySet()) { + if (!props.isEmpty()) { + for (Map.Entry entry : props.entrySet()) { if (!Objects.equals(o.getState(entry.getKey()), entry.getValue())) { return false; } @@ -86,6 +90,11 @@ public class FuzzyBlockState extends BlockState { return true; } + @Override + public BaseBlock toBaseBlock() { + return new BaseBlock(); + } + @Override public BlockState toImmutableState() { return getFullState(); @@ -158,7 +167,7 @@ public class FuzzyBlockState extends BlockState { if (values.isEmpty()) { return type.getFuzzyMatcher(); } - return new FuzzyBlockState(type, values); + return new FuzzyBlockState(type.getDefaultState(), values); } /**