Chat/galaxy/src/main/java/com/alttd/chat/commands/ChatChannel.java

85 lines
2.8 KiB
Java
Raw Normal View History

2021-08-04 13:46:45 +00:00
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.objects.channels.CustomChannel;
2022-05-24 01:06:34 +00:00
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
2021-08-04 13:46:45 +00:00
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
2021-08-05 09:40:42 +00:00
import org.bukkit.command.defaults.BukkitCommand;
2021-08-04 13:46:45 +00:00
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
2021-08-05 09:40:42 +00:00
import org.jetbrains.annotations.NotNull;
2022-05-24 01:06:34 +00:00
import java.util.*;
2021-08-05 09:40:42 +00:00
public class ChatChannel extends BukkitCommand {
CustomChannel channel;
2021-08-05 09:40:42 +00:00
String command;
2022-05-24 01:06:34 +00:00
private static List<ChatChannel> activeCommands = new ArrayList<>();
private HashSet<UUID> toggledUsers = new HashSet<>();
2021-08-05 09:40:42 +00:00
public ChatChannel(CustomChannel channel) {
2021-08-05 09:40:42 +00:00
super(channel.getChannelName().toLowerCase());
this.channel = channel;
this.command = channel.getChannelName().toLowerCase();
this.description = "Chat channel named " + channel.getChannelName() + ".";
this.usageMessage = "/" + command + " <message>";
this.setAliases(Collections.emptyList());
2022-05-24 01:06:34 +00:00
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);
2021-08-05 09:40:42 +00:00
}
2021-08-04 13:46:45 +00:00
@Override
2021-08-05 09:40:42 +00:00
public boolean execute(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
2021-08-04 13:46:45 +00:00
if(!(sender instanceof Player player)) { // must be a player
return true;
}
2022-05-24 01:06:34 +00:00
if(args.length == 0) {
toggleChannel(player.getUniqueId());
return false;
}
2021-08-04 13:46:45 +00:00
String message = StringUtils.join(args, " ", 0, args.length);
2022-05-24 01:06:34 +00:00
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) {
2021-08-04 13:46:45 +00:00
new BukkitRunnable() {
@Override
public void run() {
2021-08-05 09:40:42 +00:00
ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message);
2021-08-04 13:46:45 +00:00
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
}