From 7efcc383542837e734091c28f4883fba8427498b Mon Sep 17 00:00:00 2001 From: darksaid98 Date: Thu, 21 Mar 2024 23:46:21 +0100 Subject: [PATCH] feat: tag resolvers --- pom.xml | 360 +++++----- .../palmergames/bukkit/TownyChat/Chat.java | 664 +++++++++--------- .../bukkit/TownyChat/TownyChatFormatter.java | 64 +- .../TownyChat/channels/StandardChannel.java | 31 +- .../listener/TownyChatPlayerListener.java | 31 +- 5 files changed, 577 insertions(+), 573 deletions(-) diff --git a/pom.xml b/pom.xml index d05f6e9..3232028 100644 --- a/pom.xml +++ b/pom.xml @@ -1,181 +1,181 @@ - - - 4.0.0 - - com.palmergames.bukkit - TownyChat - jar - 0.112 - - - - CC BY-NC-ND 3.0 - http://creativecommons.org/licenses/by-nc-nd/3.0/ - Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported - - - - - TownyAdvanced - https://github.com/TownyAdvanced - - - - 17 - 1.16 - UTF-8 - 0.100.0.0 - - - - - - github - GitHub TownyAdvanced Apache Maven Packages - https://maven.pkg.github.com/TownyAdvanced - - - - - scm:git:https://github.com/TownyAdvanced/TownyChat.git - scm:git:https://github.com/TownyAdvanced/TownyChat.git - https://github.com/TownyAdvanced/TownyChat - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - papermc - https://repo.papermc.io/repository/maven-public/ - - - essentials-releases - https://repo.essentialsx.net/releases/ - - - dynmap-repo - https://repo.mikeprimm.com - - - glaremasters repo - https://repo.glaremasters.me/repository/towny/ - - - placeholderapi - https://repo.extendedclip.com/content/repositories/placeholderapi/ - - - jitpack.io - https://jitpack.io - - - - - - com.palmergames.bukkit.towny - towny - ${towny.version} - provided - - - org.spigotmc - spigot-api - 1.20.2-R0.1-SNAPSHOT - provided - - - net.essentialsx - EssentialsX - 2.19.0 - provided - - - org.bstats - bstats-bukkit - - - - - us.dynmap - dynmap-api - 3.0-SNAPSHOT - provided - - - me.clip - placeholderapi - 2.11.5 - provided - - - com.github.EssentialsX.Essentials - EssentialsXDiscord - 57600f333c - provided - - - net.dv8tion - JDA - - - - - org.jetbrains - annotations - 16.0.2 - - - com.github.bsideup.jabel - jabel-javac-plugin - 1.0.0 - provided - - - net.kyori - adventure-api - 4.14.0 - - - net.kyori - adventure-text-minimessage - 4.14.0 - - - net.kyori - adventure-text-serializer-legacy - 4.14.0 - - - - - clean package - - - org.apache.maven.plugins - maven-compiler-plugin - 3.11.0 - - 8 - ${java.version} - ${java.version} - - -Xplugin:jabel - - - - - - src - - - resources - true - - - + + + 4.0.0 + + com.palmergames.bukkit + TownyChat + jar + 0.112 + + + + CC BY-NC-ND 3.0 + http://creativecommons.org/licenses/by-nc-nd/3.0/ + Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported + + + + + TownyAdvanced + https://github.com/TownyAdvanced + + + + 17 + 1.16 + UTF-8 + 0.100.0.0 + + + + + + github + GitHub TownyAdvanced Apache Maven Packages + https://maven.pkg.github.com/TownyAdvanced + + + + + scm:git:https://github.com/TownyAdvanced/TownyChat.git + scm:git:https://github.com/TownyAdvanced/TownyChat.git + https://github.com/TownyAdvanced/TownyChat + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + papermc + https://repo.papermc.io/repository/maven-public/ + + + essentials-releases + https://repo.essentialsx.net/releases/ + + + dynmap-repo + https://repo.mikeprimm.com + + + glaremasters repo + https://repo.glaremasters.me/repository/towny/ + + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + + + jitpack.io + https://jitpack.io + + + + + + com.palmergames.bukkit.towny + towny + ${towny.version} + provided + + + org.spigotmc + spigot-api + 1.20.2-R0.1-SNAPSHOT + provided + + + net.essentialsx + EssentialsX + 2.19.0 + provided + + + org.bstats + bstats-bukkit + + + + + us.dynmap + dynmap-api + 3.0-SNAPSHOT + provided + + + me.clip + placeholderapi + 2.11.5 + provided + + + com.github.EssentialsX.Essentials + EssentialsXDiscord + 57600f333c + provided + + + net.dv8tion + JDA + + + + + org.jetbrains + annotations + 16.0.2 + + + com.github.bsideup.jabel + jabel-javac-plugin + 1.0.0 + provided + + + net.kyori + adventure-api + 4.14.0 + + + net.kyori + adventure-text-minimessage + 4.14.0 + + + net.kyori + adventure-text-serializer-legacy + 4.14.0 + + + + + clean package + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 8 + 14 + 14 + + -Xplugin:jabel + + + + + + src + + + resources + true + + + \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/Chat.java b/src/com/palmergames/bukkit/TownyChat/Chat.java index 4785d53..e2ec115 100644 --- a/src/com/palmergames/bukkit/TownyChat/Chat.java +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -1,333 +1,333 @@ -package com.palmergames.bukkit.TownyChat; - -import com.earth2me.essentials.Essentials; -import com.palmergames.adventure.platform.bukkit.BukkitAudiences; -import com.palmergames.bukkit.TownyChat.Command.ChannelCommand; -import com.palmergames.bukkit.TownyChat.Command.TownyChatCommand; -import com.palmergames.bukkit.TownyChat.Command.commandobjects.ChannelJoinAliasCommand; -import com.palmergames.bukkit.TownyChat.channels.Channel; -import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder; -import com.palmergames.bukkit.TownyChat.config.ChatSettings; -import com.palmergames.bukkit.TownyChat.config.ChannelsSettings; -import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener; -import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener; -import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; -import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration; -import com.palmergames.bukkit.towny.Towny; -import com.palmergames.bukkit.towny.scheduling.TaskScheduler; -import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler; -import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler; -import com.palmergames.util.FileMgmt; -import com.palmergames.util.JavaUtil; - -import net.kyori.adventure.text.minimessage.MiniMessage; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandMap; -import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; -import org.checkerframework.checker.nullness.qual.NonNull; -import org.dynmap.DynmapAPI; - -import java.io.File; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -/** - * Chat plugin to manage all Towny chat - * - * Website: http://code.google.com/a/eclipselabs.org/p/towny/ - * - * @author ElgarL - */ - -public class Chat extends JavaPlugin { - - private TownyChatPlayerListener TownyPlayerListener; - private ChannelsHolder channels; - - protected PluginManager pm; - private static Chat chat = null; - private final Object scheduler; - private Towny towny = null; - private DynmapAPI dynMap = null; - private Essentials essentials = null; - private BukkitAudiences adventure = null; - private MiniMessage miniMessage = null; - - private static String requiredTownyVersion = "0.100.0.0"; - public static boolean usingPlaceholderAPI = false; - public static boolean usingEssentialsDiscord = false; - boolean chatConfigError = false; - boolean channelsConfigError = false; - private static ConcurrentMap playerChannelMap; - - public Chat() { - chat = this; - this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null; - } - - @Override - public void onEnable() { - pm = getServer().getPluginManager(); - channels = new ChannelsHolder(this); - playerChannelMap = new ConcurrentHashMap<>(); - adventure = BukkitAudiences.create(this); - miniMessage = MiniMessage.builder().build(); - - checkPlugins(); - if (towny == null || !townyVersionCheck()) { - disableWithMessage("Towny version does not meet required minimum version: " + requiredTownyVersion.toString()); - return; - } else { - getLogger().info("Towny version " + towny.getDescription().getVersion() + " found."); - } - - loadConfigs(); - if (channelsConfigError || chatConfigError) { - disableWithMessage("The config could not be loaded."); - return; - } - - /* - * This executes the task with a 1 tick delay avoiding the bukkit depends bug. - * TODO: What bug is this referencing? This goes back to the first version of TownyChat. - */ - getScheduler().run(new onLoadedTask(this)); - - getCommand("townychat").setExecutor(new TownyChatCommand(this)); - getCommand("channel").setExecutor(new ChannelCommand(this)); - registerObjectCommands(); - } - - private void disableWithMessage(String message) { - getLogger().severe(message); - getLogger().severe("Disabling TownyChat..."); - pm.disablePlugin(this); - } - - private boolean townyVersionCheck() { - try { - return Towny.isTownyVersionSupported(requiredTownyVersion); - } catch (NoSuchMethodError e) { - return false; - } - } - - private void loadConfigs() { - FileMgmt.checkOrCreateFolders(new String[] { getRootPath(), getTownySettingsPath() }); - chatConfigError = !ChatSettings.loadCommentedChatConfig(); - channelsConfigError = !ChannelsSettings.loadCommentedChannelsConfig(); - } - - public static Chat getTownyChat() { - return chat; - } - - public @NonNull BukkitAudiences adventure() { - if(this.adventure == null) { - throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); - } - return this.adventure; - } - - public @NonNull MiniMessage getMiniMessage() { - return miniMessage; - } - - @Override - public void onDisable() { - unregisterPermissions(); - // reset any handles - - if (adventure != null) { - adventure.close(); - adventure = null; - } - miniMessage = null; - dynMap = null; - towny = null; - pm = null; - - channels = null; - playerChannelMap = null; - } - - /** - * Perform a reload of this plugin. - */ - public void reload() { - onDisable(); - onEnable(); - } - - /** - * Attempt to hook any plugins we want to access. - */ - private void checkPlugins() { - Plugin test; - - test = pm.getPlugin("Towny"); - if (test != null && test instanceof Towny) - towny = (Towny) test; - - test = pm.getPlugin("dynmap"); - if (test != null && pm.getPlugin("dynmap").isEnabled()) { - dynMap = (DynmapAPI) test; - } - - test = pm.getPlugin("PlaceholderAPI"); - if (test != null) { - usingPlaceholderAPI = true; - } - - test = pm.getPlugin("EssentialsDiscord"); - if (test != null) { - usingEssentialsDiscord = true; - } - - test = pm.getPlugin("Essentials"); - if (test != null && JavaUtil.classExists("com.earth2me.essentials.Essentials")) { - this.essentials = (Essentials) test; - } - - } - - public void registerEvents() { - - if (TownyPlayerListener == null) { - TownyPlayerListener = new TownyChatPlayerListener(this); - - if (TownyPlayerListener != null) - pm.registerEvents(TownyPlayerListener, this); - } - - if (usingEssentialsDiscord) { - pm.registerEvents(new EssentialsDiscordHookListener(this), this); - } - } - - public void registerPermissions() { - // Register all Permissions. - for (String perm : getChannelsHandler().getAllPermissions()) { - try { - pm.addPermission(new Permission(perm, new HashMap())); - } catch (IllegalArgumentException e) { - //permission already registered. - } - } - } - - public void unregisterPermissions() { - // Register all Permissions. - for (String perm : getChannelsHandler().getAllPermissions()) { - pm.removePermission(new Permission(perm, new HashMap())); - } - } - - - public String getRootPath() { - return getTowny().getDataFolder().getPath(); - } - - public String getTownySettingsPath() { - return getRootPath() + File.separator + "settings"; - } - - public String getChatConfigPath() { - return getTownySettingsPath() + File.separator + "ChatConfig.yml"; - } - - public String getChannelsConfigPath() { - return getTownySettingsPath() + File.separator + "Channels.yml"; - } - - /** - * @return the channels - */ - public ChannelsHolder getChannelsHandler() { - return channels; - } - - public Towny getTowny() { - return towny; - } - - public DynmapAPI getDynmap() { - return dynMap; - } - - public boolean isUsingEssentials() { - return essentials != null; - } - - public Essentials getEssentials() { - return essentials; - } - - public boolean isEssentialsMuted(Player player) { - if (!isUsingEssentials()) - return false; - return EssentialsIntegration.isMuted(player); - } - - public boolean isIgnoredByEssentials(Player sender, Player player) { - if (!isUsingEssentials()) - return false; - return EssentialsIntegration.ignoredByEssentials(sender, player); - } - - private void registerObjectCommands() { - List commands = new ArrayList(); - for (Channel channel : channels.getAllChannels().values()) { // All channels - for (String cmd : channel.getCommands()) { // All Commands of All Channels - commands.add(new ChannelJoinAliasCommand(cmd, channel, this)); - } - } - try { - final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); - - bukkitCommandMap.setAccessible(true); - CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); - - commandMap.registerAll("TownyChat", commands); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - - public TownyChatPlayerListener getTownyPlayerListener() { - return TownyPlayerListener; - } - - public Channel getPlayerChannel(Player player) { - return playerChannelMap.get(player.getUniqueId()); - } - - public void setPlayerChannel(Player player, Channel channel) { - playerChannelMap.put(player.getUniqueId(), channel); - } - - public TaskScheduler getScheduler() { - return (TaskScheduler) this.scheduler; - } - - private static boolean isFoliaClassPresent() { - try { - Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } +package com.palmergames.bukkit.TownyChat; + +import com.earth2me.essentials.Essentials; +import com.palmergames.adventure.platform.bukkit.BukkitAudiences; +import com.palmergames.bukkit.TownyChat.Command.ChannelCommand; +import com.palmergames.bukkit.TownyChat.Command.TownyChatCommand; +import com.palmergames.bukkit.TownyChat.Command.commandobjects.ChannelJoinAliasCommand; +import com.palmergames.bukkit.TownyChat.channels.Channel; +import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder; +import com.palmergames.bukkit.TownyChat.config.ChatSettings; +import com.palmergames.bukkit.TownyChat.config.ChannelsSettings; +import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener; +import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener; +import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; +import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.scheduling.TaskScheduler; +import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler; +import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler; +import com.palmergames.util.FileMgmt; +import com.palmergames.util.JavaUtil; + +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permission; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.dynmap.DynmapAPI; + +import java.io.File; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Chat plugin to manage all Towny chat + * + * Website: http://code.google.com/a/eclipselabs.org/p/towny/ + * + * @author ElgarL + */ + +public class Chat extends JavaPlugin { + + private TownyChatPlayerListener TownyPlayerListener; + private ChannelsHolder channels; + + protected PluginManager pm; + private static Chat chat = null; + private final Object scheduler; + private Towny towny = null; + private DynmapAPI dynMap = null; + private Essentials essentials = null; + private BukkitAudiences adventure = null; + private MiniMessage miniMessage = null; + + private static String requiredTownyVersion = "0.98.0.0"; + public static boolean usingPlaceholderAPI = false; + public static boolean usingEssentialsDiscord = false; + boolean chatConfigError = false; + boolean channelsConfigError = false; + private static ConcurrentMap playerChannelMap; + + public Chat() { + chat = this; + this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null; + } + + @Override + public void onEnable() { + pm = getServer().getPluginManager(); + channels = new ChannelsHolder(this); + playerChannelMap = new ConcurrentHashMap<>(); + adventure = BukkitAudiences.create(this); + miniMessage = MiniMessage.builder().build(); + + checkPlugins(); + if (towny == null || !townyVersionCheck()) { + disableWithMessage("Towny version does not meet required minimum version: " + requiredTownyVersion.toString()); + return; + } else { + getLogger().info("Towny version " + towny.getDescription().getVersion() + " found."); + } + + loadConfigs(); + if (channelsConfigError || chatConfigError) { + disableWithMessage("The config could not be loaded."); + return; + } + + /* + * This executes the task with a 1 tick delay avoiding the bukkit depends bug. + * TODO: What bug is this referencing? This goes back to the first version of TownyChat. + */ + getScheduler().run(new onLoadedTask(this)); + + getCommand("townychat").setExecutor(new TownyChatCommand(this)); + getCommand("channel").setExecutor(new ChannelCommand(this)); + registerObjectCommands(); + } + + private void disableWithMessage(String message) { + getLogger().severe(message); + getLogger().severe("Disabling TownyChat..."); + pm.disablePlugin(this); + } + + private boolean townyVersionCheck() { + try { + return Towny.isTownyVersionSupported(requiredTownyVersion); + } catch (NoSuchMethodError e) { + return false; + } + } + + private void loadConfigs() { + FileMgmt.checkOrCreateFolders(new String[] { getRootPath(), getTownySettingsPath() }); + chatConfigError = !ChatSettings.loadCommentedChatConfig(); + channelsConfigError = !ChannelsSettings.loadCommentedChannelsConfig(); + } + + public static Chat getTownyChat() { + return chat; + } + + public @NonNull BukkitAudiences adventure() { + if(this.adventure == null) { + throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); + } + return this.adventure; + } + + public @NonNull MiniMessage getMiniMessage() { + return miniMessage; + } + + @Override + public void onDisable() { + unregisterPermissions(); + // reset any handles + + if (adventure != null) { + adventure.close(); + adventure = null; + } + miniMessage = null; + dynMap = null; + towny = null; + pm = null; + + channels = null; + playerChannelMap = null; + } + + /** + * Perform a reload of this plugin. + */ + public void reload() { + onDisable(); + onEnable(); + } + + /** + * Attempt to hook any plugins we want to access. + */ + private void checkPlugins() { + Plugin test; + + test = pm.getPlugin("Towny"); + if (test != null && test instanceof Towny) + towny = (Towny) test; + + test = pm.getPlugin("dynmap"); + if (test != null && pm.getPlugin("dynmap").isEnabled()) { + dynMap = (DynmapAPI) test; + } + + test = pm.getPlugin("PlaceholderAPI"); + if (test != null) { + usingPlaceholderAPI = true; + } + + test = pm.getPlugin("EssentialsDiscord"); + if (test != null) { + usingEssentialsDiscord = true; + } + + test = pm.getPlugin("Essentials"); + if (test != null && JavaUtil.classExists("com.earth2me.essentials.Essentials")) { + this.essentials = (Essentials) test; + } + + } + + public void registerEvents() { + + if (TownyPlayerListener == null) { + TownyPlayerListener = new TownyChatPlayerListener(this); + + if (TownyPlayerListener != null) + pm.registerEvents(TownyPlayerListener, this); + } + + if (usingEssentialsDiscord) { + pm.registerEvents(new EssentialsDiscordHookListener(this), this); + } + } + + public void registerPermissions() { + // Register all Permissions. + for (String perm : getChannelsHandler().getAllPermissions()) { + try { + pm.addPermission(new Permission(perm, new HashMap())); + } catch (IllegalArgumentException e) { + //permission already registered. + } + } + } + + public void unregisterPermissions() { + // Register all Permissions. + for (String perm : getChannelsHandler().getAllPermissions()) { + pm.removePermission(new Permission(perm, new HashMap())); + } + } + + + public String getRootPath() { + return getTowny().getDataFolder().getPath(); + } + + public String getTownySettingsPath() { + return getRootPath() + File.separator + "settings"; + } + + public String getChatConfigPath() { + return getTownySettingsPath() + File.separator + "ChatConfig.yml"; + } + + public String getChannelsConfigPath() { + return getTownySettingsPath() + File.separator + "Channels.yml"; + } + + /** + * @return the channels + */ + public ChannelsHolder getChannelsHandler() { + return channels; + } + + public Towny getTowny() { + return towny; + } + + public DynmapAPI getDynmap() { + return dynMap; + } + + public boolean isUsingEssentials() { + return essentials != null; + } + + public Essentials getEssentials() { + return essentials; + } + + public boolean isEssentialsMuted(Player player) { + if (!isUsingEssentials()) + return false; + return EssentialsIntegration.isMuted(player); + } + + public boolean isIgnoredByEssentials(Player sender, Player player) { + if (!isUsingEssentials()) + return false; + return EssentialsIntegration.ignoredByEssentials(sender, player); + } + + private void registerObjectCommands() { + List commands = new ArrayList(); + for (Channel channel : channels.getAllChannels().values()) { // All channels + for (String cmd : channel.getCommands()) { // All Commands of All Channels + commands.add(new ChannelJoinAliasCommand(cmd, channel, this)); + } + } + try { + final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); + + bukkitCommandMap.setAccessible(true); + CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); + + commandMap.registerAll("TownyChat", commands); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + public TownyChatPlayerListener getTownyPlayerListener() { + return TownyPlayerListener; + } + + public Channel getPlayerChannel(Player player) { + return playerChannelMap.get(player.getUniqueId()); + } + + public void setPlayerChannel(Player player, Channel channel) { + playerChannelMap.put(player.getUniqueId(), channel); + } + + public TaskScheduler getScheduler() { + return (TaskScheduler) this.scheduler; + } + + private static boolean isFoliaClassPresent() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } } \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java index 002fd36..bbba59a 100644 --- a/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java +++ b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java @@ -1,62 +1,23 @@ package com.palmergames.bukkit.TownyChat; -import me.clip.placeholderapi.PlaceholderAPI; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.Context; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.minimessage.tag.Tag; -import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import org.bukkit.entity.Player; - import com.palmergames.bukkit.TownyChat.config.ChatSettings; import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent; -import com.palmergames.bukkit.TownyChat.util.StringReplaceManager; import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.towny.object.Government; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; -import com.palmergames.bukkit.util.Colors; import com.palmergames.util.StringMgmt; -import com.palmergames.bukkit.towny.TownyUniverse; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; +import org.bukkit.entity.Player; public class TownyChatFormatter { - private static StringReplaceManager replacer = new StringReplaceManager(); - - static { - replacer.registerReplacer("{worldname}", event -> getWorldTag(event)); - replacer.registerReplacer("{town}", event -> getTownName(event.getResident())); - replacer.registerReplacer("{townformatted}", event -> formatTownTag(event.getResident(), false, true)); - replacer.registerReplacer("{towntag}", event -> formatTownTag(event.getResident(), false, false)); - replacer.registerReplacer("{towntagoverride}", event -> formatTownTag(event.getResident(), true, false)); - replacer.registerReplacer("{nation}", event -> getNationName(event.getResident())); - replacer.registerReplacer("{nationformatted}", event -> formatNationTag(event.getResident(), false, true)); - replacer.registerReplacer("{nationtag}", event -> formatNationTag(event.getResident(), false, false)); - replacer.registerReplacer("{nationtagoverride}", event -> formatNationTag(event.getResident(), true, false)); - replacer.registerReplacer("{townytag}", event -> formatTownyTag(event.getResident(), false, false)); - replacer.registerReplacer("{townyformatted}", event -> formatTownyTag(event.getResident(), false, true)); - replacer.registerReplacer("{townytagoverride}", event -> formatTownyTag(event.getResident(), true, false)); - replacer.registerReplacer("{title}", event -> getPrefix(event.getResident(), true)); - replacer.registerReplacer("{surname}", event -> getSuffix(event.getResident(), false)); - replacer.registerReplacer("{townynameprefix}", event -> getNamePrefix(event.getResident())); - replacer.registerReplacer("{townynamepostfix}", event -> getNamePostfix(event.getResident())); - replacer.registerReplacer("{townyprefix}", event -> getPrefix(event.getResident(), false)); - replacer.registerReplacer("{townypostfix}", event -> getSuffix(event.getResident(), false)); - replacer.registerReplacer("{townycolor}", event -> getTownyColour(event.getResident())); - replacer.registerReplacer("{group}", event -> getVaultGroup(event.getEvent().getPlayer())); - replacer.registerReplacer("{permprefix}", event -> getVaultPrefixSuffix(event.getResident(), "prefix")); - replacer.registerReplacer("{permsuffix}", event -> getVaultPrefixSuffix(event.getResident(), "suffix")); - replacer.registerReplacer("{permuserprefix}", event -> getVaultPrefixSuffix(event.getResident(), "userprefix")); - replacer.registerReplacer("{permusersuffix}", event -> getVaultPrefixSuffix(event.getResident(), "usersuffix")); - replacer.registerReplacer("{permgroupprefix}", event -> getVaultPrefixSuffix(event.getResident(), "groupprefix")); - replacer.registerReplacer("{permgroupsuffix}", event -> getVaultPrefixSuffix(event.getResident(), "groupsuffix")); - replacer.registerReplacer("{playername}", event -> event.getEvent().getPlayer().getName()); - replacer.registerReplacer("{primaryresidentrank}", event -> getPrimaryRankPrefix(event.getResident())); - } static TagResolver parser(LocalTownyChatEvent event) { return TagResolver.resolver( TagResolver.resolver("worldname", Tag.inserting(Component.text(getWorldTag(event)))), @@ -101,12 +62,11 @@ public class TownyChatFormatter { return miniMessage.deserialize(message); } - public static String getChatFormat(LocalTownyChatEvent event) { - String out = replacer.replaceAll(event.getFormat(), event) // Replace the townychat tags with their values. - .replace("%", "") // Prevent user-inputted tags from breaking chat format. - .replace("{modplayername}", "%1$s") // Other plugins will replace this with the DisplayName. - .replace("{msg}", "%2$s"); // Replace the {msg} here so it's not regex parsed. - return Colors.translateColorCodes(out); + public static TagResolver getChatFormat(LocalTownyChatEvent event) { + return TagResolver.builder() + .resolver(parser(event)) + .resolver(StandardTags.defaults()) + .build(); } public static String getWorldTag(LocalTownyChatEvent event) { diff --git a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java index ddf96d1..e8c9312 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/StandardChannel.java @@ -15,6 +15,12 @@ import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.util.Colors; import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Sound; @@ -196,17 +202,24 @@ public class StandardChannel extends Channel { } private void applyFormats(AsyncPlayerChatEvent event, String originalFormat, String workingFormat, Resident resident) { + List tagResolvers = new ArrayList<>(); + // Parse out our own channelTag and msgcolour tags. - String newFormat = parseTagAndMsgColour(workingFormat); + tagResolvers = parseTagAndMsgColour(tagResolvers); // Attempt to apply the new format. - catchFormatConversionException(event, originalFormat, newFormat); +// catchFormatConversionException(event, originalFormat, newFormat); // Fire the LocalTownyChatEvent. LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident); // Format the chat line, replacing the TownyChat chat tags. - newFormat = TownyChatFormatter.getChatFormat(chatEvent); + tagResolvers.add(TownyChatFormatter.getChatFormat(chatEvent)); +// newFormat = TownyChatFormatter.getChatFormat(chatEvent); // Attempt to apply the new format. - catchFormatConversionException(event, originalFormat, newFormat); +// catchFormatConversionException(event, originalFormat, newFormat); + Component component = Chat.getTownyChat().getMiniMessage().deserialize(workingFormat, tagResolvers.toArray(new TagResolver[0])); + Audience.audience(event.getRecipients().stream().map(p -> Chat.getTownyChat().adventure().player(p)).toArray(Audience[]::new)).sendMessage(component);; + + // event.setFormat(GsonComponentSerializer.gson().serialize(component)); } private String parseTagAndMsgColour(String format) { @@ -215,6 +228,16 @@ public class StandardChannel extends Channel { .replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : "")); } + private List parseTagAndMsgColour(List tagResolvers) { + tagResolvers.add( + TagResolver.resolver( + TagResolver.resolver("channeltag", Tag.inserting(getChannelTag() != null ? Component.text(getChannelTag()) : Component.empty())), + TagResolver.resolver("msgcolour", Tag.inserting(getMessageColour() != null ? Component.text(getMessageColour()) : Component.empty())) + ) + ); + return tagResolvers; + } + private void catchFormatConversionException(AsyncPlayerChatEvent event, String format, String newFormat) { try { event.setFormat(newFormat); diff --git a/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java index 7c6e9df..374c016 100644 --- a/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java +++ b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java @@ -13,6 +13,12 @@ import com.palmergames.bukkit.towny.object.metadata.StringDataField; import com.palmergames.bukkit.towny.utils.MetaDataUtil; import com.palmergames.bukkit.util.Colors; import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.Audiences; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -21,8 +27,11 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.util.ArrayList; +import java.util.List; import java.util.UnknownFormatConversionException; import java.util.WeakHashMap; +import java.util.stream.Collectors; public class TownyChatPlayerListener implements Listener { private Chat plugin; @@ -127,9 +136,16 @@ public class TownyChatPlayerListener implements Listener { if (resident == null) return; // Nuke the channeltag and message colouring, but apply the remaining format. - String format = plugin.getPlayerChannel(player).getFormat(player).replace("{channelTag}", "").replace("{msgcolour}", ""); + String format = plugin.getPlayerChannel(player).getFormat(player); // format is left to store the original non-PAPI-parsed chat format. + List tagResolvers = new ArrayList<>(); + tagResolvers.add( + TagResolver.resolver( + TagResolver.resolver("channeltag", Tag.inserting(Component.empty())), + TagResolver.resolver("msgcolour", Tag.inserting(Component.empty())) + ) + ); String newFormat = format; // Parse any PAPI placeholders. @@ -137,19 +153,24 @@ public class TownyChatPlayerListener implements Listener { newFormat = PlaceholderAPI.setPlaceholders(player, format); // Attempt to apply the new format. - catchFormatConversionException(event, format, newFormat); +// catchFormatConversionException(event, format, newFormat); // Fire the LocalTownyChatEvent. LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident); // Format the chat line, replacing the TownyChat chat tags. - newFormat = TownyChatFormatter.getChatFormat(chatEvent); + tagResolvers.add(TownyChatFormatter.getChatFormat(chatEvent)); +// newFormat = TownyChatFormatter.getChatFormat(chatEvent); // Attempt to apply the new format. - catchFormatConversionException(event, format, newFormat); +// catchFormatConversionException(event, format, newFormat); // Set the format based on the global channelformat, with channeltag and msgcolour removed. - event.setFormat(newFormat); +// event.setFormat(newFormat); + + Component component = Chat.getTownyChat().getMiniMessage().deserialize(newFormat, tagResolvers.toArray(new TagResolver[0])); + Audience.audience(event.getRecipients().stream().map(p -> Chat.getTownyChat().adventure().player(p)).toArray(Audience[]::new)).sendMessage(component);; +// event.setFormat(GsonComponentSerializer.gson().serialize(component)); } }