From c0ff36db3dd46bd5fe48660f767df1f291bce351 Mon Sep 17 00:00:00 2001 From: Llm Dl Date: Mon, 27 Dec 2021 07:11:28 -0600 Subject: [PATCH] - Regression: NPE in TownyChatPlayerListener. - Minimum Towny version set to 0.97.5.0. - Use Towny's built-in Commented configuration & adapt to use the new width() option supplied by Spigot. --- pom.xml | 2 +- resources/changelog.txt | 5 +- .../palmergames/bukkit/TownyChat/Chat.java | 2 +- .../ChannelJoinAliasCommand.java | 3 +- .../bukkit/TownyChat/config/ChatSettings.java | 11 +- .../config/CommentedConfiguration.java | 278 ------------------ .../listener/TownyChatPlayerListener.java | 2 +- 7 files changed, 17 insertions(+), 286 deletions(-) delete mode 100644 src/com/palmergames/bukkit/TownyChat/config/CommentedConfiguration.java diff --git a/pom.xml b/pom.xml index 16d3dcb..b396a62 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,7 @@ com.github.TownyAdvanced Towny - 0.97.2.7 + 0.97.5.0 provided diff --git a/resources/changelog.txt b/resources/changelog.txt index 7aa7224..966111b 100644 --- a/resources/changelog.txt +++ b/resources/changelog.txt @@ -391,4 +391,7 @@ v0.92: - Repurpose the display modes on join ChatConfig setting, not displays the channel join message. - Fix for Spigot 1.18.1 config enabling comments by default. v0.93: - - Regression: NPE in EssentialsDiscordHookListener. \ No newline at end of file + - Regression: NPE in EssentialsDiscordHookListener. + - Regression: NPE in TownyChatPlayerListener. + - Minimum Towny version set to 0.97.5.0. + - Use Towny's built-in Commented configuration & adapt to use the new width() option supplied by Spigot. \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/Chat.java b/src/com/palmergames/bukkit/TownyChat/Chat.java index c2587f9..24745ff 100644 --- a/src/com/palmergames/bukkit/TownyChat/Chat.java +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -50,7 +50,7 @@ public class Chat extends JavaPlugin { private Towny towny = null; private DynmapAPI dynMap = null; - private static Version requiredTownyVersion = Version.fromString("0.97.2.18"); + private static Version requiredTownyVersion = Version.fromString("0.97.5.0"); public static boolean usingPlaceholderAPI = false; public static boolean usingEssentialsDiscord = false; boolean chatConfigError = false; diff --git a/src/com/palmergames/bukkit/TownyChat/Command/commandobjects/ChannelJoinAliasCommand.java b/src/com/palmergames/bukkit/TownyChat/Command/commandobjects/ChannelJoinAliasCommand.java index 04acab6..21cde00 100644 --- a/src/com/palmergames/bukkit/TownyChat/Command/commandobjects/ChannelJoinAliasCommand.java +++ b/src/com/palmergames/bukkit/TownyChat/Command/commandobjects/ChannelJoinAliasCommand.java @@ -35,7 +35,8 @@ public class ChannelJoinAliasCommand extends BukkitCommand { message = StringMgmt.join(args, " "); } if (message.isEmpty()) { - if (plugin.getPlayerChannel(player).getName().equals(channel.getName())) { + Channel chan = plugin.getPlayerChannel(player); + if (chan != null && chan.getName().equalsIgnoreCase(channel.getName())) { TownyMessaging.sendMessage(player, String.format(Translation.of("tc_you_are_already_in_channel"), channel.getName())); return true; } else { diff --git a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java index af098ae..040f55e 100644 --- a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java +++ b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java @@ -2,6 +2,7 @@ package com.palmergames.bukkit.TownyChat.config; import com.palmergames.bukkit.TownyChat.channels.channelFormats; import com.palmergames.bukkit.TownyChat.util.FileMgmt; +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; @@ -40,7 +41,7 @@ public class ChatSettings extends tag_formats { if (file != null) { // read the config.yml into memory - ChatSettings.chatConfig = new CommentedConfiguration(file); + ChatSettings.chatConfig = new CommentedConfiguration(file.toPath()); if (!chatConfig.load()) { Bukkit.getLogger().severe("[TownyChat] Failed to load ChatConfig!"); Bukkit.getLogger().severe("[TownyChat] Please check that the file passes a YAML Parser test:"); @@ -48,7 +49,11 @@ public class ChatSettings extends tag_formats { return false; } setDefaults(version, file); - chatConfig.save(); + try { + // Pre-1.18.1 this option does not exist. + chatConfig.options().width(500); + } catch (NoSuchMethodError ignored) {} + chatConfig.save(); } return true; } @@ -59,7 +64,7 @@ public class ChatSettings extends tag_formats { */ private static void setDefaults(String version, File file) { - newChatConfig = new CommentedConfiguration(file); + newChatConfig = new CommentedConfiguration(file.toPath()); newChatConfig.load(); for (ChatConfigNodes root : ChatConfigNodes.values()) { diff --git a/src/com/palmergames/bukkit/TownyChat/config/CommentedConfiguration.java b/src/com/palmergames/bukkit/TownyChat/config/CommentedConfiguration.java deleted file mode 100644 index 110e388..0000000 --- a/src/com/palmergames/bukkit/TownyChat/config/CommentedConfiguration.java +++ /dev/null @@ -1,278 +0,0 @@ -package com.palmergames.bukkit.TownyChat.config; - -import com.google.common.base.Charsets; -import com.google.common.io.Files; -import com.palmergames.bukkit.TownyChat.util.FileMgmt; - -import org.apache.commons.lang.Validate; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.configuration.file.YamlConstructor; -import org.bukkit.configuration.file.YamlRepresenter; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.representer.Representer; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.Writer; -import java.io.OutputStreamWriter; -import java.io.FileOutputStream; - -import java.util.HashMap; - -/** - * @author dumptruckman - */ -public class CommentedConfiguration extends YamlConfiguration { - - private HashMap comments; - private File file; - - private final DumperOptions yamlOptions = new DumperOptions(); - private final Representer yamlRepresenter = new YamlRepresenter(); - private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); - - public CommentedConfiguration(File file) { - - super(); - //this.load(file); - comments = new HashMap(); - this.file = file; - } - - public boolean load() { - - boolean loaded = true; - - try { - this.load(file); - } catch (FileNotFoundException e) { - loaded = false; - } catch (IOException e) { - loaded = false; - } catch (InvalidConfigurationException e) { - loaded = false; - } - - return loaded; - } - - public boolean save() { - - try { - // Spigot 1.18.1 added SnakeYaml's ability to use Comments in yaml. - // They have it enabled by default, we need to stop it happening. - yamlOptions.setProcessComments(false); - } catch (NoSuchMethodError ignored) {} - - boolean saved = true; - - // Save the config just like normal - try { - /* - * Doing some saving of our own. We have found that the implementation of YAMLComfiguration used by Bukkit will attempt to - * cap strings at 80 characters long, forming new lines in some of our longer strings (channel_formats.) - */ - this.save(file); - - } catch (Exception e) { - saved = false; - } - - // if there's comments to add and it saved fine, we need to add comments - if (!comments.isEmpty() && saved) { - // String array of each line in the config file - String[] yamlContents = FileMgmt.convertFileToString(file).split("[" + System.getProperty("line.separator") + "]"); - - // This will hold the newly formatted line - String newContents = ""; - // This holds the current path the lines are at in the config - String currentPath = ""; - // This tells if the specified path has already been commented - boolean commentedPath = false; - // This flags if the line is a node or unknown text. - boolean node = false; - // The depth of the path. (number of words separated by periods - 1) - int depth = 0; - - // Loop through the config lines - for (String line : yamlContents) { - // If the line is a node (and not something like a list value) - if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { - - // This is a new node so we need to mark it for commenting (if there are comments) - commentedPath = false; - // This is a node so flag it as one - node = true; - - // Grab the index of the end of the node name - int index = 0; - index = line.indexOf(": "); - if (index < 0) { - index = line.length() - 1; - } - // If currentPath is empty, store the node name as the currentPath. (this is only on the first iteration, i think) - if (currentPath.isEmpty()) { - currentPath = line.substring(0, index); - } else { - // Calculate the whitespace preceding the node name - int whiteSpace = 0; - for (int n = 0; n < line.length(); n++) { - if (line.charAt(n) == ' ') { - whiteSpace++; - } else { - break; - } - } - // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth - if (whiteSpace / 2 > depth) { - // Path is deeper. Add a . and the node name - currentPath += "." + line.substring(whiteSpace, index); - depth++; - } else if (whiteSpace / 2 < depth) { - // Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath - int newDepth = whiteSpace / 2; - for (int i = 0; i < depth - newDepth; i++) { - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - } - // Grab the index of the final period - int lastIndex = currentPath.lastIndexOf("."); - if (lastIndex < 0) { - // if there isn't a final period, set the current path to nothing because we're at root - currentPath = ""; - } else { - // If there is a final period, replace everything after it with nothing - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += "."; - } - // Add the new node name to the path - currentPath += line.substring(whiteSpace, index); - // Reset the depth - depth = newDepth; - } else { - // Path is same depth, replace the last path node name to the current node name - int lastIndex = currentPath.lastIndexOf("."); - if (lastIndex < 0) { - // if there isn't a final period, set the current path to nothing because we're at root - currentPath = ""; - } else { - // If there is a final period, replace everything after it with nothing - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += "."; - } - //currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += line.substring(whiteSpace, index); - - } - - } - - } else - node = false; - - if (node) { - String comment = null; - if (!commentedPath) { - // If there's a comment for the current path, retrieve it and flag that path as already commented - comment = comments.get(currentPath); - } - if (comment != null) { - // Add the comment to the beginning of the current line - line = comment + System.getProperty("line.separator") + line + System.getProperty("line.separator"); - comment = null; - commentedPath = true; - } else { - // Add a new line as it is a node, but has no comment - line += System.getProperty("line.separator"); - } - } - // Add the (modified) line to the total config String - newContents += line + ((!node) ? System.getProperty("line.separator") : ""); - - } - /* - * Due to a bukkit bug we need to strip any extra new lines from the - * beginning of this file, else they will multiply. - */ - while (newContents.startsWith(System.getProperty("line.separator"))) - newContents = newContents.replaceFirst(System.getProperty("line.separator"), ""); - - try { - // Write the string to the config file - FileMgmt.stringToFile(newContents, file); - } catch (IOException e) { - saved = false; - } - } - return saved; - } - - /** - * Adds a comment just before the specified path. The comment can be - * multiple lines. An empty string will indicate a blank line. - * - * @param path Configuration path to add comment. - * @param commentLines Comments to add. One String per line. - */ - public void addComment(String path, String... commentLines) { - - StringBuilder commentstring = new StringBuilder(); - String leadingSpaces = ""; - for (int n = 0; n < path.length(); n++) { - if (path.charAt(n) == '.') { - leadingSpaces += " "; - } - } - for (String line : commentLines) { - if (!line.isEmpty()) { - line = leadingSpaces + line; - } else { - line = " "; - } - if (commentstring.length() > 0) { - commentstring.append(System.getProperty("line.separator")); - } - commentstring.append(line); - } - comments.put(path, commentstring.toString()); - } - - - public void save(File file) throws IOException { - Validate.notNull(file, "File cannot be null"); - - Files.createParentDirs(file); - - String data = this.saveToString(); - - Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8); - - try { - writer.write(data); - } finally { - writer.close(); - } - } - - @SuppressWarnings("deprecation") - @Override - public String saveToString() { - yamlOptions.setIndent(options().indent()); - yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - yamlOptions.setWidth(10000); - yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - - - String dump = yaml.dump(getValues(false)); - - - if (dump.equals(BLANK_CONFIG)) { - dump = ""; - } - - return dump; - } - -} diff --git a/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java index 8b0cc3a..b08ca29 100644 --- a/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java +++ b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java @@ -141,7 +141,7 @@ public class TownyChatPlayerListener implements Listener { /* * Check the player for any channel modes. */ - Channel channel = plugin.getChannelsHandler().getChannel(plugin.getPlayerChannel(player).getName()); + Channel channel = plugin.getPlayerChannel(player); if (channel != null) { // Notify player he is muted if (channel.isMuted(player.getName())) {