feat: allow unlimited channel formats

- The original implementation was weirdly splitting configuration and limiting customization of the plugin.
- This moves all format configuration directly into each channels config.
- Generates default config entries.
This commit is contained in:
darksaid98 committed 2024-01-12 13:37:41 +01:00
1 parent 93df8fb296
commit 465400dfd5
8 files changed
+130 -195

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@
<groupId>com.palmergames.bukkit</groupId>
<artifactId>TownyChat</artifactId>
<packaging>jar</packaging>
<version>0.110</version>
<version>0.111</version>
<licenses>
<license>
@@ -1,27 +1,26 @@
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 org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public abstract class Channel {
private String name;
private List<String> commands;
private channelTypes type;
private String channelTag, messageColour, permission, leavePermission, channelSound, listenPermission, speakPermission;
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;
@@ -63,12 +62,42 @@ public abstract class Channel {
public channelTypes getType() {
return 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 type the type to set
*/
public void setType(channelTypes type) {
this.type = type;
}
/**
* @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
*/
@@ -496,4 +525,42 @@ 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;
}
}
}
@@ -68,7 +68,7 @@ public class StandardChannel extends Channel {
}
// Set the channel specific format
String format = ChatSettings.getChannelFormat(player, channelType);
String format = getFormat(player);
// Get the list of message recipients.
Set<Player> recipients = getRecipients(player, town, nation, channelType, event.getRecipients());
@@ -5,6 +5,8 @@ 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.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;
@@ -26,9 +28,9 @@ 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");
/**
*
*
* @return true if the channels.yml has loaded.
* @throws IOException
*/
@@ -102,7 +104,7 @@ public class ChannelsSettings {
newChannelConfig.createSection(CHANNELS_ROOT);
for (String channel : DEFAULT_CHANNELS)
newChannelConfig.createSection(CHANNELS_ROOT + "." + channel);
ConfigurationSection configurationSection = newChannelConfig.getConfigurationSection(CHANNELS_ROOT);
configurationSection.set("general", generalDefaults());
configurationSection.set("town", townDefaults());
@@ -118,6 +120,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -131,6 +135,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -143,6 +149,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -155,6 +163,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -167,6 +177,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -178,6 +190,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -189,6 +203,8 @@ public class ChannelsSettings {
Map<String, Object> channelMap = new LinkedHashMap<>();
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");
@@ -237,6 +253,8 @@ public class ChannelsSettings {
// The following will always be present in some manner
channel.setCommands(data.getCommands());
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());
@@ -268,7 +286,7 @@ public class ChannelsSettings {
/**
* Constructor
*
*
* @param name Channel name
*/
public ChannelDetails(CommentedConfiguration channelsConfig, String name) {
@@ -305,6 +323,31 @@ public class ChannelsSettings {
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("channeltag");
}
@@ -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,8 @@ 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,27 +1,20 @@
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;
public class ChatSettings {
private static Map<String, WorldFormat> worldFormatGroups = new HashMap<String, WorldFormat>();
private static CommentedConfiguration chatConfig, newChatConfig;
/**
@@ -62,9 +55,7 @@ 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 (root.getRoot() == ChatConfigNodes.VERSION.getRoot())
setNewProperty(root.getRoot(), version);
else if (root.getRoot() == ChatConfigNodes.LAST_RUN_VERSION.getRoot())
setNewProperty(root.getRoot(), getLastRunVersion(version));
@@ -75,31 +66,6 @@ public class ChatSettings {
chatConfig = newChatConfig;
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);
@@ -223,119 +189,8 @@ public class ChatSettings {
public static String getNomadColour() {
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;
}
}
}
@@ -12,9 +12,7 @@ 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 org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -129,7 +127,7 @@ public class TownyChatPlayerListener implements Listener {
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}", "");
String format = plugin.getPlayerChannel(player).getFormat(player).replace("{channelTag}", "").replace("{msgcolour}", "");
// format is left to store the original non-PAPI-parsed chat format.
String newFormat = format;
@@ -33,7 +33,6 @@ public class onLoadedTask implements Runnable {
return;
}
ChatSettings.loadWorldFormats();
plugin.getLogger().info("-******* TownyChat enabled *******-");
plugin.registerPermissions();
plugin.registerEvents();