diff --git a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java index 7287470f5..b0ac7ec99 100644 --- a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java @@ -30,6 +30,7 @@ import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.CaptionUtility; import com.plotsquared.core.configuration.caption.StaticCaption; +import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.events.PlotFlagRemoveEvent; @@ -136,7 +137,7 @@ public final class FlagCommand extends Command { TranslatableCaption.of("flag.flag_parse_error"), Template.of("flag_name", flag.getName()), Template.of("flag_value", e.getValue()), - Template.of("error", e.getErrorMessage()) + Templates.of(player, "error", e.getErrorMessage(), e.getTemplates()) ); return false; } catch (final Exception e) { @@ -333,7 +334,7 @@ public final class FlagCommand extends Command { TranslatableCaption.of("flag.flag_parse_error"), Template.of("flag_name", plotFlag.getName()), Template.of("flag_value", e.getValue()), - Template.of("error", e.getErrorMessage()) + Templates.of(player, "error", e.getErrorMessage(), e.getTemplates()) ); return; } @@ -392,7 +393,7 @@ public final class FlagCommand extends Command { TranslatableCaption.of("flag.flag_parse_error"), Template.of("flag_name", plotFlag.getName()), Template.of("flag_value", e.getValue()), - Template.of("error", e.getErrorMessage()) + Templates.of(player, "error", e.getErrorMessage(), e.getTemplates()) ); return; } @@ -462,7 +463,7 @@ public final class FlagCommand extends Command { TranslatableCaption.of("flag.flag_parse_error"), Template.of("flag_name", flag.getName()), Template.of("flag_value", e.getValue()), - Template.of("error", e.getErrorMessage()) + Templates.of(player, "error", e.getErrorMessage(), e.getTemplates()) ); return; } @@ -576,32 +577,23 @@ public final class FlagCommand extends Command { if (plotFlag != null) { player.sendMessage(TranslatableCaption.of("flag.flag_info_header")); // Flag name - new PlotMessage(Captions.FLAG_INFO_NAME.getTranslated()) - .color(Captions.FLAG_INFO_COLOR_KEY.getTranslated()).text(plotFlag.getName()) - .color(Captions.FLAG_INFO_COLOR_VALUE.getTranslated()).send(player); + player.sendMessage(TranslatableCaption.of("flag.flag_info_name"), Template.of("flag", plotFlag.getName())); // Flag category - new PlotMessage(Captions.FLAG_INFO_CATEGORY.getTranslated()) - .color(Captions.FLAG_INFO_COLOR_KEY.getTranslated()) - .text(plotFlag.getFlagCategory().getTranslated()) - .color(Captions.FLAG_INFO_COLOR_VALUE.getTranslated()).send(player); + player.sendMessage(TranslatableCaption.of("flag.flag_info_category"), + Templates.of(player, "value", plotFlag.getFlagCategory())); // Flag description - new PlotMessage(Captions.FLAG_INFO_DESCRIPTION.getTranslated()) - .color(Captions.FLAG_INFO_COLOR_KEY.getTranslated()).send(player); - new PlotMessage(plotFlag.getFlagDescription().getTranslated()) - .color(Captions.FLAG_INFO_COLOR_VALUE.getTranslated()).send(player); + // TODO maybe merge and \n instead? + player.sendMessage(TranslatableCaption.of("flag.flag_info_description")); + player.sendMessage(plotFlag.getFlagDescription()); // Flag example - new PlotMessage(Captions.FLAG_INFO_EXAMPLE.getTranslated()) - .color(Captions.FLAG_INFO_COLOR_KEY.getTranslated()) - .text("/plot flag set " + plotFlag.getName() + " " + plotFlag.getExample()) - .color(Captions.FLAG_INFO_COLOR_VALUE.getTranslated()) - .suggest("/plot flag set " + plotFlag.getName() + " " + plotFlag.getExample()) - .send(player); + player.sendMessage(TranslatableCaption.of("flag.flag_info_example"), + Template.of("flag", plotFlag.getName()), + Template.of("value", plotFlag.getExample())); // Default value final String defaultValue = player.getLocation().getPlotArea().getFlagContainer() .getFlagErased(plotFlag.getClass()).toString(); - new PlotMessage(Captions.FLAG_INFO_DEFAULT_VALUE.getTranslated()) - .color(Captions.FLAG_INFO_COLOR_KEY.getTranslated()).text(defaultValue) - .color(Captions.FLAG_INFO_COLOR_VALUE.getTranslated()).send(player); + player.sendMessage(TranslatableCaption.of("flag.flag_info_default_value"), + Template.of("value", defaultValue)); // Footer. Done this way to prevent the duplicate-message-thingy from catching it player.sendMessage(TranslatableCaption.of("flag.flag_info_footer")); } diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/FlagParseException.java b/Core/src/main/java/com/plotsquared/core/plot/flag/FlagParseException.java index b8def8c93..2175c51c3 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/FlagParseException.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/FlagParseException.java @@ -26,13 +26,15 @@ package com.plotsquared.core.plot.flag; import com.plotsquared.core.configuration.caption.Caption; -import com.plotsquared.core.configuration.caption.CaptionUtility; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; public class FlagParseException extends Exception { private final PlotFlag flag; private final String value; - private final String errorMessage; + private final Caption errorMessage; + private final Template[] templates; /** * Construct a new flag parse exception to indicate that an attempt to parse a plot @@ -44,12 +46,13 @@ public class FlagParseException extends Exception { * @param args Arguments used to format the error message */ public FlagParseException(final PlotFlag flag, final String value, - final Caption errorMessage, final Object... args) { + final Caption errorMessage, final Template... args) { super(String.format("Failed to parse flag of type '%s'. Value '%s' was not accepted.", flag.getName(), value)); this.flag = flag; this.value = value; - this.errorMessage = CaptionUtility.format(null, errorMessage, args); + this.errorMessage = errorMessage; + this.templates = args; } /** @@ -75,8 +78,15 @@ public class FlagParseException extends Exception { * * @return Error message. */ - public String getErrorMessage() { + public Caption getErrorMessage() { return errorMessage; } + /** + * Get the templates that were supplied by the flag instance. + * @return Message templates. + */ + public Template[] getTemplates() { + return templates; + } } diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/PlotFlag.java b/Core/src/main/java/com/plotsquared/core/plot/flag/PlotFlag.java index 57f17c0d6..c6ad2eeb6 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/PlotFlag.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/PlotFlag.java @@ -62,8 +62,20 @@ public abstract class PlotFlag> { this.flagDescription = Preconditions.checkNotNull(flagDescription, "flag description may not be null"); // Parse flag name + // noinspection unchecked + this.flagName = getFlagName(this.getClass()); + } + + /** + * Return the name of the flag. + * @param flagClass Flag class + * @param Value type + * @param Flag type + * @return The name of the flag implemented by the given class + */ + public static > String getFlagName(Class flagClass) { final StringBuilder flagName = new StringBuilder(); - final char[] chars = this.getClass().getSimpleName().replace("Flag", "").toCharArray(); + final char[] chars = flagClass.getSimpleName().replace("Flag", "").toCharArray(); for (int i = 0; i < chars.length; i++) { if (i == 0) { flagName.append(Character.toLowerCase(chars[i])); @@ -73,7 +85,7 @@ public abstract class PlotFlag> { flagName.append(chars[i]); } } - this.flagName = flagName.toString(); + return flagName.toString(); } /** diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/types/NumberFlag.java b/Core/src/main/java/com/plotsquared/core/plot/flag/types/NumberFlag.java index 841475609..3364f0552 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/types/NumberFlag.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/types/NumberFlag.java @@ -26,6 +26,7 @@ package com.plotsquared.core.plot.flag.types; import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.plot.flag.FlagParseException; import com.plotsquared.core.plot.flag.PlotFlag; @@ -51,8 +52,9 @@ public abstract class NumberFlag, F extends Plo @Override public F parse(@Nonnull String input) throws FlagParseException { final N parsed = parseNumber(input); if (parsed.compareTo(minimum) < 0 || parsed.compareTo(maximum) > 0) { - throw new FlagParseException(this, input, TranslatableCaption.of("invalid.number_not_in_range"), minimum, - maximum); + throw new FlagParseException(this, input, TranslatableCaption.of("invalid.number_not_in_range"), + Templates.of("min", minimum), + Templates.of("max", maximum)); } return flagOf(parsed); diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index 9916bfa69..fbc0636bc 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -294,11 +294,14 @@ public class EventDispatcher { return true; } } - return Permissions - .hasPermission(player, Captions.PERMISSION_ADMIN_INTERACT_OTHER.toString(), - false) || !(!notifyPerms || MainUtil - .sendMessage(player, Captions.FLAG_TUTORIAL_USAGE, - Captions.FLAG_USE.getTranslated())); + if (Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_INTERACT_OTHER.toString(), false)) { + return true; + } + if (notifyPerms) { + player.sendMessage(TranslatableCaption.of("commandconfig.flag_tutorial_usage"), + Template.of("flag", PlaceFlag.getFlagName(UseFlag.class))); + } + return false; } case TRIGGER_PHYSICAL: { if (plot == null) { @@ -347,9 +350,12 @@ public class EventDispatcher { false)) { return true; } - return !(!notifyPerms || MainUtil.sendMessage(player, Captions.FLAG_TUTORIAL_USAGE, - Captions.FLAG_MOB_PLACE.getTranslated() + '/' + Captions.FLAG_PLACE - .getTranslated())); + if (notifyPerms) { + player.sendMessage(TranslatableCaption.of("commandconfig.flag_tutorial_usage"), + Template.of("flag", PlotFlag.getFlagName(MobPlaceFlag.class) + + '/' + PlotFlag.getFlagName(PlaceFlag.class))); + } + return false; } case PLACE_MISC: { if (plot == null) { @@ -375,9 +381,12 @@ public class EventDispatcher { false)) { return true; } - return !(!notifyPerms || MainUtil.sendMessage(player, Captions.FLAG_TUTORIAL_USAGE, - Captions.FLAG_MISC_PLACE.getTranslated() + '/' + Captions.FLAG_PLACE - .getTranslated())); + if (notifyPerms) { + player.sendMessage(TranslatableCaption.of("commandconfig.flag_tutorial_usage"), + Template.of("flag", PlotFlag.getFlagName(MiscPlaceFlag.class) + + '/' + PlotFlag.getFlagName(PlaceFlag.class))); + } + return false; } case PLACE_VEHICLE: if (plot == null) { diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 023543f38..ea40d1bc8 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -514,11 +514,11 @@ "flag.flag_info_footer": "--------- PlotSquared Flags ---------", "flag.flag_list_categories": ": ", "flag.flag_list_flag": ">Click to view information about the flag.>", - "flag.flag_info_name": "Name: ", + "flag.flag_info_name": "Name: ", "flag.flag_info_category": "Category: ", "flag.flag_info_description": "Description: ", - "flag.flag_info_example": "Example: ", - "flag.flag_info_default_value": "Default Value: ", + "flag.flag_info_example": "Example: >/plot flag set ", + "flag.flag_info_default_value": "Default Value: ", "flags.flag_category_string": "String Flags", "flags.flag_category_integers": "Integer Flags", diff --git a/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java b/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java index 6f7764d8a..bf3024b89 100644 --- a/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java +++ b/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java @@ -27,8 +27,13 @@ package com.plotsquared.core.plot; import com.plotsquared.core.database.AbstractDBTest; import com.plotsquared.core.database.DBFunc; +import com.plotsquared.core.plot.flag.PlotFlag; +import com.plotsquared.core.plot.flag.implementations.UseFlag; import com.sk89q.worldedit.world.item.ItemType; import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; public class FlagTest { @@ -58,4 +63,9 @@ public class FlagTest { // assertEquals(flag.get(), flag2.get()); // } // } + + @Test public void testFlagName() { + String flagName = PlotFlag.getFlagName(UseFlag.class); + assertEquals("use", flagName); + } }