mirror of
https://github.com/darksaid98/TownyChat.git
synced 2026-12-04 14:38:58 +00:00
Refactoring Chat & TownyChatPlayerListener, Slimming down FileMgmt.
This commit is contained in:
1 parent
5c454b9f01
commit
d2338ede07
5 files changed
+135
-223
No files matched your search
@@ -11,9 +11,9 @@ 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;
|
||||||
import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration;
|
import com.palmergames.bukkit.TownyChat.util.EssentialsIntegration;
|
||||||
import com.palmergames.bukkit.TownyChat.util.FileMgmt;
|
|
||||||
import com.palmergames.bukkit.towny.Towny;
|
import com.palmergames.bukkit.towny.Towny;
|
||||||
import com.palmergames.bukkit.util.Version;
|
import com.palmergames.bukkit.util.Version;
|
||||||
|
import com.palmergames.util.FileMgmt;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@@ -70,29 +70,29 @@ public class Chat extends JavaPlugin {
|
|||||||
playerChannelMap = new ConcurrentHashMap<>();
|
playerChannelMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
checkPlugins();
|
checkPlugins();
|
||||||
loadConfigs();
|
if (towny == null || !townyVersionCheck(towny.getDescription().getVersion())) {
|
||||||
|
disableWithMessage("Towny version does not meet required minimum version: " + requiredTownyVersion.toString());
|
||||||
if (!townyVersionCheck(towny.getDescription().getVersion())) {
|
|
||||||
getLogger().severe("Towny version does not meet required minimum version: " + requiredTownyVersion.toString());
|
|
||||||
this.getServer().getPluginManager().disablePlugin(this);
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
getLogger().info("Towny version " + towny.getDescription().getVersion() + " found.");
|
getLogger().info("Towny version " + towny.getDescription().getVersion() + " found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadConfigs();
|
||||||
|
if (channelsConfigError || chatConfigError) {
|
||||||
|
disableWithMessage("The config could not be loaded.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This executes the task with a 1 tick delay avoiding the bukkit
|
* This executes the task with a 1 tick delay avoiding the bukkit depends bug.
|
||||||
* depends bug.
|
* TODO: What bug is this referencing? This goes back to the first version of TownyChat.
|
||||||
*/
|
*/
|
||||||
if ((towny == null) || (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1)
|
if (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1) {
|
||||||
|| (channelsConfigError) || (chatConfigError)) {
|
|
||||||
/*
|
/*
|
||||||
* We either failed to find Towny or the Scheduler failed to
|
* We either failed to find Towny or the Scheduler failed to
|
||||||
* register the task.
|
* register the task.
|
||||||
*/
|
*/
|
||||||
getLogger().severe("Could not schedule onLoadedTask.");
|
disableWithMessage("Could not schedule onLoadedTask.");
|
||||||
getLogger().severe("Disabling TownyChat...");
|
|
||||||
pm.disablePlugin(this);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,18 +101,20 @@ public class Chat extends JavaPlugin {
|
|||||||
registerObjectCommands();
|
registerObjectCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean townyVersionCheck(String version) {
|
private void disableWithMessage(String message) {
|
||||||
Version ver = Version.fromString(version);
|
getLogger().severe(message);
|
||||||
|
getLogger().severe("Disabling TownyChat...");
|
||||||
|
pm.disablePlugin(this);
|
||||||
|
}
|
||||||
|
|
||||||
return ver.compareTo(requiredTownyVersion) >= 0;
|
private boolean townyVersionCheck(String version) {
|
||||||
|
return Version.fromString(version).compareTo(requiredTownyVersion) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadConfigs() {
|
private void loadConfigs() {
|
||||||
FileMgmt.checkFolders(new String[] { getRootPath(), getChannelsPath() });
|
FileMgmt.checkOrCreateFolders(new String[] { getRootPath(), getTownySettingsPath() });
|
||||||
if (!ChatSettings.loadCommentedConfig(getChannelsPath() + File.separator + "ChatConfig.yml"))
|
chatConfigError = !ChatSettings.loadCommentedChatConfig();
|
||||||
chatConfigError = true;
|
channelsConfigError = !channelsConfig.loadChannels();
|
||||||
if (!channelsConfig.loadChannels(getChannelsPath(), "Channels.yml"))
|
|
||||||
channelsConfigError = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Chat getTownyChat() {
|
public static Chat getTownyChat() {
|
||||||
@@ -205,10 +207,18 @@ public class Chat extends JavaPlugin {
|
|||||||
return getTowny().getDataFolder().getPath();
|
return getTowny().getDataFolder().getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getChannelsPath() {
|
public String getTownySettingsPath() {
|
||||||
return getRootPath() + File.separator + "settings";
|
return getRootPath() + File.separator + "settings";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getChatConfigPath() {
|
||||||
|
return getTownySettingsPath() + File.separator + "ChatConfig.yml";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getChannelsConfigPath() {
|
||||||
|
return getTownySettingsPath() + File.separator + "Channels.yml";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the channels
|
* @return the channels
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,13 +27,11 @@ public class ChatSettings {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param filepath - Location of chatconfig.yml.
|
* @return true if the chatconfig has loaded.
|
||||||
* @param version - Chat version from plugin.yml.
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static boolean loadCommentedConfig(String filepath) {
|
public static boolean loadCommentedChatConfig() {
|
||||||
|
String filepath = Chat.getTownyChat().getChatConfigPath();
|
||||||
if (FileMgmt.checkOrCreateFile(filepath)) {
|
if (FileMgmt.checkOrCreateFile(filepath)) {
|
||||||
File file = new File(filepath);
|
File file = new File(filepath);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import com.palmergames.bukkit.TownyChat.channels.channelTypes;
|
|||||||
import com.palmergames.bukkit.TownyChat.util.FileMgmt;
|
import com.palmergames.bukkit.TownyChat.util.FileMgmt;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -34,29 +33,27 @@ public class ConfigurationHandler {
|
|||||||
* If it doesn't exist, create it from
|
* If it doesn't exist, create it from
|
||||||
* the resource in this jar.
|
* the resource in this jar.
|
||||||
*
|
*
|
||||||
* @param filepath
|
|
||||||
* @param defaultRes
|
|
||||||
* @return true if the channels were loaded
|
* @return true if the channels were loaded
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public boolean loadChannels(String filepath, String defaultRes) {
|
public boolean loadChannels() {
|
||||||
|
|
||||||
String filename = filepath + File.separator + defaultRes;
|
String filename = Chat.getTownyChat().getChannelsConfigPath();
|
||||||
|
|
||||||
Map<String, Object> file;
|
Map<String, Object> channelsMap;
|
||||||
try {
|
try {
|
||||||
file = FileMgmt.getFile(filename, defaultRes);
|
channelsMap = FileMgmt.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:");
|
||||||
Bukkit.getLogger().severe("[TownyChat] Online YAML Parser: https://yaml-online-parser.appspot.com/");
|
Bukkit.getLogger().severe("[TownyChat] Online YAML Parser: https://yaml-online-parser.appspot.com/");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (file == null || !file.containsKey("Channels"))
|
if (channelsMap == null || !channelsMap.containsKey("Channels"))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Parse the channels
|
// Parse the channels
|
||||||
Map<String, Object> allChannelNodes = (Map<String, Object>) file.get("Channels");
|
Map<String, Object> allChannelNodes = (Map<String, Object>) channelsMap.get("Channels");
|
||||||
if (allChannelNodes == null)
|
if (allChannelNodes == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ import com.palmergames.bukkit.towny.object.Translatable;
|
|||||||
import com.palmergames.bukkit.towny.object.metadata.StringDataField;
|
import com.palmergames.bukkit.towny.object.metadata.StringDataField;
|
||||||
import com.palmergames.bukkit.towny.utils.MetaDataUtil;
|
import com.palmergames.bukkit.towny.utils.MetaDataUtil;
|
||||||
import com.palmergames.bukkit.util.Colors;
|
import com.palmergames.bukkit.util.Colors;
|
||||||
import com.palmergames.bukkit.towny.TownyUniverse;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -64,49 +62,15 @@ public class TownyChatPlayerListener implements Listener {
|
|||||||
plugin.getChannelsHandler().getAllChannels().values().stream().forEach(channel -> channel.forgetPlayer(player));
|
plugin.getChannelsHandler().getAllChannels().values().stream().forEach(channel -> channel.forgetPlayer(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
||||||
if (event.isCancelled()) return;
|
|
||||||
|
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
if (event.getMessage().contains("&L") || event.getMessage().contains("&l") ||
|
// Check if the message contains colour codes we need to remove or parse.
|
||||||
event.getMessage().contains("&O") || event.getMessage().contains("&o") ||
|
testColourCodes(event, player);
|
||||||
event.getMessage().contains("&N") || event.getMessage().contains("&n") ||
|
|
||||||
event.getMessage().contains("&K") || event.getMessage().contains("&k") ||
|
|
||||||
event.getMessage().contains("&M") || event.getMessage().contains("&m") ||
|
|
||||||
event.getMessage().contains("&R") || event.getMessage().contains("&r") ) {
|
|
||||||
if (!player.hasPermission("townychat.chat.format.bold")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&L", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&l", ""));
|
|
||||||
}
|
|
||||||
if (!player.hasPermission("townychat.chat.format.italic")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&O", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&o", ""));
|
|
||||||
}
|
|
||||||
if (!player.hasPermission("townychat.chat.format.magic")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&K", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&k", ""));
|
|
||||||
}
|
|
||||||
if (!player.hasPermission("townychat.chat.format.underlined")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&N", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&n", ""));
|
|
||||||
}
|
|
||||||
if (!player.hasPermission("townychat.chat.format.strike")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&M", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&m", ""));
|
|
||||||
}
|
|
||||||
if (!player.hasPermission("townychat.chat.format.reset")) {
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&R", ""));
|
|
||||||
event.setMessage(event.getMessage().replaceAll("&r", ""));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(player.hasPermission("townychat.chat.color"))
|
|
||||||
event.setMessage(Colors.translateColorCodes(event.getMessage()));
|
|
||||||
|
|
||||||
// Check if essentials has this player muted.
|
// Check if essentials has this player muted.
|
||||||
if (!isMuted(player)) {
|
if (!isEssentialsMuted(player)) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If this was directed chat send it via the relevant channel
|
* If this was directed chat send it via the relevant channel
|
||||||
@@ -115,15 +79,7 @@ public class TownyChatPlayerListener implements Listener {
|
|||||||
Channel channel = plugin.getChannelsHandler().getChannel(directedChat.get(player));
|
Channel channel = plugin.getChannelsHandler().getChannel(directedChat.get(player));
|
||||||
|
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
// Notify player he is muted
|
if (isMutedOrSpam(event, channel, player)) {
|
||||||
if (channel.isMuted(player.getName())) {
|
|
||||||
TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_you_are_currently_muted_in_channel", channel.getName()));
|
|
||||||
event.setCancelled(true);
|
|
||||||
directedChat.remove(player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (channel.isSpam(player)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
directedChat.remove(player);
|
directedChat.remove(player);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -141,38 +97,19 @@ public class TownyChatPlayerListener implements Listener {
|
|||||||
*/
|
*/
|
||||||
Channel channel = plugin.getPlayerChannel(player);
|
Channel channel = plugin.getPlayerChannel(player);
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
// Notify player he is muted
|
if (isMutedOrSpam(event, channel, 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;
|
return;
|
||||||
}
|
|
||||||
if (channel.isSpam(player)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* Channel Chat mode set
|
|
||||||
* Process the chat
|
|
||||||
*/
|
|
||||||
channel.chatProcess(event);
|
channel.chatProcess(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find a global channel this player has permissions for.
|
/*
|
||||||
|
* Find a global channel this player has permissions for.
|
||||||
|
*/
|
||||||
channel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
|
channel = plugin.getChannelsHandler().getActiveChannel(player, channelTypes.GLOBAL);
|
||||||
|
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
// Notify player he is muted
|
if (isMutedOrSpam(event, channel, 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;
|
return;
|
||||||
}
|
|
||||||
if (channel.isSpam(player)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
channel.chatProcess(event);
|
channel.chatProcess(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -182,25 +119,59 @@ public class TownyChatPlayerListener implements Listener {
|
|||||||
* We found no channels available so modify the chat (if enabled) and exit.
|
* We found no channels available so modify the chat (if enabled) and exit.
|
||||||
*/
|
*/
|
||||||
if (ChatSettings.isModify_chat()) {
|
if (ChatSettings.isModify_chat()) {
|
||||||
|
// Set the format based on the global channelformat, with channeltag and msgcolour removed.
|
||||||
event.setFormat(ChatSettings.getChannelFormat(player, channelTypes.GLOBAL).replace("{channelTag}", "").replace("{msgcolour}", ""));
|
event.setFormat(ChatSettings.getChannelFormat(player, channelTypes.GLOBAL).replace("{channelTag}", "").replace("{msgcolour}", ""));
|
||||||
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
|
event.setFormat(TownyChatFormatter.getChatFormat(new LocalTownyChatEvent(event, TownyAPI.getInstance().getResident(player))));
|
||||||
|
|
||||||
LocalTownyChatEvent chatEvent = new LocalTownyChatEvent(event, resident);
|
|
||||||
|
|
||||||
event.setFormat(TownyChatFormatter.getChatFormat(chatEvent));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 player {@link Player} which has spoken.
|
||||||
|
*/
|
||||||
|
private void testColourCodes(AsyncPlayerChatEvent event, Player player) {
|
||||||
|
if ((event.getMessage().contains("&L") || event.getMessage().contains("&l"))
|
||||||
|
&& !player.hasPermission("townychat.chat.format.bold"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&L", "").replaceAll("&l", ""));
|
||||||
|
|
||||||
|
if ((event.getMessage().contains("&O") || event.getMessage().contains("&o"))
|
||||||
|
&& !player.hasPermission("townychat.chat.format.italic"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&O", "").replaceAll("&o", ""));
|
||||||
|
|
||||||
|
if ((event.getMessage().contains("&K") || event.getMessage().contains("&k"))
|
||||||
|
&& !player.hasPermission("townychat.chat.format.magic"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&K", "").replaceAll("&k", ""));
|
||||||
|
|
||||||
|
if ((event.getMessage().contains("&N") || event.getMessage().contains("&n"))
|
||||||
|
&& !player.hasPermission("townychat.chat.format.underlined"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&N", "").replaceAll("&n", ""));
|
||||||
|
|
||||||
|
if ((event.getMessage().contains("&M") || event.getMessage().contains("&m"))
|
||||||
|
&& !player.hasPermission("townychat.chat.format.strike"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&M", "").replaceAll("&m", ""));
|
||||||
|
|
||||||
|
if ((event.getMessage().contains("&R") || event.getMessage().contains("&r"))
|
||||||
|
&&!player.hasPermission("townychat.chat.format.reset"))
|
||||||
|
event.setMessage(event.getMessage().replaceAll("&R", "").replaceAll("&r", ""));
|
||||||
|
|
||||||
|
if (player.hasPermission("townychat.chat.color"))
|
||||||
|
event.setMessage(Colors.translateColorCodes(event.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is this player Muted via Essentials?
|
* Is this player Muted via Essentials?
|
||||||
*
|
*
|
||||||
* @param player
|
* @param player {@link Player} speaking.
|
||||||
* @return true if muted
|
* @return true if muted by Essentials.
|
||||||
*/
|
*/
|
||||||
private boolean isMuted(Player player) {
|
private boolean isEssentialsMuted(Player player) {
|
||||||
// Check if essentials has this player muted.
|
if (!plugin.getTowny().isEssentials())
|
||||||
if (plugin.getTowny().isEssentials()) {
|
return false;
|
||||||
try {
|
try {
|
||||||
|
// Check if essentials has this player muted.
|
||||||
if (plugin.getTowny().getEssentials().getUser(player).isMuted()) {
|
if (plugin.getTowny().getEssentials().getUser(player).isMuted()) {
|
||||||
TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_unable_to_talk_essentials_mute"));
|
TownyMessaging.sendErrorMsg(player, Translatable.of("tc_err_unable_to_talk_essentials_mute"));
|
||||||
return true;
|
return true;
|
||||||
@@ -210,10 +181,29 @@ public class TownyChatPlayerListener implements Listener {
|
|||||||
}
|
}
|
||||||
return false;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// From TownyChat 0.84-0.95 the symbol used to separate the channels in the meta
|
// From TownyChat 0.84-0.95 the symbol used to separate the channels in the meta
|
||||||
// was not good for non-unicode-using mysql servers.
|
// was not good for non-unicode-using mysql servers.
|
||||||
private void checkPlayerForOldMeta(Player player) {
|
private void checkPlayerForOldMeta(Player player) {
|
||||||
|
|||||||
@@ -7,11 +7,9 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
@@ -20,27 +18,16 @@ import org.yaml.snakeyaml.reader.UnicodeReader;
|
|||||||
|
|
||||||
public class FileMgmt {
|
public class FileMgmt {
|
||||||
|
|
||||||
public static void checkFolders(String[] folders) {
|
|
||||||
for (String folder : folders) {
|
|
||||||
File f = new File(folder);
|
|
||||||
if (!(f.exists() && f.isDirectory()))
|
|
||||||
f.mkdir();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static Map<String, Object> getFile(String filepath, String resource) {
|
public static Map<String, Object> getMap(String filepath, String resource) {
|
||||||
|
|
||||||
try {
|
|
||||||
File f = new File(filepath);
|
File f = new File(filepath);
|
||||||
if (!(f.exists() && f.isFile())) {
|
if (!(f.exists() && f.isFile())) {
|
||||||
// Populate a new file
|
// Populate a new file
|
||||||
try {
|
try {
|
||||||
String resString = convertStreamToString("/" + resource);
|
try (FileOutputStream fos = new FileOutputStream(f)) {
|
||||||
// If we have a plugin reference pass to load default.
|
fos.write(getResourceFileAsString("/" + resource).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
FileMgmt.stringToFile(resString, filepath);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// No resource file found
|
// No resource file found
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -53,105 +40,35 @@ public class FileMgmt {
|
|||||||
Yaml yamlChannels = new Yaml(new SafeConstructor());
|
Yaml yamlChannels = new Yaml(new SafeConstructor());
|
||||||
Object channelsRootDataNode;
|
Object channelsRootDataNode;
|
||||||
|
|
||||||
FileInputStream fileInputStream = new FileInputStream(f);
|
try (FileInputStream fileInputStream = new FileInputStream(f)) {
|
||||||
try {
|
|
||||||
channelsRootDataNode = yamlChannels.load(new UnicodeReader(fileInputStream));
|
channelsRootDataNode = yamlChannels.load(new UnicodeReader(fileInputStream));
|
||||||
if (channelsRootDataNode == null) {
|
if (channelsRootDataNode == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + f.getPath(), ex);
|
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + f.getPath(), ex);
|
||||||
} finally {
|
|
||||||
fileInputStream.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (channelsRootDataNode instanceof Map)
|
if (channelsRootDataNode instanceof Map)
|
||||||
return (Map<String, Object>) channelsRootDataNode;
|
return (Map<String, Object>) channelsRootDataNode;
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pass a resource name and it will return it's contents as a string
|
/**
|
||||||
private static String convertStreamToString(String name) throws IOException {
|
* Reads given resource file as a string.
|
||||||
if (name != null) {
|
*
|
||||||
Writer writer = new StringWriter();
|
* @param fileName path to the resource file
|
||||||
InputStream is = FileMgmt.class.getResourceAsStream(name);
|
* @return the file's contents
|
||||||
|
* @throws IOException if read fails for any reason
|
||||||
char[] buffer = new char[1024];
|
*/
|
||||||
try {
|
static String getResourceFileAsString(String fileName) throws IOException {
|
||||||
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
if (fileName == null)
|
||||||
int n;
|
|
||||||
while ((n = reader.read(buffer)) != -1) {
|
|
||||||
writer.write(buffer, 0, n);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Exception ");
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
// Failed to open a stream
|
|
||||||
throw new IOException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return writer.toString();
|
|
||||||
} else {
|
|
||||||
return "";
|
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()));
|
||||||
* Wrapper for stringToFile writes a string to a file making all newline
|
|
||||||
* codes platform specific
|
|
||||||
*
|
|
||||||
* @param source
|
|
||||||
* @param FileName
|
|
||||||
* @return true on success
|
|
||||||
*/
|
|
||||||
private static boolean stringToFile(String source, String FileName) {
|
|
||||||
|
|
||||||
if (source != null) {
|
|
||||||
// Save the string to file (*.yml)
|
|
||||||
try {
|
|
||||||
return stringToFile(source, new File(FileName));
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes the contents of a string to a file.
|
|
||||||
*
|
|
||||||
* @param source
|
|
||||||
* String to write.
|
|
||||||
* @param file
|
|
||||||
* File to write to.
|
|
||||||
* @return True on success.
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
private static boolean stringToFile(String source, File file) throws IOException {
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
|
|
||||||
|
|
||||||
source.replaceAll("\n", System.getProperty("line.separator"));
|
|
||||||
|
|
||||||
out.write(source);
|
|
||||||
out.close();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Exception ");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user