From eb181214ce32cc1cccf0ac7b97c1c35e48b67aba Mon Sep 17 00:00:00 2001 From: EtienneDx Date: Wed, 20 Jan 2021 21:30:05 +0100 Subject: [PATCH] Started extracting messages to allow easier translations --- src/me/EtienneDx/RealEstate/Config.java | 3 ++ src/me/EtienneDx/RealEstate/Messages.java | 38 +++++++++++--- src/me/EtienneDx/RealEstate/RECommand.java | 16 +++--- src/me/EtienneDx/RealEstate/RealEstate.java | 50 +++++++++---------- .../RealEstate/SendPlayerMessageTask.java | 6 +-- 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/src/me/EtienneDx/RealEstate/Config.java b/src/me/EtienneDx/RealEstate/Config.java index 4f6c2db..8313120 100644 --- a/src/me/EtienneDx/RealEstate/Config.java +++ b/src/me/EtienneDx/RealEstate/Config.java @@ -89,6 +89,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 = 8; + @ConfigField(name="RealEstate.Settings.MessagesFiles", comment="Language file to be used. You can see all languages files in the languages directory. If the language file does not exist, it will be created and you'll be able to modify it later on.") + public String languageFile = "en.yml"; + public Config() { this.pdf = RealEstate.instance.getDescription(); diff --git a/src/me/EtienneDx/RealEstate/Messages.java b/src/me/EtienneDx/RealEstate/Messages.java index 017c705..4d69565 100644 --- a/src/me/EtienneDx/RealEstate/Messages.java +++ b/src/me/EtienneDx/RealEstate/Messages.java @@ -3,6 +3,9 @@ package me.EtienneDx.RealEstate; import me.EtienneDx.AnnotationConfig.AnnotationConfig; import me.EtienneDx.AnnotationConfig.ConfigField; import me.EtienneDx.AnnotationConfig.ConfigFile; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; @@ -10,7 +13,7 @@ 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") +@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.\n You can use {0}, {1} to include the different values indicated in the comments") public class Messages extends AnnotationConfig { public PluginDescriptionFile pdf; @@ -29,6 +32,29 @@ public class Messages extends AnnotationConfig @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 String msgErrorOutOfClaim = ChatColor.RED + "You must stand inside of a claim to use this command!"; + + public String msgErrorPlayerOnly = ChatColor.RED + "Only Players can perform this command!"; + + public String msgErrorNoOngoingTransaction = ChatColor.RED + "This claim has no ongoing transactions!"; + + public String msgErrorNotRentNorLease = ChatColor.RED + "This claim is neither to rent or to lease!"; + + public String msgErrorAlreadyBought = ChatColor.RED + "This claim already has a buyer!"; + + public String msgErrorNotPartOfTransaction = ChatColor.RED + "You are not part of this transaction!"; + + public String msgErrorRentOnly = ChatColor.RED + "This command only applies to rented claims!"; + + public String msgErrorValueGreaterThanZero = ChatColor.RED + "The value must be greater than zero!"; + + public String msgErrorInvalidOption = ChatColor.RED + "Invalid option provided!"; + + public String msgListTransactionsHeader = ChatColor.DARK_BLUE + "----= " + ChatColor.WHITE + "[ " + ChatColor.GOLD + "{0} page " + ChatColor.DARK_GREEN + " {1}" + + ChatColor.GOLD + " / " + ChatColor.DARK_GREEN + "{2}" + ChatColor.WHITE + " ]" + ChatColor.DARK_BLUE + " =----"; + + public String msgListNextPage = ChatColor.GOLD + "To see the next page, type " + ChatColor.GREEN + "/re list {0} {1}"; public Messages() { @@ -38,7 +64,7 @@ public class Messages extends AnnotationConfig @Override public void loadConfig() { - this.loadConfig(RealEstate.messagesFilePath); + this.loadConfig(RealEstate.languagesDirectory + "/" + RealEstate.instance.config.languageFile); } synchronized public String getMessage(String msgTemplate, String... args) { @@ -50,18 +76,18 @@ public class Messages extends AnnotationConfig return msgTemplate.replace('$', (char) 0x00A7); } //sends a color-coded message to a player - public static void sendMessage(Player player, String msgTemplate, String... args) { + public static void sendMessage(CommandSender 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) { + public static void sendMessage(CommandSender 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) { + public static void sendMessage(CommandSender player, String message) { if (message == null || message.length() == 0) return; if (player == null) { @@ -71,7 +97,7 @@ public class Messages extends AnnotationConfig } } - public static void sendMessage(Player player, String message, long delayInTicks) { + public static void sendMessage(CommandSender player, String message, long delayInTicks) { SendPlayerMessageTask task = new SendPlayerMessageTask(player, message); if (delayInTicks > 0) { diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index 6276f06..b57c134 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -94,12 +94,12 @@ public class RECommand extends BaseCommand } else { - sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!"); + Messages.sendMessage(sender, RealEstate.instance.messages.msgErrorInvalidOption); return; } if(count == 0) { - sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction have been found!"); + Messages.sendMessage(sender, RealEstate.instance.messages.msgNoTransactionFound); } else { @@ -126,22 +126,22 @@ public class RECommand extends BaseCommand int max = Math.min(start + RealEstate.instance.config.cfgPageSize, count); if(start <= max) { - 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 + " =----"); + int pageCount = (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize); + Messages.sendMessage(sender, RealEstate.instance.messages.msgListTransactionsHeader, + typeMsg, String.valueOf(page), String.valueOf(pageCount)); for(int i = start; i < max; i++) { RealEstate.instance.log.info("transaction " + i); transactions.get(i).msgInfo(sender); } - if(page < (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize)) + if(page < pageCount) { - sender.sendMessage(ChatColor.GOLD + "To see the next page, type " + ChatColor.GREEN + "/re list " + (type != null ? type : "all") + " " + (page + 1)); + Messages.sendMessage(sender, RealEstate.instance.messages.msgListNextPage, (type != null ? type : "all"), String.valueOf(page + 1)); } } else { - Messages.sendMessage(player, RealEstate.instance.messages.msgPageNotExists); + Messages.sendMessage(sender, RealEstate.instance.messages.msgPageNotExists); } } } diff --git a/src/me/EtienneDx/RealEstate/RealEstate.java b/src/me/EtienneDx/RealEstate/RealEstate.java index 3d327d0..ada18f3 100644 --- a/src/me/EtienneDx/RealEstate/RealEstate.java +++ b/src/me/EtienneDx/RealEstate/RealEstate.java @@ -33,7 +33,7 @@ public class RealEstate extends JavaPlugin public Messages messages; BukkitCommandManager manager; public final static String pluginDirPath = "plugins" + File.separator + "RealEstate" + File.separator; - final static String messagesFilePath = RealEstate.pluginDirPath + "messages.yml"; + final static String languagesDirectory = RealEstate.pluginDirPath + "languages"; public static boolean vaultPresent = false; public static Economy econ = null; public static Permission perms = null; @@ -84,8 +84,8 @@ public class RealEstate extends JavaPlugin 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.messages.loadConfig();// loads customizable messages or defaults + this.messages.saveConfig();// save eventual default this.log.info("Customizable messages loaded."); ConfigurationSerialization.registerClass(ClaimSell.class); @@ -113,78 +113,78 @@ public class RealEstate extends JavaPlugin { return; } - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); }); manager.getCommandConditions().addCondition("claimHasTransaction", (context) -> { if(!context.getIssuer().isPlayer()) { - throw new ConditionFailedException("Only Players can perform this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorPlayerOnly); } Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(c == null) { - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); } Transaction tr = transactionsStore.getTransaction(c); if(tr == null) { - throw new ConditionFailedException("This claim has no ongoing transactions!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNoOngoingTransaction); } }); manager.getCommandConditions().addCondition("inPendingTransactionClaim", (context) -> { if(!context.getIssuer().isPlayer()) { - throw new ConditionFailedException("Only Players can perform this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorPlayerOnly); } Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(c == null) { - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); } Transaction tr = transactionsStore.getTransaction(c); if(tr == null) { - throw new ConditionFailedException("This claim is neither to rent or to lease!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotRentNorLease); } else if(tr instanceof BoughtTransaction && ((BoughtTransaction)tr).getBuyer() != null) { - throw new ConditionFailedException("This claim already has a buyer!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorAlreadyBought); } }); manager.getCommandConditions().addCondition("inBoughtClaim", (context) -> { if(!context.getIssuer().isPlayer()) { - throw new ConditionFailedException("Only Players can perform this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorPlayerOnly); } Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(c == null) { - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); } Transaction tr = transactionsStore.getTransaction(c); if(tr == null || !(tr instanceof BoughtTransaction)) { - throw new ConditionFailedException("This claim is neither to rent or to lease!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotRentNorLease); } }); manager.getCommandConditions().addCondition("partOfBoughtTransaction", context -> { if(!context.getIssuer().isPlayer()) { - throw new ConditionFailedException("Only Players can perform this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorPlayerOnly); } Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(c == null) { - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); } Transaction tr = transactionsStore.getTransaction(c); if(tr == null) { - throw new ConditionFailedException("This claim is neither to sell, rent or lease!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNoOngoingTransaction); } if(!(tr instanceof BoughtTransaction)) { - throw new ConditionFailedException("This command only applies to rented or leased claims!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotRentNorLease); } if((((BoughtTransaction)tr).buyer != null && ((BoughtTransaction)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) || (tr.getOwner() != null && (tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId()))) || @@ -192,26 +192,26 @@ public class RealEstate extends JavaPlugin { return; } - throw new ConditionFailedException("You are not part of this transaction!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotPartOfTransaction); }); manager.getCommandConditions().addCondition("partOfRent", context -> { if(!context.getIssuer().isPlayer()) { - throw new ConditionFailedException("Only Players can perform this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorPlayerOnly); } Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(c == null) { - throw new ConditionFailedException("You must stand inside of a claim to use this command!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorOutOfClaim); } Transaction tr = transactionsStore.getTransaction(c); if(tr == null) { - throw new ConditionFailedException("This claim is neither to sell, rent or lease!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNoOngoingTransaction); } if(!(tr instanceof ClaimRent)) { - throw new ConditionFailedException("This command only applies to rented claims!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorRentOnly); } if((((ClaimRent)tr).buyer != null && ((ClaimRent)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) || (tr.getOwner() != null && (tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId()))) || @@ -219,11 +219,11 @@ public class RealEstate extends JavaPlugin { return; } - throw new ConditionFailedException("You are not part of this transaction!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotPartOfTransaction); }); manager.getCommandConditions().addCondition(Double.class, "positiveDouble", (c, exec, value) -> { if(value > 0) return; - throw new ConditionFailedException("The value must be greater than zero!"); + throw new ConditionFailedException(config.chatPrefix + messages.msgErrorValueGreaterThanZero); }); } diff --git a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java index aa460d6..ba53f8b 100644 --- a/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java +++ b/src/me/EtienneDx/RealEstate/SendPlayerMessageTask.java @@ -1,14 +1,14 @@ package me.EtienneDx.RealEstate; -import org.bukkit.entity.Player; +import org.bukkit.command.CommandSender; class SendPlayerMessageTask implements Runnable { - private Player player; + private CommandSender player; private String message; - public SendPlayerMessageTask(Player player, String message) + public SendPlayerMessageTask(CommandSender player, String message) { this.player = player; this.message = message;