From 97c1d788b0f52a11d944ca969e430e00cc507469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 13 Feb 2020 12:46:31 +0100 Subject: [PATCH] Pull Captions methods into a Caption interface. --- .../plotsquared/api/PlotAPI.java | 3 +- .../plotsquared/plot/PlotSquared.java | 4 +- .../plotsquared/plot/config/Caption.java | 30 ++++++++++ .../plot/config/CaptionUtility.java | 39 +++++++++++++ .../plotsquared/plot/config/Captions.java | 56 +------------------ .../plot/config/StaticCaption.java | 22 ++++++++ .../plot/flags/FlagParseException.java | 12 ++-- .../plotsquared/plot/flags/PlotFlag.java | 14 ++--- .../plot/flags/types/BlockTypeListFlag.java | 3 +- .../plot/flags/types/BooleanFlag.java | 3 +- .../plot/flags/types/IntegerFlag.java | 5 +- .../plot/flags/types/ListFlag.java | 3 +- .../plotsquared/plot/util/MainUtil.java | 10 ++-- .../plotsquared/plot/util/StringMan.java | 4 ++ 14 files changed, 130 insertions(+), 78 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/CaptionUtility.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/StaticCaption.java diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java index 610362953..c218513c9 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java @@ -2,6 +2,7 @@ package com.github.intellectualsites.plotsquared.api; import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.Flags; @@ -164,7 +165,7 @@ import java.util.UUID; * @see #sendConsoleMessage(String) * @see Captions */ - public void sendConsoleMessage(Captions caption) { + public void sendConsoleMessage(Caption caption) { sendConsoleMessage(caption.getTranslated()); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index 2512abe83..e7334d0b2 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -5,6 +5,7 @@ import com.github.intellectualsites.plotsquared.configuration.MemorySection; import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration; import com.github.intellectualsites.plotsquared.configuration.serialization.ConfigurationSerialization; import com.github.intellectualsites.plotsquared.plot.commands.WE_Anywhere; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Configuration; import com.github.intellectualsites.plotsquared.plot.config.Settings; @@ -345,7 +346,8 @@ import java.util.zip.ZipInputStream; * @see IPlotMain#log(String) */ public static void log(Object message) { - if (message == null || message.toString().isEmpty()) { + if (message == null || (message instanceof Caption ? ((Caption) message).getTranslated().isEmpty() : + message.toString().isEmpty())) { return; } if (PlotSquared.get() == null || PlotSquared.get().getLogger() == null) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java new file mode 100644 index 000000000..b25cfe2e6 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Caption.java @@ -0,0 +1,30 @@ +package com.github.intellectualsites.plotsquared.plot.config; + +import com.github.intellectualsites.plotsquared.commands.CommandCaller; +import com.github.intellectualsites.plotsquared.plot.PlotSquared; +import com.github.intellectualsites.plotsquared.plot.util.StringMan; + +public interface Caption { + + String getTranslated(); + + default String formatted() { + return StringMan.replaceFromMap(getTranslated(), Captions.replacements); + } + + default void send(CommandCaller caller, String... args) { + send(caller, (Object[]) args); + } + + default void send(CommandCaller caller, Object... args) { + String msg = CaptionUtility.format(this, args); + if (caller == null) { + PlotSquared.log(msg); + } else { + caller.sendMessage(msg); + } + } + + boolean usePrefix(); + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/CaptionUtility.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/CaptionUtility.java new file mode 100644 index 000000000..1a14c95c3 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/CaptionUtility.java @@ -0,0 +1,39 @@ +package com.github.intellectualsites.plotsquared.plot.config; + +import com.github.intellectualsites.plotsquared.plot.util.StringMan; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class CaptionUtility { + + public static String format(String message, Object... args) { + if (args.length == 0) { + return message; + } + Map map = new LinkedHashMap<>(); + for (int i = args.length - 1; i >= 0; i--) { + String arg = "" + args[i]; + if (arg.isEmpty()) { + map.put("%s" + i, ""); + } else { + arg = Captions.color(arg); + map.put("%s" + i, arg); + } + if (i == 0) { + map.put("%s", arg); + } + } + message = StringMan.replaceFromMap(message, map); + return message; + } + + public static String format(Caption caption, Object... args) { + if (caption.usePrefix() && caption.getTranslated().length() > 0) { + return Captions.PREFIX.getTranslated() + format(caption.getTranslated(), args); + } else { + return format(caption.getTranslated(), args); + } + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java index 808fd85e5..14a6b720b 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java @@ -1,6 +1,5 @@ package com.github.intellectualsites.plotsquared.plot.config; -import com.github.intellectualsites.plotsquared.commands.CommandCaller; import com.github.intellectualsites.plotsquared.configuration.ConfigurationSection; import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; @@ -11,14 +10,12 @@ import java.io.IOException; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Set; /** * Captions class. */ -public enum Captions { +public enum Captions implements Caption { //@formatter:off // @@ -659,35 +656,6 @@ public enum Captions { this(defaultString, true, category.toLowerCase()); } - public static String format(String message, Object... args) { - if (args.length == 0) { - return message; - } - Map map = new LinkedHashMap<>(); - for (int i = args.length - 1; i >= 0; i--) { - String arg = "" + args[i]; - if (arg.isEmpty()) { - map.put("%s" + i, ""); - } else { - arg = Captions.color(arg); - map.put("%s" + i, arg); - } - if (i == 0) { - map.put("%s", arg); - } - } - message = StringMan.replaceFromMap(message, map); - return message; - } - - public static String format(Captions caption, Object... args) { - if (caption.usePrefix() && caption.translatedString.length() > 0) { - return Captions.PREFIX.getTranslated() + format(caption.translatedString, args); - } else { - return format(caption.translatedString, args); - } - } - public static String color(String string) { return StringMan.replaceFromMap(string, replacements); } @@ -778,11 +746,7 @@ public enum Captions { } } - @Override public String toString() { - return this.translatedString; - } - - public String getTranslated() { + @Override public String getTranslated() { return this.translatedString; } @@ -790,24 +754,8 @@ public enum Captions { return this.prefix; } - public String formatted() { - return StringMan.replaceFromMap(getTranslated(), replacements); - } - public String getCategory() { return this.category; } - public void send(CommandCaller caller, String... args) { - send(caller, (Object[]) args); - } - - public void send(CommandCaller caller, Object... args) { - String msg = format(this, args); - if (caller == null) { - PlotSquared.log(msg); - } else { - caller.sendMessage(msg); - } - } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/StaticCaption.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/StaticCaption.java new file mode 100644 index 000000000..fec3589df --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/StaticCaption.java @@ -0,0 +1,22 @@ +package com.github.intellectualsites.plotsquared.plot.config; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor public final class StaticCaption implements Caption { + + private final String value; + private final boolean usePrefix; + + public StaticCaption(final String value) { + this(value, true); + } + + @Override public String getTranslated() { + return this.value; + } + + @Override public boolean usePrefix() { + return this.usePrefix; + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagParseException.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagParseException.java index e49ac92dd..29ea1d334 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagParseException.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagParseException.java @@ -1,14 +1,14 @@ package com.github.intellectualsites.plotsquared.plot.flags; -import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.config.Caption; public class FlagParseException extends Exception { - private final PlotFlag flag; + private final PlotFlag flag; private final String value; - private final Captions errorMessage; + private final Caption errorMessage; - public FlagParseException(final PlotFlag flag, final String value, final Captions errorMessage) { + public FlagParseException(final PlotFlag flag, final String value, final Caption errorMessage) { super(String.format("Failed to parse flag of type '%s'. Value '%s' was not accepted.", flag.getName(), value)); this.flag = flag; @@ -30,11 +30,11 @@ public class FlagParseException extends Exception { * * @return Flag that threw the exception */ - public PlotFlag getFlag() { + public PlotFlag getFlag() { return this.flag; } - public Captions getErrorMessage() { + public Caption getErrorMessage() { return errorMessage; } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/PlotFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/PlotFlag.java index c9b1316ed..53b8a4ff8 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/PlotFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/PlotFlag.java @@ -1,6 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flags; -import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.google.common.base.Preconditions; import lombok.EqualsAndHashCode; import org.jetbrains.annotations.NotNull; @@ -17,16 +17,16 @@ import java.util.Locale; @EqualsAndHashCode(of = "value") public abstract class PlotFlag> { private final T value; - private final Captions flagCategory; - private final Captions flagDescription; + private final Caption flagCategory; + private final Caption flagDescription; /** * Construct a new flag instance. * * @param value Flag value */ - protected PlotFlag(@NotNull final T value, @NotNull final Captions flagCategory, - @NotNull final Captions flagDescription) { + protected PlotFlag(@NotNull final T value, @NotNull final Caption flagCategory, + @NotNull final Caption flagDescription) { this.value = Preconditions.checkNotNull(value, "flag value may not be null"); this.flagCategory = Preconditions.checkNotNull(flagCategory, "flag category may not be null"); this.flagDescription = Preconditions.checkNotNull(flagDescription, "flag description may not be null"); @@ -74,11 +74,11 @@ import java.util.Locale; return this.getClass().getSimpleName().toLowerCase(Locale.ENGLISH); } - public Captions getFlagDescription() { + public Caption getFlagDescription() { return this.flagDescription; } - public Captions getFlagCategory() { + public Caption getFlagCategory() { return this.flagCategory; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BlockTypeListFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BlockTypeListFlag.java index 907324d42..efb24edf7 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BlockTypeListFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BlockTypeListFlag.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flags.types; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil; @@ -12,7 +13,7 @@ import java.util.List; public class BlockTypeListFlag extends ListFlag { - public BlockTypeListFlag(List blockTypeList, Captions description) { + public BlockTypeListFlag(List blockTypeList, Caption description) { super(blockTypeList, Captions.FLAG_CATEGORY_BLOCK_LIST, description); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BooleanFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BooleanFlag.java index 5e6d75b21..ab7791372 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BooleanFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/BooleanFlag.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flags.types; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; @@ -20,7 +21,7 @@ public abstract class BooleanFlag> extends PlotFl * @param value Flag value * @param description Flag description */ - protected BooleanFlag(final boolean value, final Captions description) { + protected BooleanFlag(final boolean value, final Caption description) { super(value, Captions.FLAG_CATEGORY_BOOLEAN, description); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java index ed15a8923..e27b4f18f 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flags.types; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; @@ -7,11 +8,11 @@ import org.jetbrains.annotations.NotNull; public class IntegerFlag extends PlotFlag { - protected IntegerFlag(final int value, @NotNull Captions flagDescription) { + protected IntegerFlag(final int value, @NotNull Caption flagDescription) { super(value, Captions.FLAG_CATEGORY_INTEGERS, flagDescription); } - protected IntegerFlag(@NotNull Captions flagDescription) { + protected IntegerFlag(@NotNull Caption flagDescription) { this(0, flagDescription); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/ListFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/ListFlag.java index 1d0aabf59..2efb4b864 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/ListFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/ListFlag.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.flags.types; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import com.github.intellectualsites.plotsquared.plot.util.StringMan; @@ -10,7 +11,7 @@ import java.util.List; public abstract class ListFlag, F>> extends PlotFlag, F> { - public ListFlag(final List valueList, final Captions category, final Captions description) { + public ListFlag(final List valueList, final Captions category, final Caption description) { super(valueList, category, description); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java index 15abc1bd6..460124ef7 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java @@ -3,6 +3,8 @@ package com.github.intellectualsites.plotsquared.plot.util; import com.github.intellectualsites.plotsquared.commands.CommandCaller; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.commands.Like; +import com.github.intellectualsites.plotsquared.plot.config.Caption; +import com.github.intellectualsites.plotsquared.plot.config.CaptionUtility; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.database.DBFunc; @@ -640,7 +642,7 @@ public class MainUtil { * @param caption the message to send * @return boolean success */ - public static boolean sendMessage(CommandCaller player, Captions caption, String... args) { + public static boolean sendMessage(CommandCaller player, Caption caption, String... args) { return sendMessage(player, caption, (Object[]) args); } @@ -651,13 +653,13 @@ public class MainUtil { * @param caption the message to send * @return boolean success */ - public static boolean sendMessage(final CommandCaller player, final Captions caption, + public static boolean sendMessage(final CommandCaller player, final Caption caption, final Object... args) { if (caption.getTranslated().isEmpty()) { return true; } TaskManager.runTaskAsync(() -> { - String m = Captions.format(caption, args); + String m = CaptionUtility.format(caption, args); if (player == null) { PlotSquared.log(m); } else { @@ -789,7 +791,7 @@ public class MainUtil { value = df.format(value); } flags.append(prefix) - .append(Captions + .append(CaptionUtility .format(Captions.PLOT_FLAG_LIST.getTranslated(), entry.getKey().getName(), value)); prefix = ", "; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/StringMan.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/StringMan.java index 047b4a71f..d32bd0cda 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/StringMan.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/StringMan.java @@ -1,5 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.util; +import com.github.intellectualsites.plotsquared.plot.config.Caption; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Array; @@ -51,6 +52,9 @@ public class StringMan { if (obj instanceof String) { return (String) obj; } + if (obj instanceof Caption) { + return ((Caption) obj).getTranslated(); + } if (obj.getClass().isArray()) { StringBuilder result = new StringBuilder(); String prefix = "";