Added toggle to party as well and made it so it can be added to other channels if needed

This commit is contained in:
Teriuihi 2022-05-24 04:17:55 +02:00
parent 60e1ad2220
commit 41897f4b16
8 changed files with 171 additions and 57 deletions

View File

@ -6,10 +6,11 @@ import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.io.File;
import java.io.IOException;
@ -18,8 +19,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Pattern;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import org.spongepowered.configurate.ConfigurationOptions;
public final class Config {
private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
@ -294,6 +293,7 @@ public final class Config {
Members: <members>""";
public static Component ONLINE_PREFIX = null;
public static Component OFFLINE_PREFIX = null;
public static String PARTY_TOGGLED = "<dark_aqua>Party chat toggled <status>.</dark_aqua>";
private static void party() {
PARTY_FORMAT = getString("party.format", PARTY_FORMAT);
PARTY_SPY = getString("party.spy", PARTY_SPY);
@ -327,6 +327,7 @@ public final class Config {
ALREADY_IN_THIS_PARTY = getString("party.messages.already-in-this-party", ALREADY_IN_THIS_PARTY);
ONLINE_PREFIX = Utility.parseMiniMessage(getString("party.messages.online-prefix", "<green>■ </green>"));
OFFLINE_PREFIX = Utility.parseMiniMessage(getString("party.messages.offline-prefix", "<red>■ </red>"));
PARTY_TOGGLED = getString("party.messages.party-toggled", PARTY_TOGGLED);
}
public static String PARTY_HELP_WRAPPER = "<gold>ChatParty help:\n<commands></gold>";
@ -358,6 +359,7 @@ public final class Config {
PARTY_HELP_CHAT = getString("party.help.chat", PARTY_HELP_CHAT);
}
public static String CUSTOM_CHANNEL_TOGGLED = "<yellow>Toggled <channel> <status>.</yellow>";
private static void chatChannels() {
ConfigurationNode node = getNode("chat-channels");
if (node.empty()) {
@ -375,6 +377,8 @@ public final class Config {
getList(key + "servers", Collections.EMPTY_LIST),
getBoolean(key + "proxy", false));
}
CUSTOM_CHANNEL_TOGGLED = getString("chat-channels-messages.channel-toggled", CUSTOM_CHANNEL_TOGGLED);
}
public static String SERVERMUTEPERMISSION = "chat.command.mute-server";

View File

@ -0,0 +1,62 @@
package com.alttd.chat.objects;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public abstract class Toggleable {
private static final List<Toggleable> togglableClasses = new ArrayList<>();
public Toggleable() {
togglableClasses.add(this);
}
public static Toggleable getToggleable(UUID uuid) {
for (Toggleable toggleableClass : togglableClasses) {
if (toggleableClass.isToggled(uuid))
return toggleableClass;
}
return null;
}
public abstract boolean isToggled(UUID uuid);
public abstract void setOff(UUID uuid);
public boolean toggle(UUID uuid) {
if (isToggled(uuid)) {
setOff(uuid);
return (false);
}
for (Toggleable toggleable : togglableClasses) {
if (toggleable.isToggled(uuid)) {
setOff(uuid);
break;
}
}
setOn(uuid);
return (true);
}
public abstract void setOn(UUID uuid);
public void disableToggles(UUID uuid) {
for (Toggleable toggleable : togglableClasses) {
if (toggleable.isToggled(uuid)) {
setOff(uuid);
}
}
}
public void sendMessage(Player player, Component message) {
sendMessage(player, PlainTextComponentSerializer.plainText().serialize(message));
}
public abstract void sendMessage(Player player, String message);
}

View File

@ -3,7 +3,7 @@ package com.alttd.chat.objects.channels;
import java.util.Collection;
import java.util.HashMap;
public abstract class Channel {
public class Channel {
public static HashMap<String, Channel> channels = new HashMap<>();
protected String permission;

View File

@ -1,6 +1,6 @@
package com.alttd.chat.objects.channels;
public class DefaultChannel extends Channel{
public abstract class DefaultChannel extends Channel{
public DefaultChannel(String channelName, String format, boolean proxy) {
super(channelName, format, proxy);
}

View File

@ -1,14 +1,13 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
import com.alttd.chat.objects.channels.CustomChannel;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@ -17,8 +16,8 @@ public class ChatChannel extends BukkitCommand {
CustomChannel channel;
String command;
ToggleableForCustomChannel toggleableForCustomChannel;
private static List<ChatChannel> activeCommands = new ArrayList<>();
private HashSet<UUID> toggledUsers = new HashSet<>();
public ChatChannel(CustomChannel channel) {
super(channel.getChannelName().toLowerCase());
@ -28,28 +27,7 @@ public class ChatChannel extends BukkitCommand {
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);
this.toggleableForCustomChannel = new ToggleableForCustomChannel(channel);
}
@Override
@ -57,28 +35,19 @@ public class ChatChannel extends BukkitCommand {
if(!(sender instanceof Player player)) { // must be a player
return true;
}
if(args.length == 0) {
toggleChannel(player.getUniqueId());
player.sendMiniMessage(Config.PARTY_TOGGLED, TagResolver.resolver(
Placeholder.unparsed("channel", channel.getChannelName()),
Placeholder.unparsed("status", toggleableForCustomChannel.toggle(player.getUniqueId())
? "<green>on</green>" : "<red>off</red>")));
return false;
}
String message = StringUtils.join(args, " ", 0, args.length);
sendChannelMessage(message, player);
toggleableForCustomChannel.sendMessage(player, message);
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());
}
}

View File

@ -1,10 +1,9 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.minimessage.MiniMessage;
import com.alttd.chat.config.Config;
import com.alttd.chat.objects.Toggleable;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -13,7 +12,12 @@ import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
public class PartyChat implements CommandExecutor {
import java.util.HashSet;
import java.util.UUID;
public class PartyChat extends Toggleable implements CommandExecutor {
private final HashSet<UUID> toggledUsers = new HashSet<>();
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
@ -22,7 +26,8 @@ public class PartyChat implements CommandExecutor {
}
if(args.length == 0) {
// TODO: 08/08/2021 lock into party chat
player.sendMiniMessage(Config.PARTY_TOGGLED, Placeholder.unparsed("status",
toggle(player.getUniqueId()) ? "<green>on</green>" : "<red>off</red>"));
return true;
}
@ -37,4 +42,30 @@ public class PartyChat implements CommandExecutor {
return true;
}
@Override
public boolean isToggled(UUID uuid) {
return toggledUsers.contains(uuid);
}
@Override
public void setOff(UUID uuid) {
disableToggles(uuid);
}
@Override
public void setOn(UUID uuid) {
disableToggles(uuid);
toggledUsers.add(uuid);
}
@Override
public void sendMessage(Player player, String message) {
new BukkitRunnable() {
@Override
public void run() {
ChatPlugin.getInstance().getChatHandler().partyMessage(player, message);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
}

View File

@ -0,0 +1,48 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.objects.Toggleable;
import com.alttd.chat.objects.channels.CustomChannel;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashSet;
import java.util.UUID;
public class ToggleableForCustomChannel extends Toggleable {
private final CustomChannel customChannel;
public ToggleableForCustomChannel(CustomChannel customChannel) {
super();
this.customChannel = customChannel;
}
private final HashSet<UUID> toggledUsers = new HashSet<>();
@Override
public boolean isToggled(UUID uuid) {
return toggledUsers.contains(uuid);
}
@Override
public void setOff(UUID uuid) {
disableToggles(uuid);
}
@Override
public void setOn(UUID uuid) {
disableToggles(uuid);
toggledUsers.add(uuid);
}
@Override
public void sendMessage(Player player, String message) {
new BukkitRunnable() {
@Override
public void run() {
ChatPlugin.getInstance().getChatHandler().chatChannel(player, customChannel, message);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
}

View File

@ -1,13 +1,13 @@
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;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.ModifiableString;
import com.alttd.chat.objects.Toggleable;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.Utility;
import io.papermc.paper.chat.ChatRenderer;
@ -26,10 +26,10 @@ public class ChatListener implements Listener, ChatRenderer {
@EventHandler(ignoreCancelled = true)
public void onPlayerChat(AsyncChatEvent event) {
ChatChannel activeChannel = ChatChannel.getActiveChannel(event.getPlayer().getUniqueId());
if (activeChannel != null) {
Toggleable toggleable = Toggleable.getToggleable(event.getPlayer().getUniqueId());
if (toggleable != null) {
event.setCancelled(true);
activeChannel.sendChannelMessage(event.message(), event.getPlayer());
toggleable.sendMessage(event.getPlayer(), event.message());
return;
}
if (ChatPlugin.getInstance().serverMuted() && !event.getPlayer().hasPermission("chat.bypass-server-muted")) {