Added a way to toggle custom channels
This commit is contained in:
parent
c645201ef3
commit
60e1ad2220
|
|
@ -2,6 +2,8 @@ package com.alttd.chat.commands;
|
||||||
|
|
||||||
import com.alttd.chat.ChatPlugin;
|
import com.alttd.chat.ChatPlugin;
|
||||||
import com.alttd.chat.objects.channels.CustomChannel;
|
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.apache.commons.lang.StringUtils;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.defaults.BukkitCommand;
|
import org.bukkit.command.defaults.BukkitCommand;
|
||||||
|
|
@ -9,12 +11,14 @@ import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
|
|
||||||
public class ChatChannel extends BukkitCommand {
|
public class ChatChannel extends BukkitCommand {
|
||||||
|
|
||||||
CustomChannel channel;
|
CustomChannel channel;
|
||||||
String command;
|
String command;
|
||||||
|
private static List<ChatChannel> activeCommands = new ArrayList<>();
|
||||||
|
private HashSet<UUID> toggledUsers = new HashSet<>();
|
||||||
|
|
||||||
public ChatChannel(CustomChannel channel) {
|
public ChatChannel(CustomChannel channel) {
|
||||||
super(channel.getChannelName().toLowerCase());
|
super(channel.getChannelName().toLowerCase());
|
||||||
|
|
@ -23,6 +27,29 @@ public class ChatChannel extends BukkitCommand {
|
||||||
this.description = "Chat channel named " + channel.getChannelName() + ".";
|
this.description = "Chat channel named " + channel.getChannelName() + ".";
|
||||||
this.usageMessage = "/" + command + " <message>";
|
this.usageMessage = "/" + command + " <message>";
|
||||||
this.setAliases(Collections.emptyList());
|
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
|
@Override
|
||||||
|
|
@ -30,17 +57,28 @@ public class ChatChannel extends BukkitCommand {
|
||||||
if(!(sender instanceof Player player)) { // must be a player
|
if(!(sender instanceof Player player)) { // must be a player
|
||||||
return true;
|
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);
|
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() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message);
|
ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message);
|
||||||
}
|
}
|
||||||
}.runTaskAsynchronously(ChatPlugin.getInstance());
|
}.runTaskAsynchronously(ChatPlugin.getInstance());
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.chat.listeners;
|
package com.alttd.chat.listeners;
|
||||||
|
|
||||||
import com.alttd.chat.ChatPlugin;
|
import com.alttd.chat.ChatPlugin;
|
||||||
|
import com.alttd.chat.commands.ChatChannel;
|
||||||
import com.alttd.chat.config.Config;
|
import com.alttd.chat.config.Config;
|
||||||
import com.alttd.chat.handler.ChatHandler;
|
import com.alttd.chat.handler.ChatHandler;
|
||||||
import com.alttd.chat.managers.ChatUserManager;
|
import com.alttd.chat.managers.ChatUserManager;
|
||||||
|
|
@ -21,13 +22,16 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class ChatListener implements Listener, ChatRenderer {
|
public class ChatListener implements Listener, ChatRenderer {
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerChat(AsyncChatEvent event) {
|
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")) {
|
if (ChatPlugin.getInstance().serverMuted() && !event.getPlayer().hasPermission("chat.bypass-server-muted")) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user