mirror of
https://github.com/Zrips/Jobs.git
synced 2024-12-30 21:07:48 +01:00
Fail safe for corrupted data in locale and translatable word files
This commit is contained in:
parent
89d6e78551
commit
e7b52d2ecf
@ -510,6 +510,7 @@ public class Jobs extends JavaPlugin {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void reload() throws IOException {
|
||||
|
||||
if (saveTask != null) {
|
||||
saveTask.shutdown();
|
||||
saveTask = null;
|
||||
|
@ -53,6 +53,7 @@ import com.gamingmesh.jobs.dao.JobsDAO;
|
||||
import com.gamingmesh.jobs.dao.JobsDAOData;
|
||||
import com.gamingmesh.jobs.economy.PointsData;
|
||||
import com.gamingmesh.jobs.stuff.ChatColor;
|
||||
import com.gamingmesh.jobs.stuff.Debug;
|
||||
import com.gamingmesh.jobs.stuff.PerformCommands;
|
||||
|
||||
public class PlayerManager {
|
||||
@ -786,6 +787,7 @@ public class PlayerManager {
|
||||
boost.add(BoostOf.Dynamic, new BoostMultiplier().add(prog.getBonus()));
|
||||
boost.add(BoostOf.Item, Jobs.getPlayerManager().getItemBoost(player.getPlayer(), prog));
|
||||
boost.add(BoostOf.Area, new BoostMultiplier().add(Jobs.getRestrictedAreaManager().getRestrictedMultiplier(player.getPlayer())));
|
||||
|
||||
return boost;
|
||||
}
|
||||
|
||||
|
@ -33,186 +33,186 @@ import com.google.common.io.Files;
|
||||
*/
|
||||
|
||||
public class CommentedYamlConfiguration extends YamlConfiguration {
|
||||
private HashMap<String, String> comments;
|
||||
private HashMap<String, String> comments;
|
||||
|
||||
public CommentedYamlConfiguration() {
|
||||
super();
|
||||
comments = new HashMap<String, String>();
|
||||
public CommentedYamlConfiguration() {
|
||||
super();
|
||||
comments = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(String file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("File cannot be null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(String file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("File cannot be null");
|
||||
}
|
||||
save(new File(file));
|
||||
}
|
||||
|
||||
save(new File(file));
|
||||
@Override
|
||||
public void save(File file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("File cannot be null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File file) throws IOException {
|
||||
if (file == null) {
|
||||
throw new IllegalArgumentException("File cannot be null");
|
||||
}
|
||||
Files.createParentDirs(file);
|
||||
|
||||
Files.createParentDirs(file);
|
||||
String data = insertComments(saveToString());
|
||||
|
||||
String data = insertComments(saveToString());
|
||||
//FileWriter writer = new FileWriter(file);
|
||||
|
||||
//FileWriter writer = new FileWriter(file);
|
||||
|
||||
PrintWriter writer = new PrintWriter(file, "UTF-8");
|
||||
|
||||
//Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
|
||||
PrintWriter writer = new PrintWriter(file, "UTF-8");
|
||||
|
||||
try {
|
||||
writer.write(data);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
//Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
|
||||
|
||||
try {
|
||||
writer.write(data);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String insertComments(String yaml) {
|
||||
// if there's comments to add, we need to add comments
|
||||
if (!comments.isEmpty()) {
|
||||
// String array of each line in the config file
|
||||
String[] yamlContents = yaml.split("[" + System.getProperty("line.separator") + "]");
|
||||
private String insertComments(String yaml) {
|
||||
// if there's comments to add, we need to add comments
|
||||
if (!comments.isEmpty()) {
|
||||
// String array of each line in the config file
|
||||
String[] yamlContents = yaml.split("[" + System.getProperty("line.separator") + "]");
|
||||
|
||||
// This will hold the entire newly formatted config
|
||||
StringBuilder newContents = new StringBuilder();
|
||||
// This holds the current path the lines are at in the config
|
||||
StringBuilder currentPath = new StringBuilder();
|
||||
// This tells if the specified path has already been commented
|
||||
boolean commentedPath = false;
|
||||
// This flags if the line is a node or unknown text.
|
||||
boolean node = false;
|
||||
// The depth of the path. (number of words separated by periods - 1)
|
||||
int depth = 0;
|
||||
// This will hold the entire newly formatted config
|
||||
StringBuilder newContents = new StringBuilder();
|
||||
// This holds the current path the lines are at in the config
|
||||
StringBuilder currentPath = new StringBuilder();
|
||||
// This tells if the specified path has already been commented
|
||||
boolean commentedPath = false;
|
||||
// This flags if the line is a node or unknown text.
|
||||
boolean node = false;
|
||||
// The depth of the path. (number of words separated by periods - 1)
|
||||
int depth = 0;
|
||||
|
||||
// This will cause the first line to be ignored.
|
||||
boolean firstLine = true;
|
||||
// Loop through the config lines
|
||||
for (final String line : yamlContents) {
|
||||
if (firstLine) {
|
||||
firstLine = false;
|
||||
if (line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// If the line is a node (and not something like a list value)
|
||||
if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) {
|
||||
// This is a new node so we need to mark it for commenting (if there are comments)
|
||||
commentedPath = false;
|
||||
// This is a node so flag it as one
|
||||
node = true;
|
||||
// This will cause the first line to be ignored.
|
||||
boolean firstLine = true;
|
||||
// Loop through the config lines
|
||||
for (final String line : yamlContents) {
|
||||
if (firstLine) {
|
||||
firstLine = false;
|
||||
if (line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// If the line is a node (and not something like a list value)
|
||||
if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) {
|
||||
// This is a new node so we need to mark it for commenting (if there are comments)
|
||||
commentedPath = false;
|
||||
// This is a node so flag it as one
|
||||
node = true;
|
||||
|
||||
// Grab the index of the end of the node name
|
||||
int index = 0;
|
||||
index = line.indexOf(": ");
|
||||
if (index < 0) {
|
||||
index = line.length() - 1;
|
||||
}
|
||||
// If currentPath is empty, store the node name as the currentPath. (this is only on the first iteration, i think)
|
||||
if (currentPath.toString().isEmpty()) {
|
||||
currentPath = new StringBuilder(line.substring(0, index));
|
||||
} else {
|
||||
// Calculate the whitespace preceding the node name
|
||||
int whiteSpace = 0;
|
||||
for (int n = 0; n < line.length(); n++) {
|
||||
if (line.charAt(n) == ' ') {
|
||||
whiteSpace++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth
|
||||
if (whiteSpace / 2 > depth) {
|
||||
// Path is deeper. Add a . and the node name
|
||||
currentPath.append(".").append(line.substring(whiteSpace, index));
|
||||
depth++;
|
||||
} else if (whiteSpace / 2 < depth) {
|
||||
// Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath
|
||||
int newDepth = whiteSpace / 2;
|
||||
for (int i = 0; i < depth - newDepth; i++) {
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "");
|
||||
}
|
||||
// Grab the index of the final period
|
||||
int lastIndex = currentPath.lastIndexOf(".");
|
||||
if (lastIndex < 0) {
|
||||
// if there isn't a final period, set the current path to nothing because we're at root
|
||||
currentPath = new StringBuilder();
|
||||
} else {
|
||||
// If there is a final period, replace everything after it with nothing
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append(".");
|
||||
}
|
||||
// Add the new node name to the path
|
||||
currentPath.append(line.substring(whiteSpace, index));
|
||||
// Reset the depth
|
||||
depth = newDepth;
|
||||
} else {
|
||||
// Path is same depth, replace the last path node name to the current node name
|
||||
int lastIndex = currentPath.lastIndexOf(".");
|
||||
if (lastIndex < 0) {
|
||||
// if there isn't a final period, set the current path to nothing because we're at root
|
||||
currentPath = new StringBuilder();
|
||||
} else {
|
||||
// If there is a final period, replace everything after it with nothing
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append(".");
|
||||
}
|
||||
//currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), "");
|
||||
currentPath.append(line.substring(whiteSpace, index));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
node = false;
|
||||
}
|
||||
StringBuilder newLine = new StringBuilder(line);
|
||||
if (node) {
|
||||
String comment = null;
|
||||
if (!commentedPath) {
|
||||
// If there's a comment for the current path, retrieve it and flag that path as already commented
|
||||
comment = comments.get(currentPath.toString());
|
||||
}
|
||||
if (comment != null && !comment.isEmpty()) {
|
||||
// Add the comment to the beginning of the current line
|
||||
newLine.insert(0, System.getProperty("line.separator")).insert(0, comment);
|
||||
comment = null;
|
||||
commentedPath = true;
|
||||
}
|
||||
}
|
||||
newLine.append(System.getProperty("line.separator"));
|
||||
// Add the (modified) line to the total config String
|
||||
newContents.append(newLine.toString());
|
||||
// Grab the index of the end of the node name
|
||||
int index = 0;
|
||||
index = line.indexOf(": ");
|
||||
if (index < 0) {
|
||||
index = line.length() - 1;
|
||||
}
|
||||
// If currentPath is empty, store the node name as the currentPath. (this is only on the first iteration, i think)
|
||||
if (currentPath.toString().isEmpty()) {
|
||||
currentPath = new StringBuilder(line.substring(0, index));
|
||||
} else {
|
||||
// Calculate the whitespace preceding the node name
|
||||
int whiteSpace = 0;
|
||||
for (int n = 0; n < line.length(); n++) {
|
||||
if (line.charAt(n) == ' ') {
|
||||
whiteSpace++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth
|
||||
if (whiteSpace / 2 > depth) {
|
||||
// Path is deeper. Add a . and the node name
|
||||
currentPath.append(".").append(line.substring(whiteSpace, index));
|
||||
depth++;
|
||||
} else if (whiteSpace / 2 < depth) {
|
||||
// Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath
|
||||
int newDepth = whiteSpace / 2;
|
||||
for (int i = 0; i < depth - newDepth; i++) {
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "");
|
||||
}
|
||||
// Grab the index of the final period
|
||||
int lastIndex = currentPath.lastIndexOf(".");
|
||||
if (lastIndex < 0) {
|
||||
// if there isn't a final period, set the current path to nothing because we're at root
|
||||
currentPath = new StringBuilder();
|
||||
} else {
|
||||
// If there is a final period, replace everything after it with nothing
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append(".");
|
||||
}
|
||||
// Add the new node name to the path
|
||||
currentPath.append(line.substring(whiteSpace, index));
|
||||
// Reset the depth
|
||||
depth = newDepth;
|
||||
} else {
|
||||
// Path is same depth, replace the last path node name to the current node name
|
||||
int lastIndex = currentPath.lastIndexOf(".");
|
||||
if (lastIndex < 0) {
|
||||
// if there isn't a final period, set the current path to nothing because we're at root
|
||||
currentPath = new StringBuilder();
|
||||
} else {
|
||||
// If there is a final period, replace everything after it with nothing
|
||||
currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append(".");
|
||||
}
|
||||
//currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), "");
|
||||
currentPath.append(line.substring(whiteSpace, index));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
node = false;
|
||||
}
|
||||
StringBuilder newLine = new StringBuilder(line);
|
||||
if (node) {
|
||||
String comment = null;
|
||||
if (!commentedPath) {
|
||||
// If there's a comment for the current path, retrieve it and flag that path as already commented
|
||||
comment = comments.get(currentPath.toString());
|
||||
}
|
||||
if (comment != null && !comment.isEmpty()) {
|
||||
// Add the comment to the beginning of the current line
|
||||
newLine.insert(0, System.getProperty("line.separator")).insert(0, comment);
|
||||
comment = null;
|
||||
commentedPath = true;
|
||||
}
|
||||
}
|
||||
newLine.append(System.getProperty("line.separator"));
|
||||
// Add the (modified) line to the total config String
|
||||
newContents.append(newLine.toString());
|
||||
}
|
||||
|
||||
return newContents.toString();
|
||||
}
|
||||
return yaml;
|
||||
return newContents.toString();
|
||||
}
|
||||
return yaml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a comment just before the specified path. The comment can be multiple lines. An empty string will indicate a blank line.
|
||||
*
|
||||
* @param path Configuration path to add comment.
|
||||
* @param commentLines Comments to add. One String per line.
|
||||
*/
|
||||
public void addComment(String path, String... commentLines) {
|
||||
StringBuilder commentstring = new StringBuilder();
|
||||
String leadingSpaces = "";
|
||||
for (int n = 0; n < path.length(); n++) {
|
||||
if (path.charAt(n) == '.') {
|
||||
leadingSpaces += " ";
|
||||
}
|
||||
}
|
||||
for (String line : commentLines) {
|
||||
if (!line.isEmpty()) {
|
||||
line = leadingSpaces + "# " + line;
|
||||
}
|
||||
if (commentstring.length() > 0) {
|
||||
commentstring.append(System.getProperty("line.separator"));
|
||||
}
|
||||
commentstring.append(line);
|
||||
}
|
||||
comments.put(path, commentstring.toString());
|
||||
/**
|
||||
* Adds a comment just before the specified path. The comment can be multiple lines. An empty string will indicate a blank line.
|
||||
*
|
||||
* @param path Configuration path to add comment.
|
||||
* @param commentLines Comments to add. One String per line.
|
||||
*/
|
||||
public void addComment(String path, String... commentLines) {
|
||||
StringBuilder commentstring = new StringBuilder();
|
||||
String leadingSpaces = "";
|
||||
for (int n = 0; n < path.length(); n++) {
|
||||
if (path.charAt(n) == '.') {
|
||||
leadingSpaces += " ";
|
||||
}
|
||||
}
|
||||
for (String line : commentLines) {
|
||||
if (!line.isEmpty()) {
|
||||
line = leadingSpaces + "# " + line;
|
||||
}
|
||||
if (commentstring.length() > 0) {
|
||||
commentstring.append(System.getProperty("line.separator"));
|
||||
}
|
||||
commentstring.append(line);
|
||||
}
|
||||
comments.put(path, commentstring.toString());
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import com.gamingmesh.jobs.Jobs;
|
||||
import com.gamingmesh.jobs.container.LocaleReader;
|
||||
@ -17,6 +18,12 @@ public class LanguageManager {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
List<String> languages = new ArrayList<String>();
|
||||
|
||||
public List<String> getLanguages() {
|
||||
return languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to load the language file configuration
|
||||
*
|
||||
@ -25,7 +32,7 @@ public class LanguageManager {
|
||||
synchronized void load() {
|
||||
|
||||
// Just copying default language files, except en, that one will be generated
|
||||
List<String> languages = new ArrayList<String>();
|
||||
languages = new ArrayList<String>();
|
||||
languages.add("cs");
|
||||
languages.add("cz");
|
||||
languages.add("de");
|
||||
@ -48,6 +55,13 @@ public class LanguageManager {
|
||||
|
||||
for (String lang : languages) {
|
||||
File f = new File(plugin.getDataFolder(), "locale" + File.separator + "messages_" + lang + ".yml");
|
||||
|
||||
// Fail safe if file get corrupted and being created with corrupted data, we need to recreate it
|
||||
if ((f.length() / 1024) > 1024) {
|
||||
f.delete();
|
||||
f = new File(plugin.getDataFolder(), "locale" + File.separator + "messages_" + lang + ".yml");
|
||||
}
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(f);
|
||||
CommentedYamlConfiguration writer = new CommentedYamlConfiguration();
|
||||
|
||||
@ -141,13 +155,11 @@ public class LanguageManager {
|
||||
c.get("command.limit.output.reachedpointslimit", "&4You have reached exp limit in given time!");
|
||||
c.get("command.limit.output.reachedpointslimit2", "&eYou can check your limit with &2/jobs limit &ecommand");
|
||||
c.get("command.limit.output.notenabled", "&eMoney limit is not enabled");
|
||||
|
||||
|
||||
c.get("command.resetlimit.help.info", "Resets players payment limits");
|
||||
c.get("command.resetlimit.help.args", "[playername]");
|
||||
c.get("command.resetlimit.output.reseted", "&ePayment limits have been reset for: &2%playername%");
|
||||
|
||||
|
||||
|
||||
c.get("command.help.output.info", "Type /jobs [cmd] ? for more information about a command.");
|
||||
c.get("command.help.output.usage", "Usage: %usage%");
|
||||
c.get("command.help.output.title", "&e-------&e ======= &6Jobs &e======= &e-------");
|
||||
@ -174,7 +186,6 @@ public class LanguageManager {
|
||||
c.get("command.blockinfo.output.id", " &eBlock id: &6%blockid%");
|
||||
c.get("command.blockinfo.output.data", " &eBlock data: &6%blockdata%");
|
||||
c.get("command.blockinfo.output.usage", " &eUsage: &6%first% &eor &6%second%");
|
||||
|
||||
|
||||
c.get("command.iteminfo.help.info", "Shows item information you holding.");
|
||||
c.get("command.iteminfo.help.args", "");
|
||||
@ -182,7 +193,7 @@ public class LanguageManager {
|
||||
c.get("command.iteminfo.output.id", " &eItem id: &6%itemid%");
|
||||
c.get("command.iteminfo.output.data", " &eItem data: &6%itemdata%");
|
||||
c.get("command.iteminfo.output.usage", " &eUsage: &6%first% &eor &6%second%");
|
||||
|
||||
|
||||
c.get("command.entitylist.help.info", "Shows all possible entities can be used with plugin.");
|
||||
c.get("command.entitylist.help.args", "");
|
||||
|
||||
@ -354,7 +365,6 @@ public class LanguageManager {
|
||||
c.get("command.gtop.output.prev", "&e<<<<< Prev page &2|");
|
||||
c.get("command.gtop.output.next", "&2|&e Next Page >>>>");
|
||||
c.get("command.gtop.output.show", "&2Show from &e[from] &2until &e[until] &2global top list");
|
||||
|
||||
|
||||
c.get("command.area.help.info", "Modify restricted areas.");
|
||||
c.get("command.area.help.args", "add/remove/info/list");
|
||||
|
@ -14,6 +14,7 @@ import com.gamingmesh.jobs.container.JobInfo;
|
||||
import com.gamingmesh.jobs.container.LocaleReader;
|
||||
import com.gamingmesh.jobs.container.NameList;
|
||||
import com.gamingmesh.jobs.stuff.ChatColor;
|
||||
import com.gamingmesh.jobs.stuff.Debug;
|
||||
|
||||
public class NameTranslatorManager {
|
||||
|
||||
@ -179,7 +180,7 @@ public class NameTranslatorManager {
|
||||
langFile.saveDefaultConfig();
|
||||
}
|
||||
|
||||
languages.add("en");
|
||||
languages.addAll(Jobs.getLanguageManager().getLanguages());
|
||||
|
||||
File customLocaleFile = new File(plugin.getDataFolder(), "TranslatableWords" + File.separator + "Words_" + Jobs.getGCManager().localeString + ".yml");
|
||||
if (!customLocaleFile.exists() && !Jobs.getGCManager().localeString.equalsIgnoreCase("en"))
|
||||
@ -188,6 +189,14 @@ public class NameTranslatorManager {
|
||||
for (String lang : languages) {
|
||||
|
||||
File f = new File(plugin.getDataFolder(), "TranslatableWords" + File.separator + "Words_" + lang + ".yml");
|
||||
|
||||
// Fail safe if file get corrupted and being created with corrupted data, we need to recreate it
|
||||
if ((f.length() / 1024) > 1024) {
|
||||
f.delete();
|
||||
f = new File(plugin.getDataFolder(), "TranslatableWords" + File.separator + "Words_" + lang + ".yml");
|
||||
}
|
||||
|
||||
Bukkit.getServer().getConsoleSender().sendMessage(lang + " " + (f.length() / 1024));
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(f);
|
||||
CommentedYamlConfiguration writer = new CommentedYamlConfiguration();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user