Skip to content

Commit

Permalink
Update to 1.1.0
Browse files Browse the repository at this point in the history
+ Added setNoPermissionMessage(String)

+ Added IMessageSender interface allowing for replacing messages

+ Added IMessageSender for no permission and disallowed sender messages

* Changed out some for loops with java streams
  • Loading branch information
AntonUden committed Jan 12, 2024
1 parent e07cda9 commit c558678
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.zeeraa</groupId>
<artifactId>z-command-lib</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
Expand Down
64 changes: 62 additions & 2 deletions src/net/zeeraa/zcommandlib/command/base/ZCommandBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 = "";
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -465,7 +478,54 @@ public static List<String> 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;
}

/**
Expand Down Expand Up @@ -511,4 +571,4 @@ public AllowedSenders getAllowedSenders() {
protected void setAllowedSenders(AllowedSenders allowedSenders) {
this.allowedSenders = allowedSenders;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 9 additions & 17 deletions src/net/zeeraa/zcommandlib/command/proxy/ZCommandProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -124,35 +124,27 @@ private List<String> 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<String> matchedSubCommands = new ArrayList<String>();
for (ZSubCommand subCommand : command.getSubCommands()) {
List<String> matchedSubCommands = new ArrayList<String>();

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
Expand Down

0 comments on commit c558678

Please sign in to comment.