Add /p global list, tweaks/fixes
This commit is contained in:
@@ -139,7 +139,7 @@ public abstract class Command {
|
||||
try {
|
||||
executeWithContext(executionContext);
|
||||
} catch (Throwable t) {
|
||||
caller.getChatController().handleException(sender, executionContext, t);
|
||||
caller.getChatHandler().handleException(sender, executionContext, t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ public abstract class Command {
|
||||
//System.out.println("Post-contextfilters");
|
||||
|
||||
String message = execute(context.getSender(), context);
|
||||
context.getAddress().getChatController().sendMessage(context.getSender(), EMessageType.RESULT, message);
|
||||
context.sendMessage(EMessageType.RESULT, message);
|
||||
}
|
||||
|
||||
public abstract String execute(CommandSender sender, ExecutionContext context) throws CommandException;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.dico.dicore.command;
|
||||
|
||||
import io.dico.dicore.command.chat.IChatController;
|
||||
import io.dico.dicore.command.chat.IChatHandler;
|
||||
import io.dico.dicore.command.parameter.type.IParameterTypeSelector;
|
||||
import io.dico.dicore.command.parameter.type.MapBasedParameterTypeSelector;
|
||||
import io.dico.dicore.command.parameter.type.ParameterType;
|
||||
@@ -340,16 +340,16 @@ public final class CommandBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the chat controller at this address. The chat controller
|
||||
* Configure the chat handler at this address. The chat handler
|
||||
* is used for all children down the tree if they don't explicitly have
|
||||
* their own chat controller configured. If this isn't configured,
|
||||
* {@code ChatControllers.defaultChat()} is used.
|
||||
* their own chat handler configured. If this isn't configured,
|
||||
* {@code ChatHandlers.defaultChat()} is used.
|
||||
*
|
||||
* @param chatController the chat controller
|
||||
* @param chatHandler the chat handler
|
||||
* @return this
|
||||
*/
|
||||
public CommandBuilder setChatController(IChatController chatController) {
|
||||
cur.setChatController(chatController);
|
||||
public CommandBuilder setChatHandler(IChatHandler chatHandler) {
|
||||
cur.setChatHandler(chatHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -229,7 +229,7 @@ public class ExecutionContext {
|
||||
}
|
||||
|
||||
public Formatting getFormat(EMessageType type) {
|
||||
return address.getChatController().getChatFormatForType(type);
|
||||
return address.getChatHandler().getChatFormatForType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -345,7 +345,7 @@ public class ExecutionContext {
|
||||
if (translateColours) {
|
||||
message = Formatting.translateChars('&', message);
|
||||
}
|
||||
address.getChatController().sendMessage(this, messageType, message);
|
||||
address.getChatHandler().sendMessage(this, messageType, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,13 +367,13 @@ public class ExecutionContext {
|
||||
|
||||
public void sendHelpMessage(int page) {
|
||||
if (!muted) {
|
||||
address.getChatController().sendHelpMessage(sender, this, address, page);
|
||||
address.getChatHandler().sendHelpMessage(sender, this, address, page);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendSyntaxMessage() {
|
||||
if (!muted) {
|
||||
address.getChatController().sendSyntaxMessage(sender, this, address);
|
||||
address.getChatHandler().sendSyntaxMessage(sender, this, address);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.dico.dicore.command;
|
||||
|
||||
import io.dico.dicore.command.chat.IChatController;
|
||||
import io.dico.dicore.command.chat.IChatHandler;
|
||||
import io.dico.dicore.command.parameter.ArgumentBuffer;
|
||||
import io.dico.dicore.command.predef.PredefinedCommand;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -161,9 +161,9 @@ public interface ICommandAddress {
|
||||
ICommandDispatcher getDispatcherForTree();
|
||||
|
||||
/**
|
||||
* @return The desired chatcontroller for use by commands at this address and any sub-addresses, if they define no explicit chat controller.
|
||||
* @return The desired chathandler for use by commands at this address and any sub-addresses, if they define no explicit chat handler.
|
||||
*/
|
||||
IChatController getChatController();
|
||||
IChatHandler getChatHandler();
|
||||
|
||||
static ICommandAddress newChild() {
|
||||
return new ChildCommandAddress();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.dico.dicore.command;
|
||||
|
||||
import io.dico.dicore.command.chat.ChatControllers;
|
||||
import io.dico.dicore.command.chat.IChatController;
|
||||
import io.dico.dicore.command.chat.ChatHandlers;
|
||||
import io.dico.dicore.command.chat.IChatHandler;
|
||||
import io.dico.dicore.command.predef.DefaultGroupCommand;
|
||||
import io.dico.dicore.command.predef.HelpCommand;
|
||||
import io.dico.dicore.command.predef.PredefinedCommand;
|
||||
@@ -12,10 +12,10 @@ public abstract class ModifiableCommandAddress implements ICommandAddress {
|
||||
Map<String, ChildCommandAddress> children;
|
||||
Collection<String> childrenMainKeys = Collections.emptyList();
|
||||
|
||||
// the chat controller as configured by the programmer
|
||||
IChatController chatController;
|
||||
// cache for the algorithm that finds the first chat controller going up the tree
|
||||
transient IChatController chatControllerCache;
|
||||
// the chat handler as configured by the programmer
|
||||
IChatHandler chatHandler;
|
||||
// cache for the algorithm that finds the first chat handler going up the tree
|
||||
transient IChatHandler cachedChatHandlerFallback;
|
||||
|
||||
ModifiableCommandAddress helpChild;
|
||||
|
||||
@@ -230,30 +230,30 @@ public abstract class ModifiableCommandAddress implements ICommandAddress {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IChatController getChatController() {
|
||||
if (chatControllerCache == null) {
|
||||
if (chatController != null) {
|
||||
chatControllerCache = chatController;
|
||||
public IChatHandler getChatHandler() {
|
||||
if (cachedChatHandlerFallback == null) {
|
||||
if (chatHandler != null) {
|
||||
cachedChatHandlerFallback = chatHandler;
|
||||
} else if (!hasParent()) {
|
||||
chatControllerCache = ChatControllers.defaultChat();
|
||||
cachedChatHandlerFallback = ChatHandlers.defaultChat();
|
||||
} else {
|
||||
chatControllerCache = getParent().getChatController();
|
||||
cachedChatHandlerFallback = getParent().getChatHandler();
|
||||
}
|
||||
}
|
||||
return chatControllerCache;
|
||||
return cachedChatHandlerFallback;
|
||||
}
|
||||
|
||||
public void setChatController(IChatController chatController) {
|
||||
this.chatController = chatController;
|
||||
resetChatControllerCache(new HashSet<>());
|
||||
public void setChatHandler(IChatHandler chatHandler) {
|
||||
this.chatHandler = chatHandler;
|
||||
resetChatHandlerCache(new HashSet<>());
|
||||
}
|
||||
|
||||
void resetChatControllerCache(Set<ModifiableCommandAddress> dejaVu) {
|
||||
void resetChatHandlerCache(Set<ModifiableCommandAddress> dejaVu) {
|
||||
if (dejaVu.add(this)) {
|
||||
chatControllerCache = chatController;
|
||||
cachedChatHandlerFallback = chatHandler;
|
||||
for (ChildCommandAddress address : children.values()) {
|
||||
if (address.chatController == null) {
|
||||
address.resetChatControllerCache(dejaVu);
|
||||
if (address.chatHandler == null) {
|
||||
address.resetChatHandlerCache(dejaVu);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.dico.dicore.command;
|
||||
|
||||
import io.dico.dicore.command.parameter.ArgumentBuffer;
|
||||
import io.dico.dicore.command.predef.DefaultGroupCommand;
|
||||
import io.dico.dicore.command.registration.BukkitCommand;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -197,7 +196,7 @@ public class RootCommandAddress extends ModifiableCommandAddress implements ICom
|
||||
if (targetAddress == null) {
|
||||
targetAddress = this;
|
||||
}
|
||||
targetAddress.getChatController().handleException(sender, context, t);
|
||||
targetAddress.getChatHandler().handleException(sender, context, t);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
import io.dico.dicore.Formatting;
|
||||
import io.dico.dicore.command.CommandException;
|
||||
import io.dico.dicore.command.EMessageType;
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
@@ -9,14 +8,14 @@ import io.dico.dicore.command.chat.help.HelpPages;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AbstractChatController implements IChatController {
|
||||
public class AbstractChatHandler implements IChatHandler {
|
||||
private @NotNull HelpPages helpPages;
|
||||
|
||||
public AbstractChatController(@NotNull HelpPages helpPages) {
|
||||
public AbstractChatHandler(@NotNull HelpPages helpPages) {
|
||||
this.helpPages = helpPages;
|
||||
}
|
||||
|
||||
public AbstractChatController() {
|
||||
public AbstractChatHandler() {
|
||||
this(HelpPages.newDefaultHelpPages());
|
||||
}
|
||||
|
||||
@@ -29,43 +28,6 @@ public class AbstractChatController implements IChatController {
|
||||
this.helpPages = helpPages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ExecutionContext context, EMessageType type, String message) {
|
||||
sendMessage(context.getSender(), type, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(CommandSender sender, EMessageType type, String message) {
|
||||
if (message != null && !message.isEmpty()) {
|
||||
sender.sendMessage(filterMessage(getMessagePrefixForType(type) + getChatFormatForType(type) + message));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception) {
|
||||
sendMessage(sender, EMessageType.EXCEPTION, exception.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(CommandSender sender, ExecutionContext context, Throwable exception) {
|
||||
if (exception instanceof CommandException) {
|
||||
handleCommandException(sender, context, (CommandException) exception);
|
||||
} else {
|
||||
sendMessage(sender, EMessageType.EXCEPTION, "An internal error occurred whilst executing this command");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
|
||||
sender.sendMessage(helpPages.getHelpPage(sender, context, address, page));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
|
||||
sender.sendMessage(helpPages.getSyntax(sender, context, address));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Formatting getChatFormatForType(EMessageType type) {
|
||||
switch (type) {
|
||||
@@ -104,9 +66,29 @@ public class AbstractChatController implements IChatController {
|
||||
return "";
|
||||
}
|
||||
|
||||
protected String createMessage(EMessageType type, String message) {
|
||||
if (message == null || message.isEmpty()) return null;
|
||||
return getMessagePrefixForType(type) + getChatFormatForType(type) + message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filterMessage(String message) {
|
||||
return message;
|
||||
public String createMessage(ExecutionContext context, EMessageType type, String message) {
|
||||
return createMessage(type, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createMessage(CommandSender sender, EMessageType type, String message) {
|
||||
return createMessage(type, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
|
||||
return helpPages.getHelpPage(sender, context, address, page);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
|
||||
return helpPages.getSyntax(sender, context, address);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
/**
|
||||
* Static factory methods for {@link IChatController}
|
||||
*/
|
||||
public class ChatControllers {
|
||||
private static final IChatController defaultChat;
|
||||
|
||||
private ChatControllers() {
|
||||
|
||||
}
|
||||
|
||||
public static IChatController defaultChat() {
|
||||
return defaultChat;
|
||||
}
|
||||
|
||||
static {
|
||||
defaultChat = new AbstractChatController();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
/**
|
||||
* Static factory methods for {@link IChatHandler}
|
||||
*/
|
||||
public class ChatHandlers {
|
||||
private static final IChatHandler defaultChat;
|
||||
|
||||
private ChatHandlers() {
|
||||
|
||||
}
|
||||
|
||||
public static IChatHandler defaultChat() {
|
||||
return defaultChat;
|
||||
}
|
||||
|
||||
static {
|
||||
defaultChat = new AbstractChatHandler();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
import io.dico.dicore.Formatting;
|
||||
import io.dico.dicore.command.CommandException;
|
||||
import io.dico.dicore.command.EMessageType;
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
//TODO add methods to send JSON messages
|
||||
public interface IChatController {
|
||||
|
||||
void sendMessage(ExecutionContext context, EMessageType type, String message);
|
||||
|
||||
void sendMessage(CommandSender sender, EMessageType type, String message);
|
||||
|
||||
void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception);
|
||||
|
||||
void handleException(CommandSender sender, ExecutionContext context, Throwable exception);
|
||||
|
||||
void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page);
|
||||
|
||||
void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address);
|
||||
|
||||
Formatting getChatFormatForType(EMessageType type);
|
||||
|
||||
String getMessagePrefixForType(EMessageType type);
|
||||
|
||||
String filterMessage(String message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
import io.dico.dicore.Formatting;
|
||||
import io.dico.dicore.command.CommandException;
|
||||
import io.dico.dicore.command.EMessageType;
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
//TODO add methods to send JSON messages
|
||||
public interface IChatHandler {
|
||||
|
||||
default void sendMessage(ExecutionContext context, EMessageType type, String message) {
|
||||
message = createMessage(context, type, message);
|
||||
if (message != null) {
|
||||
context.getSender().sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
default void sendMessage(CommandSender sender, EMessageType type, String message) {
|
||||
message = createMessage(sender, type, message);
|
||||
if (message != null) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
default void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception) {
|
||||
sendMessage(context, EMessageType.EXCEPTION, exception.getMessage());
|
||||
}
|
||||
|
||||
default void handleException(CommandSender sender, ExecutionContext context, Throwable exception) {
|
||||
if (exception instanceof CommandException) {
|
||||
handleCommandException(sender, context, (CommandException) exception);
|
||||
} else {
|
||||
sendMessage(sender, EMessageType.EXCEPTION, "An internal error occurred whilst executing this command");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
default void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
|
||||
sender.sendMessage(createHelpMessage(sender, context, address, page));
|
||||
}
|
||||
|
||||
default void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
|
||||
sender.sendMessage(createSyntaxMessage(sender, context, address));
|
||||
}
|
||||
|
||||
Formatting getChatFormatForType(EMessageType type);
|
||||
|
||||
String getMessagePrefixForType(EMessageType type);
|
||||
|
||||
String createMessage(ExecutionContext context, EMessageType type, String message);
|
||||
|
||||
String createMessage(CommandSender sender, EMessageType type, String message);
|
||||
|
||||
String createHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page);
|
||||
|
||||
String createSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address);
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import io.dico.dicore.command.ModifiableCommandAddress;
|
||||
import io.dico.dicore.Formatting;
|
||||
import io.dico.dicore.command.chat.IChatController;
|
||||
import io.dico.dicore.command.chat.IChatHandler;
|
||||
import io.dico.dicore.command.chat.help.IPageBorder;
|
||||
import io.dico.dicore.command.chat.help.IPageLayout;
|
||||
import io.dico.dicore.command.chat.help.PageBorders;
|
||||
@@ -15,7 +15,7 @@ public class DefaultPageLayout implements IPageLayout {
|
||||
|
||||
@Override
|
||||
public PageBorders getPageBorders(ICommandAddress target, Permissible viewer, ExecutionContext context, int pageNum) {
|
||||
IChatController c = context.getAddress().getChatController();
|
||||
IChatHandler c = context.getAddress().getChatHandler();
|
||||
String prefix = c.getMessagePrefixForType(EMessageType.INFORMATIVE);
|
||||
Formatting informative = c.getChatFormatForType(EMessageType.INFORMATIVE);
|
||||
Formatting number = c.getChatFormatForType(EMessageType.NEUTRAL);
|
||||
|
||||
@@ -7,7 +7,6 @@ import io.dico.dicore.Formatting;
|
||||
import io.dico.dicore.command.chat.help.IHelpComponent;
|
||||
import io.dico.dicore.command.chat.help.IHelpTopic;
|
||||
import io.dico.dicore.command.chat.help.SimpleHelpComponent;
|
||||
import io.dico.dicore.command.predef.PredefinedCommand;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
|
||||
@@ -54,7 +53,7 @@ public class SubcommandsHelpTopic implements IHelpTopic {
|
||||
}
|
||||
|
||||
private static Formatting colorOf(ExecutionContext context, EMessageType type) {
|
||||
return context.getAddress().getChatController().getChatFormatForType(type);
|
||||
return context.getAddress().getChatHandler().getChatFormatForType(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class DefaultGroupCommand extends PredefinedCommand<DefaultGroupCommand>
|
||||
|
||||
@Override
|
||||
public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
|
||||
context.getAddress().getChatController().sendHelpMessage(sender, context, context.getAddress(), 1);
|
||||
context.getAddress().getChatHandler().sendHelpMessage(sender, context, context.getAddress(), 1);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public class HelpCommand extends PredefinedCommand<HelpCommand> {
|
||||
target = target.getParent();
|
||||
}
|
||||
|
||||
context.getAddress().getChatController().sendHelpMessage(sender, context, target, context.<Integer>get("page") - 1);
|
||||
context.getAddress().getChatHandler().sendHelpMessage(sender, context, target, context.<Integer>get("page") - 1);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class SyntaxCommand extends PredefinedCommand<SyntaxCommand> {
|
||||
|
||||
@Override
|
||||
public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
|
||||
context.getAddress().getChatController().sendSyntaxMessage(sender, context, context.getAddress().getParent());
|
||||
context.getAddress().getChatHandler().sendSyntaxMessage(sender, context, context.getAddress().getParent());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ fun callAsCoroutine(
|
||||
}
|
||||
|
||||
job.invokeOnCompletion {
|
||||
val cc = context.address.chatController
|
||||
val cc = context.address.chatHandler
|
||||
try {
|
||||
val result = job.getResult()
|
||||
cc.sendMessage(context.sender, EMessageType.RESULT, result)
|
||||
|
||||
Reference in New Issue
Block a user