From 444222c7f93d677f1d79eb82da9348c05de68266 Mon Sep 17 00:00:00 2001 From: sauilitired Date: Wed, 19 Dec 2018 18:36:50 +0100 Subject: [PATCH] Improve PlotBlock name matching --- .../bukkit/util/BukkitLegacyMappings.java | 29 ++++++++++++--- .../plotsquared/bukkit/util/BukkitUtil.java | 36 +++---------------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java index 87693dbc4..df27b4f2d 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java @@ -4,12 +4,20 @@ import com.github.intellectualsites.plotsquared.plot.object.LegacyPlotBlock; import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import com.github.intellectualsites.plotsquared.plot.object.StringPlotBlock; import com.github.intellectualsites.plotsquared.plot.util.LegacyMappings; -import lombok.*; -import org.bukkit.Material; - +import com.github.intellectualsites.plotsquared.plot.util.StringComparison; +import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.stream.Collectors; +import lombok.AccessLevel; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import org.bukkit.Material; /** * Borrowed from https://github.com/Phoenix616/IDConverter/blob/master/mappings/src/main/java/de/themoep/idconverter/IdMappings.java @@ -692,6 +700,16 @@ public class BukkitLegacyMappings extends LegacyMappings { } } + public Collection getPlotBlocks() { + return Arrays.stream(BLOCKS).map(block -> PlotBlock.get(block.getNewName())) + .collect(Collectors.toList()); + } + + public StringComparison.ComparisonResult getClosestsMatch(@NonNull final String string) { + final StringComparison comparison = new StringComparison<>(string, getPlotBlocks()); + return comparison.getBestMatchAdvanced(); + } + /** * Try to find a legacy plot block by any means possible. * Strategy: @@ -794,7 +812,10 @@ public class BukkitLegacyMappings extends LegacyMappings { return LegacyPlotBlock.get(numericalId, dataValue); } - + @Override + public String toString() { + return this.newName; + } } } diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java index d4f8b0931..88d51d7ed 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java @@ -448,39 +448,11 @@ import org.bukkit.material.Wool; @Override @Nullable public StringComparison.ComparisonResult getClosestBlock(String name) { - try { - final Material material = Material.valueOf(name.toUpperCase()); - return new StringComparison().new ComparisonResult(1, - PlotBlock.get(name)); - } catch (IllegalArgumentException ignored) { + final PlotBlock plotBlock = BukkitUtil.getBukkitLegacyMappings().fromAny(name); + if (plotBlock != null) { + return new StringComparison().new ComparisonResult(1, plotBlock); } - try { - byte data; - String[] split = name.split(":"); - if (split.length == 2) { - data = Byte.parseByte(split[1]); - name = split[0]; - } else { - data = 0; - } - double match; - short id; - if (MathMan.isInteger(split[0])) { - id = Short.parseShort(split[0]); - match = 0; - } else { - StringComparison.ComparisonResult comparison = - new StringComparison<>(name, Material.values()).getBestMatchAdvanced(); - match = comparison.match; - id = (short) comparison.best.getId(); - } - PlotBlock block = PlotBlock.get(id, data); - StringComparison outer = new StringComparison<>(); - return outer.new ComparisonResult(match, block); - - } catch (NumberFormatException ignored) { - } - return null; + return BukkitUtil.getBukkitLegacyMappings().getClosestsMatch(name); } @Override