Skip to content

Commit

Permalink
Update commands permissions; Always register /ignore command
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Nov 29, 2023
1 parent 40f2f5f commit 1d21486
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 100 deletions.
217 changes: 118 additions & 99 deletions spigot/src/main/java/ru/brikster/chatty/Chatty.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void registerChattyCommand(ChattyInitEvent initEvent) throws Exception {

Command<CommandSender> reloadCommand = chattyBuilder
.literal("reload")
.permission("chatty.reload")
.permission("chatty.command.reload")
.handler(handler -> {
try {
injector.getInstance(PlayerDataRepository.class).close();
Expand Down Expand Up @@ -149,113 +149,132 @@ private void initialize(ChattyInitEvent initEvent) throws Exception {

PmConfig pmConfig = injector.getInstance(PmConfig.class);

if (pmConfig.isEnable()) {
PrivateMessageSuggestionsProvider pmSuggestionsProvider = injector.getInstance(PrivateMessageSuggestionsProvider.class);
MsgCommandHandler msgCommandHandler = injector.getInstance(MsgCommandHandler.class);
ReplyCommandHandler replyCommandHandler = injector.getInstance(ReplyCommandHandler.class);
AddIgnoreCommandHandler addIgnoreCommandHandler = injector.getInstance(AddIgnoreCommandHandler.class);
RemoveIgnoreCommandHandler removeIgnoreCommandHandler = injector.getInstance(RemoveIgnoreCommandHandler.class);
IgnoreListCommandHandler ignoreListCommandHandler = injector.getInstance(IgnoreListCommandHandler.class);
PrivateMessageSuggestionsProvider pmSuggestionsProvider = injector.getInstance(PrivateMessageSuggestionsProvider.class);

if (this.asyncCommandManager == null) {
AddIgnoreCommandHandler addIgnoreCommandHandler = injector.getInstance(AddIgnoreCommandHandler.class);
RemoveIgnoreCommandHandler removeIgnoreCommandHandler = injector.getInstance(RemoveIgnoreCommandHandler.class);
IgnoreListCommandHandler ignoreListCommandHandler = injector.getInstance(IgnoreListCommandHandler.class);

if (this.asyncCommandManager == null) {
addIgnoreProxiedCommandHandler = new ProxyCommandHandler<>(addIgnoreCommandHandler);
removeIgnoreProxiedCommandHandler = new ProxyCommandHandler<>(removeIgnoreCommandHandler);
ignoreListProxiedCommandHandler = new ProxyCommandHandler<>(ignoreListCommandHandler);

initAsyncCommandManager();

if (pmConfig.isEnable()) {
MsgCommandHandler msgCommandHandler = injector.getInstance(MsgCommandHandler.class);
ReplyCommandHandler replyCommandHandler = injector.getInstance(ReplyCommandHandler.class);
msgProxiedCommandHandler = new ProxyCommandHandler<>(msgCommandHandler);
replyProxiedCommandHandler = new ProxyCommandHandler<>(replyCommandHandler);
addIgnoreProxiedCommandHandler = new ProxyCommandHandler<>(addIgnoreCommandHandler);
removeIgnoreProxiedCommandHandler = new ProxyCommandHandler<>(removeIgnoreCommandHandler);
ignoreListProxiedCommandHandler = new ProxyCommandHandler<>(ignoreListCommandHandler);

this.asyncCommandManager = new BukkitCommandManager<>(this,
AsynchronousCommandExecutionCoordinator.<CommandSender>builder()
.withAsynchronousParsing()
.build(),
Function.identity(),
Function.identity());

MessagesConfig messagesConfig = injector.getInstance(MessagesConfig.class);

//noinspection resource
new MinecraftExceptionHandler<CommandSender>()
.withHandler(ExceptionType.ARGUMENT_PARSING, (e) -> {
String argument = ((ArgumentParseException) e).getCause().toString();
return messagesConfig.getCmdArgumentParsingError()
.replaceText(AdventureUtil.createReplacement("{argument}", argument));
})
.withHandler(ExceptionType.INVALID_SYNTAX, (e) -> {
String correctSyntax = ((InvalidSyntaxException) e).getCorrectSyntax();
return messagesConfig.getCmdUsageError()
.replaceText(AdventureUtil.createReplacement("{usage}", "/" + correctSyntax));
})
.withHandler(ExceptionType.INVALID_SENDER, (e) -> messagesConfig.getCmdSenderTypeError())
.withHandler(ExceptionType.NO_PERMISSION, (e) -> messagesConfig.getCmdNoPermissionError())
.withHandler(ExceptionType.COMMAND_EXECUTION, (e) -> {
//noinspection CallToPrintStackTrace
e.printStackTrace();
return messagesConfig.getCmdExecutionError();
})
.apply(asyncCommandManager, BukkitAudiences.create(this)::sender);

asyncCommandManager.setSetting(ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);

Command<CommandSender> msgCommand = asyncCommandManager.commandBuilder("msg", "message", "m", "w", "pm", "dm")
.permission("chatty.pm")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.argument(StringArgument.greedy("message"))
.handler(msgProxiedCommandHandler)
.build();

Command<CommandSender> replyCommand = asyncCommandManager.commandBuilder("reply", "r")
.permission("chatty.pm")
.argument(StringArgument.greedy("message"))
.handler(replyProxiedCommandHandler)
.build();

asyncCommandManager
.command(msgCommand)
.command(replyCommand);

Builder<CommandSender> ignoreCommandBuilder = asyncCommandManager
.commandBuilder("ignore")
.permission("chatty.pm");

Command<CommandSender> ignoreAddCommand = ignoreCommandBuilder
.literal("add")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.handler(addIgnoreProxiedCommandHandler)
.build();

Command<CommandSender> ignoreRemoveCommand = ignoreCommandBuilder
.literal("remove", "rem", "rm", "delete")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.handler(removeIgnoreProxiedCommandHandler)
.build();

Command<CommandSender> ignoreListCommand = ignoreCommandBuilder
.handler(ignoreListProxiedCommandHandler)
.build();

asyncCommandManager
.command(ignoreAddCommand)
.command(ignoreRemoveCommand)
.command(ignoreListCommand);
} else {
registerPmCommands(pmSuggestionsProvider);
}

registerIgnoreCommand(pmSuggestionsProvider);
} else {
if (pmConfig.isEnable()) {
MsgCommandHandler msgCommandHandler = injector.getInstance(MsgCommandHandler.class);
ReplyCommandHandler replyCommandHandler = injector.getInstance(ReplyCommandHandler.class);
msgProxiedCommandHandler.setExecutionHandler(msgCommandHandler);
replyProxiedCommandHandler.setExecutionHandler(replyCommandHandler);
addIgnoreProxiedCommandHandler.setExecutionHandler(addIgnoreCommandHandler);
removeIgnoreProxiedCommandHandler.setExecutionHandler(removeIgnoreCommandHandler);
ignoreListProxiedCommandHandler.setExecutionHandler(ignoreListCommandHandler);
registerPmCommands(pmSuggestionsProvider);
}

addIgnoreProxiedCommandHandler.setExecutionHandler(addIgnoreCommandHandler);
removeIgnoreProxiedCommandHandler.setExecutionHandler(removeIgnoreCommandHandler);
ignoreListProxiedCommandHandler.setExecutionHandler(ignoreListCommandHandler);
}
}

private void initAsyncCommandManager() throws Exception {
this.asyncCommandManager = new BukkitCommandManager<>(this,
AsynchronousCommandExecutionCoordinator.<CommandSender>builder()
.withAsynchronousParsing()
.build(),
Function.identity(),
Function.identity());

MessagesConfig messagesConfig = injector.getInstance(MessagesConfig.class);

//noinspection resource
new MinecraftExceptionHandler<CommandSender>()
.withHandler(ExceptionType.ARGUMENT_PARSING, (e) -> {
String argument = ((ArgumentParseException) e).getCause().toString();
return messagesConfig.getCmdArgumentParsingError()
.replaceText(AdventureUtil.createReplacement("{argument}", argument));
})
.withHandler(ExceptionType.INVALID_SYNTAX, (e) -> {
String correctSyntax = ((InvalidSyntaxException) e).getCorrectSyntax();
return messagesConfig.getCmdUsageError()
.replaceText(AdventureUtil.createReplacement("{usage}", "/" + correctSyntax));
})
.withHandler(ExceptionType.INVALID_SENDER, (e) -> messagesConfig.getCmdSenderTypeError())
.withHandler(ExceptionType.NO_PERMISSION, (e) -> messagesConfig.getCmdNoPermissionError())
.withHandler(ExceptionType.COMMAND_EXECUTION, (e) -> {
//noinspection CallToPrintStackTrace
e.printStackTrace();
return messagesConfig.getCmdExecutionError();
})
.apply(asyncCommandManager, BukkitAudiences.create(this)::sender);

asyncCommandManager.setSetting(ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
}

private void registerIgnoreCommand(PrivateMessageSuggestionsProvider pmSuggestionsProvider) {
Builder<CommandSender> ignoreCommandBuilder = asyncCommandManager
.commandBuilder("ignore")
.permission("chatty.command.ignore");

Command<CommandSender> ignoreAddCommand = ignoreCommandBuilder
.literal("add")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.handler(addIgnoreProxiedCommandHandler)
.build();

Command<CommandSender> ignoreRemoveCommand = ignoreCommandBuilder
.literal("remove", "rem", "rm", "delete")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.handler(removeIgnoreProxiedCommandHandler)
.build();

Command<CommandSender> ignoreListCommand = ignoreCommandBuilder
.handler(ignoreListProxiedCommandHandler)
.build();

asyncCommandManager
.command(ignoreAddCommand)
.command(ignoreRemoveCommand)
.command(ignoreListCommand);
}

private void registerPmCommands(PrivateMessageSuggestionsProvider pmSuggestionsProvider) {
Command<CommandSender> msgCommand = asyncCommandManager.commandBuilder("msg", "message", "m", "w", "pm", "dm")
.permission("chatty.pm")
.argument(StringArgument.<CommandSender>builder("target")
.single()
.withSuggestionsProvider(pmSuggestionsProvider)
.build())
.argument(StringArgument.greedy("message"))
.handler(msgProxiedCommandHandler)
.build();

Command<CommandSender> replyCommand = asyncCommandManager.commandBuilder("reply", "r")
.permission("chatty.pm")
.argument(StringArgument.greedy("message"))
.handler(replyProxiedCommandHandler)
.build();

asyncCommandManager
.command(msgCommand)
.command(replyCommand);
}

@Override
public void onDisable() {
this.getServer().getScheduler().cancelTasks(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public final class PrivateMessageSuggestionsProvider implements CommandSuggestio

@Override
public @NotNull List<@NotNull String> provideSuggestions(@NotNull CommandContext<@NotNull CommandSender> commandContext, @NotNull String arg) {

List<String> suggestions = new ArrayList<>();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.getName().toLowerCase().startsWith(arg.toLowerCase())) {
Expand Down

0 comments on commit 1d21486

Please sign in to comment.