This commit is contained in:
Draycia committed 2020-08-28 03:36:20 -07:00
1 parent 1d162393bf
commit 3de6d1d0f1
4 files changed
+113 -112

No files matched your search

@@ -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<ChatChannel> {
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<ChatChannel> {
for (AliasedChannelCommand command : channelCommands) {
carbonChat.getLogger().info("Unregistering command for channel: " + command.getChatChannel().getName());
carbonChat.getCommandManager().getCommandManager().unregisterCommand(command);
CommandAPI.unregister(command.getCommandName());
}
}
@@ -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<String> commandAliases = carbonChat.getConfig().getStringList("commands.switch.aliases");
LinkedHashMap<String, Argument> 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<String, Argument> 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<String, Argument> 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<String, Argument> 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", "<color:" + getChatChannel().getChannelColor(user).toString() + ">", "channel", getChatChannel().getName()));
carbonChat.getAdventureManager().getAudiences().audience(sender).sendMessage(carbonChat.getAdventureManager().processMessage(otherMessage, "br", "\n",
"color", "<color:" + getChatChannel().getChannelColor(user).toString() + ">", "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;
}
}
@@ -58,7 +58,7 @@ public class ChannelCommand {
.register();
LinkedHashMap<String, Argument> sendMessageOtherArguments = new LinkedHashMap<>();
sendMessageOtherArguments.put("user", CarbonUtils.chatUserArgument());
sendMessageOtherArguments.put("player", CarbonUtils.chatUserArgument());
sendMessageOtherArguments.put("channel", CarbonUtils.channelArgument());
sendMessageOtherArguments.put("message", new GreedyStringArgument());
@@ -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<String> 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<String> 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;
}
}