Added a way to toggle custom channels

This commit is contained in:
Teriuihi 2022-05-24 03:06:34 +02:00
parent c645201ef3
commit 60e1ad2220
2 changed files with 49 additions and 7 deletions

View File

@ -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<ChatChannel> activeCommands = new ArrayList<>();
private HashSet<UUID> 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 + " <message>";
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;
}
}

View File

@ -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);