feat: tag resolvers

This commit is contained in:
darksaid98 committed 2024-03-21 23:46:21 +01:00
1 parent 81acd3c6e3
commit 7efcc38354
5 files changed
+577 -573

No files matched your search

+180 -180
View File
@@ -1,181 +1,181 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.palmergames.bukkit</groupId> <groupId>com.palmergames.bukkit</groupId>
<artifactId>TownyChat</artifactId> <artifactId>TownyChat</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>0.112</version> <version>0.112</version>
<licenses> <licenses>
<license> <license>
<name>CC BY-NC-ND 3.0</name> <name>CC BY-NC-ND 3.0</name>
<url>http://creativecommons.org/licenses/by-nc-nd/3.0/</url> <url>http://creativecommons.org/licenses/by-nc-nd/3.0/</url>
<comments>Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported</comments> <comments>Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported</comments>
</license> </license>
</licenses> </licenses>
<organization> <organization>
<name>TownyAdvanced</name> <name>TownyAdvanced</name>
<url>https://github.com/TownyAdvanced</url> <url>https://github.com/TownyAdvanced</url>
</organization> </organization>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<project.bukkitAPIVersion>1.16</project.bukkitAPIVersion> <project.bukkitAPIVersion>1.16</project.bukkitAPIVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<towny.version>0.100.0.0</towny.version> <towny.version>0.100.0.0</towny.version>
</properties> </properties>
<!-- For use with GitHub Package Registry --> <!-- For use with GitHub Package Registry -->
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>github</id> <id>github</id>
<name>GitHub TownyAdvanced Apache Maven Packages</name> <name>GitHub TownyAdvanced Apache Maven Packages</name>
<url>https://maven.pkg.github.com/TownyAdvanced</url> <url>https://maven.pkg.github.com/TownyAdvanced</url>
</repository> </repository>
</distributionManagement> </distributionManagement>
<scm> <scm>
<connection>scm:git:https://github.com/TownyAdvanced/TownyChat.git</connection> <connection>scm:git:https://github.com/TownyAdvanced/TownyChat.git</connection>
<developerConnection>scm:git:https://github.com/TownyAdvanced/TownyChat.git</developerConnection> <developerConnection>scm:git:https://github.com/TownyAdvanced/TownyChat.git</developerConnection>
<url>https://github.com/TownyAdvanced/TownyChat</url> <url>https://github.com/TownyAdvanced/TownyChat</url>
</scm> </scm>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository> <repository>
<id>papermc</id> <id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url> <url>https://repo.papermc.io/repository/maven-public/</url>
</repository> </repository>
<repository> <repository>
<id>essentials-releases</id> <id>essentials-releases</id>
<url>https://repo.essentialsx.net/releases/</url> <url>https://repo.essentialsx.net/releases/</url>
</repository> </repository>
<repository> <repository>
<id>dynmap-repo</id> <id>dynmap-repo</id>
<url>https://repo.mikeprimm.com</url> <url>https://repo.mikeprimm.com</url>
</repository> </repository>
<repository> <repository>
<id>glaremasters repo</id> <id>glaremasters repo</id>
<url>https://repo.glaremasters.me/repository/towny/</url> <url>https://repo.glaremasters.me/repository/towny/</url>
</repository> </repository>
<repository> <repository>
<id>placeholderapi</id> <id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url> <url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository> </repository>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.palmergames.bukkit.towny</groupId> <groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>towny</artifactId> <artifactId>towny</artifactId>
<version>${towny.version}</version> <version>${towny.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version> <version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.essentialsx</groupId> <groupId>net.essentialsx</groupId>
<artifactId>EssentialsX</artifactId> <artifactId>EssentialsX</artifactId>
<version>2.19.0</version> <version>2.19.0</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.bstats</groupId> <groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId> <artifactId>bstats-bukkit</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>us.dynmap</groupId> <groupId>us.dynmap</groupId>
<artifactId>dynmap-api</artifactId> <artifactId>dynmap-api</artifactId>
<version>3.0-SNAPSHOT</version> <version>3.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId> <artifactId>placeholderapi</artifactId>
<version>2.11.5</version> <version>2.11.5</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.EssentialsX.Essentials</groupId> <groupId>com.github.EssentialsX.Essentials</groupId>
<artifactId>EssentialsXDiscord</artifactId> <artifactId>EssentialsXDiscord</artifactId>
<version>57600f333c</version> <version>57600f333c</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>net.dv8tion</groupId> <groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId> <artifactId>JDA</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>16.0.2</version> <version>16.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.bsideup.jabel</groupId> <groupId>com.github.bsideup.jabel</groupId>
<artifactId>jabel-javac-plugin</artifactId> <artifactId>jabel-javac-plugin</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId> <artifactId>adventure-api</artifactId>
<version>4.14.0</version> <version>4.14.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId> <artifactId>adventure-text-minimessage</artifactId>
<version>4.14.0</version> <version>4.14.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId> <artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.14.0</version> <version>4.14.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version> <version>3.11.0</version>
<configuration> <configuration>
<release>8</release> <release>8</release>
<source>${java.version}</source> <source>14</source>
<target>${java.version}</target> <target>14</target>
<compilerArgs> <compilerArgs>
<arg>-Xplugin:jabel</arg> <arg>-Xplugin:jabel</arg>
</compilerArgs> </compilerArgs>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<resources> <resources>
<resource> <resource>
<directory>resources</directory> <directory>resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
</build> </build>
</project> </project>
+332 -332
View File
@@ -1,333 +1,333 @@
package com.palmergames.bukkit.TownyChat; package com.palmergames.bukkit.TownyChat;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.palmergames.adventure.platform.bukkit.BukkitAudiences; import com.palmergames.adventure.platform.bukkit.BukkitAudiences;
import com.palmergames.bukkit.TownyChat.Command.ChannelCommand; import com.palmergames.bukkit.TownyChat.Command.ChannelCommand;
import com.palmergames.bukkit.TownyChat.Command.TownyChatCommand; import com.palmergames.bukkit.TownyChat.Command.TownyChatCommand;
import com.palmergames.bukkit.TownyChat.Command.commandobjects.ChannelJoinAliasCommand; import com.palmergames.bukkit.TownyChat.Command.commandobjects.ChannelJoinAliasCommand;
import com.palmergames.bukkit.TownyChat.channels.Channel; import com.palmergames.bukkit.TownyChat.channels.Channel;
import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder; import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder;
import com.palmergames.bukkit.TownyChat.config.ChatSettings; import com.palmergames.bukkit.TownyChat.config.ChatSettings;
import com.palmergames.bukkit.TownyChat.config.ChannelsSettings; import com.palmergames.bukkit.TownyChat.config.ChannelsSettings;
import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener; import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener;
import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener; import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener;
import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask;
import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration; import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration;
import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.scheduling.TaskScheduler; import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler; import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler; import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler;
import com.palmergames.util.FileMgmt; import com.palmergames.util.FileMgmt;
import com.palmergames.util.JavaUtil; import com.palmergames.util.JavaUtil;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.dynmap.DynmapAPI; import org.dynmap.DynmapAPI;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
/** /**
* Chat plugin to manage all Towny chat * Chat plugin to manage all Towny chat
* *
* Website: http://code.google.com/a/eclipselabs.org/p/towny/ * Website: http://code.google.com/a/eclipselabs.org/p/towny/
* *
* @author ElgarL * @author ElgarL
*/ */
public class Chat extends JavaPlugin { public class Chat extends JavaPlugin {
private TownyChatPlayerListener TownyPlayerListener; private TownyChatPlayerListener TownyPlayerListener;
private ChannelsHolder channels; private ChannelsHolder channels;
protected PluginManager pm; protected PluginManager pm;
private static Chat chat = null; private static Chat chat = null;
private final Object scheduler; private final Object scheduler;
private Towny towny = null; private Towny towny = null;
private DynmapAPI dynMap = null; private DynmapAPI dynMap = null;
private Essentials essentials = null; private Essentials essentials = null;
private BukkitAudiences adventure = null; private BukkitAudiences adventure = null;
private MiniMessage miniMessage = null; private MiniMessage miniMessage = null;
private static String requiredTownyVersion = "0.100.0.0"; private static String requiredTownyVersion = "0.98.0.0";
public static boolean usingPlaceholderAPI = false; public static boolean usingPlaceholderAPI = false;
public static boolean usingEssentialsDiscord = false; public static boolean usingEssentialsDiscord = false;
boolean chatConfigError = false; boolean chatConfigError = false;
boolean channelsConfigError = false; boolean channelsConfigError = false;
private static ConcurrentMap<UUID, Channel> playerChannelMap; private static ConcurrentMap<UUID, Channel> playerChannelMap;
public Chat() { public Chat() {
chat = this; chat = this;
this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null; this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null;
} }
@Override @Override
public void onEnable() { public void onEnable() {
pm = getServer().getPluginManager(); pm = getServer().getPluginManager();
channels = new ChannelsHolder(this); channels = new ChannelsHolder(this);
playerChannelMap = new ConcurrentHashMap<>(); playerChannelMap = new ConcurrentHashMap<>();
adventure = BukkitAudiences.create(this); adventure = BukkitAudiences.create(this);
miniMessage = MiniMessage.builder().build(); miniMessage = MiniMessage.builder().build();
checkPlugins(); checkPlugins();
if (towny == null || !townyVersionCheck()) { if (towny == null || !townyVersionCheck()) {
disableWithMessage("Towny version does not meet required minimum version: " + requiredTownyVersion.toString()); disableWithMessage("Towny version does not meet required minimum version: " + requiredTownyVersion.toString());
return; return;
} else { } else {
getLogger().info("Towny version " + towny.getDescription().getVersion() + " found."); getLogger().info("Towny version " + towny.getDescription().getVersion() + " found.");
} }
loadConfigs(); loadConfigs();
if (channelsConfigError || chatConfigError) { if (channelsConfigError || chatConfigError) {
disableWithMessage("The config could not be loaded."); disableWithMessage("The config could not be loaded.");
return; return;
} }
/* /*
* This executes the task with a 1 tick delay avoiding the bukkit depends bug. * 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. * TODO: What bug is this referencing? This goes back to the first version of TownyChat.
*/ */
getScheduler().run(new onLoadedTask(this)); getScheduler().run(new onLoadedTask(this));
getCommand("townychat").setExecutor(new TownyChatCommand(this)); getCommand("townychat").setExecutor(new TownyChatCommand(this));
getCommand("channel").setExecutor(new ChannelCommand(this)); getCommand("channel").setExecutor(new ChannelCommand(this));
registerObjectCommands(); registerObjectCommands();
} }
private void disableWithMessage(String message) { private void disableWithMessage(String message) {
getLogger().severe(message); getLogger().severe(message);
getLogger().severe("Disabling TownyChat..."); getLogger().severe("Disabling TownyChat...");
pm.disablePlugin(this); pm.disablePlugin(this);
} }
private boolean townyVersionCheck() { private boolean townyVersionCheck() {
try { try {
return Towny.isTownyVersionSupported(requiredTownyVersion); return Towny.isTownyVersionSupported(requiredTownyVersion);
} catch (NoSuchMethodError e) { } catch (NoSuchMethodError e) {
return false; return false;
} }
} }
private void loadConfigs() { private void loadConfigs() {
FileMgmt.checkOrCreateFolders(new String[] { getRootPath(), getTownySettingsPath() }); FileMgmt.checkOrCreateFolders(new String[] { getRootPath(), getTownySettingsPath() });
chatConfigError = !ChatSettings.loadCommentedChatConfig(); chatConfigError = !ChatSettings.loadCommentedChatConfig();
channelsConfigError = !ChannelsSettings.loadCommentedChannelsConfig(); channelsConfigError = !ChannelsSettings.loadCommentedChannelsConfig();
} }
public static Chat getTownyChat() { public static Chat getTownyChat() {
return chat; return chat;
} }
public @NonNull BukkitAudiences adventure() { public @NonNull BukkitAudiences adventure() {
if(this.adventure == null) { if(this.adventure == null) {
throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!");
} }
return this.adventure; return this.adventure;
} }
public @NonNull MiniMessage getMiniMessage() { public @NonNull MiniMessage getMiniMessage() {
return miniMessage; return miniMessage;
} }
@Override @Override
public void onDisable() { public void onDisable() {
unregisterPermissions(); unregisterPermissions();
// reset any handles // reset any handles
if (adventure != null) { if (adventure != null) {
adventure.close(); adventure.close();
adventure = null; adventure = null;
} }
miniMessage = null; miniMessage = null;
dynMap = null; dynMap = null;
towny = null; towny = null;
pm = null; pm = null;
channels = null; channels = null;
playerChannelMap = null; playerChannelMap = null;
} }
/** /**
* Perform a reload of this plugin. * Perform a reload of this plugin.
*/ */
public void reload() { public void reload() {
onDisable(); onDisable();
onEnable(); onEnable();
} }
/** /**
* Attempt to hook any plugins we want to access. * Attempt to hook any plugins we want to access.
*/ */
private void checkPlugins() { private void checkPlugins() {
Plugin test; Plugin test;
test = pm.getPlugin("Towny"); test = pm.getPlugin("Towny");
if (test != null && test instanceof Towny) if (test != null && test instanceof Towny)
towny = (Towny) test; towny = (Towny) test;
test = pm.getPlugin("dynmap"); test = pm.getPlugin("dynmap");
if (test != null && pm.getPlugin("dynmap").isEnabled()) { if (test != null && pm.getPlugin("dynmap").isEnabled()) {
dynMap = (DynmapAPI) test; dynMap = (DynmapAPI) test;
} }
test = pm.getPlugin("PlaceholderAPI"); test = pm.getPlugin("PlaceholderAPI");
if (test != null) { if (test != null) {
usingPlaceholderAPI = true; usingPlaceholderAPI = true;
} }
test = pm.getPlugin("EssentialsDiscord"); test = pm.getPlugin("EssentialsDiscord");
if (test != null) { if (test != null) {
usingEssentialsDiscord = true; usingEssentialsDiscord = true;
} }
test = pm.getPlugin("Essentials"); test = pm.getPlugin("Essentials");
if (test != null && JavaUtil.classExists("com.earth2me.essentials.Essentials")) { if (test != null && JavaUtil.classExists("com.earth2me.essentials.Essentials")) {
this.essentials = (Essentials) test; this.essentials = (Essentials) test;
} }
} }
public void registerEvents() { public void registerEvents() {
if (TownyPlayerListener == null) { if (TownyPlayerListener == null) {
TownyPlayerListener = new TownyChatPlayerListener(this); TownyPlayerListener = new TownyChatPlayerListener(this);
if (TownyPlayerListener != null) if (TownyPlayerListener != null)
pm.registerEvents(TownyPlayerListener, this); pm.registerEvents(TownyPlayerListener, this);
} }
if (usingEssentialsDiscord) { if (usingEssentialsDiscord) {
pm.registerEvents(new EssentialsDiscordHookListener(this), this); pm.registerEvents(new EssentialsDiscordHookListener(this), this);
} }
} }
public void registerPermissions() { public void registerPermissions() {
// Register all Permissions. // Register all Permissions.
for (String perm : getChannelsHandler().getAllPermissions()) { for (String perm : getChannelsHandler().getAllPermissions()) {
try { try {
pm.addPermission(new Permission(perm, new HashMap<String, Boolean>())); pm.addPermission(new Permission(perm, new HashMap<String, Boolean>()));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
//permission already registered. //permission already registered.
} }
} }
} }
public void unregisterPermissions() { public void unregisterPermissions() {
// Register all Permissions. // Register all Permissions.
for (String perm : getChannelsHandler().getAllPermissions()) { for (String perm : getChannelsHandler().getAllPermissions()) {
pm.removePermission(new Permission(perm, new HashMap<String, Boolean>())); pm.removePermission(new Permission(perm, new HashMap<String, Boolean>()));
} }
} }
public String getRootPath() { public String getRootPath() {
return getTowny().getDataFolder().getPath(); return getTowny().getDataFolder().getPath();
} }
public String getTownySettingsPath() { public String getTownySettingsPath() {
return getRootPath() + File.separator + "settings"; return getRootPath() + File.separator + "settings";
} }
public String getChatConfigPath() { public String getChatConfigPath() {
return getTownySettingsPath() + File.separator + "ChatConfig.yml"; return getTownySettingsPath() + File.separator + "ChatConfig.yml";
} }
public String getChannelsConfigPath() { public String getChannelsConfigPath() {
return getTownySettingsPath() + File.separator + "Channels.yml"; return getTownySettingsPath() + File.separator + "Channels.yml";
} }
/** /**
* @return the channels * @return the channels
*/ */
public ChannelsHolder getChannelsHandler() { public ChannelsHolder getChannelsHandler() {
return channels; return channels;
} }
public Towny getTowny() { public Towny getTowny() {
return towny; return towny;
} }
public DynmapAPI getDynmap() { public DynmapAPI getDynmap() {
return dynMap; return dynMap;
} }
public boolean isUsingEssentials() { public boolean isUsingEssentials() {
return essentials != null; return essentials != null;
} }
public Essentials getEssentials() { public Essentials getEssentials() {
return essentials; return essentials;
} }
public boolean isEssentialsMuted(Player player) { public boolean isEssentialsMuted(Player player) {
if (!isUsingEssentials()) if (!isUsingEssentials())
return false; return false;
return EssentialsIntegration.isMuted(player); return EssentialsIntegration.isMuted(player);
} }
public boolean isIgnoredByEssentials(Player sender, Player player) { public boolean isIgnoredByEssentials(Player sender, Player player) {
if (!isUsingEssentials()) if (!isUsingEssentials())
return false; return false;
return EssentialsIntegration.ignoredByEssentials(sender, player); return EssentialsIntegration.ignoredByEssentials(sender, player);
} }
private void registerObjectCommands() { private void registerObjectCommands() {
List<Command> commands = new ArrayList<Command>(); List<Command> commands = new ArrayList<Command>();
for (Channel channel : channels.getAllChannels().values()) { // All channels for (Channel channel : channels.getAllChannels().values()) { // All channels
for (String cmd : channel.getCommands()) { // All Commands of All Channels for (String cmd : channel.getCommands()) { // All Commands of All Channels
commands.add(new ChannelJoinAliasCommand(cmd, channel, this)); commands.add(new ChannelJoinAliasCommand(cmd, channel, this));
} }
} }
try { try {
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true); bukkitCommandMap.setAccessible(true);
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
commandMap.registerAll("TownyChat", commands); commandMap.registerAll("TownyChat", commands);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public TownyChatPlayerListener getTownyPlayerListener() { public TownyChatPlayerListener getTownyPlayerListener() {
return TownyPlayerListener; return TownyPlayerListener;
} }
public Channel getPlayerChannel(Player player) { public Channel getPlayerChannel(Player player) {
return playerChannelMap.get(player.getUniqueId()); return playerChannelMap.get(player.getUniqueId());
} }
public void setPlayerChannel(Player player, Channel channel) { public void setPlayerChannel(Player player, Channel channel) {
playerChannelMap.put(player.getUniqueId(), channel); playerChannelMap.put(player.getUniqueId(), channel);
} }
public TaskScheduler getScheduler() { public TaskScheduler getScheduler() {
return (TaskScheduler) this.scheduler; return (TaskScheduler) this.scheduler;
} }
private static boolean isFoliaClassPresent() { private static boolean isFoliaClassPresent() {
try { try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
return true; return true;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
return false; return false;
} }
} }
} }
@@ -1,62 +1,23 @@
package com.palmergames.bukkit.TownyChat; 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.config.ChatSettings;
import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent; 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.TownyAPI;
import com.palmergames.bukkit.towny.TownySettings; 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.Government;
import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.util.StringMgmt; 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 { public class TownyChatFormatter {
private static StringReplaceManager<LocalTownyChatEvent> replacer = new StringReplaceManager<LocalTownyChatEvent>();
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) { static TagResolver parser(LocalTownyChatEvent event) {
return TagResolver.resolver( return TagResolver.resolver(
TagResolver.resolver("worldname", Tag.inserting(Component.text(getWorldTag(event)))), TagResolver.resolver("worldname", Tag.inserting(Component.text(getWorldTag(event)))),
@@ -101,12 +62,11 @@ public class TownyChatFormatter {
return miniMessage.deserialize(message); return miniMessage.deserialize(message);
} }
public static String getChatFormat(LocalTownyChatEvent event) { public static TagResolver getChatFormat(LocalTownyChatEvent event) {
String out = replacer.replaceAll(event.getFormat(), event) // Replace the townychat tags with their values. return TagResolver.builder()
.replace("%", "") // Prevent user-inputted tags from breaking chat format. .resolver(parser(event))
.replace("{modplayername}", "%1$s") // Other plugins will replace this with the DisplayName. .resolver(StandardTags.defaults())
.replace("{msg}", "%2$s"); // Replace the {msg} here so it's not regex parsed. .build();
return Colors.translateColorCodes(out);
} }
public static String getWorldTag(LocalTownyChatEvent event) { public static String getWorldTag(LocalTownyChatEvent event) {
@@ -15,6 +15,12 @@ import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.util.Colors; import com.palmergames.bukkit.util.Colors;
import me.clip.placeholderapi.PlaceholderAPI; 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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Sound; import org.bukkit.Sound;
@@ -196,17 +202,24 @@ public class StandardChannel extends Channel {
} }
private void applyFormats(AsyncPlayerChatEvent event, String originalFormat, String workingFormat, Resident resident) { private void applyFormats(AsyncPlayerChatEvent event, String originalFormat, String workingFormat, Resident resident) {
List<TagResolver> tagResolvers = new ArrayList<>();
// Parse out our own channelTag and msgcolour tags. // Parse out our own channelTag and msgcolour tags.
String newFormat = parseTagAndMsgColour(workingFormat); tagResolvers = parseTagAndMsgColour(tagResolvers);
// Attempt to apply the new format. // Attempt to apply the new format.
catchFormatConversionException(event, originalFormat, newFormat); // catchFormatConversionException(event, originalFormat, newFormat);
// Fire the LocalTownyChatEvent. // Fire the LocalTownyChatEvent.
LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident); LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident);
// Format the chat line, replacing the TownyChat chat tags. // 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. // 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) { private String parseTagAndMsgColour(String format) {
@@ -215,6 +228,16 @@ public class StandardChannel extends Channel {
.replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : "")); .replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : ""));
} }
private List<TagResolver> parseTagAndMsgColour(List<TagResolver> 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) { private void catchFormatConversionException(AsyncPlayerChatEvent event, String format, String newFormat) {
try { try {
event.setFormat(newFormat); event.setFormat(newFormat);
@@ -13,6 +13,12 @@ import com.palmergames.bukkit.towny.object.metadata.StringDataField;
import com.palmergames.bukkit.towny.utils.MetaDataUtil; import com.palmergames.bukkit.towny.utils.MetaDataUtil;
import com.palmergames.bukkit.util.Colors; import com.palmergames.bukkit.util.Colors;
import me.clip.placeholderapi.PlaceholderAPI; 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.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; 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.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.UnknownFormatConversionException; import java.util.UnknownFormatConversionException;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.stream.Collectors;
public class TownyChatPlayerListener implements Listener { public class TownyChatPlayerListener implements Listener {
private Chat plugin; private Chat plugin;
@@ -127,9 +136,16 @@ public class TownyChatPlayerListener implements Listener {
if (resident == null) if (resident == null)
return; return;
// Nuke the channeltag and message colouring, but apply the remaining format. // 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. // format is left to store the original non-PAPI-parsed chat format.
List<TagResolver> tagResolvers = new ArrayList<>();
tagResolvers.add(
TagResolver.resolver(
TagResolver.resolver("channeltag", Tag.inserting(Component.empty())),
TagResolver.resolver("msgcolour", Tag.inserting(Component.empty()))
)
);
String newFormat = format; String newFormat = format;
// Parse any PAPI placeholders. // Parse any PAPI placeholders.
@@ -137,19 +153,24 @@ public class TownyChatPlayerListener implements Listener {
newFormat = PlaceholderAPI.setPlaceholders(player, format); newFormat = PlaceholderAPI.setPlaceholders(player, format);
// Attempt to apply the new format. // Attempt to apply the new format.
catchFormatConversionException(event, format, newFormat); // catchFormatConversionException(event, format, newFormat);
// Fire the LocalTownyChatEvent. // Fire the LocalTownyChatEvent.
LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident); LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident);
// Format the chat line, replacing the TownyChat chat tags. // 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. // 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. // 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));
} }
} }