Replace messages with AnnotationConfig
This commit is contained in:
parent
ca0243e831
commit
8bf9ae58a0
@ -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<String, CustomizableMessage> defaults = new HashMap<String, CustomizableMessage>();
|
||||
// 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<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, 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user