From 472aadcd01d3564dba26608d9e853b985047b678 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Thu, 16 Jun 2016 02:30:16 +0200 Subject: [PATCH] Add suggestion for flags when invalid flag name was given :] --- .../plot/commands/FlagCmd.java | 14 +++++++++++++- .../com/intellectualcrafters/plot/config/C.java | 1 + .../com/intellectualcrafters/plot/flag/Flag.java | 8 +++++++- .../plot/util/StringComparison.java | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java index 9fcaf65f6..2145ebd25 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java @@ -12,6 +12,7 @@ import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.StringComparison; import com.intellectualcrafters.plot.util.StringMan; import com.plotsquared.general.commands.CommandDeclaration; @@ -63,7 +64,18 @@ public class FlagCmd extends SubCommand { if (args.length > 1) { flag = FlagManager.getFlag(args[1]); if (flag == null || flag.isReserved()) { - MainUtil.sendMessage(player, C.NOT_VALID_FLAG); + boolean suggested = false; + try { + StringComparison> stringComparison = new StringComparison<>(args[1], Flags.getFlags()); + String best = stringComparison.getBestMatch(); + if (best != null) { + MainUtil.sendMessage(player, C.NOT_VALID_FLAG_SUGGESTED, best); + suggested = true; + } + } catch (final Exception ignored) { /* Happens sometimes because of mean code */ } + if (!suggested) { + MainUtil.sendMessage(player, C.NOT_VALID_FLAG); + } return false; } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java index 293c937e0..baa45a904 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -553,6 +553,7 @@ public enum C { FLAG_TYPE("$2Type: %s", "Flag"), FLAG_DESC("$2Desc: %s", "Flag"), NOT_VALID_FLAG("$2That is not a valid flag", "Flag"), + NOT_VALID_FLAG_SUGGESTED("$2That is not a valid flag. Did you mean: $1%s", "Flag"), NOT_VALID_VALUE("$2Flag values must be alphanumerical", "Flag"), FLAG_NOT_IN_PLOT("$2The plot does not have that flag", "Flag"), FLAG_NOT_REMOVED("$2The flag could not be removed", "Flag"), diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java index 8b986dfa9..ee67af527 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java @@ -1,8 +1,9 @@ package com.intellectualcrafters.plot.flag; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.util.StringComparison; -public abstract class Flag { +public abstract class Flag implements StringComparison.StringComparable { private final String name; private boolean reserved = false; @@ -53,4 +54,9 @@ public abstract class Flag { public boolean isSet(Plot plot) { return FlagManager.getPlotFlagRaw(plot, this) != null; } + + @Override + public String getComparableString() { + return getName(); + } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java b/Core/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java index 7f29519a6..01e171442 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/StringComparison.java @@ -99,6 +99,9 @@ public class StringComparison { } public String getString(T o) { + if (o instanceof StringComparable) { + return ((StringComparable) o).getComparableString(); + } return o.toString(); } @@ -147,4 +150,8 @@ public class StringComparison { this.best = best; } } + + public interface StringComparable { + String getComparableString(); + } }