mirror of
https://github.com/darksaid98/TownyChat.git
synced 2026-12-01 14:38:57 +00:00
Merge pull request #2 from darksaid98/feat/adventure
This commit is contained in:
28 files changed
+1104
-1231
No files matched your search
@@ -69,6 +69,11 @@
|
||||
<id>placeholderapi</id>
|
||||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>milkdrinkers-releases</id>
|
||||
<name>Milkdrinkers Maven Repository</name>
|
||||
<url>https://maven.athyrium.eu/releases</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
@@ -83,11 +88,16 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.20.2-R0.1-SNAPSHOT</version>
|
||||
<groupId>io.papermc.paper</groupId>
|
||||
<artifactId>paper-api</artifactId>
|
||||
<version>1.20.4-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.milkdrinkers</groupId>
|
||||
<artifactId>colorparser</artifactId>
|
||||
<version>2.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.essentialsx</groupId>
|
||||
<artifactId>EssentialsX</artifactId>
|
||||
@@ -109,7 +119,7 @@
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.11.5</version>
|
||||
<version>2.11.6</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -127,13 +137,22 @@
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>16.0.2</version>
|
||||
<version>24.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.bsideup.jabel</groupId>
|
||||
<artifactId>jabel-javac-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>provided</scope>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-api</artifactId>
|
||||
<version>4.17.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-minimessage</artifactId>
|
||||
<version>4.17.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-text-serializer-legacy</artifactId>
|
||||
<version>4.17.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -142,15 +161,49 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<release>8</release>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<compilerArgs>
|
||||
<arg>-Xplugin:jabel</arg>
|
||||
</compilerArgs>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/*.MF</exclude>
|
||||
<exclude>META-INF/**/*.xml</exclude>
|
||||
<exclude>META-INF/**/*.properties</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>com.github.milkdrinkers.colorparser</pattern>
|
||||
<shadedPattern>${project.groupId}.${project.artifactId}.libs.colorparser</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>org.jetbrains</pattern>
|
||||
<shadedPattern>${project.groupId}.${project.artifactId}.libs.jetbrains</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.12.1</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
<release>17</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
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.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener;
|
||||
import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener;
|
||||
import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask;
|
||||
@@ -18,7 +19,7 @@ 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;
|
||||
@@ -27,7 +28,9 @@ 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 org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -40,7 +43,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* Chat plugin to manage all Towny chat
|
||||
*
|
||||
* <p>
|
||||
* Website: http://code.google.com/a/eclipselabs.org/p/towny/
|
||||
*
|
||||
* @author ElgarL
|
||||
@@ -48,33 +51,46 @@ import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class Chat extends JavaPlugin {
|
||||
|
||||
public static boolean usingPlaceholderAPI = false;
|
||||
public static boolean usingEssentialsDiscord = false;
|
||||
private static Chat chat = null;
|
||||
private static String requiredTownyVersion = "0.98.0.0";
|
||||
private static ConcurrentMap<UUID, Channel> playerChannelMap;
|
||||
private final Object scheduler;
|
||||
protected PluginManager pm;
|
||||
boolean chatConfigError = false;
|
||||
boolean channelsConfigError = false;
|
||||
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 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<UUID, Channel> playerChannelMap;
|
||||
private BukkitAudiences adventure = null;
|
||||
|
||||
public Chat() {
|
||||
chat = this;
|
||||
this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null;
|
||||
}
|
||||
|
||||
public static Chat getTownyChat() {
|
||||
return chat;
|
||||
}
|
||||
|
||||
private static boolean isFoliaClassPresent() {
|
||||
try {
|
||||
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
|
||||
return true;
|
||||
} catch (ClassNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
pm = getServer().getPluginManager();
|
||||
channels = new ChannelsHolder(this);
|
||||
playerChannelMap = new ConcurrentHashMap<>();
|
||||
adventure = BukkitAudiences.create(this);
|
||||
|
||||
checkPlugins();
|
||||
if (towny == null || !townyVersionCheck()) {
|
||||
@@ -121,8 +137,11 @@ public class Chat extends JavaPlugin {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -130,6 +149,10 @@ public class Chat extends JavaPlugin {
|
||||
unregisterPermissions();
|
||||
// reset any handles
|
||||
|
||||
if (adventure != null) {
|
||||
adventure.close();
|
||||
adventure = null;
|
||||
}
|
||||
dynMap = null;
|
||||
towny = null;
|
||||
pm = null;
|
||||
@@ -153,7 +176,7 @@ public class Chat extends JavaPlugin {
|
||||
Plugin test;
|
||||
|
||||
test = pm.getPlugin("Towny");
|
||||
if (test != null && test instanceof Towny)
|
||||
if (test instanceof Towny)
|
||||
towny = (Towny) test;
|
||||
|
||||
test = pm.getPlugin("dynmap");
|
||||
@@ -179,11 +202,8 @@ public class Chat extends JavaPlugin {
|
||||
}
|
||||
|
||||
public void registerEvents() {
|
||||
|
||||
if (TownyPlayerListener == null) {
|
||||
TownyPlayerListener = new TownyChatPlayerListener(this);
|
||||
|
||||
if (TownyPlayerListener != null)
|
||||
pm.registerEvents(TownyPlayerListener, this);
|
||||
}
|
||||
|
||||
@@ -210,7 +230,6 @@ public class Chat extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getRootPath() {
|
||||
return getTowny().getDataFolder().getPath();
|
||||
}
|
||||
@@ -276,9 +295,7 @@ public class Chat extends JavaPlugin {
|
||||
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
|
||||
|
||||
commandMap.registerAll("TownyChat", commands);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -298,13 +315,4 @@ public class Chat extends JavaPlugin {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,17 @@ package com.palmergames.bukkit.TownyChat.Command;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.channels.ChannelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.events.PlayerJoinChatChannelEvent;
|
||||
import com.palmergames.bukkit.towny.TownyMessaging;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.towny.command.BaseCommand;
|
||||
import com.palmergames.bukkit.towny.object.Translatable;
|
||||
import com.palmergames.bukkit.towny.object.Translation;
|
||||
import com.palmergames.bukkit.towny.object.Translator;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.util.ChatTools;
|
||||
import com.palmergames.bukkit.util.Colors;
|
||||
import com.palmergames.util.StringMgmt;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -27,8 +26,8 @@ import java.util.Map;
|
||||
|
||||
public class ChannelCommand extends BaseCommand implements CommandExecutor {
|
||||
|
||||
private static Chat plugin;
|
||||
private static final List<String> channel_help = new ArrayList<String>();
|
||||
private static Chat plugin;
|
||||
|
||||
static {
|
||||
|
||||
@@ -48,69 +47,6 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (command.getName().equalsIgnoreCase("channel")) {
|
||||
if (label.equalsIgnoreCase("join")) {
|
||||
parseChannelJoin(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("leave")) {
|
||||
parseChannelLeave(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("mutelist")) {
|
||||
parseChannelMuteList(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("chmute")) {
|
||||
parseChannelMute(player, args, true);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("chunmute")) {
|
||||
parseChannelMute(player, args, false);
|
||||
return true;
|
||||
}
|
||||
parseChannelCommand(player, args);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Console
|
||||
for (String line : channel_help) {
|
||||
TownyMessaging.sendMessage(sender, Colors.strip(line));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void parseChannelCommand(Player player, String[] split) {
|
||||
// So they just type /channel , We should probably send them to the help menu..
|
||||
if (split.length == 0 || split[0].equalsIgnoreCase("help") || split[0].equalsIgnoreCase("?")) {
|
||||
for (String line : channel_help)
|
||||
TownyMessaging.sendMessage(player, line);
|
||||
} else if (split[0].equalsIgnoreCase("join")) { // /channel join [chn] (/label args[0] args[1] length = 2.)
|
||||
parseChannelJoin(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("leave")) {
|
||||
parseChannelLeave(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("mute")) {
|
||||
parseChannelMute(player, StringMgmt.remFirstArg(split), true);
|
||||
} else if (split[0].equalsIgnoreCase("unmute")) {
|
||||
parseChannelMute(player, StringMgmt.remFirstArg(split), false);
|
||||
} else if (split[0].equalsIgnoreCase("mutelist")) {
|
||||
parseChannelMuteList(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("list")) {
|
||||
parseChannelList(player);
|
||||
} else if (split[0].equalsIgnoreCase("soundtoggle")) {
|
||||
parseChannelSoundToggle(player, StringMgmt.remFirstArg(split));
|
||||
} else {
|
||||
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_sub"));
|
||||
}
|
||||
}
|
||||
|
||||
public static void parseChannelList(Player player) {
|
||||
// If not our command
|
||||
Map<String, Channel> chanList = plugin.getChannelsHandler().getAllChannels();
|
||||
@@ -329,7 +265,7 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor {
|
||||
nextChannel = plugin.getChannelsHandler().getDefaultChannel();
|
||||
|
||||
if (nextChannel == null)
|
||||
nextChannel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
|
||||
nextChannel = plugin.getChannelsHandler().getActiveChannel(player, ChannelTypes.GLOBAL);
|
||||
|
||||
// If the new channel is not us, announce it
|
||||
if (nextChannel != null && !chan.getName().equalsIgnoreCase(nextChannel.getName())) {
|
||||
@@ -409,4 +345,67 @@ public class ChannelCommand extends BaseCommand implements CommandExecutor {
|
||||
TownyMessaging.sendMessage(player, Translatable.of("tc_player_channel_sound_muted"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (command.getName().equalsIgnoreCase("channel")) {
|
||||
if (label.equalsIgnoreCase("join")) {
|
||||
parseChannelJoin(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("leave")) {
|
||||
parseChannelLeave(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("mutelist")) {
|
||||
parseChannelMuteList(player, args);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("chmute")) {
|
||||
parseChannelMute(player, args, true);
|
||||
return true;
|
||||
}
|
||||
if (label.equalsIgnoreCase("chunmute")) {
|
||||
parseChannelMute(player, args, false);
|
||||
return true;
|
||||
}
|
||||
parseChannelCommand(player, args);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Console
|
||||
for (String line : channel_help) {
|
||||
TownyMessaging.sendMessage(sender, Colors.strip(line));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void parseChannelCommand(Player player, String[] split) {
|
||||
// So they just type /channel , We should probably send them to the help menu..
|
||||
if (split.length == 0 || split[0].equalsIgnoreCase("help") || split[0].equalsIgnoreCase("?")) {
|
||||
for (String line : channel_help)
|
||||
TownyMessaging.sendMessage(player, line);
|
||||
} else if (split[0].equalsIgnoreCase("join")) { // /channel join [chn] (/label args[0] args[1] length = 2.)
|
||||
parseChannelJoin(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("leave")) {
|
||||
parseChannelLeave(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("mute")) {
|
||||
parseChannelMute(player, StringMgmt.remFirstArg(split), true);
|
||||
} else if (split[0].equalsIgnoreCase("unmute")) {
|
||||
parseChannelMute(player, StringMgmt.remFirstArg(split), false);
|
||||
} else if (split[0].equalsIgnoreCase("mutelist")) {
|
||||
parseChannelMuteList(player, StringMgmt.remFirstArg(split));
|
||||
} else if (split[0].equalsIgnoreCase("list")) {
|
||||
parseChannelList(player);
|
||||
} else if (split[0].equalsIgnoreCase("soundtoggle")) {
|
||||
parseChannelSoundToggle(player, StringMgmt.remFirstArg(split));
|
||||
} else {
|
||||
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_sub"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ import java.util.List;
|
||||
*/
|
||||
public class TownyChatCommand extends BaseCommand implements CommandExecutor {
|
||||
|
||||
private Chat plugin;
|
||||
private static final List<String> townychat_help = new ArrayList<String>();
|
||||
|
||||
static {
|
||||
@@ -27,6 +26,8 @@ public class TownyChatCommand extends BaseCommand implements CommandExecutor {
|
||||
townychat_help.add(ChatTools.formatCommand("", "/townychat", "reload", ""));
|
||||
}
|
||||
|
||||
private Chat plugin;
|
||||
|
||||
public TownyChatCommand(Chat instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
-1
@@ -8,7 +8,6 @@ import com.palmergames.bukkit.towny.object.Translatable;
|
||||
import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
|
||||
import com.palmergames.bukkit.util.Colors;
|
||||
import com.palmergames.util.StringMgmt;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
|
||||
@@ -1,217 +0,0 @@
|
||||
package com.palmergames.bukkit.TownyChat;
|
||||
|
||||
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.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;
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
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 String getWorldTag(LocalTownyChatEvent event) {
|
||||
return String.format(ChatSettings.getWorldTag(), event.getEvent().getPlayer().getWorld().getName());
|
||||
}
|
||||
|
||||
public static String getTownName(Resident resident) {
|
||||
return resident.hasTown() ? resident.getTownOrNull().getName() : "";
|
||||
}
|
||||
|
||||
public static String getNationName(Resident resident) {
|
||||
return resident.hasNation() ? resident.getNationOrNull().getName() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resident
|
||||
* @param override use full names if no tag is present
|
||||
* @param full Only use full names (no tags).
|
||||
* @return string containing the correctly formatted nation/town data
|
||||
*/
|
||||
public static String formatTownyTag(Resident resident, Boolean override, Boolean full) {
|
||||
if (resident.hasTown()) {
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
|
||||
String townTag = getTag(town);
|
||||
Nation nation = null;
|
||||
String nationTag = null;
|
||||
if (resident.hasNation()) {
|
||||
nation = TownyAPI.getInstance().getResidentNationOrNull(resident);
|
||||
nationTag = getTag(nation);
|
||||
}
|
||||
|
||||
String nTag = "", tTag = "";
|
||||
|
||||
//Force use of full names only
|
||||
if (full) {
|
||||
nationTag = "";
|
||||
townTag = "";
|
||||
}
|
||||
// Load town tags/names
|
||||
if (townTag != null && !townTag.isEmpty())
|
||||
tTag = townTag;
|
||||
else if (override || full)
|
||||
tTag = getName(town);
|
||||
|
||||
// Load the nation tags/names
|
||||
if ((nationTag != null) && !nationTag.isEmpty())
|
||||
nTag = nationTag;
|
||||
else if (resident.hasNation() && (override || full))
|
||||
nTag = getName(nation);
|
||||
|
||||
// Output depending on what tags are present
|
||||
if ((!tTag.isEmpty()) && (!nTag.isEmpty())) {
|
||||
if (ChatSettings.getBothTag().contains("%t") || ChatSettings.getBothTag().contains("%n")) {
|
||||
// Then it contains %s & %s
|
||||
// Small suttle change so that an issue is solved, it is documented in the config.
|
||||
// But only after addition of this. (v0.50)
|
||||
return ChatSettings.getBothTag().replace("%t", tTag).replace("%n", nTag);
|
||||
} else {
|
||||
return String.format(ChatSettings.getBothTag(), nTag, tTag);
|
||||
}
|
||||
}
|
||||
|
||||
if (!nTag.isEmpty()) {
|
||||
return String.format(ChatSettings.getNationTag(), nTag);
|
||||
}
|
||||
|
||||
if (!tTag.isEmpty()) {
|
||||
return String.format(ChatSettings.getTownTag(), tTag);
|
||||
}
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String formatTownTag(Resident resident, Boolean override, Boolean full) {
|
||||
if (resident.hasTown()) {
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
|
||||
if (full)
|
||||
return String.format(ChatSettings.getTownTag(), getName(town));
|
||||
else if (town.hasTag())
|
||||
return String.format(ChatSettings.getTownTag(), getTag(town));
|
||||
else if (override)
|
||||
return String.format(ChatSettings.getTownTag(), getName(town));
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String formatNationTag(Resident resident, Boolean override, Boolean full) {
|
||||
if (resident.hasNation()) {
|
||||
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident);
|
||||
if (full)
|
||||
return String.format(ChatSettings.getNationTag(), getName(nation));
|
||||
else if (nation.hasTag())
|
||||
return String.format(ChatSettings.getNationTag(), getTag(nation));
|
||||
else if (override)
|
||||
return String.format(ChatSettings.getNationTag(), getName(nation));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getNamePrefix(Resident resident) {
|
||||
|
||||
if (resident == null)
|
||||
return "";
|
||||
if (resident.isKing())
|
||||
return TownySettings.getKingPrefix(resident);
|
||||
else if (resident.isMayor())
|
||||
return TownySettings.getMayorPrefix(resident);
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getNamePostfix(Resident resident) {
|
||||
|
||||
if (resident == null)
|
||||
return "";
|
||||
if (resident.isKing())
|
||||
return TownySettings.getKingPostfix(resident);
|
||||
else if (resident.isMayor())
|
||||
return TownySettings.getMayorPostfix(resident);
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getPrimaryRankPrefix(Resident resident) {
|
||||
if (resident == null)
|
||||
return "";
|
||||
String rank = resident.getPrimaryRankPrefix();
|
||||
return rank.isEmpty() ? "" : rank + " ";
|
||||
}
|
||||
|
||||
private static String getName(Government gov) {
|
||||
return StringMgmt.remUnderscore(gov.getName());
|
||||
}
|
||||
|
||||
private static String getTag(Government gov) {
|
||||
return gov.getTag();
|
||||
}
|
||||
|
||||
public static String getPrefix(Resident resident, boolean blank) {
|
||||
return resident.hasTitle() ? resident.getTitle() + " " : blank ? "" : getNamePrefix(resident);
|
||||
}
|
||||
|
||||
public static String getSuffix(Resident resident, boolean blank) {
|
||||
return resident.hasSurname() ? " " + resident.getSurname() : blank ? "" : getNamePostfix(resident);
|
||||
}
|
||||
|
||||
public static String getTownyColour(Resident resident) {
|
||||
return !resident.hasTown()
|
||||
? ChatSettings.getNomadColour()
|
||||
: resident.isMayor() ? (resident.isKing() ? ChatSettings.getKingColour() : ChatSettings.getMayorColour()) : ChatSettings.getResidentColour();
|
||||
}
|
||||
|
||||
public static String getVaultGroup(Player player) {
|
||||
return TownyUniverse.getInstance().getPermissionSource().getPlayerGroup(player);
|
||||
}
|
||||
|
||||
public static String getVaultPrefixSuffix(Resident resident, String slug) {
|
||||
return TownyUniverse.getInstance().getPermissionSource().getPrefixSuffix(resident, slug);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.palmergames.bukkit.TownyChat;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent;
|
||||
|
||||
public class TownyChatReplacer {
|
||||
private final Function<LocalTownyChatEvent, String> func;
|
||||
|
||||
public TownyChatReplacer(Function<LocalTownyChatEvent, String> func) {
|
||||
this.func = func;
|
||||
}
|
||||
|
||||
public String getWith(LocalTownyChatEvent event) {
|
||||
return func.apply(event);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,33 @@
|
||||
package com.palmergames.bukkit.TownyChat.channels;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.towny.TownyMessaging;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import com.palmergames.bukkit.towny.object.Translatable;
|
||||
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
|
||||
import com.palmergames.bukkit.towny.object.metadata.StringDataField;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public abstract class Channel {
|
||||
|
||||
protected ConcurrentMap<String, Integer> absentPlayers = null;
|
||||
protected ConcurrentMap<String, Integer> mutedPlayers = null;
|
||||
private String name;
|
||||
private List<String> commands;
|
||||
private channelTypes type;
|
||||
private String channelTag, messageColour, permission, leavePermission, channelSound, listenPermission, speakPermission;
|
||||
private ChannelTypes type;
|
||||
private String channelTag, format, messageColour, permission, leavePermission, channelSound, listenPermission, speakPermission;
|
||||
private HashMap<String, WorldFormat> worldFormatGroups = new HashMap<>();
|
||||
private double range;
|
||||
private boolean hooked = false;
|
||||
private boolean autojoin = true;
|
||||
private boolean focusable = true;
|
||||
private double spamtime;
|
||||
private WeakHashMap<Player, Long> spammers = new WeakHashMap<>();
|
||||
protected ConcurrentMap<String, Integer> absentPlayers = null;
|
||||
protected ConcurrentMap<String, Integer> mutedPlayers = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -39,88 +37,131 @@ public abstract class Channel {
|
||||
public Channel(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the commands
|
||||
*/
|
||||
public List<String> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param commands the commands to set
|
||||
*/
|
||||
public void setCommands(List<String> commands) {
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the type
|
||||
*/
|
||||
public channelTypes getType() {
|
||||
public ChannelTypes getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type the type to set
|
||||
*/
|
||||
public void setType(channelTypes type) {
|
||||
public void setType(ChannelTypes type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the format
|
||||
*/
|
||||
public String getFormat(Player p) {
|
||||
return ChatSettings.isPer_world() ? getWorldFormat(p) : format;
|
||||
}
|
||||
|
||||
private String getWorldFormat(Player p) {
|
||||
String worldName = p.getWorld().getName().toLowerCase(Locale.ROOT);
|
||||
if (worldFormatGroups.containsKey(worldName)) {
|
||||
return worldFormatGroups.get(worldName).getFormat();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param format the format to set
|
||||
*/
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param worldFormats the worldFormats to set
|
||||
*/
|
||||
public void setWorldFormats(HashMap<String, WorldFormat> worldFormats) {
|
||||
worldFormatGroups = worldFormats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the channelTag
|
||||
*/
|
||||
public String getChannelTag() {
|
||||
return channelTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param channelTag the channelTag to set
|
||||
*/
|
||||
public void setChannelTag(String channelTag) {
|
||||
this.channelTag = channelTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the messageColour
|
||||
*/
|
||||
public String getMessageColour() {
|
||||
return messageColour;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageColour the messageColour to set
|
||||
*/
|
||||
public void setMessageColour(String messageColour) {
|
||||
this.messageColour = messageColour;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the permission
|
||||
*/
|
||||
public String getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param permission the permission to set
|
||||
*/
|
||||
public void setPermission(String permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the range
|
||||
*/
|
||||
public double getRange() {
|
||||
return range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param range the range to set
|
||||
*/
|
||||
public void setRange(double range) {
|
||||
this.range = range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param event the event to process
|
||||
*/
|
||||
public abstract void chatProcess(AsyncPlayerChatEvent event);
|
||||
public abstract void chatProcess(AsyncChatEvent event);
|
||||
|
||||
/*
|
||||
* Used to reset channel settings for a given player
|
||||
@@ -167,8 +208,7 @@ public abstract class Channel {
|
||||
*/
|
||||
public boolean isPresent(String name) {
|
||||
if (absentPlayers == null) return true;
|
||||
if (absentPlayers.containsKey(name)) return false;
|
||||
return true;
|
||||
return !absentPlayers.containsKey(name);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -276,25 +316,17 @@ public abstract class Channel {
|
||||
return mutedPlayers.keySet();
|
||||
}
|
||||
|
||||
public void setHooked(boolean hooked) {
|
||||
this.hooked = hooked;
|
||||
}
|
||||
|
||||
public boolean isHooked() {
|
||||
return hooked;
|
||||
}
|
||||
|
||||
/**
|
||||
* deprecated since 0.110, this setting has been unused for an unknown amount of time.
|
||||
* @param autojoin
|
||||
*/
|
||||
@Deprecated
|
||||
public void setAutoJoin(boolean autojoin) {
|
||||
this.autojoin = autojoin;
|
||||
public void setHooked(boolean hooked) {
|
||||
this.hooked = hooked;
|
||||
}
|
||||
|
||||
/**
|
||||
* deprecated since 0.110, this setting has been unused for an unknown amount of time.
|
||||
*
|
||||
* @param autojoin
|
||||
*/
|
||||
@Deprecated
|
||||
@@ -302,12 +334,24 @@ public abstract class Channel {
|
||||
return autojoin;
|
||||
}
|
||||
|
||||
/**
|
||||
* deprecated since 0.110, this setting has been unused for an unknown amount of time.
|
||||
*
|
||||
* @param autojoin
|
||||
*/
|
||||
@Deprecated
|
||||
public void setAutoJoin(boolean autojoin) {
|
||||
this.autojoin = autojoin;
|
||||
}
|
||||
|
||||
public boolean isFocusable() {
|
||||
return focusable;
|
||||
}
|
||||
|
||||
public void setFocusable(boolean focusable) {
|
||||
this.focusable = focusable;
|
||||
}
|
||||
|
||||
public double getSpam_time() {
|
||||
return spamtime;
|
||||
}
|
||||
@@ -479,8 +523,9 @@ public abstract class Channel {
|
||||
}
|
||||
|
||||
public boolean isGovernmentChannel() {
|
||||
return type.equals(channelTypes.TOWN) || type.equals(channelTypes.NATION) || type.equals(channelTypes.ALLIANCE);
|
||||
return type.equals(ChannelTypes.TOWN) || type.equals(ChannelTypes.NATION) || type.equals(ChannelTypes.ALLIANCE);
|
||||
}
|
||||
|
||||
public boolean hasPermission(Player player) {
|
||||
return getPermission() != null && TownyUniverse.getInstance().getPermissionSource().testPermission(player, getPermission());
|
||||
}
|
||||
@@ -496,4 +541,43 @@ public abstract class Channel {
|
||||
return hasPermission(player);
|
||||
return TownyUniverse.getInstance().getPermissionSource().testPermission(player, getListenPermissionNode());
|
||||
}
|
||||
|
||||
public static class WorldFormat {
|
||||
private String name;
|
||||
private String format;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param name the world name
|
||||
*/
|
||||
public WorldFormat(String name, String format) {
|
||||
super();
|
||||
this.name = name.toLowerCase();
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the world name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the specific format this channel has for this world
|
||||
*
|
||||
* @return format
|
||||
*/
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param format the format to set for this world
|
||||
*/
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.palmergames.bukkit.TownyChat.channels;
|
||||
|
||||
public enum ChannelTypes {
|
||||
TOWN,
|
||||
NATION,
|
||||
ALLIANCE,
|
||||
DEFAULT,
|
||||
GLOBAL,
|
||||
PRIVATE
|
||||
}
|
||||
@@ -3,15 +3,10 @@ package com.palmergames.bukkit.TownyChat.channels;
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author ElgarL
|
||||
*
|
||||
*/
|
||||
public class ChannelsHolder {
|
||||
|
||||
@@ -21,7 +16,8 @@ public class ChannelsHolder {
|
||||
// Container for all channels
|
||||
private Map<String, Channel> channels = new LinkedHashMap<>();
|
||||
|
||||
/** Constructor
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param plugin
|
||||
*/
|
||||
@@ -30,14 +26,14 @@ public class ChannelsHolder {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void setDefaultChannel(Channel channel) {
|
||||
defaultChan = channel;
|
||||
}
|
||||
|
||||
public Channel getDefaultChannel() {
|
||||
return defaultChan;
|
||||
}
|
||||
|
||||
public void setDefaultChannel(Channel channel) {
|
||||
defaultChan = channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the channels
|
||||
*/
|
||||
@@ -45,13 +41,6 @@ public class ChannelsHolder {
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param channels the channels to set
|
||||
*/
|
||||
public void setChannels(HashMap<String, Channel> channels) {
|
||||
this.channels = channels;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unlikely-arg-type")
|
||||
public void addChannel(Channel chan) {
|
||||
if (isChannel(chan.getName()))
|
||||
@@ -71,6 +60,14 @@ public class ChannelsHolder {
|
||||
public Map<String, Channel> getChannels() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param channels the channels to set
|
||||
*/
|
||||
public void setChannels(HashMap<String, Channel> channels) {
|
||||
this.channels = channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a channel we are able to talk in, and have not left, starting with the greatest range.
|
||||
*
|
||||
@@ -78,7 +75,7 @@ public class ChannelsHolder {
|
||||
* @param type
|
||||
* @return channel or null if none.
|
||||
*/
|
||||
public Channel getActiveChannel(Player player, channelTypes type) {
|
||||
public Channel getActiveChannel(Player player, ChannelTypes type) {
|
||||
|
||||
return getActiveChannel(player, type, false);
|
||||
}
|
||||
@@ -91,7 +88,7 @@ public class ChannelsHolder {
|
||||
* @param unlimitedRange true when a channel has to have an unlimited range.
|
||||
* @return channel or null if none.
|
||||
*/
|
||||
public Channel getActiveChannel(Player player, channelTypes type, boolean unlimitedRange) {
|
||||
public Channel getActiveChannel(Player player, ChannelTypes type, boolean unlimitedRange) {
|
||||
|
||||
Channel local = null;
|
||||
Channel global = null;
|
||||
@@ -138,7 +135,7 @@ public class ChannelsHolder {
|
||||
* @param type
|
||||
* @return channel or null if none.
|
||||
*/
|
||||
public Channel getChannel(Player player, channelTypes type) {
|
||||
public Channel getChannel(Player player, ChannelTypes type) {
|
||||
|
||||
Channel local = null;
|
||||
Channel global = null;
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
package com.palmergames.bukkit.TownyChat.channels;
|
||||
|
||||
import com.github.milkdrinkers.colorparser.ColorParser;
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.TownyChatFormatter;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.events.AsyncChatHookEvent;
|
||||
import com.palmergames.bukkit.TownyChat.util.Adventure;
|
||||
import com.palmergames.bukkit.TownyChat.util.TownyChatFormatter;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.events.PlayerJoinChatChannelEvent;
|
||||
import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent;
|
||||
import com.palmergames.bukkit.TownyChat.listener.InternalTownyChatEvent;
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.TownyMessaging;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
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.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.util.Colors;
|
||||
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
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.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Sound;
|
||||
@@ -23,95 +31,129 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.dynmap.DynmapAPI;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UnknownFormatConversionException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StandardChannel extends Channel {
|
||||
|
||||
private Chat plugin;
|
||||
private final Chat plugin;
|
||||
|
||||
public StandardChannel(Chat instance, String name) {
|
||||
super(name);
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chatProcess(AsyncPlayerChatEvent event) {
|
||||
@Deprecated
|
||||
public void chatProcess(AsyncPlayerChatEvent e) {
|
||||
IllegalCallerException illegalCallerException = new IllegalCallerException("A plugin is using the deprecated method Channel#chatProcess(AsyncPlayerChatEvent) which is no longer supported. Please use Channel#chatProcess(AsyncChatEvent)!");
|
||||
Chat.getTownyChat().getComponentLogger().warn(
|
||||
Component.text(illegalCallerException + Arrays.toString(illegalCallerException.getStackTrace()))
|
||||
);
|
||||
}
|
||||
|
||||
channelTypes channelType = this.getType();
|
||||
Player player = event.getPlayer();
|
||||
public void chatProcess(AsyncChatEvent e) {
|
||||
ChannelTypes channelType = this.getType();
|
||||
Player p = e.getPlayer();
|
||||
Resident res = TownyAPI.getInstance().getResident(p);
|
||||
boolean notifyjoin = false;
|
||||
|
||||
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
|
||||
if (resident == null)
|
||||
if (res == null) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
|
||||
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident);
|
||||
}
|
||||
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(res);
|
||||
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(res);
|
||||
|
||||
// If the channel would require a town/nation which is null, cancel and fail early.
|
||||
if (town == null && channelType.equals(channelTypes.TOWN) ||
|
||||
nation == null && (channelType.equals(channelTypes.NATION) || channelType.equals(channelTypes.ALLIANCE))) {
|
||||
event.setCancelled(true);
|
||||
if (
|
||||
town == null && channelType.equals(ChannelTypes.TOWN) ||
|
||||
nation == null && (channelType.equals(ChannelTypes.NATION) || channelType.equals(ChannelTypes.ALLIANCE))
|
||||
) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// If player sends a message to a channel they have left
|
||||
// tell the channel to add the player back
|
||||
if (isAbsent(player.getName())) {
|
||||
join(player);
|
||||
// If player sends a message in a channel they have left, rejoin the channel
|
||||
if (isAbsent(p.getName())) {
|
||||
join(p);
|
||||
notifyjoin = true;
|
||||
Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(player, this));
|
||||
Bukkit.getPluginManager().callEvent(new PlayerJoinChatChannelEvent(p, this));
|
||||
}
|
||||
|
||||
// Set the channel specific format
|
||||
String format = ChatSettings.getChannelFormat(player, channelType);
|
||||
final String channelMessageString = Chat.usingPlaceholderAPI ?
|
||||
PlaceholderAPI.setPlaceholders(p, getFormat(p)) :
|
||||
getFormat(p);
|
||||
|
||||
// Get the list of message recipients.
|
||||
Set<Player> recipients = getRecipients(player, town, nation, channelType, event.getRecipients());
|
||||
final InternalTownyChatEvent chatEvent = new InternalTownyChatEvent(e, res);
|
||||
final Component finalMessage = MiniMessage.builder()
|
||||
.tags(
|
||||
TagResolver.builder()
|
||||
.resolvers(
|
||||
TownyChatFormatter.parser(chatEvent),
|
||||
TagResolver.resolver(
|
||||
"channeltag", Tag.inserting(ColorParser.of(this.getChannelTag()).parseLegacy().build())
|
||||
),
|
||||
TagResolver.resolver(
|
||||
"msgcolour", Tag.inserting(ColorParser.of(this.getMessageColour()).parseLegacy().build())
|
||||
),
|
||||
StandardTags.defaults(),
|
||||
TagResolver.resolver( // TODO Parse color codes, the player has permissions to use
|
||||
"msg", Tag.inserting(e.message())
|
||||
)
|
||||
)
|
||||
.build()
|
||||
)
|
||||
.build().deserialize(channelMessageString);
|
||||
|
||||
// Try sending an alone message if it is called for.
|
||||
trySendingAloneMessage(player, recipients);
|
||||
// Get recipients
|
||||
final Set<Player> recipients = getRecipients(p, town, nation, channelType);
|
||||
|
||||
// format is left to store the original non-PAPI-parsed chat format.
|
||||
String newFormat = format;
|
||||
// Parse any PAPI placeholders.
|
||||
if (Chat.usingPlaceholderAPI)
|
||||
newFormat = PlaceholderAPI.setPlaceholders(player, format);
|
||||
// Try sending an alone message if the player is alone in a channel.
|
||||
trySendingAloneMessage(p, recipients);
|
||||
|
||||
/*
|
||||
* Only modify GLOBAL channelType chat (general and local chat channels) if isModifyChat() is true.
|
||||
*/
|
||||
if (!(channelType.equals(channelTypes.GLOBAL) && !ChatSettings.isModify_chat()))
|
||||
applyFormats(event, format, newFormat, resident);
|
||||
|
||||
/*
|
||||
* Set recipients for Bukkit to send this message to.
|
||||
*/
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().addAll(recipients);
|
||||
// Calculate recipients and set recipients
|
||||
e.message(finalMessage);
|
||||
e.viewers().clear();
|
||||
e.viewers().add(Audience.audience(recipients));
|
||||
|
||||
// If the server has marked this Channel as hooked, fire the AsyncChatHookEvent.
|
||||
// If the event is cancelled, cancel the chat entirely.
|
||||
// Fires its own sendSpyMessage().
|
||||
if (isHooked())
|
||||
if (!sendOffHookedMessage(event, channelType)) {
|
||||
event.setCancelled(true);
|
||||
if (isHooked()) {
|
||||
AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(e, this, recipients);
|
||||
Bukkit.getServer().getPluginManager().callEvent(hookEvent);
|
||||
if (hookEvent.isCancelled()) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send spy message if this was never hooked.
|
||||
if (!isHooked())
|
||||
sendSpyMessage(event, channelType);
|
||||
e.viewers().clear();
|
||||
e.viewers().add(Audience.audience(hookEvent.getRecipients()));
|
||||
|
||||
/*
|
||||
* Send spy message before another plugin changes any of the recipients, so we
|
||||
* know which people can see it.
|
||||
*/
|
||||
sendSpyMessage(e, channelType, recipients);
|
||||
}
|
||||
|
||||
// Add console as listener and set custom rendered for message
|
||||
e.viewers().add(Bukkit.getServer().getConsoleSender());
|
||||
e.renderer(
|
||||
(source, sourceDisplayName, message, viewer) -> e.message()
|
||||
);
|
||||
|
||||
/*
|
||||
* Send spy message before another plugin changes any of the recipients, so we
|
||||
* know which people can see it.
|
||||
*/
|
||||
sendSpyMessage(e, channelType, recipients);
|
||||
|
||||
// Play the channel sound, if used.
|
||||
tryPlayChannelSound(event.getRecipients());
|
||||
tryPlayChannelSound(recipients);
|
||||
|
||||
if (notifyjoin)
|
||||
TownyMessaging.sendMessage(player, "You join " + Colors.translateColorCodes(getMessageColour()) + getName());
|
||||
TownyMessaging.sendMessage(p, "You join " + Colors.translateColorCodes(getMessageColour()) + getName());
|
||||
|
||||
/*
|
||||
* Perform any last channel specific functions like logging this chat and
|
||||
@@ -125,18 +167,17 @@ public class StandardChannel extends Channel {
|
||||
break;
|
||||
case PRIVATE:
|
||||
case GLOBAL:
|
||||
tryPostToDynmap(player, event.getMessage());
|
||||
tryPostToDynmap(p, e.message());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Player> getRecipients(Player player, Town town, Nation nation, channelTypes channelType, Set<Player> recipients) {
|
||||
private Set<Player> getRecipients(Player player, Town town, Nation nation, ChannelTypes channelType) {
|
||||
return switch (channelType) {
|
||||
case TOWN -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(town)));
|
||||
case NATION -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayers(nation)));
|
||||
case ALLIANCE -> new HashSet<>(findRecipients(player, TownyAPI.getInstance().getOnlinePlayersAlliance(nation)));
|
||||
case DEFAULT -> new HashSet<>(findRecipients(player, new ArrayList<>(recipients)));
|
||||
case GLOBAL, PRIVATE -> new HashSet<>(findRecipients(player, new ArrayList<>(recipients)));
|
||||
case DEFAULT, GLOBAL, PRIVATE -> new HashSet<>(findRecipients(player, new ArrayList<>(Bukkit.getOnlinePlayers())));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -149,9 +190,8 @@ public class StandardChannel extends Channel {
|
||||
*/
|
||||
private Set<Player> findRecipients(Player sender, List<Player> playerList) {
|
||||
// Refresh the potential channels a player can see, if they are not currently in the channel.
|
||||
playerList.stream().forEach(p -> refreshPlayer(this, p));
|
||||
playerList.forEach(p -> refreshPlayer(this, p));
|
||||
return playerList.stream()
|
||||
.filter(p -> hasListenPermission(p)) // Check permission.
|
||||
.filter(p -> testDistance(sender, p, getRange())) // Within range.
|
||||
.filter(p -> !plugin.isIgnoredByEssentials(sender, p)) // Check essentials ignore.
|
||||
.filter(p -> !isAbsent(p.getName())) // Check if player is purposefully absent.
|
||||
@@ -191,98 +231,22 @@ public class StandardChannel extends Channel {
|
||||
|
||||
private void trySendingAloneMessage(Player sender, Set<Player> recipients) {
|
||||
if (ChatSettings.isUsingAloneMessage() &&
|
||||
recipients.stream().filter(p -> sender.canSee(p)).count() < 2) // sender will usually be a recipient of their own message.
|
||||
recipients.stream().filter(sender::canSee).count() < 2) // sender will usually be a recipient of their own message.
|
||||
TownyMessaging.sendMessage(sender, ChatSettings.getUsingAloneMessageString());
|
||||
}
|
||||
|
||||
private void applyFormats(AsyncPlayerChatEvent event, String originalFormat, String workingFormat, Resident resident) {
|
||||
// Parse out our own channelTag and msgcolour tags.
|
||||
String newFormat = parseTagAndMsgColour(workingFormat);
|
||||
// Attempt to apply the new format.
|
||||
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);
|
||||
// Attempt to apply the new format.
|
||||
catchFormatConversionException(event, originalFormat, newFormat);
|
||||
}
|
||||
|
||||
private String parseTagAndMsgColour(String format) {
|
||||
return format
|
||||
.replace("{channelTag}", Colors.translateColorCodes(getChannelTag() != null ? getChannelTag() : ""))
|
||||
.replace("{msgcolour}", Colors.translateColorCodes(getMessageColour() != null ? getMessageColour() : ""));
|
||||
}
|
||||
|
||||
private void catchFormatConversionException(AsyncPlayerChatEvent event, String format, String newFormat) {
|
||||
try {
|
||||
event.setFormat(newFormat);
|
||||
} catch (UnknownFormatConversionException e) {
|
||||
// This exception is usually thrown when a PAPI placeholder did not get parsed
|
||||
// and has left behind a % symbol followed by something that String#format
|
||||
// cannot handle.
|
||||
boolean percentSymbol = format.contains("%" + e.getConversion());
|
||||
String errmsg = "TownyChat tried to apply a chat format that is not allowed: '" +
|
||||
newFormat + "', because of the " + e.getConversion() + " symbol" +
|
||||
(percentSymbol ? ", found after a %. There is probably a PAPIPlaceholder that could not be parsed." : "." +
|
||||
" You should attempt to correct this in your towny\\settings\\chatconfig.yml file and use /townychat reload.");
|
||||
Chat.getTownyChat().getLogger().severe(errmsg);
|
||||
|
||||
if (percentSymbol)
|
||||
// Attempt to remove the unparsed placeholder and send this right back.
|
||||
catchFormatConversionException(event, format, purgePAPI(newFormat, "%" + e.getConversion()));
|
||||
else
|
||||
// Just let the chat go, this results in an error in the log, and TownyChat not being able to format chat.
|
||||
event.setFormat(format);
|
||||
}
|
||||
}
|
||||
|
||||
private String purgePAPI(String format, String startOfPlaceholder) {
|
||||
return format.replaceAll(startOfPlaceholder + ".*%", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Send off TownyChat's {@link AsyncChatHookEvent} which allows other plugins to
|
||||
* cancel or modify TownyChat's messaging.
|
||||
*
|
||||
* @param event {@link AsyncPlayerChatEvent} that has caused a message.
|
||||
* @param channelType {@link channelTypes} which this message is being sent through.
|
||||
* @return false if the AsyncChatHookEvent is cancelled.
|
||||
*/
|
||||
private boolean sendOffHookedMessage(AsyncPlayerChatEvent event, channelTypes channelType) {
|
||||
AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(event, this, !Bukkit.getServer().isPrimaryThread());
|
||||
Bukkit.getServer().getPluginManager().callEvent(hookEvent);
|
||||
if (hookEvent.isCancelled())
|
||||
return false;
|
||||
/*
|
||||
* Send spy message before another plugin changes any of the recipients, so we
|
||||
* know which people can see it.
|
||||
*/
|
||||
sendSpyMessage(event, channelType);
|
||||
|
||||
if (hookEvent.isChanged()) {
|
||||
event.setMessage(hookEvent.getMessage());
|
||||
event.setFormat(hookEvent.getFormat());
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().addAll(hookEvent.getRecipients());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends messages to spies who have not already seen the message naturally.
|
||||
*
|
||||
* @param event - Chat Event.
|
||||
* @param type - Channel Type
|
||||
*/
|
||||
private void sendSpyMessage(AsyncPlayerChatEvent event, channelTypes type) {
|
||||
Set<Player> recipients = event.getRecipients();
|
||||
private void sendSpyMessage(AsyncChatEvent event, ChannelTypes type, Set<Player> recipients) {
|
||||
Set<Player> spies = getSpies();
|
||||
String format = formatSpyMessage(type, event.getPlayer());
|
||||
if (format == null) return;
|
||||
|
||||
String message = Colors.translateColorCodes(event.getMessage());
|
||||
String message = Adventure.plainText().serialize(event.message());
|
||||
// Remove spies who've already seen the message naturally.
|
||||
spies.stream()
|
||||
.filter(spy -> !recipients.contains(spy))
|
||||
@@ -307,7 +271,7 @@ public class StandardChannel extends Channel {
|
||||
* @return format - Message format.
|
||||
*/
|
||||
@Nullable
|
||||
private String formatSpyMessage(channelTypes type, Player player) {
|
||||
private String formatSpyMessage(ChannelTypes type, Player player) {
|
||||
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
|
||||
if (resident == null)
|
||||
return null;
|
||||
@@ -317,8 +281,8 @@ public class StandardChannel extends Channel {
|
||||
return ChatColor.GOLD + "[SPY] " + ChatColor.WHITE + channelPrefix + resident.getName() + ": ";
|
||||
}
|
||||
|
||||
private String getGovtChannelSpyingPrefix(Resident resident, channelTypes type, String channelPrefix) {
|
||||
String slug = type.equals(channelTypes.TOWN)
|
||||
private String getGovtChannelSpyingPrefix(Resident resident, ChannelTypes type, String channelPrefix) {
|
||||
String slug = type.equals(ChannelTypes.TOWN)
|
||||
? TownyAPI.getInstance().getResidentTownOrNull(resident).getName() // Town chat.
|
||||
: TownyAPI.getInstance().getResidentNationOrNull(resident).getName(); // Nation or Alliance chat.
|
||||
return channelPrefix + "[" + slug + "] ";
|
||||
@@ -347,14 +311,15 @@ public class StandardChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Try to send a message to dynmap's web chat.
|
||||
*
|
||||
* @param player Player which has spoken.
|
||||
* @param message Message being spoken.
|
||||
*/
|
||||
private void tryPostToDynmap(Player player, String message) {
|
||||
private void tryPostToDynmap(Player player, Component message) {
|
||||
if (super.getRange() > 0)
|
||||
return;
|
||||
DynmapAPI dynMap = plugin.getDynmap();
|
||||
if (dynMap != null)
|
||||
dynMap.postPlayerMessageToWeb(player, message);
|
||||
dynMap.postPlayerMessageToWeb(player, Adventure.plainText().serialize(message));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.palmergames.bukkit.TownyChat.channels;
|
||||
|
||||
|
||||
public enum channelTypes {
|
||||
TOWN,
|
||||
NATION,
|
||||
ALLIANCE,
|
||||
DEFAULT,
|
||||
GLOBAL,
|
||||
PRIVATE
|
||||
}
|
||||
@@ -4,7 +4,6 @@ package com.palmergames.bukkit.TownyChat.config;
|
||||
* Dummy class used to make Channels.yml have update-able comments.
|
||||
*
|
||||
* @author LlmDl
|
||||
*
|
||||
*/
|
||||
public enum ChannelsNodes {
|
||||
COMMENTS("channelscomments", "",
|
||||
|
||||
@@ -3,47 +3,26 @@ package com.palmergames.bukkit.TownyChat.config;
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.StandardChannel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.channels.ChannelTypes;
|
||||
import com.palmergames.bukkit.config.CommentedConfiguration;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.towny.object.TownyWorld;
|
||||
import com.palmergames.util.FileMgmt;
|
||||
import com.palmergames.util.StringMgmt;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class ChannelsSettings {
|
||||
|
||||
private static final String CHANNELS_ROOT = "Channels";
|
||||
private static CommentedConfiguration channelConfig, newChannelConfig;
|
||||
private final static List<String> DEFAULT_CHANNELS = Arrays.asList("general", "town", "nation", "alliance", "admin", "mod", "local");
|
||||
|
||||
private static final String CHANNEL_TAG = "channeltag";
|
||||
private static final String LEGACY_CHANNEL_TAG = "channelTag";
|
||||
private static final String SPAM_TIME = "spam_time";
|
||||
private static final String RANGE = "range";
|
||||
private static final String DEFAULT = "default";
|
||||
private static final String PERMISSION = "permission";
|
||||
private static final String MESSAGECOLOUR = "messagecolour";
|
||||
private static final String TYPE = "type";
|
||||
private static final String COMMANDS = "commands";
|
||||
private static final String FOCUSABLE = "focusable";
|
||||
private static final String HOOKED = "hooked";
|
||||
private static final String SOUND = "sound";
|
||||
private static final String SPEAKPERMISSION = "speakpermission";
|
||||
private static final String LISTENPERMISSION = "listenpermission";
|
||||
private static final String LEAVEPERMISSION = "leavepermission";
|
||||
private static CommentedConfiguration channelConfig, newChannelConfig;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if the channels.yml has loaded.
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -133,83 +112,97 @@ public class ChannelsSettings {
|
||||
|
||||
private static Map<String, Object> generalDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "g");
|
||||
channelMap.put(TYPE, "GLOBAL");
|
||||
channelMap.put(CHANNEL_TAG, "&f[g]");
|
||||
channelMap.put(MESSAGECOLOUR, "&f");
|
||||
channelMap.put(PERMISSION, "towny.chat.general");
|
||||
channelMap.put(DEFAULT, "true");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put(SPAM_TIME, "0.5");
|
||||
channelMap.put("commands", "g");
|
||||
channelMap.put("type", "GLOBAL");
|
||||
channelMap.put("format", "{channelTag} {worldname}{townytagoverride}{townycolor}{permprefix}{group} {townyprefix}{modplayername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[g]");
|
||||
channelMap.put("messagecolour", "&f");
|
||||
channelMap.put("permission", "towny.chat.general");
|
||||
channelMap.put("default", "true");
|
||||
channelMap.put("range", "-1");
|
||||
channelMap.put("spam_time", "0.5");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> townDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "tc");
|
||||
channelMap.put(TYPE, "TOWN");
|
||||
channelMap.put(CHANNEL_TAG, "&f[&3TC&f]");
|
||||
channelMap.put(MESSAGECOLOUR, "&b");
|
||||
channelMap.put(PERMISSION, "towny.chat.town");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put(SPAM_TIME, "0.5");
|
||||
channelMap.put("commands", "tc");
|
||||
channelMap.put("type", "TOWN");
|
||||
channelMap.put("format", "{channelTag} {townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[&3TC&f]");
|
||||
channelMap.put("messagecolour", "&b");
|
||||
channelMap.put("permission", "towny.chat.town");
|
||||
channelMap.put("range", "-1");
|
||||
channelMap.put("spam_time", "0.5");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> nationDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "nc");
|
||||
channelMap.put(TYPE, "NATION");
|
||||
channelMap.put(CHANNEL_TAG, "&f[&6NC&f]");
|
||||
channelMap.put(MESSAGECOLOUR, "&e");
|
||||
channelMap.put(PERMISSION, "towny.chat.nation");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put(SPAM_TIME, "0.5");
|
||||
channelMap.put("commands", "nc");
|
||||
channelMap.put("type", "NATION");
|
||||
channelMap.put("format", "{channelTag} {towntagoverride}{townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[&6NC&f]");
|
||||
channelMap.put("messagecolour", "&e");
|
||||
channelMap.put("permission", "towny.chat.nation");
|
||||
channelMap.put("range", "-1");
|
||||
channelMap.put("spam_time", "0.5");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> allianceDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "ac");
|
||||
channelMap.put(TYPE, "ALLIANCE");
|
||||
channelMap.put(CHANNEL_TAG, "&f[&2AC&f]");
|
||||
channelMap.put(MESSAGECOLOUR, "&a");
|
||||
channelMap.put(PERMISSION, "towny.chat.alliance");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put(SPAM_TIME, "0.5");
|
||||
channelMap.put("commands", "ac");
|
||||
channelMap.put("type", "ALLIANCE");
|
||||
channelMap.put("format", "{channelTag} {towntagoverride}{townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[&2AC&f]");
|
||||
channelMap.put("messagecolour", "&a");
|
||||
channelMap.put("permission", "towny.chat.alliance");
|
||||
channelMap.put("range", "-1");
|
||||
channelMap.put("spam_time", "0.5");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> adminDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "a,admin");
|
||||
channelMap.put(TYPE, "DEFAULT");
|
||||
channelMap.put(CHANNEL_TAG, "&f[&4ADMIN&f]");
|
||||
channelMap.put(MESSAGECOLOUR, "&c");
|
||||
channelMap.put(PERMISSION, "towny.chat.admin");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put("commands", "a,admin");
|
||||
channelMap.put("type", "DEFAULT");
|
||||
channelMap.put("format", "{channelTag} {permprefix}{playername}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[&4ADMIN&f]");
|
||||
channelMap.put("messagecolour", "&c");
|
||||
channelMap.put("permission", "towny.chat.admin");
|
||||
channelMap.put("range", "-1");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> modDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "m,mod");
|
||||
channelMap.put(TYPE, "DEFAULT");
|
||||
channelMap.put(CHANNEL_TAG, "&f[&9MOD&f]");
|
||||
channelMap.put(MESSAGECOLOUR, "&5");
|
||||
channelMap.put(PERMISSION, "towny.chat.mod");
|
||||
channelMap.put(RANGE, "-1");
|
||||
channelMap.put("commands", "m,mod");
|
||||
channelMap.put("type", "DEFAULT");
|
||||
channelMap.put("format", "{channelTag} {permprefix}{playername}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[&9MOD&f]");
|
||||
channelMap.put("messagecolour", "&5");
|
||||
channelMap.put("permission", "towny.chat.mod");
|
||||
channelMap.put("range", "-1");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
private static Map<String, Object> localDefaults() {
|
||||
Map<String, Object> channelMap = new LinkedHashMap<>();
|
||||
channelMap.put(COMMANDS, "l,lc");
|
||||
channelMap.put(TYPE, "GLOBAL");
|
||||
channelMap.put(CHANNEL_TAG, "&f[local]");
|
||||
channelMap.put(MESSAGECOLOUR, "&f");
|
||||
channelMap.put(PERMISSION, "towny.chat.local");
|
||||
channelMap.put(RANGE, "100");
|
||||
channelMap.put("commands", "l,lc");
|
||||
channelMap.put("type", "GLOBAL");
|
||||
channelMap.put("format", "{channelTag} {worldname}{townytagoverride}{townycolor}{permprefix}{group} {townyprefix}{modplayername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
channelMap.put("worldformats", new LinkedHashMap<>());
|
||||
channelMap.put("channelTag", "&f[local]");
|
||||
channelMap.put("messagecolour", "&f");
|
||||
channelMap.put("permission", "towny.chat.local");
|
||||
channelMap.put("range", "100");
|
||||
return channelMap;
|
||||
}
|
||||
|
||||
@@ -252,7 +245,9 @@ public class ChannelsSettings {
|
||||
ChannelDetails data = new ChannelsSettings.ChannelDetails(channelConfig, channelName);
|
||||
// The following will always be present in some manner
|
||||
channel.setCommands(data.getCommands());
|
||||
channel.setType(channelTypes.valueOf(data.getType()));
|
||||
channel.setType(ChannelTypes.valueOf(data.getType()));
|
||||
channel.setFormat(data.getFormat());
|
||||
channel.setWorldFormats(data.getWorldFormats());
|
||||
channel.setRange(data.getRange());
|
||||
channel.setHooked(data.isHooked());
|
||||
channel.setFocusable(data.isFocusable());
|
||||
@@ -318,92 +313,116 @@ public class ChannelsSettings {
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return (String) channelSettingsMap.getOrDefault(TYPE, "DEFAULT");
|
||||
return (String) channelSettingsMap.getOrDefault("type", "DEFAULT");
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return (String) channelSettingsMap.getOrDefault("format", "{channelTag} {worldname}{townytagoverride}{townycolor}{permprefix}{group} {townyprefix}{modplayername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}");
|
||||
}
|
||||
|
||||
public HashMap<String, Channel.WorldFormat> getWorldFormats() {
|
||||
HashMap<String, Channel.WorldFormat> worldFormats = new HashMap<>();
|
||||
if (!ChatSettings.isPer_world())
|
||||
return worldFormats;
|
||||
|
||||
if (!channelSettingsMap.containsKey("worldformats"))
|
||||
return worldFormats;
|
||||
|
||||
for (TownyWorld world : TownyUniverse.getInstance().getTownyWorlds()) {
|
||||
String worldName = world.getName();
|
||||
String path = "worldformats." + worldName;
|
||||
if (channelSettingsMap.containsKey(path)) {
|
||||
worldFormats.put(
|
||||
worldName.toLowerCase(Locale.ROOT),
|
||||
new Channel.WorldFormat(worldName, (String) channelSettingsMap.getOrDefault(path, getFormat()))
|
||||
);
|
||||
}
|
||||
}
|
||||
return worldFormats;
|
||||
}
|
||||
|
||||
public boolean hasChannelTag() {
|
||||
return channelSettingsMap.containsKey(CHANNEL_TAG) || channelSettingsMap.containsKey(LEGACY_CHANNEL_TAG);
|
||||
return channelSettingsMap.containsKey("channeltag");
|
||||
}
|
||||
|
||||
public String getChannelTag() {
|
||||
String slug = channelSettingsMap.containsKey(LEGACY_CHANNEL_TAG) ? LEGACY_CHANNEL_TAG : CHANNEL_TAG;
|
||||
return (String) channelSettingsMap.getOrDefault(slug, "");
|
||||
return (String) channelSettingsMap.getOrDefault("channeltag", "");
|
||||
}
|
||||
|
||||
public boolean hasMessageColour() {
|
||||
return channelSettingsMap.containsKey(MESSAGECOLOUR);
|
||||
return channelSettingsMap.containsKey("messagecolour");
|
||||
}
|
||||
|
||||
public String getMessageColour() {
|
||||
return (String) channelSettingsMap.getOrDefault(MESSAGECOLOUR, "");
|
||||
return (String) channelSettingsMap.getOrDefault("messagecolour", "");
|
||||
}
|
||||
|
||||
public boolean hasPermission() {
|
||||
return channelSettingsMap.containsKey(PERMISSION);
|
||||
return channelSettingsMap.containsKey("permission");
|
||||
}
|
||||
|
||||
public String getPermission() {
|
||||
return (String) channelSettingsMap.getOrDefault(PERMISSION, "");
|
||||
return (String) channelSettingsMap.getOrDefault("permission", "");
|
||||
}
|
||||
|
||||
public boolean hasLeavePermission() {
|
||||
return channelSettingsMap.containsKey(LEAVEPERMISSION);
|
||||
return channelSettingsMap.containsKey("leavepermission");
|
||||
}
|
||||
|
||||
public String getLeavePermission() {
|
||||
return (String) channelSettingsMap.getOrDefault(LEAVEPERMISSION, "towny.chat.leave." + name);
|
||||
return (String) channelSettingsMap.getOrDefault("leavepermission", "towny.chat.leave." + name);
|
||||
}
|
||||
|
||||
public boolean hasListenPermission() {
|
||||
return channelSettingsMap.containsKey(LISTENPERMISSION);
|
||||
return channelSettingsMap.containsKey("listenpermission");
|
||||
}
|
||||
|
||||
public String getListenPermission() {
|
||||
return (String) channelSettingsMap.getOrDefault(LISTENPERMISSION, "");
|
||||
return (String) channelSettingsMap.getOrDefault("listenpermission", "");
|
||||
}
|
||||
|
||||
public boolean hasSpeakPermission() {
|
||||
return channelSettingsMap.containsKey(SPEAKPERMISSION);
|
||||
return channelSettingsMap.containsKey("speakpermission");
|
||||
}
|
||||
|
||||
public String getSpeakPermission() {
|
||||
return (String) channelSettingsMap.getOrDefault(SPEAKPERMISSION, "");
|
||||
return (String) channelSettingsMap.getOrDefault("speakpermission", "");
|
||||
}
|
||||
|
||||
public boolean hasSound() {
|
||||
return channelSettingsMap.containsKey(SOUND);
|
||||
return channelSettingsMap.containsKey("sound");
|
||||
}
|
||||
|
||||
public String getSound() {
|
||||
return (String) channelSettingsMap.getOrDefault(SOUND, "");
|
||||
return (String) channelSettingsMap.getOrDefault("sound", "");
|
||||
}
|
||||
|
||||
public boolean hasSpamTime() {
|
||||
return channelSettingsMap.containsKey(SPAM_TIME);
|
||||
return channelSettingsMap.containsKey("spam_time");
|
||||
}
|
||||
|
||||
public double getSpamTime() {
|
||||
return Double.valueOf((String) channelSettingsMap.get(SPAM_TIME));
|
||||
return Double.valueOf((String) channelSettingsMap.get("spam_time"));
|
||||
}
|
||||
|
||||
public double getRange() {
|
||||
return Double.valueOf((String) channelSettingsMap.getOrDefault(RANGE, -1));
|
||||
return Double.valueOf((String) channelSettingsMap.getOrDefault("range", -1));
|
||||
}
|
||||
|
||||
public boolean isHooked() {
|
||||
return getBoolean(channelSettingsMap.getOrDefault(HOOKED, false));
|
||||
return getBoolean(channelSettingsMap.getOrDefault("hooked", false));
|
||||
}
|
||||
|
||||
public boolean isFocusable() {
|
||||
return getBoolean(channelSettingsMap.getOrDefault(FOCUSABLE, true));
|
||||
return getBoolean(channelSettingsMap.getOrDefault("focusable", true));
|
||||
}
|
||||
|
||||
public boolean isDefault() {
|
||||
return getBoolean(channelSettingsMap.getOrDefault(DEFAULT, false));
|
||||
return getBoolean(channelSettingsMap.getOrDefault("default", false));
|
||||
}
|
||||
|
||||
public List<String> getCommands() {
|
||||
Object object = channelSettingsMap.get(COMMANDS);
|
||||
Object object = channelSettingsMap.get("commands");
|
||||
if (object instanceof String string) {
|
||||
return Arrays.asList(string.split(","));
|
||||
}
|
||||
|
||||
@@ -80,32 +80,6 @@ public enum ChatConfigNodes {
|
||||
"# #RRGGBB",
|
||||
"# &#RRGGBB",
|
||||
"# {#RRGGBB}"),
|
||||
|
||||
CHANNEL_FORMATS(
|
||||
"channel_formats",
|
||||
"",
|
||||
""),
|
||||
CHANNEL_FORMATS_GLOBAL(
|
||||
"channel_formats.global",
|
||||
"{channelTag} {worldname}{townytagoverride}{townycolor}{permprefix}{group} {townyprefix}{modplayername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}",
|
||||
"# This is the format which will be used for GLOBAL chat/channels.",
|
||||
"# This is also the format used when you have modify_chat.enable: true, but use other plugins to handle chat."),
|
||||
CHANNEL_FORMATS_TOWN(
|
||||
"channel_formats.town",
|
||||
"{channelTag} {townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}",
|
||||
"# TOWN channel types."),
|
||||
CHANNEL_FORMATS_NATION(
|
||||
"channel_formats.nation",
|
||||
"{channelTag} {towntagoverride}{townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}",
|
||||
"# NATION channel types."),
|
||||
CHANNEL_FORMATS_ALLIANCE(
|
||||
"channel_formats.alliance",
|
||||
"{channelTag} {towntagoverride}{townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f:{msgcolour} {msg}",
|
||||
"# ALLIANCE channel types."),
|
||||
CHANNEL_FORMATS_DEFAULT(
|
||||
"channel_formats.default",
|
||||
"{channelTag} {permprefix}{playername}{permsuffix}&f:{msgcolour} {msg}",
|
||||
"# DEFAULT channel types."),
|
||||
TAG_FORMATS(
|
||||
"tag_formats",
|
||||
"",
|
||||
@@ -174,9 +148,7 @@ public enum ChatConfigNodes {
|
||||
"allow_exclamation_point_to_shout",
|
||||
"true",
|
||||
"",
|
||||
"# If true players that say !somewords will have their message sent to a global type channel with unlimited range (usually your general chat.)"),
|
||||
WORLDS("worlds","","");
|
||||
|
||||
"# If true players that say !somewords will have their message sent to a global type channel with unlimited range (usually your general chat.)");
|
||||
|
||||
|
||||
private final String Root;
|
||||
|
||||
@@ -1,31 +1,21 @@
|
||||
package com.palmergames.bukkit.TownyChat.config;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||
import com.palmergames.bukkit.config.CommentedConfiguration;
|
||||
import com.palmergames.bukkit.config.ConfigNodes;
|
||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
||||
import com.palmergames.bukkit.towny.object.TownyWorld;
|
||||
import com.palmergames.util.FileMgmt;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class ChatSettings {
|
||||
|
||||
private static Map<String, WorldFormat> worldFormatGroups = new HashMap<String, WorldFormat>();
|
||||
private static CommentedConfiguration chatConfig, newChatConfig;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if the chatconfig has loaded.
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -62,11 +52,9 @@ public class ChatSettings {
|
||||
if (root.getComments().length > 0)
|
||||
addComment(root.getRoot(), root.getComments());
|
||||
|
||||
if (root.getRoot() == ChatConfigNodes.WORLDS.getRoot())
|
||||
setWorldDefaults(); // Per-worlds section.
|
||||
else if (root.getRoot() == ChatConfigNodes.VERSION.getRoot())
|
||||
if (Objects.equals(root.getRoot(), ChatConfigNodes.VERSION.getRoot()))
|
||||
setNewProperty(root.getRoot(), version);
|
||||
else if (root.getRoot() == ChatConfigNodes.LAST_RUN_VERSION.getRoot())
|
||||
else if (Objects.equals(root.getRoot(), ChatConfigNodes.LAST_RUN_VERSION.getRoot()))
|
||||
setNewProperty(root.getRoot(), getLastRunVersion(version));
|
||||
else
|
||||
// A regular config node.
|
||||
@@ -76,31 +64,6 @@ public class ChatSettings {
|
||||
newChatConfig = null;
|
||||
}
|
||||
|
||||
private static void setWorldDefaults() {
|
||||
if (isPer_world()) {
|
||||
for (TownyWorld world : TownyUniverse.getInstance().getTownyWorlds()) {
|
||||
String path = "worlds." + world;
|
||||
if (!chatConfig.contains(path )
|
||||
|| !chatConfig.contains(path + ".global")
|
||||
|| !chatConfig.contains(path + ".town")
|
||||
|| !chatConfig.contains(path + ".nation")
|
||||
|| !chatConfig.contains(path + ".alliance")
|
||||
|| !chatConfig.contains(path + ".default")) {
|
||||
newChatConfig.createSection(path);
|
||||
ConfigurationSection worldsection = newChatConfig.getConfigurationSection(path);
|
||||
worldsection.set("global", getOrDefault(path + ".global", ChatConfigNodes.CHANNEL_FORMATS_GLOBAL));
|
||||
worldsection.set("town", getOrDefault(path + ".town", ChatConfigNodes.CHANNEL_FORMATS_TOWN));
|
||||
worldsection.set("nation", getOrDefault(path + ".nation", ChatConfigNodes.CHANNEL_FORMATS_NATION));
|
||||
worldsection.set("alliance", getOrDefault(path + ".alliance", ChatConfigNodes.CHANNEL_FORMATS_ALLIANCE));
|
||||
worldsection.set("default", getOrDefault(path + ".default", ChatConfigNodes.CHANNEL_FORMATS_DEFAULT));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (chatConfig.contains(ChatConfigNodes.WORLDS.getRoot()))
|
||||
newChatConfig.set(ChatConfigNodes.WORLDS.getRoot(), chatConfig.get(ChatConfigNodes.WORLDS.getRoot()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void addComment(String root, String... comments) {
|
||||
newChatConfig.addComment(root.toLowerCase(), comments);
|
||||
}
|
||||
@@ -224,118 +187,7 @@ public class ChatSettings {
|
||||
return getString(ChatConfigNodes.COLOUR_NOMAD);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get ChannelType Formats
|
||||
*/
|
||||
|
||||
public static String getChannelFormat(Player player, channelTypes type) {
|
||||
if (isPer_world()) {
|
||||
String worldName = player.getWorld().getName().toLowerCase(Locale.ROOT);
|
||||
if (worldFormatGroups.containsKey(worldName)) {
|
||||
String format = worldFormatGroups.get(worldName).getFormat(type);
|
||||
if (format != null && !format.isEmpty())
|
||||
return format;
|
||||
}
|
||||
}
|
||||
return switch(type) {
|
||||
case GLOBAL, PRIVATE -> getString(ChatConfigNodes.CHANNEL_FORMATS_GLOBAL);
|
||||
case NATION -> getString(ChatConfigNodes.CHANNEL_FORMATS_NATION);
|
||||
case TOWN -> getString(ChatConfigNodes.CHANNEL_FORMATS_TOWN);
|
||||
case ALLIANCE -> getString(ChatConfigNodes.CHANNEL_FORMATS_ALLIANCE);
|
||||
case DEFAULT -> getString(ChatConfigNodes.CHANNEL_FORMATS_DEFAULT);
|
||||
};
|
||||
}
|
||||
|
||||
public static void loadWorldFormats() {
|
||||
if (!isPer_world())
|
||||
return;
|
||||
for (TownyWorld world : TownyUniverse.getInstance().getTownyWorlds()) {
|
||||
String worldName = world.getName();
|
||||
String path = "worlds." + worldName;
|
||||
if (chatConfig.contains(path)) {
|
||||
ChatSettings.WorldFormat worldFormat = new ChatSettings().new WorldFormat(worldName);
|
||||
worldFormat.setGLOBAL(getOrDefault(path + ".global", ChatConfigNodes.CHANNEL_FORMATS_GLOBAL));
|
||||
worldFormat.setDEFAULT(getOrDefault(path + ".default", ChatConfigNodes.CHANNEL_FORMATS_DEFAULT));
|
||||
worldFormat.setTOWN(getOrDefault(path + ".town", ChatConfigNodes.CHANNEL_FORMATS_TOWN));
|
||||
worldFormat.setNATION(getOrDefault(path + ".nation", ChatConfigNodes.CHANNEL_FORMATS_NATION));
|
||||
worldFormat.setALLIANCE(getOrDefault(path + ".alliance", ChatConfigNodes.CHANNEL_FORMATS_ALLIANCE));
|
||||
addWorldFormat(worldFormat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getOrDefault(String configPath, ChatConfigNodes channelNode) {
|
||||
return String.valueOf(chatConfig.get(configPath, channelNode.getDefault()));
|
||||
}
|
||||
|
||||
private static void addWorldFormat(WorldFormat format) {
|
||||
worldFormatGroups.put(format.getName().toLowerCase(Locale.ROOT), format);
|
||||
}
|
||||
|
||||
public class WorldFormat {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public WorldFormat(String name) {
|
||||
super();
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
public String getFormat(channelTypes type) {
|
||||
return switch(type) {
|
||||
case GLOBAL, PRIVATE -> GLOBAL;
|
||||
case NATION -> NATION;
|
||||
case TOWN -> TOWN;
|
||||
case ALLIANCE -> ALLIANCE;
|
||||
case DEFAULT -> DEFAULT;
|
||||
};
|
||||
}
|
||||
|
||||
private String name, GLOBAL, TOWN, NATION, ALLIANCE, DEFAULT;
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GLOBAL the GLOBAL format to set
|
||||
*/
|
||||
public void setGLOBAL(String GLOBAL) {
|
||||
this.GLOBAL = GLOBAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TOWN the TOWN format to set
|
||||
*/
|
||||
public void setTOWN(String TOWN) {
|
||||
this.TOWN = TOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NATION the NATION format to set
|
||||
*/
|
||||
public void setNATION(String NATION) {
|
||||
this.NATION = NATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ALLIANCE the ALLIANCE format to set
|
||||
*/
|
||||
public void setALLIANCE(String ALLIANCE) {
|
||||
this.ALLIANCE = ALLIANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DEFAULT the DEFAULT format to set
|
||||
*/
|
||||
public void setDEFAULT(String DEFAULT) {
|
||||
this.DEFAULT = DEFAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
package com.palmergames.bukkit.TownyChat.events;
|
||||
|
||||
import com.github.milkdrinkers.colorparser.ColorParser;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.util.Adventure;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/*
|
||||
@@ -26,19 +30,86 @@ import java.util.Set;
|
||||
* permission: 'towny.chat.general'
|
||||
* range: '-1'
|
||||
*/
|
||||
public class AsyncChatHookEvent extends Event {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
protected AsyncPlayerChatEvent event;
|
||||
protected boolean changed;
|
||||
protected Channel channel;
|
||||
protected Set<Player> recipients;
|
||||
public class AsyncChatHookEvent extends Event implements Cancellable {
|
||||
private static final HandlerList HANDLER_LIST = new HandlerList();
|
||||
private String legacyMessage;
|
||||
private boolean cancelled;
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLER_LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return HANDLER_LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
|
||||
private final AsyncChatEvent event;
|
||||
private final Channel channel;
|
||||
private final Set<Player> recipients;
|
||||
|
||||
public AsyncChatHookEvent(AsyncChatEvent e, Channel channel, Set<Player> recipients) {
|
||||
super(e.isAsynchronous());
|
||||
this.event = e;
|
||||
this.cancelled = e.isCancelled();
|
||||
this.legacyMessage = Adventure.plainText().serialize(message());
|
||||
|
||||
public AsyncChatHookEvent(AsyncPlayerChatEvent event, Channel channel, boolean async) {
|
||||
super(async);
|
||||
this.event = event;
|
||||
this.changed = false;
|
||||
this.channel = channel;
|
||||
this.recipients = new HashSet<>(event.getRecipients());
|
||||
this.recipients = recipients;
|
||||
}
|
||||
|
||||
public AsyncChatEvent getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getFormat() {
|
||||
return "undefined";
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setFormat(String format) {
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getMessage() {
|
||||
return legacyMessage;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setMessage(String message) {
|
||||
legacyMessage = message;
|
||||
}
|
||||
|
||||
public Component message() {
|
||||
return event.message();
|
||||
}
|
||||
|
||||
public void message(Component message) {
|
||||
event.message(message);
|
||||
}
|
||||
|
||||
public Set<Player> getRecipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
public void setRecipients(Set<Player> recipients) {
|
||||
this.recipients.clear();
|
||||
this.recipients.addAll(recipients);
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return event.getPlayer();
|
||||
}
|
||||
|
||||
public Channel getChannel() {
|
||||
@@ -48,68 +119,16 @@ public class AsyncChatHookEvent extends Event {
|
||||
/*
|
||||
* Returns true if the hooked event was changed
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public boolean isChanged() {
|
||||
return changed;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Informs Chat if the event was changed or not
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public void setChanged(boolean changed) {
|
||||
this.changed = changed;
|
||||
}
|
||||
|
||||
public AsyncPlayerChatEvent getAsyncPlayerChatEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return event.getFormat();
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return event.getMessage();
|
||||
}
|
||||
|
||||
public Set<Player> getRecipients() {
|
||||
return this.recipients;
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return event.isCancelled();
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return event.getPlayer();
|
||||
}
|
||||
|
||||
public void setRecipients(Set<Player> recipients) {
|
||||
changed = true;
|
||||
this.recipients.clear();
|
||||
this.recipients.addAll(recipients);
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
changed = true;
|
||||
event.setFormat(format);
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
changed = true;
|
||||
event.setMessage(message);
|
||||
}
|
||||
|
||||
public void setCancelled(boolean cancel) {
|
||||
changed = (cancel);
|
||||
event.setCancelled(cancel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.palmergames.bukkit.TownyChat.events;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
@@ -23,11 +22,11 @@ public class PlayerJoinChatChannelEvent extends Event {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.palmergames.bukkit.TownyChat.handler;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.ChannelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.util.Adventure;
|
||||
import com.palmergames.bukkit.towny.TownyMessaging;
|
||||
import com.palmergames.bukkit.towny.object.Translatable;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class ChatEventProcess {
|
||||
public static void processChatEvent(AsyncChatEvent e, WeakHashMap<Player, String> directedChat) {
|
||||
if (e.isCancelled())
|
||||
return;
|
||||
|
||||
Player p = e.getPlayer();
|
||||
|
||||
// Save the message
|
||||
final Component originalMessage = e.originalMessage();
|
||||
final String originalMessageString = Adventure.plainText().serialize(originalMessage);
|
||||
|
||||
// Check if the player is muted by essentials
|
||||
if (isEssentialsMuted(p)) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean userForcedGlobal = ChatSettings.isExclamationPoint() && originalMessageString.startsWith("!");
|
||||
|
||||
// If the player is using /g or /n to send their message
|
||||
final boolean isDirectedChat = directedChat.containsKey(p);
|
||||
|
||||
// Get the user channel or select the first global channel
|
||||
Channel channel = isDirectedChat ? Chat.getTownyChat().getChannelsHandler().getChannel(directedChat.get(p)) : Chat.getTownyChat().getPlayerChannel(p);
|
||||
if (channel == null || !channel.hasSpeakPermission(p)) {
|
||||
channel = Chat.getTownyChat().getChannelsHandler().getActiveChannel(p, ChannelTypes.GLOBAL, userForcedGlobal);
|
||||
}
|
||||
|
||||
// No valid channel
|
||||
if (channel == null) {
|
||||
e.setCancelled(true);
|
||||
if (isDirectedChat)
|
||||
directedChat.remove(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// If muted
|
||||
if (isMutedOrSpam(e, channel, p)) {
|
||||
e.setCancelled(true);
|
||||
if (isDirectedChat)
|
||||
directedChat.remove(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// Post to chat
|
||||
channel.chatProcess(e);
|
||||
|
||||
// Remove player from directed chat if using it
|
||||
if (isDirectedChat && (!Chat.usingEssentialsDiscord || e.isCancelled())) {
|
||||
directedChat.remove(p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the player is channel-muted or channel-spamming and cancel the
|
||||
* {@link AsyncChatEvent} if this is the case.
|
||||
*
|
||||
* @param e {@link AsyncChatEvent} which has fired.
|
||||
* @param channel {@link Channel} being spoken in to.
|
||||
* @param p {@link Player} speaking.
|
||||
* @return true if the chat is muted or spammed.
|
||||
*/
|
||||
private static boolean isMutedOrSpam(AsyncChatEvent e, Channel channel, Player p) {
|
||||
if (channel.isMuted(p.getName())) {
|
||||
TownyMessaging.sendErrorMsg(p, Translatable.of("tc_err_you_are_currently_muted_in_channel", channel.getName()));
|
||||
return true;
|
||||
}
|
||||
return channel.isSpam(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this player Muted via Essentials?
|
||||
*
|
||||
* @param p {@link Player} speaking.
|
||||
* @return true if muted by Essentials.
|
||||
*/
|
||||
private static boolean isEssentialsMuted(Player p) {
|
||||
// Check if essentials has this player muted.
|
||||
if (Chat.getTownyChat().isEssentialsMuted(p)) {
|
||||
TownyMessaging.sendErrorMsg(p, Translatable.of("tc_err_unable_to_talk_essentials_mute"));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.palmergames.bukkit.TownyChat.listener;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.channels.ChannelTypes;
|
||||
import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -31,21 +31,21 @@ public class EssentialsDiscordHookListener implements Listener {
|
||||
}
|
||||
Channel channel = plugin.getChannelsHandler().getChannel(directChat);
|
||||
if (channel != null) {
|
||||
event.setCancelled(channel.getType() != channelTypes.GLOBAL || channel.getRange() != -1);
|
||||
event.setCancelled(channel.getType() != ChannelTypes.GLOBAL || channel.getRange() != -1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (Channel curChannel : plugin.getChannelsHandler().getAllChannels().values()) {
|
||||
if (plugin.getPlayerChannel(event.getPlayer()) != null && plugin.getPlayerChannel(event.getPlayer()).getName().equals(curChannel.getName())) {
|
||||
event.setCancelled(curChannel.getType() != channelTypes.GLOBAL || curChannel.getRange() != -1);
|
||||
event.setCancelled(curChannel.getType() != ChannelTypes.GLOBAL || curChannel.getRange() != -1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Channel channel = plugin.getChannelsHandler().getActiveChannel(event.getPlayer(), channelTypes.GLOBAL);
|
||||
Channel channel = plugin.getChannelsHandler().getActiveChannel(event.getPlayer(), ChannelTypes.GLOBAL);
|
||||
if (channel != null) {
|
||||
event.setCancelled(channel.getType() != channelTypes.GLOBAL || channel.getRange() != -1);
|
||||
event.setCancelled(channel.getType() != ChannelTypes.GLOBAL || channel.getRange() != -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.palmergames.bukkit.TownyChat.listener;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public class InternalTownyChatEvent {
|
||||
private final AsyncChatEvent event;
|
||||
private final Resident resident;
|
||||
|
||||
public InternalTownyChatEvent(AsyncChatEvent event, Resident resident) {
|
||||
this.event = event;
|
||||
this.resident = resident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resident associated with the chat event's talking player.
|
||||
*
|
||||
* @return resident associated with the chat event's talking player
|
||||
*/
|
||||
public Resident getResident() {
|
||||
return resident;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the AsyncChatEvent
|
||||
*/
|
||||
public AsyncChatEvent getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for getting the chat event's format
|
||||
*
|
||||
* @return the chat event's format
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public String getFormat() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for setting the chat event's format.
|
||||
*
|
||||
* @param format the chat event's format
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public void setFormat(String format) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for getting the chat event's message
|
||||
*
|
||||
* @return the chat event's message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event message
|
||||
* @return event message
|
||||
*/
|
||||
public Component message() {
|
||||
return event.message();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the event message
|
||||
* @param message event message
|
||||
*/
|
||||
public void message(Component message) {
|
||||
event.message(message);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.palmergames.bukkit.TownyChat.listener;
|
||||
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
|
||||
public class LocalTownyChatEvent {
|
||||
private AsyncPlayerChatEvent event;
|
||||
private Resident resident;
|
||||
|
||||
public LocalTownyChatEvent(AsyncPlayerChatEvent event, Resident resident) {
|
||||
this.event = event;
|
||||
this.resident = resident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resident associated with the chat event's talking player.
|
||||
* @return resident associated with the chat event's talking player
|
||||
*/
|
||||
public Resident getResident() {
|
||||
return resident;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the PlayerChatEvent
|
||||
*/
|
||||
public AsyncPlayerChatEvent getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for setting the chat event's format.
|
||||
* @param format
|
||||
*/
|
||||
public void setFormat(String format) {
|
||||
event.setFormat(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for getting the chat event's format
|
||||
* @return the chat event's format
|
||||
*/
|
||||
public String getFormat() {
|
||||
return event.getFormat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for getting the chat event's message
|
||||
* @return the chat event's message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return event.getMessage();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.palmergames.bukkit.TownyChat.listener;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.TownyChatFormatter;
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.handler.ChatEventProcess;
|
||||
import com.palmergames.bukkit.towny.TownyAPI;
|
||||
import com.palmergames.bukkit.towny.TownyMessaging;
|
||||
import com.palmergames.bukkit.towny.object.Resident;
|
||||
@@ -12,220 +11,93 @@ import com.palmergames.bukkit.towny.object.Translatable;
|
||||
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 io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.UnknownFormatConversionException;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class TownyChatPlayerListener implements Listener {
|
||||
private Chat plugin;
|
||||
|
||||
public WeakHashMap<Player, String> directedChat = new WeakHashMap<>();
|
||||
private final Chat plugin;
|
||||
|
||||
public TownyChatPlayerListener(Chat instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onPlayerJoin(final PlayerJoinEvent event) {
|
||||
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||
final Player p = e.getPlayer();
|
||||
plugin.getScheduler().runLater(p, () -> {
|
||||
checkPlayerForOldMeta(p);
|
||||
|
||||
plugin.getScheduler().runLater(event.getPlayer(), () -> loginPlayer(event.getPlayer()), 2L);
|
||||
|
||||
}
|
||||
|
||||
private void loginPlayer(Player player) {
|
||||
checkPlayerForOldMeta(player);
|
||||
|
||||
refreshPlayerChannels(player);
|
||||
refreshPlayerChannels(p);
|
||||
|
||||
Channel channel = plugin.getChannelsHandler().getDefaultChannel();
|
||||
if (channel != null && channel.hasSpeakPermission(player)) {
|
||||
plugin.setPlayerChannel(player, channel);
|
||||
if (channel != null && channel.hasSpeakPermission(p)) {
|
||||
plugin.setPlayerChannel(p, channel);
|
||||
if (ChatSettings.getShowChannelMessageOnServerJoin())
|
||||
TownyMessaging.sendMessage(player, Translatable.of("tc_you_are_now_talking_in_channel", channel.getName()));
|
||||
TownyMessaging.sendMessage(p, Translatable.of("tc_you_are_now_talking_in_channel", channel.getName()));
|
||||
}
|
||||
}, 2L);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onPlayerQuit(final PlayerQuitEvent event) {
|
||||
refreshPlayerChannels(event.getPlayer());
|
||||
public void onPlayerQuit(PlayerQuitEvent e) {
|
||||
refreshPlayerChannels(e.getPlayer());
|
||||
}
|
||||
|
||||
private void refreshPlayerChannels(Player player) {
|
||||
plugin.getChannelsHandler().getAllChannels().values().stream().forEach(channel -> channel.forgetPlayer(player));
|
||||
private void refreshPlayerChannels(Player p) {
|
||||
plugin.getChannelsHandler().getAllChannels().values().forEach(channel -> channel.forgetPlayer(p));
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
// Check if the message contains colour codes we need to remove or parse.
|
||||
testColourCodes(event, player);
|
||||
|
||||
// Check if essentials has this player muted.
|
||||
if (!isEssentialsMuted(player)) {
|
||||
|
||||
boolean forceGlobal = ChatSettings.isExclamationPoint() ? event.getMessage().startsWith("!") : false;
|
||||
|
||||
/*
|
||||
* If this was directed chat send it via the relevant channel
|
||||
*/
|
||||
if (directedChat.containsKey(player)) {
|
||||
Channel channel = plugin.getChannelsHandler().getChannel(directedChat.get(player));
|
||||
|
||||
if (channel != null) {
|
||||
if (isMutedOrSpam(event, channel, player)) {
|
||||
directedChat.remove(player);
|
||||
return;
|
||||
}
|
||||
channel.chatProcess(event);
|
||||
if (!Chat.usingEssentialsDiscord || event.isCancelled()) {
|
||||
directedChat.remove(player);
|
||||
}
|
||||
return;
|
||||
}
|
||||
directedChat.remove(player);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the player for any channel modes.
|
||||
*/
|
||||
Channel channel = plugin.getPlayerChannel(player);
|
||||
if (!forceGlobal && channel != null && channel.hasSpeakPermission(player)) {
|
||||
if (isMutedOrSpam(event, channel, player))
|
||||
return;
|
||||
channel.chatProcess(event);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a global channel this player has permissions for.
|
||||
*/
|
||||
channel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL, forceGlobal);
|
||||
if (channel != null) {
|
||||
if (isMutedOrSpam(event, channel, player))
|
||||
return;
|
||||
if (forceGlobal)
|
||||
event.setMessage(event.getMessage().substring(1));
|
||||
channel.chatProcess(event);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* We found no channels available so modify the chat (if enabled) and exit.
|
||||
*/
|
||||
if (ChatSettings.isModify_chat()) {
|
||||
Resident resident = TownyAPI.getInstance().getResident(player);
|
||||
if (resident == null)
|
||||
return;
|
||||
// Nuke the channeltag and message colouring, but apply the remaining format.
|
||||
String format = ChatSettings.getChannelFormat(player, channelTypes.GLOBAL).replace("{channelTag}", "").replace("{msgcolour}", "");
|
||||
|
||||
// format is left to store the original non-PAPI-parsed chat format.
|
||||
String newFormat = format;
|
||||
|
||||
// Parse any PAPI placeholders.
|
||||
if (Chat.usingPlaceholderAPI)
|
||||
newFormat = PlaceholderAPI.setPlaceholders(player, format);
|
||||
|
||||
// Attempt to apply the new format.
|
||||
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);
|
||||
|
||||
// Attempt to apply the new format.
|
||||
catchFormatConversionException(event, format, newFormat);
|
||||
|
||||
// Set the format based on the global channelformat, with channeltag and msgcolour removed.
|
||||
event.setFormat(newFormat);
|
||||
}
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onPlayerChat(AsyncChatEvent e) {
|
||||
ChatEventProcess.processChatEvent(e, directedChat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove colour tags that a player doesn't not have permission for,
|
||||
* and colour messages if the player is allowed.
|
||||
*
|
||||
* @param event {@link AsyncPlayerChatEvent} which has been fired.
|
||||
* @param event {@link AsyncChatEvent} which has been fired.
|
||||
* @param player {@link Player} which has spoken.
|
||||
*/
|
||||
private void testColourCodes(AsyncPlayerChatEvent event, Player player) {
|
||||
if ((event.getMessage().contains("&L") || event.getMessage().contains("&l"))
|
||||
private void testColourCodes(AsyncChatEvent event, Player player) {
|
||||
Component message = event.message();
|
||||
String messageString = message.toString();
|
||||
|
||||
if ((messageString.contains("&L") || messageString.contains("&l"))
|
||||
&& !player.hasPermission("townychat.chat.format.bold"))
|
||||
event.setMessage(event.getMessage().replaceAll("&L", "").replaceAll("&l", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&L", "").replaceAll("&l", "")));
|
||||
|
||||
if ((event.getMessage().contains("&O") || event.getMessage().contains("&o"))
|
||||
if ((messageString.contains("&O") || messageString.contains("&o"))
|
||||
&& !player.hasPermission("townychat.chat.format.italic"))
|
||||
event.setMessage(event.getMessage().replaceAll("&O", "").replaceAll("&o", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&O", "").replaceAll("&o", "")));
|
||||
|
||||
if ((event.getMessage().contains("&K") || event.getMessage().contains("&k"))
|
||||
if ((messageString.contains("&K") || messageString.contains("&k"))
|
||||
&& !player.hasPermission("townychat.chat.format.magic"))
|
||||
event.setMessage(event.getMessage().replaceAll("&K", "").replaceAll("&k", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&K", "").replaceAll("&k", "")));
|
||||
|
||||
if ((event.getMessage().contains("&N") || event.getMessage().contains("&n"))
|
||||
if ((messageString.contains("&N") || messageString.contains("&n"))
|
||||
&& !player.hasPermission("townychat.chat.format.underlined"))
|
||||
event.setMessage(event.getMessage().replaceAll("&N", "").replaceAll("&n", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&N", "").replaceAll("&n", "")));
|
||||
|
||||
if ((event.getMessage().contains("&M") || event.getMessage().contains("&m"))
|
||||
if ((messageString.contains("&M") || messageString.contains("&m"))
|
||||
&& !player.hasPermission("townychat.chat.format.strike"))
|
||||
event.setMessage(event.getMessage().replaceAll("&M", "").replaceAll("&m", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&M", "").replaceAll("&m", "")));
|
||||
|
||||
if ((event.getMessage().contains("&R") || event.getMessage().contains("&r"))
|
||||
if ((messageString.contains("&R") || messageString.contains("&r"))
|
||||
&& !player.hasPermission("townychat.chat.format.reset"))
|
||||
event.setMessage(event.getMessage().replaceAll("&R", "").replaceAll("&r", ""));
|
||||
event.message(Component.text(messageString.replaceAll("&R", "").replaceAll("&r", "")));
|
||||
|
||||
if (player.hasPermission("townychat.chat.color"))
|
||||
event.setMessage(Colors.translateColorCodes(event.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this player Muted via Essentials?
|
||||
*
|
||||
* @param player {@link Player} speaking.
|
||||
* @return true if muted by Essentials.
|
||||
*/
|
||||
private boolean isEssentialsMuted(Player player) {
|
||||
// Check if essentials has this player muted.
|
||||
if (plugin.isEssentialsMuted(player)) {
|
||||
TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_unable_to_talk_essentials_mute"));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the player is channel-muted or channel-spamming and cancel the
|
||||
* {@link AsyncPlayerChatEvent} if this is the case.
|
||||
*
|
||||
* @param event {@link AsyncPlayerChatEvent} which has fired.
|
||||
* @param channel {@link Channel} being spoken in to.
|
||||
* @param player {@link Player} speaking.
|
||||
* @return true if the chat is muted or spammed.
|
||||
*/
|
||||
private boolean isMutedOrSpam(AsyncPlayerChatEvent event, Channel channel, Player player) {
|
||||
if (channel.isMuted(player.getName())) {
|
||||
TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_you_are_currently_muted_in_channel", channel.getName()));
|
||||
event.setCancelled(true);
|
||||
return true;
|
||||
}
|
||||
if (channel.isSpam(player)) {
|
||||
event.setCancelled(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
event.message(Component.text(Colors.translateColorCodes(messageString)));
|
||||
}
|
||||
|
||||
// From TownyChat 0.84-0.95 the symbol used to separate the channels in the meta
|
||||
@@ -271,30 +143,4 @@ public class TownyChatPlayerListener implements Listener {
|
||||
return new String(charray);
|
||||
}
|
||||
|
||||
private void catchFormatConversionException(AsyncPlayerChatEvent event, String format, String newFormat) {
|
||||
try {
|
||||
event.setFormat(newFormat);
|
||||
} catch (UnknownFormatConversionException e) {
|
||||
// This exception is usually thrown when a PAPI placeholder did not get parsed
|
||||
// and has left behind a % symbol followed by something that String#format
|
||||
// cannot handle.
|
||||
boolean percentSymbol = format.contains("%" + e.getConversion());
|
||||
String errmsg = "TownyChat tried to apply a chat format that is not allowed: '" +
|
||||
newFormat + "', because of the " + e.getConversion() + " symbol" +
|
||||
(percentSymbol ? ", found after a %. There is probably a PAPIPlaceholder that could not be parsed." : "." +
|
||||
" You should attempt to correct this in your towny\\settings\\chatconfig.yml file and use /townychat reload.");
|
||||
Chat.getTownyChat().getLogger().severe(errmsg);
|
||||
|
||||
if (percentSymbol)
|
||||
// Attempt to remove the unparsed placeholder and send this right back.
|
||||
catchFormatConversionException(event, format, purgePAPI(newFormat, "%" + e.getConversion()));
|
||||
else
|
||||
// Just let the chat go, this results in an error in the log, and TownyChat not being able to format chat.
|
||||
event.setFormat(format);
|
||||
}
|
||||
}
|
||||
|
||||
private String purgePAPI(String format, String startOfPlaceholder) {
|
||||
return format.replaceAll(startOfPlaceholder + ".*%", "");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.palmergames.bukkit.TownyChat.tasks;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.towny.Towny;
|
||||
|
||||
/*
|
||||
@@ -33,7 +32,6 @@ public class onLoadedTask implements Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
ChatSettings.loadWorldFormats();
|
||||
plugin.getLogger().info("-******* TownyChat enabled *******-");
|
||||
plugin.registerPermissions();
|
||||
plugin.registerEvents();
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.palmergames.bukkit.TownyChat.util;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
|
||||
public class Adventure {
|
||||
private static final PlainTextComponentSerializer plainText = PlainTextComponentSerializer.plainText();
|
||||
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
public static PlainTextComponentSerializer plainText() {
|
||||
return plainText;
|
||||
}
|
||||
|
||||
public static MiniMessage miniMessage() {
|
||||
return miniMessage;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.palmergames.bukkit.TownyChat.util;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class EssentialsIntegration {
|
||||
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.palmergames.bukkit.TownyChat.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.TownyChatReplacer;
|
||||
import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent;
|
||||
|
||||
|
||||
/**
|
||||
* Register keyword regex's to be dynamically replaced.
|
||||
*
|
||||
* Credits to:
|
||||
* - http://stackoverflow.com/questions/1326682/java-replacing-multiple-different-substring-in-a-string-at-once-or-in-the-most-e/1326962#1326962
|
||||
*
|
||||
* Improved in v 2.0 to use Java 8's Function.
|
||||
*
|
||||
* @author Chris H (Shade) 1.0, LlmDl 2.0
|
||||
* @version 2.0
|
||||
*/
|
||||
public class StringReplaceManager<E> {
|
||||
private List<Pattern> replacementPatterns = new ArrayList<Pattern>();
|
||||
private List<TownyChatReplacer> replacements = new ArrayList<TownyChatReplacer>();
|
||||
|
||||
public boolean registerReplacer(String chatSlug, Function<LocalTownyChatEvent, String> replacement) {
|
||||
Pattern pattern = Pattern.compile(Pattern.quote(chatSlug));
|
||||
replacementPatterns.add(pattern);
|
||||
replacements.add(new TownyChatReplacer(replacement));
|
||||
return true;
|
||||
}
|
||||
|
||||
public String replaceAll(String format, LocalTownyChatEvent e) {
|
||||
String out = format;
|
||||
for (int i = 0; i < replacementPatterns.size(); i++) {
|
||||
Matcher matcher = replacementPatterns.get(i).matcher(out);
|
||||
TownyChatReplacer replacer = replacements.get(i);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
while (matcher.find()) {
|
||||
if (replacer == null) {
|
||||
matcher.appendReplacement(sb, e.toString());
|
||||
} else {
|
||||
try {
|
||||
String replacement = Matcher.quoteReplacement(replacer.getWith(e));
|
||||
matcher.appendReplacement(sb, replacement);
|
||||
} catch (Exception exception) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
out = sb.toString();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
package com.palmergames.bukkit.TownyChat.util;
|
||||
|
||||
import com.github.milkdrinkers.colorparser.ColorParser;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.listener.InternalTownyChatEvent;
|
||||
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.util.StringMgmt;
|
||||
import net.kyori.adventure.text.minimessage.tag.Tag;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TownyChatFormatter {
|
||||
public static @NotNull TagResolver parser(InternalTownyChatEvent e) {
|
||||
final Player p = e.getEvent().getPlayer();
|
||||
final Resident res = e.getResident();
|
||||
|
||||
return TagResolver.resolver(
|
||||
TagResolver.resolver("worldname", Tag.inserting(ColorParser.of(getWorldTag(e)).parseLegacy().build())),
|
||||
TagResolver.resolver("town", Tag.inserting(ColorParser.of(getTownName(res)).parseLegacy().build())),
|
||||
TagResolver.resolver("townformatted", Tag.inserting(ColorParser.of(formatTownTag(res, false, true)).parseLegacy().build())),
|
||||
TagResolver.resolver("towntag", Tag.inserting(ColorParser.of(formatTownTag(res, false, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("towntagoverride", Tag.inserting(ColorParser.of(formatTownTag(res, true, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("nation", Tag.inserting(ColorParser.of(getNationName(res)).parseLegacy().build())),
|
||||
TagResolver.resolver("nationformatted", Tag.inserting(ColorParser.of(formatNationTag(res, false, true)).parseLegacy().build())),
|
||||
TagResolver.resolver("nationtag", Tag.inserting(ColorParser.of(formatNationTag(res, false, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("nationtagoverride", Tag.inserting(ColorParser.of(formatNationTag(res, true, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("townytag", Tag.inserting(ColorParser.of(formatTownyTag(res, false, true)).parseLegacy().build())),
|
||||
TagResolver.resolver("townyformatted", Tag.inserting(ColorParser.of(formatTownyTag(res, false, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("townytagoverride", Tag.inserting(ColorParser.of(formatTownyTag(res, true, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("title", Tag.inserting(ColorParser.of(getPrefix(res, true)).parseLegacy().build())),
|
||||
TagResolver.resolver("surname", Tag.inserting(ColorParser.of(getSuffix(res, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("townynameprefix", Tag.inserting(ColorParser.of(getNamePrefix(res)).parseLegacy().build())),
|
||||
TagResolver.resolver("townynamepostfix", Tag.inserting(ColorParser.of(getNamePostfix(res)).parseLegacy().build())),
|
||||
TagResolver.resolver("townyprefix", Tag.inserting(ColorParser.of(getPrefix(res, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("townypostfix", Tag.inserting(ColorParser.of(getSuffix(res, false)).parseLegacy().build())),
|
||||
TagResolver.resolver("townycolor", Tag.inserting(ColorParser.of(getTownyColour(res)).parseLegacy().build())),
|
||||
TagResolver.resolver("group", Tag.inserting(ColorParser.of(getVaultGroup(p)).parseLegacy().build())),
|
||||
TagResolver.resolver("permprefix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "prefix")).parseLegacy().build())),
|
||||
TagResolver.resolver("permsuffix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "suffix")).parseLegacy().build())),
|
||||
TagResolver.resolver("permuserprefix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "userprefix")).parseLegacy().build())),
|
||||
TagResolver.resolver("permusersuffix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "usersuffix")).parseLegacy().build())),
|
||||
TagResolver.resolver("permgroupprefix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "groupprefix")).parseLegacy().build())),
|
||||
TagResolver.resolver("permgroupsuffix", Tag.inserting(ColorParser.of(getVaultPrefixSuffix(res, "groupsuffix")).parseLegacy().build())),
|
||||
TagResolver.resolver("playername", Tag.inserting(ColorParser.of(p.getName()).parseLegacy().build())),
|
||||
TagResolver.resolver("modplayername", Tag.inserting(ColorParser.of(Adventure.plainText().serialize(p.displayName())).parseLegacy().build())),
|
||||
TagResolver.resolver("primaryresidentrank", Tag.inserting(ColorParser.of(getPrimaryRankPrefix(res)).parseLegacy().build()))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static String getWorldTag(InternalTownyChatEvent event) {
|
||||
return String.format(ChatSettings.getWorldTag(), event.getEvent().getPlayer().getWorld().getName());
|
||||
}
|
||||
|
||||
public static String getTownName(Resident resident) {
|
||||
return resident.getTownOrNull() != null ? resident.getTownOrNull().getName() : "";
|
||||
}
|
||||
|
||||
public static String getNationName(Resident resident) {
|
||||
return resident.getNationOrNull() != null ? resident.getNationOrNull().getName() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resident the resident
|
||||
* @param override use full names if no tag is present
|
||||
* @param full Only use full names (no tags).
|
||||
* @return string containing the correctly formatted nation/town data
|
||||
*/
|
||||
public static String formatTownyTag(Resident resident, Boolean override, Boolean full) {
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
|
||||
|
||||
if (town == null)
|
||||
return "";
|
||||
|
||||
String townTag = getTag(town);
|
||||
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(resident);
|
||||
String nationTag = nation != null ? getTag(nation) : "";
|
||||
String nTag = "", tTag = "";
|
||||
|
||||
// Force use of full names only
|
||||
if (full) {
|
||||
nationTag = "";
|
||||
townTag = "";
|
||||
}
|
||||
|
||||
// Load town tags/names
|
||||
if (townTag != null && !townTag.isEmpty())
|
||||
tTag = townTag;
|
||||
else if (override || full)
|
||||
tTag = getName(town);
|
||||
|
||||
// Load the nation tags/names
|
||||
if ((nationTag != null) && !nationTag.isEmpty())
|
||||
nTag = nationTag;
|
||||
else if (nation != null && (override || full))
|
||||
nTag = getName(nation);
|
||||
|
||||
// Output depending on what tags are present
|
||||
if ((!tTag.isEmpty()) && (!nTag.isEmpty())) {
|
||||
if (ChatSettings.getBothTag().contains("%t") || ChatSettings.getBothTag().contains("%n")) {
|
||||
// Then it contains %s & %s
|
||||
// Small suttle change so that an issue is solved, it is documented in the config.
|
||||
// But only after addition of this. (v0.50)
|
||||
return ChatSettings.getBothTag().replace("%t", tTag).replace("%n", nTag);
|
||||
} else {
|
||||
return String.format(ChatSettings.getBothTag(), nTag, tTag);
|
||||
}
|
||||
}
|
||||
|
||||
if (!nTag.isEmpty()) {
|
||||
return String.format(ChatSettings.getNationTag(), nTag);
|
||||
}
|
||||
|
||||
if (!tTag.isEmpty()) {
|
||||
return String.format(ChatSettings.getTownTag(), tTag);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String formatTownTag(Resident resident, Boolean override, Boolean full) {
|
||||
Town town = TownyAPI.getInstance().getResidentTownOrNull(resident);
|
||||
|
||||
if (town == null)
|
||||
return "";
|
||||
|
||||
if (full)
|
||||
return String.format(ChatSettings.getTownTag(), getName(town));
|
||||
else if (town.hasTag())
|
||||
return String.format(ChatSettings.getTownTag(), getTag(town));
|
||||
else if (override)
|
||||
return String.format(ChatSettings.getTownTag(), getName(town));
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String formatNationTag(Resident res, Boolean override, Boolean full) {
|
||||
Nation nation = TownyAPI.getInstance().getResidentNationOrNull(res);
|
||||
|
||||
if (nation == null)
|
||||
return "";
|
||||
|
||||
if (full)
|
||||
return String.format(ChatSettings.getNationTag(), getName(nation));
|
||||
else if (nation.hasTag())
|
||||
return String.format(ChatSettings.getNationTag(), getTag(nation));
|
||||
else if (override)
|
||||
return String.format(ChatSettings.getNationTag(), getName(nation));
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getNamePrefix(Resident res) {
|
||||
if (res == null)
|
||||
return "";
|
||||
|
||||
if (res.isKing())
|
||||
return TownySettings.getKingPrefix(res);
|
||||
else if (res.isMayor())
|
||||
return TownySettings.getMayorPrefix(res);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getNamePostfix(Resident res) {
|
||||
if (res == null)
|
||||
return "";
|
||||
|
||||
if (res.isKing())
|
||||
return TownySettings.getKingPostfix(res);
|
||||
else if (res.isMayor())
|
||||
return TownySettings.getMayorPostfix(res);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getPrimaryRankPrefix(Resident res) {
|
||||
if (res == null)
|
||||
return "";
|
||||
|
||||
String rank = res.getPrimaryRankPrefix();
|
||||
return rank.isEmpty() ? "" : rank + " ";
|
||||
}
|
||||
|
||||
private static String getName(Government gov) {
|
||||
return StringMgmt.remUnderscore(gov.getName());
|
||||
}
|
||||
|
||||
private static String getTag(Government gov) {
|
||||
return gov.getTag();
|
||||
}
|
||||
|
||||
public static String getPrefix(Resident res, boolean isBlank) {
|
||||
return res.hasTitle() ? res.getTitle() + " " : isBlank ? "" : getNamePrefix(res);
|
||||
}
|
||||
|
||||
public static String getSuffix(Resident res, boolean isBlank) {
|
||||
return res.hasSurname() ? " " + res.getSurname() : isBlank ? "" : getNamePostfix(res);
|
||||
}
|
||||
|
||||
public static String getTownyColour(Resident res) {
|
||||
return !res.hasTown()
|
||||
? ChatSettings.getNomadColour()
|
||||
: res.isMayor() ? (res.isKing() ? ChatSettings.getKingColour() : ChatSettings.getMayorColour()) : ChatSettings.getResidentColour();
|
||||
}
|
||||
|
||||
public static String getVaultGroup(Player p) {
|
||||
return TownyUniverse.getInstance().getPermissionSource().getPlayerGroup(p);
|
||||
}
|
||||
|
||||
public static String getVaultPrefixSuffix(Resident res, String slug) {
|
||||
return TownyUniverse.getInstance().getPermissionSource().getPrefixSuffix(res, slug);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user