Started extracting messages to allow easier translations

This commit is contained in:
EtienneDx 2021-01-20 21:30:05 +01:00
parent 93735c51da
commit eb181214ce
5 changed files with 71 additions and 42 deletions

View File

@ -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") @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; 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() public Config()
{ {
this.pdf = RealEstate.instance.getDescription(); this.pdf = RealEstate.instance.getDescription();

View File

@ -3,6 +3,9 @@ package me.EtienneDx.RealEstate;
import me.EtienneDx.AnnotationConfig.AnnotationConfig; import me.EtienneDx.AnnotationConfig.AnnotationConfig;
import me.EtienneDx.AnnotationConfig.ConfigField; import me.EtienneDx.AnnotationConfig.ConfigField;
import me.EtienneDx.AnnotationConfig.ConfigFile; import me.EtienneDx.AnnotationConfig.ConfigFile;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
@ -10,7 +13,7 @@ import org.bukkit.plugin.PluginDescriptionFile;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 class Messages extends AnnotationConfig
{ {
public PluginDescriptionFile pdf; public PluginDescriptionFile pdf;
@ -30,6 +33,29 @@ public class Messages extends AnnotationConfig
@ConfigField(name="RealEstate.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 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() public Messages()
{ {
this.pdf = RealEstate.instance.getDescription(); this.pdf = RealEstate.instance.getDescription();
@ -38,7 +64,7 @@ public class Messages extends AnnotationConfig
@Override @Override
public void loadConfig() public void loadConfig()
{ {
this.loadConfig(RealEstate.messagesFilePath); this.loadConfig(RealEstate.languagesDirectory + "/" + RealEstate.instance.config.languageFile);
} }
synchronized public String getMessage(String msgTemplate, String... args) { synchronized public String getMessage(String msgTemplate, String... args) {
@ -50,18 +76,18 @@ public class Messages extends AnnotationConfig
return msgTemplate.replace('$', (char) 0x00A7); return msgTemplate.replace('$', (char) 0x00A7);
} }
//sends a color-coded message to a player //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); sendMessage(player, msgTemplate, 0, args);
} }
//sends a color-coded message to a player //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); String message = RealEstate.instance.messages.getMessage(msgTemplate, args);
sendMessage(player, message, delayInTicks); sendMessage(player, message, delayInTicks);
} }
//sends a color-coded message to a player //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 (message == null || message.length() == 0) return;
if (player == null) { 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); SendPlayerMessageTask task = new SendPlayerMessageTask(player, message);
if (delayInTicks > 0) { if (delayInTicks > 0) {

View File

@ -94,12 +94,12 @@ public class RECommand extends BaseCommand
} }
else else
{ {
sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!"); Messages.sendMessage(sender, RealEstate.instance.messages.msgErrorInvalidOption);
return; return;
} }
if(count == 0) if(count == 0)
{ {
sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction have been found!"); Messages.sendMessage(sender, RealEstate.instance.messages.msgNoTransactionFound);
} }
else else
{ {
@ -126,22 +126,22 @@ public class RECommand extends BaseCommand
int max = Math.min(start + RealEstate.instance.config.cfgPageSize, count); int max = Math.min(start + RealEstate.instance.config.cfgPageSize, count);
if(start <= max) if(start <= max)
{ {
sender.sendMessage(ChatColor.DARK_BLUE + "----= " + ChatColor.WHITE + "[ " + ChatColor.GOLD + typeMsg + " page " + ChatColor.DARK_GREEN + " " + int pageCount = (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize);
page + ChatColor.GOLD + " / " + ChatColor.DARK_GREEN + (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize) + Messages.sendMessage(sender, RealEstate.instance.messages.msgListTransactionsHeader,
ChatColor.WHITE + " ]" + ChatColor.DARK_BLUE + " =----"); typeMsg, String.valueOf(page), String.valueOf(pageCount));
for(int i = start; i < max; i++) for(int i = start; i < max; i++)
{ {
RealEstate.instance.log.info("transaction " + i); RealEstate.instance.log.info("transaction " + i);
transactions.get(i).msgInfo(sender); 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 else
{ {
Messages.sendMessage(player, RealEstate.instance.messages.msgPageNotExists); Messages.sendMessage(sender, RealEstate.instance.messages.msgPageNotExists);
} }
} }
} }

View File

@ -33,7 +33,7 @@ public class RealEstate extends JavaPlugin
public Messages messages; public Messages messages;
BukkitCommandManager manager; BukkitCommandManager manager;
public final static String pluginDirPath = "plugins" + File.separator + "RealEstate" + File.separator; 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 boolean vaultPresent = false;
public static Economy econ = null; public static Economy econ = null;
public static Permission perms = null; public static Permission perms = null;
@ -84,8 +84,8 @@ public class RealEstate extends JavaPlugin
this.config.saveConfig();// save eventual default this.config.saveConfig();// save eventual default
this.messages = new Messages(); this.messages = new Messages();
this.messages.loadConfig(this.messagesFilePath);// loads customizable messages or defaults this.messages.loadConfig();// loads customizable messages or defaults
this.messages.saveConfig(this.messagesFilePath);// save eventual default this.messages.saveConfig();// save eventual default
this.log.info("Customizable messages loaded."); this.log.info("Customizable messages loaded.");
ConfigurationSerialization.registerClass(ClaimSell.class); ConfigurationSerialization.registerClass(ClaimSell.class);
@ -113,78 +113,78 @@ public class RealEstate extends JavaPlugin
{ {
return; 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) -> { manager.getCommandConditions().addCondition("claimHasTransaction", (context) -> {
if(!context.getIssuer().isPlayer()) 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); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == 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); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null) if(tr == null)
{ {
throw new ConditionFailedException("This claim has no ongoing transactions!"); throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNoOngoingTransaction);
} }
}); });
manager.getCommandConditions().addCondition("inPendingTransactionClaim", (context) -> { manager.getCommandConditions().addCondition("inPendingTransactionClaim", (context) -> {
if(!context.getIssuer().isPlayer()) 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); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == 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); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null) 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) 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) -> { manager.getCommandConditions().addCondition("inBoughtClaim", (context) -> {
if(!context.getIssuer().isPlayer()) 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); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == 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); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null || !(tr instanceof BoughtTransaction)) 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 -> { manager.getCommandConditions().addCondition("partOfBoughtTransaction", context -> {
if(!context.getIssuer().isPlayer()) 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); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == 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); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null) 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)) 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())) || if((((BoughtTransaction)tr).buyer != null && ((BoughtTransaction)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) ||
(tr.getOwner() != null && (tr.getOwner().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; return;
} }
throw new ConditionFailedException("You are not part of this transaction!"); throw new ConditionFailedException(config.chatPrefix + messages.msgErrorNotPartOfTransaction);
}); });
manager.getCommandConditions().addCondition("partOfRent", context -> { manager.getCommandConditions().addCondition("partOfRent", context -> {
if(!context.getIssuer().isPlayer()) 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); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == 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); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null) 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)) 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())) || if((((ClaimRent)tr).buyer != null && ((ClaimRent)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) ||
(tr.getOwner() != null && (tr.getOwner().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; 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) -> { manager.getCommandConditions().addCondition(Double.class, "positiveDouble", (c, exec, value) -> {
if(value > 0) return; if(value > 0) return;
throw new ConditionFailedException("The value must be greater than zero!"); throw new ConditionFailedException(config.chatPrefix + messages.msgErrorValueGreaterThanZero);
}); });
} }

View File

@ -1,14 +1,14 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import org.bukkit.entity.Player; import org.bukkit.command.CommandSender;
class SendPlayerMessageTask implements Runnable class SendPlayerMessageTask implements Runnable
{ {
private Player player; private CommandSender player;
private String message; private String message;
public SendPlayerMessageTask(Player player, String message) public SendPlayerMessageTask(CommandSender player, String message)
{ {
this.player = player; this.player = player;
this.message = message; this.message = message;