From 87a03269327eba340d7cc723234db9138c1026c0 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 26 Aug 2016 02:05:13 +0200 Subject: [PATCH] Ajout d'une classe abstraite pour les commandes Bungee et Spigot --- .../java/util/commands/AbstractCommand.java | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/fr/pandacube/java/util/commands/AbstractCommand.java diff --git a/src/fr/pandacube/java/util/commands/AbstractCommand.java b/src/fr/pandacube/java/util/commands/AbstractCommand.java new file mode 100644 index 0000000..1db6eaa --- /dev/null +++ b/src/fr/pandacube/java/util/commands/AbstractCommand.java @@ -0,0 +1,141 @@ +package fr.pandacube.java.util.commands; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +public class AbstractCommand { + + 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 : + *

+ * + * getLastParams(new String[] {"test", "bouya", "chaka", "bukkit"}, 1); + * + *

+ * 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 + * @param index + * @return + */ + public static String getLastParams(String[] args, int index) { + if (index < 0 || index >= args.length) return null; + return String.join(" ", Arrays.copyOfRange(args, index, args.length)); + } + + /** + * Prends en charge les tokens avec des espaces, mais retourne les + * propositions complètes + * + * @param token + * @param allProposal + * @return + */ + public static List getTabProposalFromToken(String token, Collection allProposal) { + List ret = new ArrayList(); + + for (String s : allProposal) + if (s.toLowerCase().startsWith(token.toLowerCase())) ret.add(s); + + if (ret.isEmpty()) ret.addAll(allProposal); + + ret.sort(null); // String implents Comparable + + return ret; + } + + + + public static final TabProposal TAB_NO_PROPOSAL = () -> Collections.emptyList(); + + public static TabProposal TAB_PROPOSAL(Collection proposals) { + return () -> proposals; + } + + public static TabProposal TAB_INTEGERS(int startIncluded, int endIncluded) { + List proposals = new ArrayList<>(endIncluded - startIncluded + 1); + for (int i = startIncluded; i <= endIncluded; i++) { + proposals.add(Integer.toString(i)); + } + return () -> proposals; + } + + public static TabProposal TAB_PROPOSAL_LAST_PARAMS(String[] args, int index, Collection proposals) { + String lastParamToken = getLastParams(args, index); + String[] splittedToken = lastParamToken.split(" ", -1); + int currentTokenPosition = splittedToken.length - 1; + String[] previousTokens = Arrays.copyOf(splittedToken, currentTokenPosition); + + List currentTokenProposal = new ArrayList<>(); + for (String p : proposals) { + String[] splittedProposal = p.split(" ", -1); + if (splittedProposal.length <= currentTokenPosition) + continue; + if (!Arrays.equals(Arrays.copyOf(splittedToken, currentTokenPosition), previousTokens)) + continue; + if (splittedProposal[currentTokenPosition].isEmpty()) + continue; + + currentTokenProposal.add(splittedProposal[currentTokenPosition]); + } + + return () -> currentTokenProposal; + } + + @FunctionalInterface + public interface TabProposal { + public abstract Collection getProposal(); + } + + + + + /** + * 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 static class BadCommandUsage extends RuntimeException { + private static final long serialVersionUID = 1L; + + public BadCommandUsage() { + super(); + } + + public BadCommandUsage(Throwable cause) { + super(cause); + } + + public BadCommandUsage(String message) { + super(message); + } + + public BadCommandUsage(String message, Throwable cause) { + super(message, cause); + } + } + + + +}