diff --git a/pom.xml b/pom.xml index 4cff99d..5159100 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 net.zeeraa z-command-lib - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT src diff --git a/src/net/zeeraa/zcommandlib/command/base/ZCommandBase.java b/src/net/zeeraa/zcommandlib/command/base/ZCommandBase.java index de1388c..d533900 100644 --- a/src/net/zeeraa/zcommandlib/command/base/ZCommandBase.java +++ b/src/net/zeeraa/zcommandlib/command/base/ZCommandBase.java @@ -15,7 +15,10 @@ import net.zeeraa.zcommandlib.command.ZCommand; import net.zeeraa.zcommandlib.command.ZSubCommand; import net.zeeraa.zcommandlib.command.help.HelpSubCommand; +import net.zeeraa.zcommandlib.command.messages.IMessageSender; +import net.zeeraa.zcommandlib.command.messages.implementation.DefaultChatMessageSender; import net.zeeraa.zcommandlib.command.utils.AllowedSenders; + /** * This class is the shared code between {@link ZCommand} and * {@link ZSubCommand} @@ -43,6 +46,11 @@ public abstract class ZCommandBase { private boolean emptyTabMode; + private String noPermissionMessage; + + private IMessageSender noPermissionMessageSender; + private IMessageSender disallowedSenderMessageSender; + public ZCommandBase(String name) { this.name = name; this.description = ""; @@ -60,6 +68,11 @@ public ZCommandBase(String name) { this.emptyTabMode = false; this.filterAutocomplete = false; + + this.noPermissionMessage = ChatColor.RED + "You dont have permission to use this command"; + + this.noPermissionMessageSender = new DefaultChatMessageSender(); + this.disallowedSenderMessageSender = new DefaultChatMessageSender(); } /** @@ -465,7 +478,54 @@ public static List generateAliasList(String... aliases) { * permission */ public String getNoPermissionMessage() { - return ChatColor.RED + "You dont have permission to use this command"; + return noPermissionMessage; + } + + /** + * Set the message that is shown to {@link CommandSender} that does not have + * permission + * + * @param noPermissionMessage a {@link String} that will be sent to + * {@link CommandSender} without permission + */ + protected void setNoPermissionMessage(String noPermissionMessage) { + this.noPermissionMessage = noPermissionMessage; + } + + /** + * Get the {@link IMessageSender} used when showing disallowed sender messages + * + * @return {@link IMessageSender} responsible for disallowed sender message + */ + public IMessageSender getDisallowedSenderMessageSender() { + return disallowedSenderMessageSender; + } + + /** + * Get the {@link IMessageSender} used when showing no permission messages + * + * @return {@link IMessageSender} responsible for no permission message + */ + public IMessageSender getNoPermissionMessageSender() { + return noPermissionMessageSender; + } + + /** + * Set the {@link IMessageSender} used for sending disallowed sender messages + * + * @param disallowedSenderMessageSender The new {@link IMessageSender} to use + */ + protected void setDisallowedSenderMessageSender(IMessageSender disallowedSenderMessageSender) { + this.disallowedSenderMessageSender = disallowedSenderMessageSender; + } + + /** + * Set the {@link IMessageSender} used for sending no permission messages + * + * @param disallowedSenderMessageSender The new {@link IMessageSender} to use + */ + protected void setNoPermissionMessageSender(IMessageSender noPermissionMessageSender) { + this.noPermissionMessageSender = noPermissionMessageSender; } /** @@ -511,4 +571,4 @@ public AllowedSenders getAllowedSenders() { protected void setAllowedSenders(AllowedSenders allowedSenders) { this.allowedSenders = allowedSenders; } -} +} \ No newline at end of file diff --git a/src/net/zeeraa/zcommandlib/command/messages/IMessageSender.java b/src/net/zeeraa/zcommandlib/command/messages/IMessageSender.java new file mode 100644 index 0000000..0573b38 --- /dev/null +++ b/src/net/zeeraa/zcommandlib/command/messages/IMessageSender.java @@ -0,0 +1,9 @@ +package net.zeeraa.zcommandlib.command.messages; + +import org.bukkit.command.CommandSender; + +import net.zeeraa.zcommandlib.command.base.ZCommandBase; + +public interface IMessageSender { + void sendMessage(ZCommandBase command, CommandSender sender, String message); +} \ No newline at end of file diff --git a/src/net/zeeraa/zcommandlib/command/messages/implementation/DefaultChatMessageSender.java b/src/net/zeeraa/zcommandlib/command/messages/implementation/DefaultChatMessageSender.java new file mode 100644 index 0000000..58bf840 --- /dev/null +++ b/src/net/zeeraa/zcommandlib/command/messages/implementation/DefaultChatMessageSender.java @@ -0,0 +1,13 @@ +package net.zeeraa.zcommandlib.command.messages.implementation; + +import org.bukkit.command.CommandSender; + +import net.zeeraa.zcommandlib.command.base.ZCommandBase; +import net.zeeraa.zcommandlib.command.messages.IMessageSender; + +public class DefaultChatMessageSender implements IMessageSender { + @Override + public void sendMessage(ZCommandBase command, CommandSender sender, String message) { + sender.sendMessage(message); + } +} \ No newline at end of file diff --git a/src/net/zeeraa/zcommandlib/command/proxy/ZCommandProxy.java b/src/net/zeeraa/zcommandlib/command/proxy/ZCommandProxy.java index 680289f..85beec7 100644 --- a/src/net/zeeraa/zcommandlib/command/proxy/ZCommandProxy.java +++ b/src/net/zeeraa/zcommandlib/command/proxy/ZCommandProxy.java @@ -91,13 +91,13 @@ private boolean recursiceCommandExecutionCheck(ZCommandBase command, CommandSend if (!(sender instanceof ConsoleCommandSender)) { if (!command.hasSenderPermission(sender)) { - sender.sendMessage(command.getNoPermissionMessage()); + command.getNoPermissionMessageSender().sendMessage(command, sender, command.getNoPermissionMessage()); return false; } } if (!command.getAllowedSenders().isAllowed(sender)) { - sender.sendMessage(command.getAllowedSenders().getErrorMessage()); + command.getDisallowedSenderMessageSender().sendMessage(command, sender, command.getAllowedSenders().getErrorMessage()); return false; } @@ -124,35 +124,27 @@ private List recursiceCommandTabCheck(ZCommandBase command, CommandSende if (command.isFilterAutocomplete()) { // Filter Collections.sort(commandResults, String.CASE_INSENSITIVE_ORDER); - for (String string : commandResults) { - if (StringUtil.startsWithIgnoreCase(string, lastWord)) { - result.add(string); - } - } + commandResults.stream().filter(c -> StringUtil.startsWithIgnoreCase(c, lastWord)).forEach(result::add); } else { // Do not filter result.addAll(commandResults); } // Find aliases for sub commands - ArrayList matchedSubCommands = new ArrayList(); - - for (ZSubCommand subCommand : command.getSubCommands()) { + List matchedSubCommands = new ArrayList(); + + command.getSubCommands().forEach(subCommand -> { if (StringUtil.startsWithIgnoreCase(subCommand.getName(), lastWord)) { matchedSubCommands.add(subCommand.getName()); } - for (String subCommandAlias : subCommand.getAliases()) { - if (StringUtil.startsWithIgnoreCase(subCommandAlias, lastWord)) { - matchedSubCommands.add(subCommandAlias); - } - } - } + subCommand.getAliases().stream().filter(sub -> StringUtil.startsWithIgnoreCase(sub, lastWord)).forEach(matchedSubCommands::add); + }); Collections.sort(matchedSubCommands, String.CASE_INSENSITIVE_ORDER); result.addAll(0, matchedSubCommands); - + return result; } else { // Recursive check for sub commands