diff --git a/pandalib-cli/pom.xml b/pandalib-cli/pom.xml
index 7450e08..0c2f5a6 100644
--- a/pandalib-cli/pom.xml
+++ b/pandalib-cli/pom.xml
@@ -49,12 +49,12 @@
bungeecord-config${bungeecord.version}
-
-
- com.mojang
- brigadier
- 1.0.17
-
+
+
+ fr.pandacube.lib
+ pandalib-commands
+ ${project.version}
+
diff --git a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java
index 0739595..1eb692c 100644
--- a/pandalib-cli/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java
+++ b/pandalib-cli/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java
@@ -16,7 +16,7 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import fr.pandacube.lib.chat.ChatStatic;
-import fr.pandacube.lib.core.commands.SuggestionsSupplier;
+import fr.pandacube.lib.commands.SuggestionsSupplier;
import fr.pandacube.lib.util.Log;
import fr.pandacube.lib.reflect.Reflect;
diff --git a/pandalib-commands/pom.xml b/pandalib-commands/pom.xml
new file mode 100644
index 0000000..eac787a
--- /dev/null
+++ b/pandalib-commands/pom.xml
@@ -0,0 +1,43 @@
+
+
+
+ pandalib-parent
+ fr.pandacube.lib
+ 1.0-SNAPSHOT
+ ../pom.xml
+
+ 4.0.0
+
+ pandalib-commands
+ jar
+
+
+
+ minecraft-libraries
+ Minecraft Libraries
+ https://libraries.minecraft.net
+
+
+ bungeecord-repo
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+
+
+
+ fr.pandacube.lib
+ pandalib-util
+ ${project.version}
+
+
+
+ com.mojang
+ brigadier
+ 1.0.18
+
+
+
+
+
\ No newline at end of file
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java b/pandalib-commands/src/main/java/fr/pandacube/lib/commands/SuggestionsSupplier.java
similarity index 60%
rename from pandalib-core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java
rename to pandalib-commands/src/main/java/fr/pandacube/lib/commands/SuggestionsSupplier.java
index e6631c5..22f1ec9 100644
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java
+++ b/pandalib-commands/src/main/java/fr/pandacube/lib/commands/SuggestionsSupplier.java
@@ -1,4 +1,6 @@
-package fr.pandacube.lib.core.commands;
+package fr.pandacube.lib.commands;
+
+import fr.pandacube.lib.util.ListUtil;
import java.util.ArrayList;
import java.util.Arrays;
@@ -11,25 +13,51 @@ import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;
-import fr.pandacube.lib.util.ListUtil;
-import fr.pandacube.lib.util.TimeUtil;
-
+/**
+ * Functionnal interface providing suggestions for an argument of a command.
+ * @param the type of the command sender.
+ */
@FunctionalInterface
public interface SuggestionsSupplier {
+
+
+ /**
+ * Gets the suggestion based on the parameters.
+ * @param sender the sender of the command
+ * @param tokenIndex the index, in {@code args}, of the currently typed token
+ * @param token the token current value.
+ * @param args all the space-separated parameters.
+ * @return the suggestions to show to the user.
+ */
+ List getSuggestions(S sender, int tokenIndex, String token, String[] args);
+
+
+
+
+
+
+
/**
- * Number of suggestion visible at once without having to scroll
+ * Number of suggestion visible at once without having to scroll IG.
*/
int VISIBLE_SUGGESTION_COUNT = 10;
-
-
- List getSuggestions(S sender, int tokenIndex, String token, String[] args);
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+ /**
+ * Provides a filter that only accepts Strings that starts with the provided token, ignoring the case.
+ * @param token the token to filter with.
+ * @return a filter for the provided token.
+ */
static Predicate filter(String token) {
return suggestion -> suggestion != null && suggestion.toLowerCase().startsWith(token.toLowerCase());
}
@@ -37,49 +65,122 @@ public interface SuggestionsSupplier {
/**
* Filter the provided {@link Stream} of string according to the provided token, using the filter returned by {@link #filter(String)},
* then returns the strings collected into a {@link List}.
- *
+ *
* This methods consume the provided stream, so will not be usable anymore.
+ * @param stream the stream to filter and collet.
+ * @param token the token to consider for filtering.
+ * @return the stream, filtered and collected into a {@link List}.
*/
static List collectFilteredStream(Stream stream, String token) {
return stream.filter(filter(token)).sorted().collect(Collectors.toList());
}
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest nothing.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier empty() { return (s, ti, t, a) -> Collections.emptyList(); }
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the supplied {@link Collection}.
+ * The provided {@link Supplier} will be called on each request for suggestions.
+ * @param streamSupplier a {@link Supplier} that provide an instance of {@link Collection} to iterate through for
+ * suggestions.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier fromCollectionsSupplier(Supplier> streamSupplier) {
return (s, ti, token, a) -> collectFilteredStream(streamSupplier.get().stream(), token);
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the supplied {@link Stream}.
+ * The provided {@link Supplier} will be called on each request for suggestions.
+ * @param streamSupplier a {@link Supplier} that provide an instance of {@link Stream} to iterate through for
+ * suggestions.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier fromStreamSupplier(Supplier> streamSupplier) {
return (s, ti, token, a) -> collectFilteredStream(streamSupplier.get(), token);
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the provided {@link Collection}.
+ * If the provided collection is modified, the changes will not be reflected on the suggestions.
+ * @param suggestions the list of suggestion.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier fromCollection(Collection suggestions) {
return fromStreamSupplier(suggestions::stream);
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the provided strings.
+ * If the provided array is modified, the changes will not be reflected on the suggestions.
+ * @param suggestions the suggestions.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier fromArray(String... suggestions) {
return fromStreamSupplier(() -> Arrays.stream(suggestions));
}
-
-
+
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the provided enum.
+ * @param enumClass the class of the enum from which to suggest its values.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ * @param the enum type.
+ */
static , S> SuggestionsSupplier fromEnum(Class enumClass) {
return fromEnumValues(enumClass.getEnumConstants());
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the values from the provided enum.
+ * @param enumClass the class of the enum from which to suggest its values.
+ * @param lowerCase true to lowercase the enum values names, false to keep the case.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ * @param the enum type.
+ */
static , S> SuggestionsSupplier fromEnum(Class enumClass, boolean lowerCase) {
return fromEnumValues(lowerCase, enumClass.getEnumConstants());
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the provided enum values.
+ * @param enumValues the values from an enum.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ * @param the enum type.
+ */
@SafeVarargs
static , S> SuggestionsSupplier fromEnumValues(E... enumValues) {
return fromEnumValues(false, enumValues);
}
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest the provided enum values.
+ * @param lowerCase true to lowercase the enum values names, false to keep the case.
+ * @param enumValues the values from an enum.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ * @param the enum type.
+ */
@SafeVarargs
static , S> SuggestionsSupplier fromEnumValues(boolean lowerCase, E... enumValues) {
return (s, ti, token, a) -> {
@@ -89,21 +190,31 @@ public interface SuggestionsSupplier {
return collectFilteredStream(st, token);
};
}
-
-
-
+
+
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggest {@code "true"} or {@code "false"}.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier booleanValues() {
return fromCollection(Arrays.asList("true", "false"));
}
-
-
-
-
+
+
+
+
/**
- * Create a {@link SuggestionsSupplier} that suggest numbers according to the provided range.
- *
- * The current implementation only support range that include either -1 or 1.
+ * Creates a {@link SuggestionsSupplier} that suggest numbers according to the provided int range.
+ * The current implementation only support ranges that include either -1 or 1.
+ * @param min the minimum value of the interval, included.
+ * @param max the maximum value of the interval, included.
+ * @param compact if the suggestion must be compact. Disabling this feature may have big performance impact
+ * on large intervals.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
*/
static SuggestionsSupplier fromIntRange(int min, int max, boolean compact) {
return fromLongRange(min, max, compact);
@@ -113,9 +224,14 @@ public interface SuggestionsSupplier {
/**
- * Create a {@link SuggestionsSupplier} that suggest numbers according to the provided range.
- *
- * The current implementation only support range that include either -1 or 1.
+ * Creates a {@link SuggestionsSupplier} that suggest numbers according to the provided long range.
+ * The current implementation only support ranges that include either -1 or 1.
+ * @param min the minimum value of the interval, included.
+ * @param max the maximum value of the interval, included.
+ * @param compact if the suggestion must be compact. Disabling this feature may have big performance impact
+ * on large intervals.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
*/
static SuggestionsSupplier fromLongRange(long min, long max, boolean compact) {
if (max < min) {
@@ -186,9 +302,11 @@ public interface SuggestionsSupplier {
}
-
-
-
+ /**
+ * Creates a {@link SuggestionsSupplier} that suggests duration strings, like "3d" for 3 days, or "1h" for 1 hour.
+ * @return a new {@link SuggestionsSupplier}.
+ * @param the type of the command sender.
+ */
static SuggestionsSupplier suggestDuration() {
final List emptyTokenSuggestions = DURATION_SUFFIXES.stream().map(p -> "1" + p).collect(Collectors.toList());
return (s, ti, token, args) -> {
@@ -232,13 +350,15 @@ public interface SuggestionsSupplier {
-
-
-
-
+
+
+
+
/**
- * Create a {@link SuggestionsSupplier} that support greedy strings argument using the suggestion from this {@link SuggestionsSupplier}.
- * @param index the index of the first argument of the greedy string argument
+ * Creates a {@link SuggestionsSupplier} that provides the suggestions of this instance, but with support for
+ * greedy string (arguments that goes until the end of the command).
+ * @param index the index of the first argument of the greedy string argument.
+ * @return a {@link SuggestionsSupplier} that supports quotable strings.
*/
default SuggestionsSupplier greedyString(int index) {
@@ -246,8 +366,8 @@ public interface SuggestionsSupplier {
if (ti < index)
return Collections.emptyList();
-
- String gToken = AbstractCommand.getLastParams(args, index);
+
+ String gToken = String.join(" ", Arrays.copyOfRange(args, index, args.length));
String[] splitGToken = gToken.split(" ", -1);
int currentTokenPosition = splitGToken.length - 1;
String[] prevWordsGToken = Arrays.copyOf(splitGToken, currentTokenPosition);
@@ -270,11 +390,13 @@ public interface SuggestionsSupplier {
return currentTokenProposal;
};
}
-
-
-
-
+
+ /**
+ * Creates a {@link SuggestionsSupplier} that provides the suggestions of this instance, but with support for
+ * quotable strings, as used by the Brigadier argument type StringArgumentType.string().
+ * @return a {@link SuggestionsSupplier} that supports quotable strings.
+ */
default SuggestionsSupplier quotableString() {
return (s, ti, token, a) -> {
boolean startWithQuote = token.length() > 0 && (token.charAt(0) == '"' || token.charAt(0) == '\'');
@@ -298,7 +420,7 @@ public interface SuggestionsSupplier {
}
// inspired from com.mojang.brigadier.StringReader#readQuotedString()
- static String unescapeBrigadierQuotable(String input, char quote) {
+ private static String unescapeBrigadierQuotable(String input, char quote) {
StringBuilder builder = new StringBuilder(input.length());
boolean escaped = false;
for (char c : input.toCharArray()) {
@@ -321,18 +443,18 @@ public interface SuggestionsSupplier {
}
// from com.mojang.brigadier.StringReader#isAllowedInUnquotedString(char)
- static boolean isAllowedInBrigadierUnquotedString(char c) {
+ private static boolean isAllowedInBrigadierUnquotedString(char c) {
return c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'
|| c == '_' || c == '-' || c == '.' || c == '+';
}
- static boolean isAllowedInBrigadierUnquotedString(String s) {
+ private static boolean isAllowedInBrigadierUnquotedString(String s) {
for (char c : s.toCharArray())
if (!isAllowedInBrigadierUnquotedString(c))
return false;
return true;
}
- static String escapeBrigadierQuotable(final String input) {
+ private static String escapeBrigadierQuotable(final String input) {
final StringBuilder result = new StringBuilder("\"");
for (int i = 0; i < input.length(); i++) {
@@ -346,11 +468,17 @@ public interface SuggestionsSupplier {
result.append("\"");
return result.toString();
}
-
-
-
-
-
+
+
+
+
+
+ /**
+ * Creates a new {@link SuggestionsSupplier} that provides the suggestions of this instance, if and only if the
+ * provided predicate returns true for the command sender.
+ * @param check the predicate to test.
+ * @return a new {@link SuggestionsSupplier}.
+ */
default SuggestionsSupplier requires(Predicate check) {
return (s, ti, to, a) -> check.test(s) ? getSuggestions(s, ti, to, a) : Collections.emptyList();
}
@@ -358,8 +486,10 @@ public interface SuggestionsSupplier {
/**
- * Returns a new {@link SuggestionsSupplier} containing all the element of this instance then the element of the provided one,
+ * Creates a new {@link SuggestionsSupplier} containing all the element of this instance then the element of the provided one,
* with all duplicated values removed using {@link Stream#distinct()}.
+ * @param other another {@link SuggestionsSupplier} to merge with.
+ * @return a new {@link SuggestionsSupplier}.
*/
default SuggestionsSupplier merge(SuggestionsSupplier other) {
return (s, ti, to, a) -> {
@@ -373,8 +503,10 @@ public interface SuggestionsSupplier {
/**
- * Returns a new {@link SuggestionsSupplier} containing all the suggestions of this instance,
+ * Creates a new {@link SuggestionsSupplier} containing all the suggestions of this instance,
* but if this list is still empty, returns the suggestions from the provided one.
+ * @param other another {@link SuggestionsSupplier} to fallback to.
+ * @return a new {@link SuggestionsSupplier}.
*/
default SuggestionsSupplier orIfEmpty(SuggestionsSupplier other) {
return (s, ti, to, a) -> {
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java
deleted file mode 100644
index a1e4729..0000000
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package fr.pandacube.lib.core.commands;
-
-import java.util.Arrays;
-
-import fr.pandacube.lib.chat.ChatStatic;
-
-public class AbstractCommand extends ChatStatic {
-
- public final String commandName;
-
- public AbstractCommand(String cmdName) {
- commandName = cmdName;
- }
-
-
- /**
- *
- * Concatène les chaines de caractères passés dans args (avec
- * " " comme séparateur), en ommettant
- * celles qui se trouvent avant index.
- * Par exemple :
- *
- * retournera la chaine "bouya chaka bukkit"
- *
- * @param args liste des arguments d'une commandes.
- * Le premier élément est l'argument qui suit le nom de la commande.
- * Usuellement, ce paramètre correspond au paramètre
- * args de la méthode onCommand
- */
- public static String getLastParams(String[] args, int index) {
- return String.join(" ", Arrays.copyOfRange(args, index, args.length));
- }
-
-
-
-}
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java
deleted file mode 100644
index 5ac04e1..0000000
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package fr.pandacube.lib.core.commands;
-
-import java.util.logging.Logger;
-
-/**
- * Throw an instance of this exception to indicate to the plugin command handler
- * that the user has missused the command. The message, if provided, must indicate
- * the reason of the mussusage of the command. It will be displayed on the screen
- * with eventually indication of how to use the command (help command for example).
- * If a {@link Throwable} cause is provided, it will be relayed to the plugin {@link Logger}.
- *
- */
-public class BadCommandUsage extends RuntimeException {
-
- public BadCommandUsage() {
- super();
- }
-
- public BadCommandUsage(Throwable cause) {
- super(cause);
- }
-
- public BadCommandUsage(String message) {
- super(message);
- }
-
- public BadCommandUsage(String message, Throwable cause) {
- super(message, cause);
- }
-}
\ No newline at end of file
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java
index 9d7dabd..2d7ab4b 100644
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java
+++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java
@@ -12,24 +12,23 @@ import fr.pandacube.lib.chat.ChatColorUtil;
import fr.pandacube.lib.util.Log;
/**
- * Class tht loads a specific config file or directory
- *
+ * Class that loads a specific config file or directory.
*/
public abstract class AbstractConfig {
/**
- * Correspond au dossier ou au fichier de configuration traité par la sous-classe
- * courante de {@link AbstractConfig}
+ * The {@link File} corresponging to this config file or directory.
*/
protected final File configFile;
/**
+ * Creates a new {@link AbstractConfig}.
* @param configDir the parent directory
* @param fileOrDirName The name of the config file or folder
* @param type if the provided name is a file or a directory
- * @throws IOException if we cannot create the file
+ * @throws IOException if we cannot create the file or directory.
*/
- public AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException {
+ protected AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException {
configFile = new File(configDir, fileOrDirName);
if (type == FileType.DIR)
configFile.mkdir();
@@ -38,14 +37,15 @@ public abstract class AbstractConfig {
}
/**
- * Gets the lines from the config file
- * @param ignoreEmpty true if we ignore the empty lines
- * @param ignoreHashtagComment true if we ignore the comment lines (starting with {@code #})
- * @param trimOutput true if we want to trim all lines using {@link String#trim()}
- * @param f the file to read
- * @return the list of lines, filtered according to the parameters
+ * Gets the lines from the provided file.
+ * @param ignoreEmpty {@code true} if we ignore the empty lines.
+ * @param ignoreHashtagComment {@code true} if we ignore the comment lines (starting with {@code #}).
+ * @param trimOutput {@code true} if we want to trim all lines using {@link String#trim()}.
+ * @param f the file to read.
+ * @return the list of lines, filtered according to the parameters, or null if it’s not a regular file.
+ * @throws IOException if an IO error occurs.
*/
- protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException {
+ protected static List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException {
if (!f.isFile())
return null;
@@ -74,26 +74,27 @@ public abstract class AbstractConfig {
return lines;
}
-
-
+
+
/**
- * Retourne toutes les lignes du fichier de configuration
- * @param ignoreEmpty true si on doit ignorer les lignes vides
- * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #)
- * @param trimOutput true si on doit appeller la méthode String.trim() sur chaque ligne retournée
- * @return la liste des lignes utiles
+ * Gets the lines from the config file.
+ * @param ignoreEmpty {@code true} if we ignore the empty lines.
+ * @param ignoreHashtagComment {@code true} if we ignore the comment lines (starting with {@code #}).
+ * @param trimOutput {@code true} if we want to trim all lines using {@link String#trim()}.
+ * @return the list of lines, filtered according to the parameters, or null if it’s not a regular file.
+ * @throws IOException if an IO error occurs.
*/
protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException {
return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile);
}
-
-
-
+
+
+ /**
+ * Gets the list of files in the config directory.
+ * @return the list of files in the config directory, or null if this config is not a directory.
+ */
protected List getFileList() {
- if (!configFile.isDirectory())
- return null;
-
- return Arrays.asList(configFile.listFiles());
+ return configFile.isDirectory() ? Arrays.asList(configFile.listFiles()) : null;
}
@@ -101,43 +102,45 @@ public abstract class AbstractConfig {
/**
- * Découpe une chaine de caractère contenant une série de noeuds
- * de permissions séparés par des point-virgules et la retourne sous forme d'une liste.
- * @param perms la chaine de permissions à traiter
- * @return null si le paramètre est nulle ou si perms.equals("*"), ou alors la chaine splittée.
+ * Splits the provided string into a list of permission nodes.
+ * The permission nodes must be separated by {@code ";"}.
+ * @param perms one or more permissions nodes, separated by {@code ";"}.
+ * @return {@code null} if the parameter is null or is equal to {@code "*"}, or the string splitted using {@code ";"}.
*/
public static List splitPermissionsString(String perms) {
if (perms == null || perms.equals("*"))
return null;
- return getSplittedString(perms, ";");
+ return List.of(perms.split(";"));
}
-
-
-
-
-
- public static List getSplittedString(String value, String split) {
- return List.of(value.split(split));
- }
-
+ /**
+ * Utility method to that translate the {@code '&'} formated string to the legacy format.
+ * @param string the string to convert.
+ * @return a legacy formated string (using {@code '§'}).
+ */
public static String getTranslatedColorCode(String string) {
return ChatColorUtil.translateAlternateColorCodes('&', string);
}
-
-
-
-
+
+
+ /**
+ * Logs the message as a warning into console, prefixed with the context of this config.
+ * @param message the message to log.
+ */
protected void warning(String message) {
- Log.warning("Erreur dans la configuration de '"+configFile.getName()+"' : "+message);
+ Log.warning("Error in configuration '"+configFile.getName()+"': " + message);
}
-
-
-
-
+
+
+ /**
+ * The type of config.
+ */
protected enum FileType {
- FILE, DIR
+ /** A config file. */
+ FILE,
+ /** A config directory. */
+ DIR
}
}
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java
index b379b66..35ece54 100644
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java
+++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java
@@ -3,10 +3,23 @@ package fr.pandacube.lib.core.config;
import java.io.File;
import java.io.IOException;
+/**
+ * An abstract manager for a set of configuration files and folders.
+ * Its uses is to manage the loading/reloading of the configuration of a plugin.
+ */
public abstract class AbstractConfigManager {
-
+
+ /**
+ * The global configuration directory.
+ * May be the one provided by the environmenet API (like Plugin.getPluginFolder() in Bukkit).
+ */
protected final File configDir;
-
+
+ /**
+ * Create a new instance of config manager.
+ * @param configD the config directory.
+ * @throws IOException if an IO error occurs.
+ */
public AbstractConfigManager(File configD) throws IOException {
configDir = configD;
@@ -16,18 +29,24 @@ public abstract class AbstractConfigManager {
}
/**
- * Implementation must close all closeable configuration (saving for example)
+ * Closes the configuration. May handle saving of any non-saved data.
+ * @throws IOException if an IO error occurs.
*/
public abstract void close() throws IOException;
/**
- * Implementation must init all config data
+ * Loads (or reloads) the configuration data.
+ * @throws IOException if an IO error occurs.
*/
public abstract void init() throws IOException;
-
-
-
-
+
+
+ /**
+ * Utility method to close then reload the config.
+ * @throws IOException if an IO error occurs.
+ * @see #close()
+ * @see #init()
+ */
public synchronized void reloadConfig() throws IOException {
close();
init();
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/util/Json.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java
similarity index 82%
rename from pandalib-core/src/main/java/fr/pandacube/lib/core/util/Json.java
rename to pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java
index d3237d1..ae18175 100644
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/util/Json.java
+++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java
@@ -1,4 +1,4 @@
-package fr.pandacube.lib.core.util;
+package fr.pandacube.lib.core.json;
import java.io.IOException;
import java.lang.reflect.Constructor;
@@ -17,13 +17,40 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
+/**
+ * Provides pre-instanciated {@link Gson} instances, all with support for Java records.
+ */
public class Json {
+
+ /**
+ * {@link Gson} instance with {@link GsonBuilder#setLenient()} and support for Java records.
+ */
public static final Gson gson = build(Function.identity());
+
+ /**
+ * {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#setPrettyPrinting()}
+ * and support for Java records.
+ */
public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting);
+
+ /**
+ * {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()}
+ * and support for Java records.
+ */
public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls);
+
+ /**
+ * {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()},
+ * {@link GsonBuilder#setPrettyPrinting()} and support for Java records.
+ */
public static final Gson gsonSerializeNullsPrettyPrinting = build(b -> b.serializeNulls().setPrettyPrinting());
+
+
+
+
+
private static Gson build(Function builderModifier) {
return builderModifier
.apply(new GsonBuilder().registerTypeAdapterFactory(new RecordAdapterFactory()).setLenient()).create();
@@ -32,6 +59,10 @@ public class Json {
+
+
+
+
// from https://github.com/google/gson/issues/1794#issuecomment-812964421
private static class RecordAdapterFactory implements TypeAdapterFactory {
@Override
diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/TypeConverter.java
similarity index 71%
rename from pandalib-core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java
rename to pandalib-core/src/main/java/fr/pandacube/lib/core/json/TypeConverter.java
index 25237dd..765b61c 100644
--- a/pandalib-core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java
+++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/TypeConverter.java
@@ -1,4 +1,4 @@
-package fr.pandacube.lib.core.util;
+package fr.pandacube.lib.core.json;
import java.util.ArrayList;
import java.util.HashMap;
@@ -8,13 +8,16 @@ import java.util.Map;
import com.google.gson.JsonElement;
/**
- * Utility for conversion of basic Java types and JsonElements types
- * @author Marc
- *
+ * Provides conversion between Java types and {@link JsonElement} types.
*/
public class TypeConverter {
-
-
+
+ /**
+ * Converts the provided object to an {@link Integer}.
+ * @param o the object to convert.
+ * @return a the object converted to an {@link Integer}.
+ * @throws ConvertionException is a conversion error occurs.
+ */
public static Integer toInteger(Object o) {
if (o == null) {
return null;
@@ -44,14 +47,26 @@ public class TypeConverter {
throw new ConvertionException("No integer convertion available for an instance of "+o.getClass());
}
-
+
+ /**
+ * Converts the provided object to a primitive int.
+ * @param o the object to convert.
+ * @return a the object converted to a primitive int.
+ * @throws ConvertionException is a conversion error occurs.
+ */
public static int toPrimInt(Object o) {
Integer val = toInteger(o);
if (val == null)
throw new ConvertionException("null values can't be converted to primitive int");
return val;
}
-
+
+ /**
+ * Converts the provided object to a {@link Double}.
+ * @param o the object to convert.
+ * @return a the object converted to a {@link Double}.
+ * @throws ConvertionException is a conversion error occurs.
+ */
public static Double toDouble(Object o) {
if (o == null) {
return null;
@@ -82,14 +97,26 @@ public class TypeConverter {
throw new ConvertionException("No double convertion available for an instance of "+o.getClass());
}
-
+
+ /**
+ * Converts the provided object to a primitive double.
+ * @param o the object to convert.
+ * @return a the object converted to a primitive double.
+ * @throws ConvertionException is a conversion error occurs.
+ */
public static double toPrimDouble(Object o) {
Double val = toDouble(o);
if (val == null)
throw new ConvertionException("null values can't converted to primitive int");
return val;
}
-
+
+ /**
+ * Converts the provided object to a {@link String}.
+ * @param o the object to convert.
+ * @return a the object converted to a {@link String}.
+ * @throws ConvertionException is a conversion error occurs.
+ */
public static String toString(Object o) {
if (o == null) {
return null;
@@ -112,11 +139,13 @@ public class TypeConverter {
}
/**
- *
- * @param o the object to convert to good type
+ * Converts the provided object to a {@link Map}.
+ * @param o the object to convert.
* @param mapIntKeys if the String key representing an int should be duplicated as integer type,
* which map to the same value as the original String key. For example, if a key is "12" and map
- * to the object o, an integer key 12 will be added and map to the same object o
+ * to the object o, an integer key 12 will be added and map to the same object o.
+ * @return a the object converted to a {@link Map}.
+ * @throws ConvertionException is a conversion error occurs.
*/
@SuppressWarnings("unchecked")
public static Map