From f71ca32140e0c0ae55f134d0c60c286fb7aedc54 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 5 Oct 2020 21:00:35 +0200 Subject: [PATCH] Reimplement biome pattern (#681) * Reimplement biome pattern * Improve suggestions and messages --- .../object/pattern/BiomeApplyingPattern.java | 28 +++++++ .../extension/factory/PatternFactory.java | 2 + .../parser/pattern/BiomePatternParser.java | 74 +++++++++++++++++++ .../registry/NamespacedRegistry.java | 32 +------- 4 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 worldedit-core/src/main/java/com/boydti/fawe/object/pattern/BiomeApplyingPattern.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BiomePatternParser.java diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/pattern/BiomeApplyingPattern.java b/worldedit-core/src/main/java/com/boydti/fawe/object/pattern/BiomeApplyingPattern.java new file mode 100644 index 000000000..783dc23c8 --- /dev/null +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/pattern/BiomeApplyingPattern.java @@ -0,0 +1,28 @@ +package com.boydti.fawe.object.pattern; + +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.world.biome.BiomeType; +import com.sk89q.worldedit.world.block.BaseBlock; + +public class BiomeApplyingPattern extends AbstractExtentPattern { + private final BiomeType biomeType; + + public BiomeApplyingPattern(Extent extent, BiomeType biomeType) { + super(extent); + this.biomeType = biomeType; + } + + @Override + public BaseBlock apply(BlockVector3 position) { + getExtent().setBiome(position, this.biomeType); + // don't change the block, everything should remain the same + return getExtent().getFullBlock(position); + } + + @Override + public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException { + return extent.setBiome(set, this.biomeType); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java index fc309bf82..e8bad9814 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.extension.factory; import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.extension.factory.parser.pattern.BiomePatternParser; import com.sk89q.worldedit.extension.factory.parser.pattern.BlockCategoryPatternParser; import com.sk89q.worldedit.extension.factory.parser.pattern.ClipboardPatternParser; import com.sk89q.worldedit.extension.factory.parser.pattern.PerlinPatternParser; @@ -64,6 +65,7 @@ public final class PatternFactory extends AbstractFactory { register(new VoronoiPatternParser(worldEdit)); register(new PerlinPatternParser(worldEdit)); register(new RidgedMultiFractalPatternParser(worldEdit)); + register(new BiomePatternParser(worldEdit)); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BiomePatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BiomePatternParser.java new file mode 100644 index 000000000..7f65f4ff3 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BiomePatternParser.java @@ -0,0 +1,74 @@ +package com.sk89q.worldedit.extension.factory.parser.pattern; + +import com.boydti.fawe.object.pattern.BiomeApplyingPattern; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.extension.factory.parser.RichParser; +import com.sk89q.worldedit.extension.input.InputParseException; +import com.sk89q.worldedit.extension.input.NoMatchException; +import com.sk89q.worldedit.extension.input.ParserContext; +import com.sk89q.worldedit.function.pattern.Pattern; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.world.biome.BiomeType; +import com.sk89q.worldedit.world.biome.BiomeTypes; +import org.jetbrains.annotations.NotNull; + +import java.util.stream.Stream; + +public class BiomePatternParser extends RichParser { + private final static String BIOME_PREFIX = "$"; + + /** + * Create a new biome pattern parser. + * + * @param worldEdit the worldedit instance. + */ + public BiomePatternParser(WorldEdit worldEdit) { + super(worldEdit, "#biome"); + } + + // overridden to provide $ too + @Override + public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { + if (input.startsWith(BIOME_PREFIX)) { + String biomeId = input.substring(1); + BiomeType biomeType = BiomeTypes.get(biomeId); + if (biomeType == null) { + throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-biome", TextComponent.of(biomeId))); + } + return new BiomeApplyingPattern(context.requireExtent(), biomeType); + } else { + return super.parseFromInput(input, context); + } + } + + // overridden to provide $ too + @Override + public Stream getSuggestions(String input) { + if (input.startsWith(BIOME_PREFIX)) { + return BiomeType.REGISTRY.getSuggestions(input.substring(1)).map(biome -> BIOME_PREFIX + biome); + } else { + return super.getSuggestions(input); + } + } + + @Override + protected Stream getSuggestions(String argumentInput, int index) { + if (index == 0) { + return BiomeType.REGISTRY.getSuggestions(argumentInput); + } + return Stream.empty(); + } + + @Override + protected Pattern parseFromInput(@NotNull String[] arguments, ParserContext context) throws InputParseException { + if (arguments.length != 1) { + throw new InputParseException("Invalid amount of arguments. Expected: #biome[]"); + } + BiomeType biomeType = BiomeTypes.get(arguments[0]); + if (biomeType == null) { + throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-biome", TextComponent.of(arguments[0]))); + } + return new BiomeApplyingPattern(context.requireExtent(), biomeType); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java index 0de3f78a1..3f8bca337 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java @@ -19,19 +19,18 @@ package com.sk89q.worldedit.registry; +import com.sk89q.worldedit.command.util.SuggestionHelper; + +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.Set; -import java.util.function.Predicate; import java.util.stream.Stream; -import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; -import static org.enginehub.piston.converter.SuggestionHelper.byPrefix; public final class NamespacedRegistry extends Registry { private static final String MINECRAFT_NAMESPACE = "minecraft"; @@ -107,29 +106,6 @@ public final class NamespacedRegistry extends Registry { } public Stream getSuggestions(String input) { - if (input.isEmpty() || input.equals(":")) { - final Set namespaces = getKnownNamespaces(); - if (namespaces.size() == 1) { - return keySet().stream(); - } else { - return namespaces.stream().map(s -> s + ":"); - } - } - if (input.startsWith(":")) { // special case - search across namespaces - final String term = input.substring(1).toLowerCase(Locale.ROOT); - Predicate search = byPrefix(term); - return keySet().stream().filter(s -> search.test(s.substring(s.indexOf(':') + 1))); - } - // otherwise, we actually have some text to search - if (input.indexOf(':') < 0) { - // don't yet have namespace - search namespaces + default - final String lowerSearch = input.toLowerCase(Locale.ROOT); - String defKey = getDefaultNamespace() + ":" + lowerSearch; - return Stream.concat(keySet().stream().filter(s -> s.startsWith(defKey)), - getKnownNamespaces().stream().filter(n -> n.startsWith(lowerSearch)).map(n -> n + ":")); - } - // have a namespace - search that - Predicate search = byPrefix(input.toLowerCase(Locale.ROOT)); - return keySet().stream().filter(search); + return SuggestionHelper.getNamespacedRegistrySuggestions(this, input); } }