From 3de6d1d0f17c5309fe713bdbbe012588787358f2 Mon Sep 17 00:00:00 2001 From: Draycia Date: Fri, 28 Aug 2020 03:36:20 -0700 Subject: [PATCH] 15/15 --- .../carbon/channels/ChannelRegistry.java | 10 +- .../commands/AliasedChannelCommand.java | 138 ++++++++++++++---- .../carbon/commands/ChannelCommand.java | 2 +- .../carbon/managers/CommandManager.java | 75 ---------- 4 files changed, 113 insertions(+), 112 deletions(-) diff --git a/src/main/java/net/draycia/carbon/channels/ChannelRegistry.java b/src/main/java/net/draycia/carbon/channels/ChannelRegistry.java index be622b93..7b50c2ab 100644 --- a/src/main/java/net/draycia/carbon/channels/ChannelRegistry.java +++ b/src/main/java/net/draycia/carbon/channels/ChannelRegistry.java @@ -1,6 +1,6 @@ package net.draycia.carbon.channels; -import co.aikar.commands.BukkitCommandManager; +import dev.jorel.commandapi.CommandAPI; import net.draycia.carbon.CarbonChat; import net.draycia.carbon.commands.AliasedChannelCommand; import net.draycia.carbon.util.Registry; @@ -27,12 +27,8 @@ public class ChannelRegistry implements Registry { boolean registerSuccessful = registry.putIfAbsent(key, value) == null; if (registerSuccessful) { - BukkitCommandManager commandManager = carbonChat.getCommandManager().getCommandManager(); - commandManager.getCommandReplacements().addReplacement("channelName", value.getAliases()); + AliasedChannelCommand command = new AliasedChannelCommand(carbonChat, value); - AliasedChannelCommand command = new AliasedChannelCommand(value); - - commandManager.registerCommand(command); channelCommands.add(command); if (value instanceof Listener) { @@ -61,7 +57,7 @@ public class ChannelRegistry implements Registry { for (AliasedChannelCommand command : channelCommands) { carbonChat.getLogger().info("Unregistering command for channel: " + command.getChatChannel().getName()); - carbonChat.getCommandManager().getCommandManager().unregisterCommand(command); + CommandAPI.unregister(command.getCommandName()); } } diff --git a/src/main/java/net/draycia/carbon/commands/AliasedChannelCommand.java b/src/main/java/net/draycia/carbon/commands/AliasedChannelCommand.java index 7ed6397e..78cd27b4 100644 --- a/src/main/java/net/draycia/carbon/commands/AliasedChannelCommand.java +++ b/src/main/java/net/draycia/carbon/commands/AliasedChannelCommand.java @@ -1,58 +1,138 @@ package net.draycia.carbon.commands; -import co.aikar.commands.BaseCommand; -import co.aikar.commands.ConditionFailedException; import co.aikar.commands.annotation.*; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.Argument; +import dev.jorel.commandapi.arguments.GreedyStringArgument; import net.draycia.carbon.CarbonChat; import net.draycia.carbon.channels.ChatChannel; import net.draycia.carbon.storage.ChatUser; +import net.draycia.carbon.util.CarbonUtils; import net.kyori.adventure.text.Component; -import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.LinkedHashMap; +import java.util.List; + @CommandAlias("%channelName") @CommandPermission("carbonchat.switch") -public class AliasedChannelCommand extends BaseCommand { - - @Dependency - private CarbonChat carbonChat; +public class AliasedChannelCommand { + private final CarbonChat carbonChat; private final ChatChannel chatChannel; + private final String commandName; - public AliasedChannelCommand(ChatChannel chatChannel) { + public AliasedChannelCommand(CarbonChat carbonChat, ChatChannel chatChannel) { + this.carbonChat = carbonChat; this.chatChannel = chatChannel; + + this.commandName = carbonChat.getConfig().getString("commands.switch.name", "switch"); + List commandAliases = carbonChat.getConfig().getStringList("commands.switch.aliases"); + + LinkedHashMap setChannelArguments = new LinkedHashMap<>(); + + new CommandAPICommand(commandName) + .withArguments(setChannelArguments) + .withAliases(commandAliases.toArray(new String[0])) + .withPermission(dev.jorel.commandapi.CommandPermission.fromString("carbonchat.switch")) + .executesPlayer(this::setChannel) + .register(); + + LinkedHashMap setOtherChannelArguments = new LinkedHashMap<>(); + setOtherChannelArguments.put("player", CarbonUtils.chatUserArgument()); + + new CommandAPICommand(commandName) + .withArguments(setOtherChannelArguments) + .withAliases(commandAliases.toArray(new String[0])) + .withPermission(dev.jorel.commandapi.CommandPermission.fromString("carbonchat.switch.other")) + .executes(this::setOtherChannel) + .register(); + + LinkedHashMap sendMessageArguments = new LinkedHashMap<>(); + sendMessageArguments.put("message", new GreedyStringArgument()); + + new CommandAPICommand(commandName) + .withArguments(sendMessageArguments) + .withAliases(commandAliases.toArray(new String[0])) + .withPermission(dev.jorel.commandapi.CommandPermission.fromString("carbonchat.switch.message")) + .executesPlayer(this::sendMessage) + .register(); + + LinkedHashMap sendMessageOtherArguments = new LinkedHashMap<>(); + sendMessageOtherArguments.put("player", CarbonUtils.chatUserArgument()); + sendMessageOtherArguments.put("message", new GreedyStringArgument()); + + new CommandAPICommand(commandName) + .withArguments(sendMessageOtherArguments) + .withAliases(commandAliases.toArray(new String[0])) + .withPermission(dev.jorel.commandapi.CommandPermission.fromString("carbonchat.switch.other.message")) + .executes(this::sendMessageOther) + .register(); } - @Default - @Syntax("[message]") - public void baseCommand(Player player, @Optional String[] args) { + private void setChannel(Player player, Object[] args) { ChatUser user = carbonChat.getUserService().wrap(player); - if (!chatChannel.canPlayerUse(user)) { - throw new ConditionFailedException(chatChannel.getCannotUseMessage()); - } - - if (user.getChannelSettings(chatChannel).isIgnored()) { - throw new ConditionFailedException(chatChannel.getCannotUseMessage()); - } - - if (args == null || args.length == 0) { - user.setSelectedChannel(getChatChannel()); - - user.sendMessage(carbonChat.getAdventureManager().processMessage(getChatChannel().getSwitchMessage(), + if (user.getChannelSettings(getChatChannel()).isIgnored()) { + user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, getChatChannel().getCannotUseMessage(), + "br", "\n", "color", "<" + getChatChannel().getChannelColor(user).toString() + ">", "channel", getChatChannel().getName())); - } else { - Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> { - Component component = getChatChannel().sendMessage(user, String.join(" ", args), false); - carbonChat.getLogger().info(CarbonChat.LEGACY.serialize(component) - .replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%")); - }); + return; } + + user.setSelectedChannel(getChatChannel()); + + user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(player, getChatChannel().getSwitchMessage(), + "br", "\n", + "color", "<" + getChatChannel().getChannelColor(user).toString() + ">", + "channel", getChatChannel().getName())); + } + + private void setOtherChannel(CommandSender sender, Object[] args) { + // @CommandPermission("carbonchat.switch.others") + ChatUser user = (ChatUser) args[0]; + + user.setSelectedChannel(getChatChannel()); + + String message = getChatChannel().getSwitchMessage(); + String otherMessage = getChatChannel().getSwitchOtherMessage(); + + user.sendMessage(carbonChat.getAdventureManager().processMessage(message, "br", "\n", + "color", "", "channel", getChatChannel().getName())); + + carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage, "br", "\n", + "color", "", "channel", getChatChannel().getName(), + "player", user.asOfflinePlayer().getName())); + } + + private void sendMessage(Player player, Object[] args) { + ChatUser user = carbonChat.getUserService().wrap(player); + String message = (String) args[1]; + + Component component = getChatChannel().sendMessage(user, message, false); + + carbonChat.getLogger().info(CarbonChat.LEGACY.serialize(component) + .replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%")); + } + + private void sendMessageOther(CommandSender sender, Object[] args) { + ChatUser user = (ChatUser) args[0]; + String message = (String) args[2]; + + Component component = getChatChannel().sendMessage(user, message, false); + + carbonChat.getLogger().info(CarbonChat.LEGACY.serialize(component) + .replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%")); } public ChatChannel getChatChannel() { return chatChannel; } + + public String getCommandName() { + return commandName; + } } diff --git a/src/main/java/net/draycia/carbon/commands/ChannelCommand.java b/src/main/java/net/draycia/carbon/commands/ChannelCommand.java index 0aea0333..3b051a68 100644 --- a/src/main/java/net/draycia/carbon/commands/ChannelCommand.java +++ b/src/main/java/net/draycia/carbon/commands/ChannelCommand.java @@ -58,7 +58,7 @@ public class ChannelCommand { .register(); LinkedHashMap sendMessageOtherArguments = new LinkedHashMap<>(); - sendMessageOtherArguments.put("user", CarbonUtils.chatUserArgument()); + sendMessageOtherArguments.put("player", CarbonUtils.chatUserArgument()); sendMessageOtherArguments.put("channel", CarbonUtils.channelArgument()); sendMessageOtherArguments.put("message", new GreedyStringArgument()); diff --git a/src/main/java/net/draycia/carbon/managers/CommandManager.java b/src/main/java/net/draycia/carbon/managers/CommandManager.java index 3f737dfc..1b3b0734 100644 --- a/src/main/java/net/draycia/carbon/managers/CommandManager.java +++ b/src/main/java/net/draycia/carbon/managers/CommandManager.java @@ -1,82 +1,11 @@ package net.draycia.carbon.managers; -import co.aikar.commands.*; import net.draycia.carbon.CarbonChat; -import net.draycia.carbon.channels.ChatChannel; import net.draycia.carbon.commands.*; -import net.draycia.carbon.storage.ChatUser; - -import java.util.ArrayList; -import java.util.List; public class CommandManager { - private final BukkitCommandManager commandManager; - public CommandManager(CarbonChat carbonChat) { - - commandManager = new BukkitCommandManager(carbonChat); - - commandManager.enableUnstableAPI("help"); - - commandManager.getCommandCompletions().registerCompletion("chatchannel", (context) -> { - ChatUser user = carbonChat.getUserService().wrap(context.getPlayer()); - - List completions = new ArrayList<>(); - - for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) { - if (chatChannel.canPlayerUse(user)) { - completions.add(chatChannel.getKey()); - } - } - - return completions; - }); - - commandManager.getCommandCompletions().registerCompletion("channel", (context) -> { - List completions = new ArrayList<>(); - - for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) { - completions.add(chatChannel.getKey()); - } - - return completions; - }); - - commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> { - String name = context.popFirstArg(); - - for (ChatChannel chatChannel : carbonChat.getChannelManager().getRegistry().values()) { - if (chatChannel.getKey().equalsIgnoreCase(name)) { - return chatChannel; - } - } - - return null; - }); - - commandManager.getCommandContexts().registerContext(ChatUser.class, (context) -> { - return carbonChat.getUserService().wrap(context.popFirstArg()); - }); - - commandManager.getCommandConditions().addCondition(ChatChannel.class,"canuse", (context, execution, value) -> { - if (value == null) { - throw new ConditionFailedException(carbonChat.getLanguage().getString("cannot-use-channel")); - } - - ChatUser user = carbonChat.getUserService().wrap(context.getIssuer().getPlayer()); - - if (!value.canPlayerUse(user)) { - throw new ConditionFailedException(value.getCannotUseMessage()); - } - }); - - commandManager.getCommandConditions().addCondition(ChatChannel.class,"exists", (context, execution, value) -> { - if (value == null) { - throw new ConditionFailedException(carbonChat.getLanguage().getString("cannot-use-channel")); - } - }); - reloadCommands(carbonChat); } @@ -97,8 +26,4 @@ public class CommandManager { new ToggleCommand(carbonChat); } - public BukkitCommandManager getCommandManager() { - return commandManager; - } - }