From 60e1ad222091b3a393018546fbdb7ebbd7093933 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Tue, 24 May 2022 03:06:34 +0200 Subject: [PATCH] Added a way to toggle custom channels --- .../com/alttd/chat/commands/ChatChannel.java | 46 +++++++++++++++++-- .../alttd/chat/listeners/ChatListener.java | 10 ++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/galaxy/src/main/java/com/alttd/chat/commands/ChatChannel.java b/galaxy/src/main/java/com/alttd/chat/commands/ChatChannel.java index 7c97ce7..49b234f 100644 --- a/galaxy/src/main/java/com/alttd/chat/commands/ChatChannel.java +++ b/galaxy/src/main/java/com/alttd/chat/commands/ChatChannel.java @@ -2,6 +2,8 @@ package com.alttd.chat.commands; import com.alttd.chat.ChatPlugin; import com.alttd.chat.objects.channels.CustomChannel; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; @@ -9,12 +11,14 @@ import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; -import java.util.Collections; +import java.util.*; public class ChatChannel extends BukkitCommand { CustomChannel channel; String command; + private static List activeCommands = new ArrayList<>(); + private HashSet toggledUsers = new HashSet<>(); public ChatChannel(CustomChannel channel) { super(channel.getChannelName().toLowerCase()); @@ -23,6 +27,29 @@ public class ChatChannel extends BukkitCommand { this.description = "Chat channel named " + channel.getChannelName() + "."; this.usageMessage = "/" + command + " "; this.setAliases(Collections.emptyList()); + activeCommands.add(this); + } + + public static ChatChannel getActiveChannel(UUID uuid) { + for (ChatChannel activeCommand : activeCommands) { + if (activeCommand.toggledUsers.contains(uuid)) + return activeCommand; + } + return (null); + } + + private void toggleChannel(UUID uuid) { + if (toggledUsers.contains(uuid)) { + toggledUsers.remove(uuid); + return; + } + ChatChannel activeChannel = getActiveChannel(uuid); + if (activeChannel == null) { + toggledUsers.add(uuid); + return; + } + activeChannel.toggleChannel(uuid); + toggledUsers.add(uuid); } @Override @@ -30,17 +57,28 @@ public class ChatChannel extends BukkitCommand { if(!(sender instanceof Player player)) { // must be a player return true; } - if(args.length == 0) return false; + if(args.length == 0) { + toggleChannel(player.getUniqueId()); + return false; + } String message = StringUtils.join(args, " ", 0, args.length); + sendChannelMessage(message, player); + + return false; + } + + public void sendChannelMessage(Component message, Player player) { + sendChannelMessage(PlainTextComponentSerializer.plainText().serialize(message), player); + } + + public void sendChannelMessage(String message, Player player) { new BukkitRunnable() { @Override public void run() { ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message); } }.runTaskAsynchronously(ChatPlugin.getInstance()); - - return false; } } diff --git a/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java b/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java index b06ad67..405bc9d 100755 --- a/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java +++ b/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java @@ -1,6 +1,7 @@ package com.alttd.chat.listeners; import com.alttd.chat.ChatPlugin; +import com.alttd.chat.commands.ChatChannel; import com.alttd.chat.config.Config; import com.alttd.chat.handler.ChatHandler; import com.alttd.chat.managers.ChatUserManager; @@ -21,13 +22,16 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; -import java.util.HashMap; - public class ChatListener implements Listener, ChatRenderer { @EventHandler(ignoreCancelled = true) public void onPlayerChat(AsyncChatEvent event) { - + ChatChannel activeChannel = ChatChannel.getActiveChannel(event.getPlayer().getUniqueId()); + if (activeChannel != null) { + event.setCancelled(true); + activeChannel.sendChannelMessage(event.message(), event.getPlayer()); + return; + } if (ChatPlugin.getInstance().serverMuted() && !event.getPlayer().hasPermission("chat.bypass-server-muted")) { event.setCancelled(true);