From 9f9fda72b79434d74e8c2e13cce11a13201867b2 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 23 Jul 2018 12:48:11 +1000 Subject: [PATCH] Re-add legacy support to block parser, and fix query tool. --- .../com/sk89q/worldedit/blocks/BaseBlock.java | 6 +- .../worldedit/command/tool/QueryTool.java | 6 +- .../extension/factory/DefaultBlockParser.java | 126 ++++++++++-------- .../extension/input/ParserContext.java | 19 +++ .../worldedit/world/block/BlockState.java | 5 + .../world/registry/LegacyMapper.java | 5 +- 6 files changed, 108 insertions(+), 59 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java index 10e0814d1..d9c976c65 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java @@ -227,7 +227,11 @@ public class BaseBlock implements BlockStateHolder, TileEntityBlock { @Override public String toString() { - return "Block{State: " + this.toImmutableState().toString() + ", NBT: " + String.valueOf(getNbtData()) + "}"; +// if (getNbtData() != null) { // TODO Maybe make some JSON serialiser to make this not awful. +// return blockState.getAsString() + " {" + String.valueOf(getNbtData()) + "}"; +// } else { + return blockState.getAsString(); +// } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java index 435da2cdb..5a8f6165e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java @@ -47,10 +47,10 @@ public class QueryTool implements BlockTool { BlockStateHolder block = editSession.getFullBlock(clicked.toVector()); player.print("\u00A79@" + clicked.toVector() + ": " + "\u00A7e" - + "#" + block.getBlockType() + "\u00A77" + " (" - + block.getBlockType().getId() + ") " + + block.getBlockType().getName() + "\u00A77" + " (" + + block.toString() + ") " + "\u00A7f" - + "[" + block.getStates().toString() + "]" + " (" + world.getBlockLightLevel(clicked.toVector()) + "/" + world.getBlockLightLevel(clicked.toVector().add(0, 1, 0)) + ")"); + + " (" + world.getBlockLightLevel(clicked.toVector()) + "/" + world.getBlockLightLevel(clicked.toVector().add(0, 1, 0)) + ")"); if (block instanceof MobSpawnerBlock) { player.printRaw("\u00A7e" + "Mob Type: " diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java index 5d58d0e49..7c90051bc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java @@ -44,6 +44,7 @@ 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.registry.LegacyMapper; import java.util.HashMap; import java.util.Map; @@ -185,71 +186,90 @@ class DefaultBlockParser extends InputParser { } private BlockStateHolder parseLogic(String input, ParserContext context) throws InputParseException { - BlockType blockType; + BlockType blockType = null; Map, Object> blockStates = new HashMap<>(); String[] blockAndExtraData = input.trim().split("\\|"); blockAndExtraData[0] = woolMapper(blockAndExtraData[0]); - Matcher matcher = blockStatePattern.matcher(blockAndExtraData[0]); - if (!matcher.matches() || matcher.groupCount() < 2 || matcher.groupCount() > 3) { - throw new InputParseException("Invalid format"); - } - String typeString = matcher.group(1); - String[] stateProperties = EMPTY_STRING_ARRAY; - if (matcher.groupCount() >= 2 && matcher.group(2) != null) { - stateProperties = matcher.group(2).split(","); - } - 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; - } + BlockState state = null; - 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(); - final BlockVector primaryPosition; + // Legacy matcher + if (context.isTryingLegacy()) { 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. - blockType = BlockTypes.get(typeString); - - if (blockType == null) { - throw new NoMatchException("Does not match a valid block type: '" + input + "'"); + 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) { } } - BlockState state; + if (state == null) { + Matcher matcher = blockStatePattern.matcher(blockAndExtraData[0]); // TODO Move away from regex because it's hella slow + if (!matcher.matches() || matcher.groupCount() < 2 || matcher.groupCount() > 3) { + throw new InputParseException("Invalid format"); + } + String typeString = matcher.group(1); + String[] stateProperties = EMPTY_STRING_ARRAY; + if (matcher.groupCount() >= 2 && matcher.group(2) != null) { + stateProperties = matcher.group(2).split(","); + } - if (!context.isPreferringWildcard()) { - // No wildcards allowed => eliminate them. (Start with default state) - state = blockType.getDefaultState(); - } else { - state = new BlockState(blockType, blockStates); + 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; + } + + 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(); + final BlockVector primaryPosition; + 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. + blockType = BlockTypes.get(typeString); + + if (blockType == null) { + throw new NoMatchException("Does not match a valid block type: '" + input + "'"); + } + } + + if (!context.isPreferringWildcard()) { + // No wildcards allowed => eliminate them. (Start with default state) + state = blockType.getDefaultState(); + } else { + state = new BlockState(blockType, blockStates); + } + + state = applyProperties(state, stateProperties); } - state = applyProperties(state, stateProperties); - // Check if the item is allowed if (context.isRestricted()) { Actor actor = context.requireActor(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java index b2f61e880..d6fced5eb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java @@ -40,6 +40,7 @@ public class ParserContext { private @Nullable World world; private @Nullable Actor actor; private boolean restricted = true; + private boolean tryLegacy = true; private boolean preferringWildcard; /** @@ -60,6 +61,7 @@ public class ParserContext { setActor(other.getActor()); setRestricted(other.isRestricted()); setPreferringWildcard(other.isPreferringWildcard()); + setTryLegacy(other.isTryingLegacy()); } /** @@ -229,4 +231,21 @@ public class ParserContext { this.preferringWildcard = preferringWildcard; } + /** + * Set whether legacy IDs should be attempted. + * + * @param tryLegacy true if legacy IDs should be attempted + */ + public void setTryLegacy(boolean tryLegacy) { + this.tryLegacy = tryLegacy; + } + + /** + * Get whether legacy IDs should be tried. + * + * @return true if legacy should be tried + */ + public boolean isTryingLegacy() { + return tryLegacy; + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index 7adfdded5..8828d12f2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -212,4 +212,9 @@ public class BlockState implements BlockStateHolder { this.values.put(property, value); return this; } + + @Override + public String toString() { + return getAsString(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/LegacyMapper.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/LegacyMapper.java index 8950b8242..6b1be416f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/LegacyMapper.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/LegacyMapper.java @@ -81,13 +81,14 @@ public class LegacyMapper { ParserContext parserContext = new ParserContext(); parserContext.setPreferringWildcard(false); parserContext.setRestricted(false); + parserContext.setTryLegacy(false); // This is legacy. Don't match itself. for (Map.Entry blockEntry : dataFile.blocks.entrySet()) { try { blockMap.put(blockEntry.getKey(), (BlockState) WorldEdit.getInstance().getBlockFactory().parseFromInput(blockEntry.getValue(), parserContext)); } catch (Exception e) { - log.warning("Unknown block: " + blockEntry.getValue()); + log.fine("Unknown block: " + blockEntry.getValue()); } } @@ -95,7 +96,7 @@ public class LegacyMapper { try { itemMap.put(itemEntry.getKey(), ItemTypes.get(itemEntry.getValue())); } catch (Exception e) { - log.warning("Unknown item: " + itemEntry.getValue()); + log.fine("Unknown item: " + itemEntry.getValue()); } } }