From 3410d27851eb3c8447d4905ee64229d6e55d3223 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Wed, 13 May 2020 19:53:08 +0300 Subject: [PATCH 1/7] Introduce messages.yml for further localization --- .gitignore | 7 +- pom.xml | 4 +- src/me/EtienneDx/RealEstate/Config.java | 111 +++++++++++++++++- .../RealEstate/CustomizableMessage.java | 15 +++ src/me/EtienneDx/RealEstate/Messages.java | 10 ++ src/me/EtienneDx/RealEstate/RECommand.java | 30 +++-- src/me/EtienneDx/RealEstate/RealEstate.java | 1 + .../RealEstate/SendPlayerMessageTask.java | 32 +++++ 8 files changed, 191 insertions(+), 19 deletions(-) create mode 100644 src/me/EtienneDx/RealEstate/CustomizableMessage.java create mode 100644 src/me/EtienneDx/RealEstate/Messages.java create mode 100644 src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java diff --git a/.gitignore b/.gitignore index 2637b8b..5c2d75b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ .classpath .project +.idea .settings/* bin/* target/* -build.bat -/bin/ -/target/ +build.bat +/bin/ +/target/ diff --git a/pom.xml b/pom.xml index c597cdd..8a8bb5c 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ maven-shade-plugin 3.2.1 - ${project.build.directory}/dependency-reduced-pom.xml + false co.aikar.commands @@ -64,7 +64,7 @@ bungeecord-repo - https://()oss.sonatype.org/content/repositories/snapshots + https://oss.sonatype.org/content/repositories/snapshots jitpack.io diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index c3f505b..7b0c533 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -1,10 +1,15 @@ package me.EtienneDx.RealEstate; +import java.io.File; +import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import org.bukkit.ChatColor; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import me.EtienneDx.AnnotationConfig.AnnotationConfig; @@ -18,6 +23,7 @@ public class Config extends AnnotationConfig public final String configFilePath = RealEstate.pluginDirPath + "config.yml"; public final String logFilePath = RealEstate.pluginDirPath + "GriefProtection_RealEstate.log"; + final static String messagesFilePath = RealEstate.pluginDirPath + File.separator + "messages.yml"; public final String chatPrefix = "[" + ChatColor.GOLD + "RealEstate" + ChatColor.WHITE + "] "; @ConfigField(name="RealEstate.Keywords.SignsHeader", comment = "What is displayed in top of the signs") @@ -88,7 +94,9 @@ public class Config extends AnnotationConfig @ConfigField(name="RealEstate.Settings.PageSize", comment = "How many Real Estate offer should be shown by page using the '/re list' command") public int cfgPageSize = 20; - + + private String[] messages; + public Config() { this.pdf = RealEstate.instance.getDescription(); @@ -118,4 +126,105 @@ public class Config extends AnnotationConfig //YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(this.configFilePath)); this.loadConfig(this.configFilePath); } + + public void loadMessages() { + Messages[] messageIDs = Messages.values(); + this.messages = new String[Messages.values().length]; + + HashMap defaults = new HashMap(); + // initialize defaults + this.addDefault(defaults, Messages.NoTransactionFound, "No transaction found at your location!", null); + this.addDefault(defaults, Messages.PageMustBePositive, "Page must be a positive option!", null); + this.addDefault(defaults, Messages.PageNotExists, "This page does not exist!", null); + this.addDefault(defaults, Messages.RenewRentNow, "Automatic renew is now $a{0} $bfor this {1}", "0: the status; 1: a claim type"); + this.addDefault(defaults, Messages.RenewRentCurrently, "Automatic renew is currently $a{0} $bfor this {1}", "0: the status; 1: a claim type"); + + + // load the config file + FileConfiguration config = YamlConfiguration.loadConfiguration(new File(messagesFilePath)); + + // for each message ID + for (Messages messageID : messageIDs) { + //get default for this message + CustomizableMessage messageData = defaults.get(messageID.name()); + + // if default is missing, log an error and use some fake data for now so that the plugin can run + if (messageData == null) { + RealEstate.instance.log.info("Missing message for " + messageID.name() + ". Please contact the developer."); + messageData = new CustomizableMessage(messageID, "Missing message! ID: " + messageID.name() + ". Please contact a server admin.", null); + } + + // read the message from the file, use default if necessary + this.messages[messageID.ordinal()] = config.getString("Messages." + messageID.name() + ".Text", messageData.text); + config.set("Messages." + messageID.name() + ".Text", this.messages[messageID.ordinal()]); + + this.messages[messageID.ordinal()] = this.messages[messageID.ordinal()].replace('$', (char) 0x00A7); + + if (messageData.notes != null) { + messageData.notes = config.getString("Messages." + messageID.name() + ".Notes", messageData.notes); + config.set("Messages." + messageID.name() + ".Notes", messageData.notes); + } + } + + //save any changes + try { + config.options().header("Use a YAML editor like NotepadPlusPlus to edit this file. \nAfter editing, back up your changes before reloading the server in case you made a syntax error. \nUse dollar signs ($) for formatting codes, which are documented here: http://minecraft.gamepedia.com/Formatting_codes"); + config.save(messagesFilePath); + } catch (IOException exception) { + RealEstate.instance.log.info("Unable to write to the configuration file at \"" + messagesFilePath + "\""); + } + + defaults.clear(); + RealEstate.instance.log.info("Customizable messages loaded."); + System.gc(); + } + + private void addDefault(HashMap defaults, + Messages id, String text, String notes) { + CustomizableMessage message = new CustomizableMessage(id, text, notes); + defaults.put(id.name(), message); + } + + synchronized public String getMessage(Messages messageID, String... args) { + String message = messages[messageID.ordinal()]; + + for (int i = 0; i < args.length; i++) { + String param = args[i]; + message = message.replace("{" + i + "}", param); + } + + return message; + } + //sends a color-coded message to a player + public static void sendMessage(Player player, ChatColor color, Messages messageID, String... args) { + sendMessage(player, color, messageID, 0, args); + } + + //sends a color-coded message to a player + public static void sendMessage(Player player, ChatColor color, Messages messageID, long delayInTicks, String... args) { + String message = RealEstate.instance.config.getMessage(messageID, args); + sendMessage(player, color, message, delayInTicks); + } + + //sends a color-coded message to a player + public static void sendMessage(Player player, ChatColor color, String message) { + if (message == null || message.length() == 0) return; + + if (player == null) { + RealEstate.instance.log.info(color + message); + } else { + player.sendMessage(RealEstate.instance.config.chatPrefix + color + message); + } + } + + public static void sendMessage(Player player, ChatColor color, String message, long delayInTicks) { + SendPlayerMessageTask task = new SendPlayerMessageTask(player, color, message); + + if (delayInTicks > 0) { + RealEstate.instance.getServer().getScheduler().runTaskLater(RealEstate.instance, task, delayInTicks); + } else { + task.run(); + } + } + } diff --git a/src/me/EtienneDx/RealEstate/CustomizableMessage.java b/src/me/EtienneDx/RealEstate/CustomizableMessage.java new file mode 100644 index 0000000..daceb08 --- /dev/null +++ b/src/me/EtienneDx/RealEstate/CustomizableMessage.java @@ -0,0 +1,15 @@ +package me.EtienneDx.RealEstate; + +public class CustomizableMessage +{ + public Messages id; + public String text; + public String notes; + + public CustomizableMessage(Messages id, String text, String notes) + { + this.id = id; + this.text = text; + this.notes = notes; + } +} \ No newline at end of file diff --git a/src/me/EtienneDx/RealEstate/Messages.java b/src/me/EtienneDx/RealEstate/Messages.java new file mode 100644 index 0000000..d23aa86 --- /dev/null +++ b/src/me/EtienneDx/RealEstate/Messages.java @@ -0,0 +1,10 @@ +package me.EtienneDx.RealEstate; + +public enum Messages +{ + NoTransactionFound, + PageMustBePositive, + PageNotExists, + RenewRentNow, + RenewRentCurrently +} diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index 755a512..eecce8a 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -1,5 +1,6 @@ package me.EtienneDx.RealEstate; +import java.awt.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -50,19 +51,24 @@ public class RECommand extends BaseCommand } else { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction found at your location!"); + Config.sendMessage(player, ChatColor.RED, Messages.NoTransactionFound); } + } @Subcommand("list") @Description("Displays the list of all real estate offers currently existing") @CommandCompletion("all|sell|rent|lease") @Syntax("[all|sell|rent|lease] ") - public static void list(CommandSender player, @Optional String type, @Default("1") int page) + public static void list(CommandSender sender, @Optional String type, @Default("1") int page) { + Player player = null; + if (sender instanceof Player) { + player = (Player) sender; + } if(page <= 0) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Page must be a positive option!"); + Config.sendMessage(player, ChatColor.RED, Messages.PageMustBePositive); return; } int count = 0; @@ -91,12 +97,12 @@ public class RECommand extends BaseCommand } else { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!"); + sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!"); return; } if(count == 0) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction have been found!"); + sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction have been found!"); } else { @@ -123,22 +129,22 @@ public class RECommand extends BaseCommand int max = Math.min(start + RealEstate.instance.config.cfgPageSize, count); if(start <= max) { - player.sendMessage(ChatColor.DARK_BLUE + "----= " + ChatColor.WHITE + "[ " + ChatColor.GOLD + typeMsg + " page " + ChatColor.DARK_GREEN + " " + + sender.sendMessage(ChatColor.DARK_BLUE + "----= " + ChatColor.WHITE + "[ " + ChatColor.GOLD + typeMsg + " page " + ChatColor.DARK_GREEN + " " + page + ChatColor.GOLD + " / " + ChatColor.DARK_GREEN + (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize) + ChatColor.WHITE + " ]" + ChatColor.DARK_BLUE + " =----"); for(int i = start; i < max; i++) { RealEstate.instance.log.info("transaction " + i); - transactions.get(i).msgInfo(player); + transactions.get(i).msgInfo(sender); } if(page < (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize)) { - player.sendMessage(ChatColor.GOLD + "To see the next page, type " + ChatColor.GREEN + "/re list " + (type != null ? type : "all") + " " + (page + 1)); + sender.sendMessage(ChatColor.GOLD + "To see the next page, type " + ChatColor.GREEN + "/re list " + (type != null ? type : "all") + " " + (page + 1)); } } else { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This page does not exist!"); + Config.sendMessage(player, ChatColor.RED, Messages.PageNotExists); } } } @@ -161,8 +167,7 @@ public class RECommand extends BaseCommand } if(newStatus == null) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is currently " + - ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!"); + Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType); } else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable")) { @@ -172,8 +177,7 @@ public class RECommand extends BaseCommand { cr.autoRenew = newStatus.equalsIgnoreCase("enable"); RealEstate.transactionsStore.saveData(); - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is now " + - ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!"); + Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType); } else { diff --git a/src/me/EtienneDx/RealEstate/RealEstate.java b/src/me/EtienneDx/RealEstate/RealEstate.java index e450f07..865dff6 100644 --- a/src/me/EtienneDx/RealEstate/RealEstate.java +++ b/src/me/EtienneDx/RealEstate/RealEstate.java @@ -79,6 +79,7 @@ public class RealEstate extends JavaPlugin } this.config = new Config(); this.config.loadConfig();// loads config or default + this.config.loadMessages();// loads messages this.config.saveConfig();// save eventual default ConfigurationSerialization.registerClass(ClaimSell.class); diff --git a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java new file mode 100644 index 0000000..21ccedc --- /dev/null +++ b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java @@ -0,0 +1,32 @@ +package me.EtienneDx.RealEstate; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +import java.util.logging.Logger; + +class SendPlayerMessageTask implements Runnable +{ + private Player player; + private ChatColor color; + private String message; + + + public SendPlayerMessageTask(Player player, ChatColor color, String message) + { + this.player = player; + this.color = color; + this.message = message; + } + + @Override + public void run() + { + if(player == null) + { + RealEstate.instance.log.info(color + message); + return; + } + Config.sendMessage(this.player, this.color, this.message); + } +} From 7a91bac6eef5eb6d3df2a7d2f4c48a11608d2af6 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Thu, 14 May 2020 07:50:15 +0300 Subject: [PATCH 2/7] Update src/me/EtienneDx/RealEstate/Config.java Co-authored-by: EtienneDx <32039535+EtienneDx@users.noreply.github.com> --- src/me/EtienneDx/RealEstate/Config.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index 7b0c533..dc43f93 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -23,7 +23,8 @@ public class Config extends AnnotationConfig public final String configFilePath = RealEstate.pluginDirPath + "config.yml"; public final String logFilePath = RealEstate.pluginDirPath + "GriefProtection_RealEstate.log"; - final static String messagesFilePath = RealEstate.pluginDirPath + File.separator + "messages.yml"; + final static String messagesFilePath = RealEstate.pluginDirPath + "messages.yml"; + public final String chatPrefix = "[" + ChatColor.GOLD + "RealEstate" + ChatColor.WHITE + "] "; @ConfigField(name="RealEstate.Keywords.SignsHeader", comment = "What is displayed in top of the signs") From 104b130d28d8a37e5625f73b988eae5575ee8668 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Thu, 14 May 2020 07:50:49 +0300 Subject: [PATCH 3/7] Update src/me/EtienneDx/RealEstate/Config.java Co-authored-by: EtienneDx <32039535+EtienneDx@users.noreply.github.com> --- src/me/EtienneDx/RealEstate/Config.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index dc43f93..3fa7e37 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -134,11 +134,12 @@ public class Config extends AnnotationConfig HashMap defaults = new HashMap(); // initialize defaults - this.addDefault(defaults, Messages.NoTransactionFound, "No transaction found at your location!", null); - this.addDefault(defaults, Messages.PageMustBePositive, "Page must be a positive option!", null); - this.addDefault(defaults, Messages.PageNotExists, "This page does not exist!", null); - this.addDefault(defaults, Messages.RenewRentNow, "Automatic renew is now $a{0} $bfor this {1}", "0: the status; 1: a claim type"); - this.addDefault(defaults, Messages.RenewRentCurrently, "Automatic renew is currently $a{0} $bfor this {1}", "0: the status; 1: a claim type"); + this.addDefault(defaults, Messages.NoTransactionFound, "$cNo transaction found at your location!", null); + this.addDefault(defaults, Messages.PageMustBePositive, "$cPage must be a positive option!", null); + this.addDefault(defaults, Messages.PageNotExists, "$cThis page does not exist!", null); + this.addDefault(defaults, Messages.RenewRentNow, "$bAutomatic renew is now $a{0} $bfor this {1}", "0: the status; 1: a claim type"); + this.addDefault(defaults, Messages.RenewRentCurrently, "$bAutomatic renew is currently $a{0} $bfor this {1}", "0: the status; 1: a claim type"); + // load the config file From e240af74f0a1eacd8eb1ece59502e424cd4e6c22 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Thu, 14 May 2020 07:51:02 +0300 Subject: [PATCH 4/7] Update src/me/EtienneDx/RealEstate/Config.java Co-authored-by: EtienneDx <32039535+EtienneDx@users.noreply.github.com> --- src/me/EtienneDx/RealEstate/Config.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index 3fa7e37..055a6b4 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -178,7 +178,6 @@ public class Config extends AnnotationConfig defaults.clear(); RealEstate.instance.log.info("Customizable messages loaded."); - System.gc(); } private void addDefault(HashMap defaults, From ca0243e831050291a9378091f93fefaff55362d7 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Thu, 14 May 2020 10:47:22 +0300 Subject: [PATCH 5/7] Remove redundant coloring from messages --- src/me/EtienneDx/RealEstate/Config.java | 18 +++++++++--------- src/me/EtienneDx/RealEstate/RECommand.java | 13 +++++-------- .../RealEstate/SendPlayerMessageTask.java | 11 +++-------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index 055a6b4..3e4494c 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -197,29 +197,29 @@ public class Config extends AnnotationConfig return message; } //sends a color-coded message to a player - public static void sendMessage(Player player, ChatColor color, Messages messageID, String... args) { - sendMessage(player, color, messageID, 0, args); + public static void sendMessage(Player player, Messages messageID, String... args) { + sendMessage(player, messageID, 0, args); } //sends a color-coded message to a player - public static void sendMessage(Player player, ChatColor color, Messages messageID, long delayInTicks, String... args) { + public static void sendMessage(Player player, Messages messageID, long delayInTicks, String... args) { String message = RealEstate.instance.config.getMessage(messageID, args); - sendMessage(player, color, message, delayInTicks); + sendMessage(player, message, delayInTicks); } //sends a color-coded message to a player - public static void sendMessage(Player player, ChatColor color, String message) { + public static void sendMessage(Player player, String message) { if (message == null || message.length() == 0) return; if (player == null) { - RealEstate.instance.log.info(color + message); + RealEstate.instance.log.info(message); } else { - player.sendMessage(RealEstate.instance.config.chatPrefix + color + message); + player.sendMessage(RealEstate.instance.config.chatPrefix + message); } } - public static void sendMessage(Player player, ChatColor color, String message, long delayInTicks) { - SendPlayerMessageTask task = new SendPlayerMessageTask(player, color, message); + public static void sendMessage(Player player, String message, long delayInTicks) { + SendPlayerMessageTask task = new SendPlayerMessageTask(player, message); if (delayInTicks > 0) { RealEstate.instance.getServer().getScheduler().runTaskLater(RealEstate.instance, task, delayInTicks); diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index eecce8a..dcbf846 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -1,9 +1,6 @@ package me.EtienneDx.RealEstate; -import java.awt.*; import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; import java.util.UUID; import org.bukkit.Bukkit; @@ -51,7 +48,7 @@ public class RECommand extends BaseCommand } else { - Config.sendMessage(player, ChatColor.RED, Messages.NoTransactionFound); + Config.sendMessage(player, Messages.NoTransactionFound); } } @@ -68,7 +65,7 @@ public class RECommand extends BaseCommand } if(page <= 0) { - Config.sendMessage(player, ChatColor.RED, Messages.PageMustBePositive); + Config.sendMessage(player, Messages.PageMustBePositive); return; } int count = 0; @@ -144,7 +141,7 @@ public class RECommand extends BaseCommand } else { - Config.sendMessage(player, ChatColor.RED, Messages.PageNotExists); + Config.sendMessage(player, Messages.PageNotExists); } } } @@ -167,7 +164,7 @@ public class RECommand extends BaseCommand } if(newStatus == null) { - Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType); + Config.sendMessage(player, Messages.RenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType); } else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable")) { @@ -177,7 +174,7 @@ public class RECommand extends BaseCommand { cr.autoRenew = newStatus.equalsIgnoreCase("enable"); RealEstate.transactionsStore.saveData(); - Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType); + Config.sendMessage(player, Messages.RenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType); } else { diff --git a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java index 21ccedc..d124d51 100644 --- a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java +++ b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java @@ -1,21 +1,16 @@ package me.EtienneDx.RealEstate; -import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import java.util.logging.Logger; - class SendPlayerMessageTask implements Runnable { private Player player; - private ChatColor color; private String message; - public SendPlayerMessageTask(Player player, ChatColor color, String message) + public SendPlayerMessageTask(Player player, String message) { this.player = player; - this.color = color; this.message = message; } @@ -24,9 +19,9 @@ class SendPlayerMessageTask implements Runnable { if(player == null) { - RealEstate.instance.log.info(color + message); + RealEstate.instance.log.info(message); return; } - Config.sendMessage(this.player, this.color, this.message); + Config.sendMessage(this.player, this.message); } } From 8bf9ae58a0ada2d975891ea4266297a766c24210 Mon Sep 17 00:00:00 2001 From: Dmitry Rendov Date: Fri, 15 May 2020 17:02:27 +0300 Subject: [PATCH 6/7] Replace messages with AnnotationConfig --- src/me/EtienneDx/RealEstate/Config.java | 112 +----------------- .../RealEstate/CustomizableMessage.java | 15 --- src/me/EtienneDx/RealEstate/Messages.java | 88 ++++++++++++-- src/me/EtienneDx/RealEstate/RECommand.java | 10 +- src/me/EtienneDx/RealEstate/RealEstate.java | 8 +- .../RealEstate/SendPlayerMessageTask.java | 2 +- 6 files changed, 95 insertions(+), 140 deletions(-) delete mode 100644 src/me/EtienneDx/RealEstate/CustomizableMessage.java diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index 3e4494c..c3f505b 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -1,15 +1,10 @@ package me.EtienneDx.RealEstate; -import java.io.File; -import java.io.IOException; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import me.EtienneDx.AnnotationConfig.AnnotationConfig; @@ -23,8 +18,6 @@ public class Config extends AnnotationConfig public final String configFilePath = RealEstate.pluginDirPath + "config.yml"; public final String logFilePath = RealEstate.pluginDirPath + "GriefProtection_RealEstate.log"; - final static String messagesFilePath = RealEstate.pluginDirPath + "messages.yml"; - public final String chatPrefix = "[" + ChatColor.GOLD + "RealEstate" + ChatColor.WHITE + "] "; @ConfigField(name="RealEstate.Keywords.SignsHeader", comment = "What is displayed in top of the signs") @@ -95,9 +88,7 @@ public class Config extends AnnotationConfig @ConfigField(name="RealEstate.Settings.PageSize", comment = "How many Real Estate offer should be shown by page using the '/re list' command") public int cfgPageSize = 20; - - private String[] messages; - + public Config() { this.pdf = RealEstate.instance.getDescription(); @@ -127,105 +118,4 @@ public class Config extends AnnotationConfig //YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(this.configFilePath)); this.loadConfig(this.configFilePath); } - - public void loadMessages() { - Messages[] messageIDs = Messages.values(); - this.messages = new String[Messages.values().length]; - - HashMap defaults = new HashMap(); - // initialize defaults - this.addDefault(defaults, Messages.NoTransactionFound, "$cNo transaction found at your location!", null); - this.addDefault(defaults, Messages.PageMustBePositive, "$cPage must be a positive option!", null); - this.addDefault(defaults, Messages.PageNotExists, "$cThis page does not exist!", null); - this.addDefault(defaults, Messages.RenewRentNow, "$bAutomatic renew is now $a{0} $bfor this {1}", "0: the status; 1: a claim type"); - this.addDefault(defaults, Messages.RenewRentCurrently, "$bAutomatic renew is currently $a{0} $bfor this {1}", "0: the status; 1: a claim type"); - - - - // load the config file - FileConfiguration config = YamlConfiguration.loadConfiguration(new File(messagesFilePath)); - - // for each message ID - for (Messages messageID : messageIDs) { - //get default for this message - CustomizableMessage messageData = defaults.get(messageID.name()); - - // if default is missing, log an error and use some fake data for now so that the plugin can run - if (messageData == null) { - RealEstate.instance.log.info("Missing message for " + messageID.name() + ". Please contact the developer."); - messageData = new CustomizableMessage(messageID, "Missing message! ID: " + messageID.name() + ". Please contact a server admin.", null); - } - - // read the message from the file, use default if necessary - this.messages[messageID.ordinal()] = config.getString("Messages." + messageID.name() + ".Text", messageData.text); - config.set("Messages." + messageID.name() + ".Text", this.messages[messageID.ordinal()]); - - this.messages[messageID.ordinal()] = this.messages[messageID.ordinal()].replace('$', (char) 0x00A7); - - if (messageData.notes != null) { - messageData.notes = config.getString("Messages." + messageID.name() + ".Notes", messageData.notes); - config.set("Messages." + messageID.name() + ".Notes", messageData.notes); - } - } - - //save any changes - try { - config.options().header("Use a YAML editor like NotepadPlusPlus to edit this file. \nAfter editing, back up your changes before reloading the server in case you made a syntax error. \nUse dollar signs ($) for formatting codes, which are documented here: http://minecraft.gamepedia.com/Formatting_codes"); - config.save(messagesFilePath); - } catch (IOException exception) { - RealEstate.instance.log.info("Unable to write to the configuration file at \"" + messagesFilePath + "\""); - } - - defaults.clear(); - RealEstate.instance.log.info("Customizable messages loaded."); - } - - private void addDefault(HashMap defaults, - Messages id, String text, String notes) { - CustomizableMessage message = new CustomizableMessage(id, text, notes); - defaults.put(id.name(), message); - } - - synchronized public String getMessage(Messages messageID, String... args) { - String message = messages[messageID.ordinal()]; - - for (int i = 0; i < args.length; i++) { - String param = args[i]; - message = message.replace("{" + i + "}", param); - } - - return message; - } - //sends a color-coded message to a player - public static void sendMessage(Player player, Messages messageID, String... args) { - sendMessage(player, messageID, 0, args); - } - - //sends a color-coded message to a player - public static void sendMessage(Player player, Messages messageID, long delayInTicks, String... args) { - String message = RealEstate.instance.config.getMessage(messageID, args); - sendMessage(player, message, delayInTicks); - } - - //sends a color-coded message to a player - public static void sendMessage(Player player, String message) { - if (message == null || message.length() == 0) return; - - if (player == null) { - RealEstate.instance.log.info(message); - } else { - player.sendMessage(RealEstate.instance.config.chatPrefix + message); - } - } - - public static void sendMessage(Player player, String message, long delayInTicks) { - SendPlayerMessageTask task = new SendPlayerMessageTask(player, message); - - if (delayInTicks > 0) { - RealEstate.instance.getServer().getScheduler().runTaskLater(RealEstate.instance, task, delayInTicks); - } else { - task.run(); - } - } - } diff --git a/src/me/EtienneDx/RealEstate/CustomizableMessage.java b/src/me/EtienneDx/RealEstate/CustomizableMessage.java deleted file mode 100644 index daceb08..0000000 --- a/src/me/EtienneDx/RealEstate/CustomizableMessage.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.EtienneDx.RealEstate; - -public class CustomizableMessage -{ - public Messages id; - public String text; - public String notes; - - public CustomizableMessage(Messages id, String text, String notes) - { - this.id = id; - this.text = text; - this.notes = notes; - } -} \ No newline at end of file diff --git a/src/me/EtienneDx/RealEstate/Messages.java b/src/me/EtienneDx/RealEstate/Messages.java index d23aa86..afc5309 100644 --- a/src/me/EtienneDx/RealEstate/Messages.java +++ b/src/me/EtienneDx/RealEstate/Messages.java @@ -1,10 +1,84 @@ package me.EtienneDx.RealEstate; -public enum Messages +import me.EtienneDx.AnnotationConfig.AnnotationConfig; +import me.EtienneDx.AnnotationConfig.ConfigField; +import me.EtienneDx.AnnotationConfig.ConfigFile; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginDescriptionFile; + +import java.util.Arrays; +import java.util.List; + +@ConfigFile(header = "Use a YAML editor like NotepadPlusPlus to edit this file. \nAfter editing, back up your changes before reloading the server in case you made a syntax error. \nUse dollar signs ($) for formatting codes, which are documented here: http://minecraft.gamepedia.com/Formatting_codes") +public class Messages extends AnnotationConfig { - NoTransactionFound, - PageMustBePositive, - PageNotExists, - RenewRentNow, - RenewRentCurrently -} + public PluginDescriptionFile pdf; + + @ConfigField(name="RealEstate.Messages.NoTransactionFound") + public String msgNoTransactionFound = "$cNo transaction found at your location!"; + + @ConfigField(name="RealEstate.Messages.PageMustBePositive") + public String msgPageMustBePositive = "$cPage must be a positive option"; + + @ConfigField(name="RealEstate.Messages.PageNotExists") + public String msgPageNotExists = "$cThis page does not exist!"; + + @ConfigField(name="RealEstate.Messages.RenewRentNow", comment = "0: enabled/disabled; 1: type of claim") + public String msgRenewRentNow = "$bAutomatic renew is now $a{0} $bfor this {1}"; + + @ConfigField(name="RealEstate.Messages.RenewRentCurrently", comment = "0: enabled/disabled; 1: type of claim") + public String msgRenewRentCurrently = "$bAutomatic renew is currently $a{0} $bfor this {1}"; + + public Messages() + { + this.pdf = RealEstate.instance.getDescription(); + } + + @Override + public void loadConfig() + { + this.loadConfig(RealEstate.messagesFilePath); + } + + synchronized public String getMessage(String msgTemplate, String... args) { + for (int i = 0; i < args.length; i++) { + String param = args[i]; + msgTemplate = msgTemplate.replace("{" + i + "}", param); + } + + return msgTemplate.replace('$', (char) 0x00A7); + } + //sends a color-coded message to a player + public static void sendMessage(Player player, String msgTemplate, String... args) { + sendMessage(player, msgTemplate, 0, args); + } + + //sends a color-coded message to a player + public static void sendMessage(Player player, String msgTemplate, long delayInTicks, String... args) { + String message = RealEstate.instance.messages.getMessage(msgTemplate, args); + sendMessage(player, message, delayInTicks); + } + + //sends a color-coded message to a player + public static void sendMessage(Player player, String message) { + if (message == null || message.length() == 0) return; + + if (player == null) { + RealEstate.instance.log.info(message); + } else { + player.sendMessage(RealEstate.instance.config.chatPrefix + message.replace('$', (char) 0x00A7)); + } + } + + public static void sendMessage(Player player, String message, long delayInTicks) { + SendPlayerMessageTask task = new SendPlayerMessageTask(player, message); + + if (delayInTicks > 0) { + RealEstate.instance.getServer().getScheduler().runTaskLater(RealEstate.instance, task, delayInTicks); + } else { + task.run(); + } + } + +} \ No newline at end of file diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index dcbf846..6276f06 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -48,7 +48,7 @@ public class RECommand extends BaseCommand } else { - Config.sendMessage(player, Messages.NoTransactionFound); + Messages.sendMessage(player, RealEstate.instance.messages.msgNoTransactionFound); } } @@ -65,7 +65,7 @@ public class RECommand extends BaseCommand } if(page <= 0) { - Config.sendMessage(player, Messages.PageMustBePositive); + Messages.sendMessage(player, RealEstate.instance.messages.msgPageMustBePositive); return; } int count = 0; @@ -141,7 +141,7 @@ public class RECommand extends BaseCommand } else { - Config.sendMessage(player, Messages.PageNotExists); + Messages.sendMessage(player, RealEstate.instance.messages.msgPageNotExists); } } } @@ -164,7 +164,7 @@ public class RECommand extends BaseCommand } if(newStatus == null) { - Config.sendMessage(player, Messages.RenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType); + Messages.sendMessage(player, RealEstate.instance.messages.msgRenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType); } else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable")) { @@ -174,7 +174,7 @@ public class RECommand extends BaseCommand { cr.autoRenew = newStatus.equalsIgnoreCase("enable"); RealEstate.transactionsStore.saveData(); - Config.sendMessage(player, Messages.RenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType); + Messages.sendMessage(player, RealEstate.instance.messages.msgRenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType); } else { diff --git a/src/me/EtienneDx/RealEstate/RealEstate.java b/src/me/EtienneDx/RealEstate/RealEstate.java index 865dff6..262aa0d 100644 --- a/src/me/EtienneDx/RealEstate/RealEstate.java +++ b/src/me/EtienneDx/RealEstate/RealEstate.java @@ -30,8 +30,10 @@ public class RealEstate extends JavaPlugin { public Logger log; public Config config; + public Messages messages; BukkitCommandManager manager; public final static String pluginDirPath = "plugins" + File.separator + "RealEstate" + File.separator; + final static String messagesFilePath = RealEstate.pluginDirPath + "messages.yml"; public static boolean vaultPresent = false; public static Economy econ = null; public static Permission perms = null; @@ -79,9 +81,13 @@ public class RealEstate extends JavaPlugin } this.config = new Config(); this.config.loadConfig();// loads config or default - this.config.loadMessages();// loads messages this.config.saveConfig();// save eventual default + this.messages = new Messages(); + this.messages.loadConfig(this.messagesFilePath);// loads customizable messages or defaults + this.messages.saveConfig(this.messagesFilePath);// save eventual default + this.log.info("Customizable messages loaded."); + ConfigurationSerialization.registerClass(ClaimSell.class); ConfigurationSerialization.registerClass(ClaimRent.class); ConfigurationSerialization.registerClass(ClaimLease.class); diff --git a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java index d124d51..aa460d6 100644 --- a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java +++ b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java @@ -22,6 +22,6 @@ class SendPlayerMessageTask implements Runnable RealEstate.instance.log.info(message); return; } - Config.sendMessage(this.player, this.message); + Messages.sendMessage(this.player, this.message); } } From 6c967caa89d8420b5a958831920dc783d1029b18 Mon Sep 17 00:00:00 2001 From: EtienneDx <32039535+EtienneDx@users.noreply.github.com> Date: Fri, 15 May 2020 18:18:24 +0200 Subject: [PATCH 7/7] Update src/me/EtienneDx/RealEstate/Messages.java --- src/me/EtienneDx/RealEstate/Messages.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/me/EtienneDx/RealEstate/Messages.java b/src/me/EtienneDx/RealEstate/Messages.java index afc5309..017c705 100644 --- a/src/me/EtienneDx/RealEstate/Messages.java +++ b/src/me/EtienneDx/RealEstate/Messages.java @@ -15,19 +15,19 @@ public class Messages extends AnnotationConfig { public PluginDescriptionFile pdf; - @ConfigField(name="RealEstate.Messages.NoTransactionFound") + @ConfigField(name="RealEstate.NoTransactionFound") public String msgNoTransactionFound = "$cNo transaction found at your location!"; - @ConfigField(name="RealEstate.Messages.PageMustBePositive") + @ConfigField(name="RealEstate.PageMustBePositive") public String msgPageMustBePositive = "$cPage must be a positive option"; - @ConfigField(name="RealEstate.Messages.PageNotExists") + @ConfigField(name="RealEstate.PageNotExists") public String msgPageNotExists = "$cThis page does not exist!"; - @ConfigField(name="RealEstate.Messages.RenewRentNow", comment = "0: enabled/disabled; 1: type of claim") + @ConfigField(name="RealEstate.RenewRentNow", comment = "0: enabled/disabled; 1: type of claim") public String msgRenewRentNow = "$bAutomatic renew is now $a{0} $bfor this {1}"; - @ConfigField(name="RealEstate.Messages.RenewRentCurrently", comment = "0: enabled/disabled; 1: type of claim") + @ConfigField(name="RealEstate.RenewRentCurrently", comment = "0: enabled/disabled; 1: type of claim") public String msgRenewRentCurrently = "$bAutomatic renew is currently $a{0} $bfor this {1}"; public Messages() @@ -81,4 +81,4 @@ public class Messages extends AnnotationConfig } } -} \ No newline at end of file +}