diff --git a/src/com/palmergames/bukkit/TownyChat/Chat.java b/src/com/palmergames/bukkit/TownyChat/Chat.java index a78c666..7029633 100644 --- a/src/com/palmergames/bukkit/TownyChat/Chat.java +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -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.ChannelsHolder; 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.TownyChatPlayerListener; import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; @@ -46,7 +46,7 @@ public class Chat extends JavaPlugin { private TownyChatPlayerListener TownyPlayerListener; private ChannelsHolder channels; - private ConfigurationHandler channelsConfig; + private ChannelConfigurationHandler channelsConfig; protected PluginManager pm; private static Chat chat = null; @@ -65,7 +65,7 @@ public class Chat extends JavaPlugin { chat = this; pm = getServer().getPluginManager(); - channelsConfig = new ConfigurationHandler(this); + channelsConfig = new ChannelConfigurationHandler(this); channels = new ChannelsHolder(this); playerChannelMap = new ConcurrentHashMap<>(); @@ -229,7 +229,7 @@ public class Chat extends JavaPlugin { /** * @return the data */ - public ConfigurationHandler getConfigurationHandler() { + public ChannelConfigurationHandler getConfigurationHandler() { return channelsConfig; } diff --git a/src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java b/src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java deleted file mode 100644 index 7c50ed8..0000000 --- a/src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java b/src/com/palmergames/bukkit/TownyChat/config/ChannelConfigurationHandler.java similarity index 67% rename from src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java rename to src/com/palmergames/bukkit/TownyChat/config/ChannelConfigurationHandler.java index 7cbbab2..e88f607 100644 --- a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java +++ b/src/com/palmergames/bukkit/TownyChat/config/ChannelConfigurationHandler.java @@ -4,17 +4,28 @@ 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.util.FileMgmt; 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.Arrays; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; -public class ConfigurationHandler { +public class ChannelConfigurationHandler { private Chat plugin; @@ -22,7 +33,7 @@ public class ConfigurationHandler { * * @param plugin */ - public ConfigurationHandler(Chat plugin) { + public ChannelConfigurationHandler(Chat plugin) { super(); this.plugin = plugin; } @@ -42,7 +53,7 @@ public class ConfigurationHandler { Map channelsMap; try { - channelsMap = FileMgmt.getMap(filename, "Channels.yml"); + channelsMap = getMap(filename, "Channels.yml"); } catch (Exception e) { Bukkit.getLogger().severe("[TownyChat] Failed to load Channels.yml!"); 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 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) 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())); + } + } } diff --git a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java index f01d267..a914511 100644 --- a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java +++ b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java @@ -1,7 +1,6 @@ package com.palmergames.bukkit.TownyChat.config; import com.palmergames.bukkit.TownyChat.Chat; -import com.palmergames.bukkit.TownyChat.channels.channelFormats; import com.palmergames.bukkit.TownyChat.channels.channelTypes; import com.palmergames.bukkit.config.CommentedConfiguration; import com.palmergames.bukkit.config.ConfigNodes; @@ -22,7 +21,7 @@ import java.util.Map; public class ChatSettings { - private static Map worldFormatGroups = new HashMap(); + private static Map worldFormatGroups = new HashMap(); private static CommentedConfiguration chatConfig, newChatConfig; /** @@ -224,16 +223,9 @@ public class ChatSettings { public static String getChannelFormat(Player player, channelTypes type) { if (isPer_world()) { - String name = player.getWorld().getName().toLowerCase(Locale.ROOT); - if (worldFormatGroups.containsKey(name)) { - channelFormats worldFormat = worldFormatGroups.get(name); - 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(); - }; + 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; } @@ -254,7 +246,7 @@ public class ChatSettings { String worldName = world.getName(); String path = "worlds." + worldName; 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.setDEFAULT(getOrDefault(path + ".default", ChatConfigNodes.CHANNEL_FORMATS_DEFAULT)); 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()); } - private static void addWorldFormat(channelFormats format) { + 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; + } + } } \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java b/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java deleted file mode 100644 index 8079bb8..0000000 --- a/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java +++ /dev/null @@ -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 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) 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())); - } - } - -} \ No newline at end of file