Pull Captions methods into a Caption interface.

This commit is contained in:
Alexander Söderberg 2020-02-13 12:46:31 +01:00
parent 1c4c680e18
commit 97c1d788b0
14 changed files with 130 additions and 78 deletions

View File

@ -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());
}

View File

@ -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) {

View File

@ -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();
}

View File

@ -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<String, String> 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);
}
}
}

View File

@ -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
//<editor-fold desc="Static Flags">
@ -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<String, String> 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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<T, F extends PlotFlag<T, F>> {
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;
}

View File

@ -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<BlockType, BlockTypeListFlag> {
public BlockTypeListFlag(List<BlockType> blockTypeList, Captions description) {
public BlockTypeListFlag(List<BlockType> blockTypeList, Caption description) {
super(blockTypeList, Captions.FLAG_CATEGORY_BLOCK_LIST, description);
}

View File

@ -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<F extends PlotFlag<Boolean, F>> 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);
}

View File

@ -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<Integer, IntegerFlag> {
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);
}

View File

@ -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<V, F extends PlotFlag<List<V>, F>> extends PlotFlag<List<V>, F> {
public ListFlag(final List<V> valueList, final Captions category, final Captions description) {
public ListFlag(final List<V> valueList, final Captions category, final Caption description) {
super(valueList, category, description);
}

View File

@ -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 = ", ";

View File

@ -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 = "";