Removes possibility of plugin collision
Allows the string formatter to be instantiated, and makes most methods non-static. Makes the translator plugin-independent.
This commit is contained in:
parent
7299e86afc
commit
7cd9ae087a
@ -1,33 +1,12 @@
|
||||
package net.knarcraft.knarlib;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
/**
|
||||
* KnarLib's main class
|
||||
*
|
||||
* <p>Make sure you initialize KnarLib by running setPlugin() during onEnable! Some methods will not work without it.</p>
|
||||
*/
|
||||
public final class KnarLib {
|
||||
|
||||
private static JavaPlugin plugin;
|
||||
|
||||
/**
|
||||
* Gets a plugin instance for use with the bukkit scheduler or similar
|
||||
*
|
||||
* @return <p>A plugin instance</p>
|
||||
*/
|
||||
public static JavaPlugin getPlugin() {
|
||||
return plugin;
|
||||
private KnarLib() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an instance of the plugin used with KnarLib
|
||||
*
|
||||
* @param plugin <p>The plugin instance to use</p>
|
||||
*/
|
||||
public static void setPlugin(JavaPlugin plugin) {
|
||||
KnarLib.plugin = plugin;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package net.knarcraft.knarlib.formatting;
|
||||
|
||||
import net.knarcraft.knarlib.KnarLib;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.ColorHelper;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
@ -11,21 +10,30 @@ import org.bukkit.command.CommandSender;
|
||||
*/
|
||||
public final class StringFormatter {
|
||||
|
||||
private final static String pluginName = KnarLib.getPlugin().getDescription().getName();
|
||||
|
||||
private StringFormatter() {
|
||||
private final String pluginName;
|
||||
private final Translator translator;
|
||||
|
||||
/**
|
||||
* Instantiates a new string formatter
|
||||
*
|
||||
* <p>You may leave one of these as null if not in use, for example if you don't use translatable strings.</p>
|
||||
*
|
||||
* @param pluginName <p>The name of the using plugin (used for the message prefix)</p>
|
||||
* @param translator <p>The translator to use for translating messages</p>
|
||||
*/
|
||||
public StringFormatter(String pluginName, Translator translator) {
|
||||
this.pluginName = pluginName;
|
||||
this.translator = translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a message signifying a successful action
|
||||
*
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param translator <p>The translator to use for the translation</p>
|
||||
* @param message <p>The translatable message to display</p>
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param message <p>The translatable message to display</p>
|
||||
*/
|
||||
public static void displaySuccessMessage(CommandSender sender, Translator translator, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(translator.getTranslatedMessage(message)));
|
||||
public void displaySuccessMessage(CommandSender sender, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(this.translator.getTranslatedMessage(message)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,19 +42,18 @@ public final class StringFormatter {
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param message <p>The raw message to display</p>
|
||||
*/
|
||||
public static void displaySuccessMessage(CommandSender sender, String message) {
|
||||
public void displaySuccessMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a message signifying an unsuccessful action
|
||||
*
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param translator <p>The translator to use for the translation</p>
|
||||
* @param message <p>The translatable message to display</p>
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param message <p>The translatable message to display</p>
|
||||
*/
|
||||
public static void displayErrorMessage(CommandSender sender, Translator translator, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(translator.getTranslatedMessage(message)));
|
||||
public void displayErrorMessage(CommandSender sender, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(this.translator.getTranslatedMessage(message)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +62,7 @@ public final class StringFormatter {
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param message <p>The raw message to display</p>
|
||||
*/
|
||||
public static void displayErrorMessage(CommandSender sender, String message) {
|
||||
public void displayErrorMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message));
|
||||
}
|
||||
|
||||
@ -65,36 +72,38 @@ public final class StringFormatter {
|
||||
* @param message <p>The message to format</p>
|
||||
* @return <p>The formatted message</p>
|
||||
*/
|
||||
private static String getFormattedMessage(String message) {
|
||||
return "[" + pluginName + "] " + ChatColor.RESET + ColorHelper.translateColorCodes(message, ColorConversion.NORMAL);
|
||||
private String getFormattedMessage(String message) {
|
||||
message = ColorHelper.translateColorCodes(message, ColorConversion.NORMAL);
|
||||
if (this.pluginName == null) {
|
||||
return message;
|
||||
} else {
|
||||
return "[" + this.pluginName + "] " + ChatColor.RESET + message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a placeholder in a string
|
||||
*
|
||||
* @param translator <p>The translator to use for the translation</p>
|
||||
* @param translatableMessage <p>The translatable message to replace in</p>
|
||||
* @param placeholder <p>The placeholder to replace</p>
|
||||
* @param replacement <p>The replacement value</p>
|
||||
* @return <p>The input string with the placeholder replaced</p>
|
||||
*/
|
||||
public static String replacePlaceholder(Translator translator, TranslatableMessage translatableMessage, String placeholder,
|
||||
String replacement) {
|
||||
return replacePlaceholder(translator.getTranslatedMessage(translatableMessage), placeholder, replacement);
|
||||
public String replacePlaceholder(TranslatableMessage translatableMessage, String placeholder, String replacement) {
|
||||
return replacePlaceholder(this.translator.getTranslatedMessage(translatableMessage), placeholder, replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces placeholders in a string
|
||||
*
|
||||
* @param translator <p>The translator to use for the translation</p>
|
||||
* @param translatableMessage <p>The translatable message to replace in</p>
|
||||
* @param placeholders <p>The placeholders to replace</p>
|
||||
* @param replacements <p>The replacement values</p>
|
||||
* @return <p>The input string with placeholders replaced</p>
|
||||
*/
|
||||
public static String replacePlaceholders(Translator translator, TranslatableMessage translatableMessage, String[] placeholders,
|
||||
String[] replacements) {
|
||||
return replacePlaceholders(translator.getTranslatedMessage(translatableMessage), placeholders, replacements);
|
||||
public String replacePlaceholders(TranslatableMessage translatableMessage, String[] placeholders,
|
||||
String[] replacements) {
|
||||
return replacePlaceholders(this.translator.getTranslatedMessage(translatableMessage), placeholders, replacements);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,11 +1,10 @@
|
||||
package net.knarcraft.knarlib.formatting;
|
||||
|
||||
import net.knarcraft.knarlib.KnarLib;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.ColorHelper;
|
||||
import net.knarcraft.knarlib.util.FileHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@ -47,11 +46,12 @@ public final class Translator {
|
||||
/**
|
||||
* Loads the languages used by this translator
|
||||
*
|
||||
* @param dataFolder <p>The data folder to load custom strings from</p>
|
||||
* @param selectedLanguage <p>The currently selected language</p>
|
||||
*/
|
||||
public void loadLanguages(String selectedLanguage) {
|
||||
public void loadLanguages(String dataFolder, String selectedLanguage) {
|
||||
backupTranslatedMessages = loadTranslatedMessages("en");
|
||||
translatedMessages = loadCustomTranslatedMessages(selectedLanguage);
|
||||
translatedMessages = loadCustomTranslatedMessages(dataFolder, selectedLanguage);
|
||||
if (translatedMessages == null) {
|
||||
translatedMessages = loadTranslatedMessages(selectedLanguage);
|
||||
}
|
||||
@ -89,7 +89,7 @@ public final class Translator {
|
||||
BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml");
|
||||
return loadTranslatableMessages(language, reader);
|
||||
} catch (FileNotFoundException e) {
|
||||
KnarLib.getPlugin().getLogger().log(Level.SEVERE, "Unable to load translated messages");
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Unable to load translated messages");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -97,23 +97,25 @@ public final class Translator {
|
||||
/**
|
||||
* Tries to load translated messages from a custom strings.yml file
|
||||
*
|
||||
* @param language <p>The selected language</p>
|
||||
* @param dataFolder <p>The data folder to load custom strings from</p>
|
||||
* @param language <p>The selected language</p>
|
||||
* @return <p>The loaded translated strings, or null if no custom language file exists</p>
|
||||
*/
|
||||
public Map<TranslatableMessage, String> loadCustomTranslatedMessages(String language) {
|
||||
JavaPlugin instance = KnarLib.getPlugin();
|
||||
|
||||
File strings = new File(instance.getDataFolder(), "strings.yml");
|
||||
public Map<TranslatableMessage, String> loadCustomTranslatedMessages(String dataFolder, String language) {
|
||||
if (dataFolder == null) {
|
||||
return null;
|
||||
}
|
||||
File strings = new File(dataFolder, "strings.yml");
|
||||
if (!strings.exists()) {
|
||||
instance.getLogger().log(Level.FINEST, "Strings file not found");
|
||||
Bukkit.getLogger().log(Level.FINEST, "Strings file not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
instance.getLogger().log(Level.INFO, "Loading custom strings...");
|
||||
Bukkit.getLogger().log(Level.INFO, "Loading custom strings...");
|
||||
return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings))));
|
||||
} catch (FileNotFoundException e) {
|
||||
instance.getLogger().log(Level.WARNING, "Unable to load custom messages");
|
||||
Bukkit.getLogger().log(Level.WARNING, "Unable to load custom messages");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user