Introduce messages.yml for further localization

This commit is contained in:
Dmitry Rendov 2020-05-13 19:53:08 +03:00 committed by Dmitry Rendov
parent a97b667795
commit 3410d27851
8 changed files with 191 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.classpath .classpath
.project .project
.idea
.settings/* .settings/*
bin/* bin/*
target/* target/*

View File

@ -34,7 +34,7 @@
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version> <version>3.2.1</version>
<configuration> <configuration>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation> <createDependencyReducedPom>false</createDependencyReducedPom>
<relocations> <relocations>
<relocation> <relocation>
<pattern>co.aikar.commands</pattern> <pattern>co.aikar.commands</pattern>
@ -64,7 +64,7 @@
</repository> </repository>
<repository> <repository>
<id>bungeecord-repo</id> <id>bungeecord-repo</id>
<url>https://()oss.sonatype.org/content/repositories/snapshots</url> <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository> </repository>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>

View File

@ -1,10 +1,15 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import java.io.File;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import me.EtienneDx.AnnotationConfig.AnnotationConfig; 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 configFilePath = RealEstate.pluginDirPath + "config.yml";
public final String logFilePath = RealEstate.pluginDirPath + "GriefProtection_RealEstate.log"; 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 + "] "; public final String chatPrefix = "[" + ChatColor.GOLD + "RealEstate" + ChatColor.WHITE + "] ";
@ConfigField(name="RealEstate.Keywords.SignsHeader", comment = "What is displayed in top of the signs") @ConfigField(name="RealEstate.Keywords.SignsHeader", comment = "What is displayed in top of the signs")
@ -89,6 +95,8 @@ 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 = 20; public int cfgPageSize = 20;
private String[] messages;
public Config() public Config()
{ {
this.pdf = RealEstate.instance.getDescription(); this.pdf = RealEstate.instance.getDescription();
@ -118,4 +126,105 @@ public class Config extends AnnotationConfig
//YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(this.configFilePath)); //YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(this.configFilePath));
this.loadConfig(this.configFilePath); this.loadConfig(this.configFilePath);
} }
public void loadMessages() {
Messages[] messageIDs = Messages.values();
this.messages = new String[Messages.values().length];
HashMap<String, CustomizableMessage> defaults = new HashMap<String, CustomizableMessage>();
// 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<String, CustomizableMessage> 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();
}
}
} }

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
package me.EtienneDx.RealEstate;
public enum Messages
{
NoTransactionFound,
PageMustBePositive,
PageNotExists,
RenewRentNow,
RenewRentCurrently
}

View File

@ -1,5 +1,6 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -50,19 +51,24 @@ public class RECommand extends BaseCommand
} }
else else
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction found at your location!"); Config.sendMessage(player, ChatColor.RED, Messages.NoTransactionFound);
} }
} }
@Subcommand("list") @Subcommand("list")
@Description("Displays the list of all real estate offers currently existing") @Description("Displays the list of all real estate offers currently existing")
@CommandCompletion("all|sell|rent|lease") @CommandCompletion("all|sell|rent|lease")
@Syntax("[all|sell|rent|lease] <page>") @Syntax("[all|sell|rent|lease] <page>")
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) 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; return;
} }
int count = 0; int count = 0;
@ -91,12 +97,12 @@ public class RECommand extends BaseCommand
} }
else else
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!"); sender.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Invalid option provided!");
return; return;
} }
if(count == 0) 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 else
{ {
@ -123,22 +129,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)
{ {
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) + page + ChatColor.GOLD + " / " + ChatColor.DARK_GREEN + (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize) +
ChatColor.WHITE + " ]" + ChatColor.DARK_BLUE + " =----"); ChatColor.WHITE + " ]" + ChatColor.DARK_BLUE + " =----");
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(player); transactions.get(i).msgInfo(sender);
} }
if(page < (int)Math.ceil(count / (double)RealEstate.instance.config.cfgPageSize)) 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 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) if(newStatus == null)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is currently " + Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentCurrently, cr.autoRenew ? "enabled" : "disabled", claimType);
ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!");
} }
else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable")) else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable"))
{ {
@ -172,8 +177,7 @@ public class RECommand extends BaseCommand
{ {
cr.autoRenew = newStatus.equalsIgnoreCase("enable"); cr.autoRenew = newStatus.equalsIgnoreCase("enable");
RealEstate.transactionsStore.saveData(); RealEstate.transactionsStore.saveData();
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is now " + Config.sendMessage(player, ChatColor.AQUA, Messages.RenewRentNow, cr.autoRenew ? "enabled" : "disabled", claimType);
ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!");
} }
else else
{ {

View File

@ -79,6 +79,7 @@ public class RealEstate extends JavaPlugin
} }
this.config = new Config(); this.config = new Config();
this.config.loadConfig();// loads config or default this.config.loadConfig();// loads config or default
this.config.loadMessages();// loads messages
this.config.saveConfig();// save eventual default this.config.saveConfig();// save eventual default
ConfigurationSerialization.registerClass(ClaimSell.class); ConfigurationSerialization.registerClass(ClaimSell.class);

View File

@ -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);
}
}