mirror of
https://github.com/darksaid98/TownyChat.git
synced 2026-12-04 14:38:58 +00:00
Replace channelFormats with ChatSettings inner class WorldFormat
This commit is contained in:
1 parent
d2338ede07
commit
483a79c2b3
5 files changed
+145
-205
No files matched your search
@@ -6,7 +6,7 @@ import com.palmergames.bukkit.TownyChat.Command.commandobjects.ChannelJoinAliasC
|
|||||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||||
import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder;
|
import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder;
|
||||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||||
import com.palmergames.bukkit.TownyChat.config.ConfigurationHandler;
|
import com.palmergames.bukkit.TownyChat.config.ChannelConfigurationHandler;
|
||||||
import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener;
|
import com.palmergames.bukkit.TownyChat.listener.EssentialsDiscordHookListener;
|
||||||
import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener;
|
import com.palmergames.bukkit.TownyChat.listener.TownyChatPlayerListener;
|
||||||
import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask;
|
import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask;
|
||||||
@@ -46,7 +46,7 @@ public class Chat extends JavaPlugin {
|
|||||||
|
|
||||||
private TownyChatPlayerListener TownyPlayerListener;
|
private TownyChatPlayerListener TownyPlayerListener;
|
||||||
private ChannelsHolder channels;
|
private ChannelsHolder channels;
|
||||||
private ConfigurationHandler channelsConfig;
|
private ChannelConfigurationHandler channelsConfig;
|
||||||
|
|
||||||
protected PluginManager pm;
|
protected PluginManager pm;
|
||||||
private static Chat chat = null;
|
private static Chat chat = null;
|
||||||
@@ -65,7 +65,7 @@ public class Chat extends JavaPlugin {
|
|||||||
|
|
||||||
chat = this;
|
chat = this;
|
||||||
pm = getServer().getPluginManager();
|
pm = getServer().getPluginManager();
|
||||||
channelsConfig = new ConfigurationHandler(this);
|
channelsConfig = new ChannelConfigurationHandler(this);
|
||||||
channels = new ChannelsHolder(this);
|
channels = new ChannelsHolder(this);
|
||||||
playerChannelMap = new ConcurrentHashMap<>();
|
playerChannelMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@@ -229,7 +229,7 @@ public class Chat extends JavaPlugin {
|
|||||||
/**
|
/**
|
||||||
* @return the data
|
* @return the data
|
||||||
*/
|
*/
|
||||||
public ConfigurationHandler getConfigurationHandler() {
|
public ChannelConfigurationHandler getConfigurationHandler() {
|
||||||
return channelsConfig;
|
return channelsConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,108 +0,0 @@
|
|||||||
package com.palmergames.bukkit.TownyChat.channels;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class channelFormats {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
*/
|
|
||||||
public channelFormats(String name) {
|
|
||||||
super();
|
|
||||||
this.name = name.toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String name, GLOBAL, TOWN, NATION, ALLIANCE, DEFAULT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the name
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param name
|
|
||||||
* the name to set
|
|
||||||
*/
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the gLOBAL
|
|
||||||
*/
|
|
||||||
public String getGLOBAL() {
|
|
||||||
return GLOBAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param GLOBAL
|
|
||||||
* the gLOBAL to set
|
|
||||||
*/
|
|
||||||
public void setGLOBAL(String GLOBAL) {
|
|
||||||
this.GLOBAL = GLOBAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the TOWN
|
|
||||||
*/
|
|
||||||
public String getTOWN() {
|
|
||||||
return this.TOWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TOWN
|
|
||||||
* the TOWN to set
|
|
||||||
*/
|
|
||||||
public void setTOWN(String TOWN) {
|
|
||||||
this.TOWN = TOWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the NATION
|
|
||||||
*/
|
|
||||||
public String getNATION() {
|
|
||||||
return this.NATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param NATION
|
|
||||||
* the nATION to set
|
|
||||||
*/
|
|
||||||
public void setNATION(String NATION) {
|
|
||||||
this.NATION = NATION;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the ALLIANCE
|
|
||||||
*/
|
|
||||||
public String getALLIANCE() {
|
|
||||||
return this.ALLIANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ALLIANCE
|
|
||||||
* the ALLIANCE to set
|
|
||||||
*/
|
|
||||||
public void setALLIANCE(String ALLIANCE) {
|
|
||||||
this.ALLIANCE = ALLIANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the DEFAULT
|
|
||||||
*/
|
|
||||||
public String getDEFAULT() {
|
|
||||||
return this.DEFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param DEFAULT
|
|
||||||
* the dEFAULT to set
|
|
||||||
*/
|
|
||||||
public void setDEFAULT(String DEFAULT) {
|
|
||||||
this.DEFAULT = DEFAULT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+68
-4
@@ -4,17 +4,28 @@ import com.palmergames.bukkit.TownyChat.Chat;
|
|||||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||||
import com.palmergames.bukkit.TownyChat.channels.StandardChannel;
|
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.TownyChat.util.FileMgmt;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
|
import org.yaml.snakeyaml.reader.UnicodeReader;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class ConfigurationHandler {
|
public class ChannelConfigurationHandler {
|
||||||
|
|
||||||
private Chat plugin;
|
private Chat plugin;
|
||||||
|
|
||||||
@@ -22,7 +33,7 @@ public class ConfigurationHandler {
|
|||||||
*
|
*
|
||||||
* @param plugin
|
* @param plugin
|
||||||
*/
|
*/
|
||||||
public ConfigurationHandler(Chat plugin) {
|
public ChannelConfigurationHandler(Chat plugin) {
|
||||||
super();
|
super();
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
@@ -42,7 +53,7 @@ public class ConfigurationHandler {
|
|||||||
|
|
||||||
Map<String, Object> channelsMap;
|
Map<String, Object> channelsMap;
|
||||||
try {
|
try {
|
||||||
channelsMap = FileMgmt.getMap(filename, "Channels.yml");
|
channelsMap = getMap(filename, "Channels.yml");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Bukkit.getLogger().severe("[TownyChat] Failed to load Channels.yml!");
|
Bukkit.getLogger().severe("[TownyChat] Failed to load Channels.yml!");
|
||||||
Bukkit.getLogger().severe("[TownyChat] Please check that the file passes a YAML Parser test:");
|
Bukkit.getLogger().severe("[TownyChat] Please check that the file passes a YAML Parser test:");
|
||||||
@@ -153,4 +164,57 @@ public class ConfigurationHandler {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Object> getMap(String filepath, String resource) {
|
||||||
|
|
||||||
|
File f = new File(filepath);
|
||||||
|
if (!(f.exists() && f.isFile())) {
|
||||||
|
// Populate a new file
|
||||||
|
try {
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(f)) {
|
||||||
|
fos.write(getResourceFileAsString("/" + resource).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// No resource file found
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f = new File(filepath);
|
||||||
|
|
||||||
|
Yaml yamlChannels = new Yaml(new SafeConstructor());
|
||||||
|
Object channelsRootDataNode;
|
||||||
|
|
||||||
|
try (FileInputStream fileInputStream = new FileInputStream(f)) {
|
||||||
|
channelsRootDataNode = yamlChannels.load(new UnicodeReader(fileInputStream));
|
||||||
|
if (channelsRootDataNode == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + f.getPath(), ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channelsRootDataNode instanceof Map)
|
||||||
|
return (Map<String, Object>) channelsRootDataNode;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads given resource file as a string.
|
||||||
|
*
|
||||||
|
* @param fileName path to the resource file
|
||||||
|
* @return the file's contents
|
||||||
|
* @throws IOException if read fails for any reason
|
||||||
|
*/
|
||||||
|
static String getResourceFileAsString(String fileName) throws IOException {
|
||||||
|
if (fileName == null)
|
||||||
|
return "";
|
||||||
|
try (InputStream is = ChannelConfigurationHandler.class.getResourceAsStream(fileName);
|
||||||
|
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
||||||
|
BufferedReader reader = new BufferedReader(isr)) {
|
||||||
|
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.palmergames.bukkit.TownyChat.config;
|
package com.palmergames.bukkit.TownyChat.config;
|
||||||
|
|
||||||
import com.palmergames.bukkit.TownyChat.Chat;
|
import com.palmergames.bukkit.TownyChat.Chat;
|
||||||
import com.palmergames.bukkit.TownyChat.channels.channelFormats;
|
|
||||||
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
||||||
import com.palmergames.bukkit.config.CommentedConfiguration;
|
import com.palmergames.bukkit.config.CommentedConfiguration;
|
||||||
import com.palmergames.bukkit.config.ConfigNodes;
|
import com.palmergames.bukkit.config.ConfigNodes;
|
||||||
@@ -22,7 +21,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class ChatSettings {
|
public class ChatSettings {
|
||||||
|
|
||||||
private static Map<String, channelFormats> worldFormatGroups = new HashMap<String, channelFormats>();
|
private static Map<String, WorldFormat> worldFormatGroups = new HashMap<String, WorldFormat>();
|
||||||
private static CommentedConfiguration chatConfig, newChatConfig;
|
private static CommentedConfiguration chatConfig, newChatConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -224,16 +223,9 @@ public class ChatSettings {
|
|||||||
|
|
||||||
public static String getChannelFormat(Player player, channelTypes type) {
|
public static String getChannelFormat(Player player, channelTypes type) {
|
||||||
if (isPer_world()) {
|
if (isPer_world()) {
|
||||||
String name = player.getWorld().getName().toLowerCase(Locale.ROOT);
|
String worldName = player.getWorld().getName().toLowerCase(Locale.ROOT);
|
||||||
if (worldFormatGroups.containsKey(name)) {
|
if (worldFormatGroups.containsKey(worldName)) {
|
||||||
channelFormats worldFormat = worldFormatGroups.get(name);
|
String format = worldFormatGroups.get(worldName).getFormat(type);
|
||||||
String format = switch (type) {
|
|
||||||
case GLOBAL, PRIVATE -> worldFormat.getGLOBAL();
|
|
||||||
case NATION -> worldFormat.getNATION();
|
|
||||||
case TOWN -> worldFormat.getTOWN();
|
|
||||||
case ALLIANCE -> worldFormat.getALLIANCE();
|
|
||||||
case DEFAULT -> worldFormat.getDEFAULT();
|
|
||||||
};
|
|
||||||
if (format != null && !format.isEmpty())
|
if (format != null && !format.isEmpty())
|
||||||
return format;
|
return format;
|
||||||
}
|
}
|
||||||
@@ -254,7 +246,7 @@ public class ChatSettings {
|
|||||||
String worldName = world.getName();
|
String worldName = world.getName();
|
||||||
String path = "worlds." + worldName;
|
String path = "worlds." + worldName;
|
||||||
if (chatConfig.contains(path)) {
|
if (chatConfig.contains(path)) {
|
||||||
channelFormats worldFormat = new channelFormats(worldName);
|
ChatSettings.WorldFormat worldFormat = new ChatSettings().new WorldFormat(worldName);
|
||||||
worldFormat.setGLOBAL(getOrDefault(path + ".global", ChatConfigNodes.CHANNEL_FORMATS_GLOBAL));
|
worldFormat.setGLOBAL(getOrDefault(path + ".global", ChatConfigNodes.CHANNEL_FORMATS_GLOBAL));
|
||||||
worldFormat.setDEFAULT(getOrDefault(path + ".default", ChatConfigNodes.CHANNEL_FORMATS_DEFAULT));
|
worldFormat.setDEFAULT(getOrDefault(path + ".default", ChatConfigNodes.CHANNEL_FORMATS_DEFAULT));
|
||||||
worldFormat.setTOWN(getOrDefault(path + ".town", ChatConfigNodes.CHANNEL_FORMATS_TOWN));
|
worldFormat.setTOWN(getOrDefault(path + ".town", ChatConfigNodes.CHANNEL_FORMATS_TOWN));
|
||||||
@@ -269,7 +261,74 @@ public class ChatSettings {
|
|||||||
return (String) chatConfig.get(configPath != null ? configPath : channelNode.getDefault());
|
return (String) chatConfig.get(configPath != null ? configPath : channelNode.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addWorldFormat(channelFormats format) {
|
private static void addWorldFormat(WorldFormat format) {
|
||||||
worldFormatGroups.put(format.getName().toLowerCase(Locale.ROOT), 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,75 +0,0 @@
|
|||||||
package com.palmergames.bukkit.TownyChat.util;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.yaml.snakeyaml.Yaml;
|
|
||||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
|
||||||
import org.yaml.snakeyaml.reader.UnicodeReader;
|
|
||||||
|
|
||||||
|
|
||||||
public class FileMgmt {
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static Map<String, Object> getMap(String filepath, String resource) {
|
|
||||||
|
|
||||||
File f = new File(filepath);
|
|
||||||
if (!(f.exists() && f.isFile())) {
|
|
||||||
// Populate a new file
|
|
||||||
try {
|
|
||||||
try (FileOutputStream fos = new FileOutputStream(f)) {
|
|
||||||
fos.write(getResourceFileAsString("/" + resource).getBytes(StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// No resource file found
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f = new File(filepath);
|
|
||||||
|
|
||||||
Yaml yamlChannels = new Yaml(new SafeConstructor());
|
|
||||||
Object channelsRootDataNode;
|
|
||||||
|
|
||||||
try (FileInputStream fileInputStream = new FileInputStream(f)) {
|
|
||||||
channelsRootDataNode = yamlChannels.load(new UnicodeReader(fileInputStream));
|
|
||||||
if (channelsRootDataNode == null) {
|
|
||||||
throw new NullPointerException();
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + f.getPath(), ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (channelsRootDataNode instanceof Map)
|
|
||||||
return (Map<String, Object>) channelsRootDataNode;
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads given resource file as a string.
|
|
||||||
*
|
|
||||||
* @param fileName path to the resource file
|
|
||||||
* @return the file's contents
|
|
||||||
* @throws IOException if read fails for any reason
|
|
||||||
*/
|
|
||||||
static String getResourceFileAsString(String fileName) throws IOException {
|
|
||||||
if (fileName == null)
|
|
||||||
return "";
|
|
||||||
try (InputStream is = FileMgmt.class.getResourceAsStream(fileName);
|
|
||||||
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
|
||||||
BufferedReader reader = new BufferedReader(isr)) {
|
|
||||||
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user