diff --git a/pom.xml b/pom.xml
index 6e6b4ea..73e62dc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
net.landofrails
MessageAPI
- 1.0.0
+ 1.2.2
jar
MessageAPI
diff --git a/src/main/java/net/landofrails/MessageAPI/api/ELanguage.java b/src/main/java/net/landofrails/MessageAPI/api/ELanguage.java
index 288aa16..73ec11d 100644
--- a/src/main/java/net/landofrails/MessageAPI/api/ELanguage.java
+++ b/src/main/java/net/landofrails/MessageAPI/api/ELanguage.java
@@ -2,6 +2,13 @@
import java.util.Optional;
+/**
+ *
+ * Contains almost every minecraft language.
+ *
+ * @author Daniel
+ *
+ */
public enum ELanguage {
// @formatter:off
@@ -133,8 +140,9 @@ public enum ELanguage {
CHINESE_SIMPLIFIED("简体中文 (中国) (Jiǎntǐ Zhōngwén (Zhōngguó))", "zh_cn"),
CHINESE_TRADITIONAL_HONGKONG("繁體中文 (香港) (Fántǐ Zhōngwén (Xiānggǎng))", "zh_hk"),
CHINESE_TRADITIONAL_TAIWAN("繁體中文 (台灣) (Fántǐ Zhōngwén (Táiwān))", "zh_tw");
+
// @formatter:on
-
+
private String name;
private String locale;
diff --git a/src/main/java/net/landofrails/MessageAPI/api/MessageAPI.java b/src/main/java/net/landofrails/MessageAPI/api/MessageAPI.java
index fcc8959..e1ff524 100644
--- a/src/main/java/net/landofrails/MessageAPI/api/MessageAPI.java
+++ b/src/main/java/net/landofrails/MessageAPI/api/MessageAPI.java
@@ -3,11 +3,14 @@
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
+import java.util.AbstractMap.SimpleEntry;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.bukkit.Bukkit;
+import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -20,13 +23,10 @@ public final class MessageAPI & IMessageAPI> {
public static final ELanguage DEFAULT_LANGUAGE = ELanguage.AMERICAN_ENGLISH;
- private Map configuration = new EnumMap<>(ELanguage.class);
- private Map configurationFiles = new EnumMap<>(ELanguage.class);
+ private Map> configs = new EnumMap<>(ELanguage.class);
private MessageAPI(JavaPlugin plugin, Class enumClass) {
-
checkConfigs(plugin, enumClass);
-
}
/**
@@ -46,13 +46,13 @@ public static & IMessageAPI> MessageAPI register(JavaPlugi
/**
*
* Returns the players language, if that fails it will return the value from
- * {@link MessageAPI#defaultLanguage()}.
+ * {@link MessageAPI#serverLanguage()}.
*
* @param player
* @return ELanguage
*/
public static ELanguage getLanguage(Player player) {
- return ELanguage.getFromLocale(player.getLocale().toLowerCase()).orElse(defaultLanguage());
+ return ELanguage.getFromLocale(player.getLocale().toLowerCase()).orElse(serverLanguage());
}
/**
@@ -62,7 +62,7 @@ public static ELanguage getLanguage(Player player) {
*
* @return ELanguage
*/
- private static ELanguage defaultLanguage() {
+ public static ELanguage serverLanguage() {
try {
return MessageAPIPlugin.getInstance().getServerLanguage();
} catch (Exception e) {
@@ -70,23 +70,66 @@ private static ELanguage defaultLanguage() {
}
}
+ /**
+ *
+ * Sends a message to the console. Calls {@link MessageAPI#serverLanguage()}
+ * for the wanted language.
+ *
+ * @param message
+ * @param arguments
+ */
public void sendToConsole(IMessageAPI message, Object... arguments) {
- String text = getTranslatedText(message, defaultLanguage(), true, arguments);
+ String text = getTranslatedText(message, serverLanguage(), true, arguments);
Bukkit.getConsoleSender().sendMessage(text);
}
+ /**
+ *
+ * Sends a message to a player. Calls {@link MessageAPI#getLanguage(Player)}
+ * for the players language.
+ *
+ * @param player
+ * @param message
+ * @param arguments
+ */
public void sendTo(Player player, IMessageAPI message, Object... arguments) {
String text = getTranslatedText(message, getLanguage(player), true, arguments);
player.sendMessage(text);
}
+ public void sendTo(CommandSender sender, IMessageAPI message, Object... arguments) {
+ ELanguage lang = sender instanceof Player ? getLanguage((Player) sender) : serverLanguage();
+ String text = getTranslatedText(message, lang, true, arguments);
+ sender.sendMessage(text);
+ }
+
+ /**
+ *
+ * Calls {@link MessageAPI#sendTo(Player, IMessageAPI, Object...)} for every
+ * player.
+ *
+ * @param players
+ * @param message
+ * @param arguments
+ */
public void sendTo(List players, IMessageAPI message, Object... arguments) {
players.forEach(p -> sendTo(p, message, true, arguments));
}
+ /**
+ *
+ * Translates the text in the given language. Arguments are inserted via
+ * {@link MessageFormat#format(String, Object...)}.
+ *
+ * @param message
+ * @param language
+ * @param returnPathIfMissing
+ * @param arguments
+ * @return
+ */
public String getTranslatedText(IMessageAPI message, ELanguage language, boolean returnPathIfMissing, Object... arguments) {
- if (configuration.containsKey(language)) {
- FileConfiguration config = configuration.get(language);
+ if (configs.containsKey(language)) {
+ FileConfiguration config = configs.get(language).getKey();
if (config.contains(message.getPath())) {
return MessageFormat.format(config.getString(message.getPath()), arguments);
}
@@ -94,12 +137,19 @@ public String getTranslatedText(IMessageAPI message, ELanguage language, boolean
return returnPathIfMissing ? message.getPath() : null;
}
+ /**
+ *
+ * Creates lang files for the enum constants. Loads all lang files.
+ *
+ * @param plugin
+ * @param enumClass
+ */
private void checkConfigs(JavaPlugin plugin, Class enumClass) {
// For existing constant languages
for (E enumConstant : enumClass.getEnumConstants()) {
for (TranslatedText text : enumConstant.getText()) {
- if (!configuration.containsKey(text.getLanguage())) {
+ if (!configs.containsKey(text.getLanguage())) {
// Create config
FileConfiguration config = new YamlConfiguration();
try {
@@ -107,15 +157,15 @@ private void checkConfigs(JavaPlugin plugin, Class enumClass) {
folder.mkdirs();
File configFile = new File(folder, text.getLanguage().getLocale() + ".yml");
configFile.createNewFile();
- configurationFiles.put(text.getLanguage(), configFile);
config.load(configFile);
+
+ configs.put(text.getLanguage(), new SimpleEntry(config, configFile));
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
- configuration.put(text.getLanguage(), config);
}
- FileConfiguration config = configuration.get(text.getLanguage());
+ FileConfiguration config = configs.get(text.getLanguage()).getKey();
if (!config.contains(enumConstant.getPath())) {
config.set(enumConstant.getPath(), text.getText());
}
@@ -124,9 +174,9 @@ private void checkConfigs(JavaPlugin plugin, Class enumClass) {
}
// Save changes made
- for (ELanguage lang : configuration.keySet()) {
+ for (ELanguage lang : configs.keySet()) {
try {
- configuration.get(lang).save(configurationFiles.get(lang));
+ configs.get(lang).getKey().save(configs.get(lang).getValue());
} catch (IOException e) {
e.printStackTrace();
}
@@ -134,7 +184,7 @@ private void checkConfigs(JavaPlugin plugin, Class enumClass) {
// check if there are other lang files
for (ELanguage lang : ELanguage.values()) {
- if (!configuration.containsKey(lang)) {
+ if (!configs.containsKey(lang)) {
// Create config
FileConfiguration config = new YamlConfiguration();
try {
@@ -142,9 +192,8 @@ private void checkConfigs(JavaPlugin plugin, Class enumClass) {
folder.mkdirs();
File configFile = new File(folder, lang.getLocale() + ".yml");
if (configFile.exists()) {
- configurationFiles.put(lang, configFile);
config.load(configFile);
- configuration.put(lang, config);
+ configs.put(lang, new SimpleEntry(config, configFile));
}
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
diff --git a/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIMessages.java b/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIMessages.java
index bca79bb..3681741 100644
--- a/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIMessages.java
+++ b/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIMessages.java
@@ -1,7 +1,13 @@
package net.landofrails.MessageAPI.plugin;
import static net.landofrails.MessageAPI.api.ELanguage.AMERICAN_ENGLISH;
+import static net.landofrails.MessageAPI.api.ELanguage.AUSTRALIAN_ENGLISH;
+import static net.landofrails.MessageAPI.api.ELanguage.AUSTRIAN_GERMAN;
+import static net.landofrails.MessageAPI.api.ELanguage.BRITISH_ENGLISH;
+import static net.landofrails.MessageAPI.api.ELanguage.CANADIAN_ENGLISH;
import static net.landofrails.MessageAPI.api.ELanguage.GERMAN;
+import static net.landofrails.MessageAPI.api.ELanguage.NEW_ZEALAND_ENGLISH;
+import static net.landofrails.MessageAPI.api.ELanguage.SWISS_GERMAN;
import net.landofrails.MessageAPI.api.IMessageAPI;
import net.landofrails.MessageAPI.api.TranslatedText;
@@ -12,12 +18,89 @@ public enum MessageAPIMessages implements IMessageAPI {
LOADING_START(
"loading.start",
new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Loading phase started.."),
- new TranslatedText(GERMAN, "[MessageAPI] Lade-Phase begonnen..")
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Loading phase started.."),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Loading phase started.."),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Loading phase started.."),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Loading phase started.."),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Lade-Phase begonnen.."),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Lade-Phase begonnen.."),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Lade-Phase begonnen..")
),
LOADING_END(
"loading.end",
new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Loading phase finished."),
- new TranslatedText(GERMAN, "[MessageAPI] Lade-Phase beendet.")
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Loading phase finished."),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Loading phase finished."),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Loading phase finished."),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Loading phase finished."),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Lade-Phase beendet."),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Lade-Phase beendet."),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Lade-Phase beendet.")
+ ),
+
+ REGISTER_COMMAND_START(
+ "register_command.start",
+ new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Starting command registration.."),
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Starting command registration.."),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Starting command registration.."),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Starting command registration.."),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Starting command registration.."),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Registriere Befehl.."),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Registriere Befehl.."),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Registriere Befehl..")
+ ),
+ REGISTER_COMMAND_END(
+ "register_command.end",
+ new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Command registrated."),
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Command registrated."),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Command registrated."),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Command registrated."),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Command registrated."),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Befehl registriert."),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Befehl registriert."),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Befehl registriert.")
+ ),
+
+ DISABLING_PLUGIN(
+ "disabling_plugin",
+ new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Disabling plugin."),
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Disabling plugin."),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Disabling plugin."),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Disabling plugin."),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Disabling plugin."),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Plugin wird gestoppt."),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Plugin wird gestoppt."),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Plugin wird gestoppt.")
+ ),
+
+ LANGUAGE_SERVER(
+ "language.server",
+ new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] Server language: {0}, {1}"),
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] Server language: {0}, {1}"),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] Server language: {0}, {1}"),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] Server language: {0}, {1}"),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] Server language: {0}, {1}"),
+
+ new TranslatedText(GERMAN, "[MessageAPI] Server Sprache: {0}, {1}"),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] Server Sprache: {0}, {1}"),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] Server Sprache: {0}, {1}")
+ ),
+ LANGUAGE_PLAYER(
+ "language.player",
+ new TranslatedText(AMERICAN_ENGLISH, "[MessageAPI] {0}s language: {1}, {2}"),
+ new TranslatedText(AUSTRALIAN_ENGLISH, "[MessageAPI] {0}s language: {1}, {2}"),
+ new TranslatedText(CANADIAN_ENGLISH, "[MessageAPI] {0}s language: {1}, {2}"),
+ new TranslatedText(BRITISH_ENGLISH, "[MessageAPI] {0}s language: {1}, {2}"),
+ new TranslatedText(NEW_ZEALAND_ENGLISH, "[MessageAPI] {0}s language: {1}, {2}"),
+
+ new TranslatedText(GERMAN, "[MessageAPI] {0}s Sprache: {1}, {2}"),
+ new TranslatedText(SWISS_GERMAN, "[MessageAPI] {0}s Sprache: {1}, {2}"),
+ new TranslatedText(AUSTRIAN_GERMAN, "[MessageAPI] {0}s Sprache: {1}, {2}")
);
// @formatter:on
diff --git a/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIPlugin.java b/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIPlugin.java
index 158b275..16b6a59 100644
--- a/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIPlugin.java
+++ b/src/main/java/net/landofrails/MessageAPI/plugin/MessageAPIPlugin.java
@@ -11,6 +11,7 @@
import net.landofrails.MessageAPI.api.ELanguage;
import net.landofrails.MessageAPI.api.MessageAPI;
+import net.landofrails.MessageAPI.plugin.utils.MessageAPIPluginUtils;
public class MessageAPIPlugin extends JavaPlugin {
@@ -39,6 +40,7 @@ public void onLoad() {
@Override
public void onEnable() {
+ messageAPI.sendToConsole(MessageAPIMessages.REGISTER_COMMAND_START);
try {
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
@@ -48,11 +50,13 @@ public void onEnable() {
commandMap.register("lang", new BukkitCommand("lang") {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
- if (sender instanceof Player) {
- sender.sendMessage("PlayerLang: " + MessageAPI.getLanguage((Player) sender));
- sender.sendMessage("ServerLang: " + getServerLanguage().getName());
- } else {
- System.out.println("Du bist kein Spieler!");
+ ELanguage lang = MessageAPI.serverLanguage();
+ messageAPI.sendTo(sender, MessageAPIMessages.LANGUAGE_SERVER, lang.getName(), lang.getLocale());
+
+ for (Player p : Bukkit.getOnlinePlayers()) {
+ ELanguage playerLang = MessageAPI.getLanguage(p);
+ messageAPI.sendTo(sender, MessageAPIMessages.LANGUAGE_PLAYER, p.getName(), playerLang.getName(),
+ playerLang.getLocale());
}
return true;
}
@@ -62,11 +66,14 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
+ messageAPI.sendToConsole(MessageAPIMessages.REGISTER_COMMAND_END);
}
@Override
public void onDisable() {
-
+ MessageAPIPluginUtils.unRegisterBukkitCommand(this, Bukkit.getPluginCommand("lang"));
+ messageAPI.sendToConsole(MessageAPIMessages.DISABLING_PLUGIN);
+ messageAPI = null;
}
private void checkConfig() {
diff --git a/src/main/java/net/landofrails/MessageAPI/plugin/utils/MessageAPIPluginUtils.java b/src/main/java/net/landofrails/MessageAPI/plugin/utils/MessageAPIPluginUtils.java
new file mode 100644
index 0000000..d6a3bc1
--- /dev/null
+++ b/src/main/java/net/landofrails/MessageAPI/plugin/utils/MessageAPIPluginUtils.java
@@ -0,0 +1,41 @@
+package net.landofrails.MessageAPI.plugin.utils;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+
+import org.bukkit.command.Command;
+import org.bukkit.command.PluginCommand;
+import org.bukkit.command.SimpleCommandMap;
+import org.bukkit.plugin.java.JavaPlugin;
+
+public class MessageAPIPluginUtils {
+
+ private static Object getPrivateField(Object object, String field)
+ throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
+ Class> clazz = object.getClass();
+ Field objectField = clazz.getDeclaredField(field);
+ objectField.setAccessible(true);
+ Object result = objectField.get(object);
+ objectField.setAccessible(false);
+ return result;
+ }
+
+ public static void unRegisterBukkitCommand(JavaPlugin plugin, PluginCommand cmd) {
+ try {
+ Object result = getPrivateField(plugin.getServer().getPluginManager(), "commandMap");
+ SimpleCommandMap commandMap = (SimpleCommandMap) result;
+ Object map = getPrivateField(commandMap, "knownCommands");
+ @SuppressWarnings("unchecked")
+ HashMap knownCommands = (HashMap) map;
+ knownCommands.remove(cmd.getName());
+ for (String alias : cmd.getAliases()) {
+ if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(plugin.getName())) {
+ knownCommands.remove(alias);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}