From ab0c236711b788391820f949bda9d963d9be202c Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Mon, 23 Dec 2013 14:31:27 -0600 Subject: [PATCH] Add a language file and system. --- README.md | 35 ++++++------ pom.xml | 4 +- .../java/com/graywolf336/jail/JailIO.java | 57 ++++++++++++++++++- .../java/com/graywolf336/jail/JailMain.java | 1 + .../graywolf336/jail/enums/LangString.java | 10 ++++ src/main/resources/en.yml | 8 +-- 6 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/enums/LangString.java diff --git a/README.md b/README.md index 5f1e36a..ea52733 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,19 @@ -[Jail 3.0](http://ci.graywolf336.com/job/Jail/) -==== -This plugins adds Jail to your Minecraft server. Admins can define several jails and then jail/unjail people or jail them on time basis. Plugin also offers wide variety of protections, so players won't escape out of your jail. - -[![Build Status](http://ci.graywolf336.com/job/Jail/badge/icon)](http://ci.graywolf336.com/job/Jail/) - -ToDo -=== -* About everything - -Done -=== -* Muted Prisoners are now muted -* New command system, internally we handle commands a lot better -* Delete commands are now remove - -[Jail 3.0 JavaDoc](http://ci.graywolf336.com/job/Jail/javadoc) +[Jail 3.0](http://ci.graywolf336.com/job/Jail/) +==== +This plugins adds Jail to your Minecraft server. Admins can define several jails and then jail/unjail people or jail them on time basis. Plugin also offers wide variety of protections, so players won't escape out of your jail. + +[![Build Status](http://ci.graywolf336.com/job/Jail/badge/icon)](http://ci.graywolf336.com/job/Jail/) + +ToDo +=== +* About everything + +Done +=== +* Muted Prisoners are now muted +* New command system, internally we handle commands a lot better +* Delete commands are now remove +* Language system + +[Jail 3.0 JavaDoc](http://ci.graywolf336.com/job/Jail/javadoc) ==== \ No newline at end of file diff --git a/pom.xml b/pom.xml index f963df1..9858714 100644 --- a/pom.xml +++ b/pom.xml @@ -43,14 +43,14 @@ org.bukkit bukkit - 1.7.2-R0.1-SNAPSHOT + 1.7.2-R0.3-SNAPSHOT jar org.bukkit craftbukkit - 1.7.2-R0.1-SNAPSHOT + 1.7.2-R0.3-SNAPSHOT jar diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index 286d6e8..dc94a40 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -12,6 +12,7 @@ import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.SimpleLocation; +import com.graywolf336.jail.enums.LangString; /** * Handles all the saving and loading of the plugin's data. @@ -23,7 +24,7 @@ import com.graywolf336.jail.beans.SimpleLocation; */ public class JailIO { private JailMain pl; - private FileConfiguration flat; + private FileConfiguration flat, lang; private int storage; //0 = flatfile, 1 = sqlite, 2 = mysql public JailIO(JailMain plugin) { @@ -39,6 +40,60 @@ public class JailIO { } } + /** Loads the language file from disk, if there is none then we save the default one. */ + public void loadLanguage() { + String language = pl.getConfig().getString("system.language"); + boolean save = false; + File langFile = new File(pl.getDataFolder(), language + ".yml"); + + //File or folder already exists, let's check + if(langFile.exists()) { + if(langFile.isFile()) { + lang = YamlConfiguration.loadConfiguration(langFile); + }else { + pl.getLogger().severe("The language file can not be a folder, please double check your setup. Because of that, we are reverting back to English as the language."); + lang = YamlConfiguration.loadConfiguration(pl.getResource("en.yml")); + save = true; + } + }else { + pl.getLogger().info("Loading the default language of: en"); + pl.getLogger().info("If you wish to change this, please rename 'en.yml' to whatever you wish and set the config value to the name of the file."); + lang = YamlConfiguration.loadConfiguration(pl.getResource("en.yml")); + save = true; + } + + //If we have flagged to save the language file, let's save it as en.yml as this flag usually means they didn't have it loaded. + if(save) { + try { + lang.save(new File(pl.getDataFolder(), "en.yml")); + } catch (IOException e) { + pl.getLogger().severe("Unable to save the language file: " + e.getMessage()); + } + } + } + + /** Returns the message in the language, no variables are replaced.*/ + public String getLanguageString(LangString langString) { + return getLanguageString(langString, new String[] {}); + } + + /** + * Returns the message in the language, with the provided variables being replaced. + * + * @param langString Which {@link LangString} we should be getting to send. + * @param variables All the variables to replace, in order from 0 to however many. + * @return The message as a colorful message. + */ + public String getLanguageString(LangString langString, String... variables) { + String message = lang.getString("language." + langString.toString().toLowerCase()); + + for (int i = 0; i < variables.length; i++) { + message = message.replaceAll("%" + i + "%", variables[i]); + } + + return Util.getColorfulMessage(message); + } + /** * Prepares the storage engine to be used. */ diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index e023e81..fbb7809 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -22,6 +22,7 @@ public class JailMain extends JavaPlugin { jm = new JailManager(this); io = new JailIO(this); + io.loadLanguage(); io.prepareStorage(); io.loadJails(); diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java new file mode 100644 index 0000000..8e2b409 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -0,0 +1,10 @@ +package com.graywolf336.jail.enums; + +public enum LangString { + /** The message sent when players are jailed without a reason. */ + JAILED, + /** The message sent when players are jailed with a reason. */ + JAILEDWITHREASON, + /** The message sent when players are released from jail. */ + UNJAILED; +} diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 8688650..880eb34 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -1,4 +1,4 @@ -Messages: - Jailed: "&cYou've been jailed!" - JailedWithReason: "&cYou've been jailed for: %s" - UnJailed: "&2You have been released! Please respect the server rules." \ No newline at end of file +language: + jailed: "&cYou've been jailed!" + jailedwithreason: "&cYou've been jailed for: %s" + unjailed: "&2You have been released! Please respect the server rules." \ No newline at end of file