Fix charset issues with locale file reading

This commit is contained in:
Esophose 2019-12-13 07:02:05 -07:00
parent 2d7c78a06c
commit a6c6b9d8ac
7 changed files with 18 additions and 15 deletions

View file

@ -25,7 +25,7 @@ dependencies {
compile 'org.slf4j:slf4j-nop:1.7.25' compile 'org.slf4j:slf4j-nop:1.7.25'
compile 'com.zaxxer:HikariCP:3.2.0' compile 'com.zaxxer:HikariCP:3.2.0'
compile 'com.googlecode.json-simple:json-simple:1.1.1' compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'me.clip:placeholderapi:2.10.4' shadow 'me.clip:placeholderapi:2.10.4'
shadow 'org.xerial:sqlite-jdbc:3.23.1' shadow 'org.xerial:sqlite-jdbc:3.23.1'
shadow 'org.spigotmc:spigot-api:1.15-R0.1-SNAPSHOT' shadow 'org.spigotmc:spigot-api:1.15-R0.1-SNAPSHOT'
} }

View file

@ -1,15 +1,16 @@
package dev.esophose.playerparticles.config; package dev.esophose.playerparticles.config;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.nio.charset.Charset; import java.io.StringReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -66,7 +67,7 @@ public class CommentedFileConfigurationHelper {
String pluginName = this.getPluginName(); String pluginName = this.getPluginName();
StringBuilder whole = new StringBuilder(); StringBuilder whole = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = Files.newBufferedReader(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8);
String currentLine; String currentLine;
while ((currentLine = reader.readLine()) != null) { while ((currentLine = reader.readLine()) != null) {
@ -81,7 +82,7 @@ public class CommentedFileConfigurationHelper {
} }
String config = whole.toString(); String config = whole.toString();
Reader configStream = new InputStreamReader(new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))); Reader configStream = new StringReader(config);
reader.close(); reader.close();
return configStream; return configStream;
@ -105,7 +106,7 @@ public class CommentedFileConfigurationHelper {
int comments = 0; int comments = 0;
String currentLine; String currentLine;
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = Files.newBufferedReader(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8);
while ((currentLine = reader.readLine()) != null) while ((currentLine = reader.readLine()) != null)
if (currentLine.trim().startsWith("#")) if (currentLine.trim().startsWith("#"))
@ -210,7 +211,7 @@ public class CommentedFileConfigurationHelper {
} }
} }
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()))) { try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) {
writer.write(stringBuilder.toString()); writer.write(stringBuilder.toString());
writer.flush(); writer.flush();
} catch (IOException e) { } catch (IOException e) {

View file

@ -43,7 +43,7 @@ public class GuiInventoryLoadPresetGroups extends GuiInventory {
particles.sort(Comparator.comparingInt(ParticlePair::getId)); particles.sort(Comparator.comparingInt(ParticlePair::getId));
String[] lore = new String[particles.size() + 1]; String[] lore = new String[particles.size() + 1];
lore[0] = localeManager.getLocaleMessage("gui-color-subtext") + localeManager.getLocaleMessage("gui-click-to-load", StringPlaceholders.single("id", particles.size())); lore[0] = localeManager.getLocaleMessage("gui-color-subtext") + localeManager.getLocaleMessage("gui-click-to-load", StringPlaceholders.single("amount", particles.size()));
int i = 1; int i = 1;
for (ParticlePair particle : particles) { for (ParticlePair particle : particles) {
StringPlaceholders stringPlaceholders = StringPlaceholders.builder("id", particle.getId()) StringPlaceholders stringPlaceholders = StringPlaceholders.builder("id", particle.getId())

View file

@ -304,7 +304,7 @@ public class FrenchLocale implements Locale {
this.put("gui-select-data-description", "Mets les paramètres de la particule à &b%data%"); this.put("gui-select-data-description", "Mets les paramètres de la particule à &b%data%");
this.put("gui-select-data-note", "note #%note%"); this.put("gui-select-data-note", "note #%note%");
this.put("#38", "GUI Color Name Messages");
this.put("gui-edit-data-color-red", "&crouge"); this.put("gui-edit-data-color-red", "&crouge");
this.put("gui-edit-data-color-orange", "&6orange"); this.put("gui-edit-data-color-orange", "&6orange");
this.put("gui-edit-data-color-yellow", "&ejaune"); this.put("gui-edit-data-color-yellow", "&ejaune");

View file

@ -303,7 +303,7 @@ public class GermanLocale implements Locale {
this.put("gui-select-data-description", "Setzt die Partikeldaten auf &b%data%"); this.put("gui-select-data-description", "Setzt die Partikeldaten auf &b%data%");
this.put("gui-select-data-note", "Hinweis #%note%"); this.put("gui-select-data-note", "Hinweis #%note%");
this.put("#38", "GUI Color Name Messages");
this.put("gui-edit-data-color-red", "Rot"); this.put("gui-edit-data-color-red", "Rot");
this.put("gui-edit-data-color-orange", "Orange"); this.put("gui-edit-data-color-orange", "Orange");
this.put("gui-edit-data-color-yellow", "Gelb"); this.put("gui-edit-data-color-yellow", "Gelb");

View file

@ -304,7 +304,7 @@ public class VietnameseLocale implements Locale {
this.put("gui-select-data-description", "Thiết lập DỮ liệu của Hạt thành &b%data%"); this.put("gui-select-data-description", "Thiết lập DỮ liệu của Hạt thành &b%data%");
this.put("gui-select-data-note", "note #%note%"); this.put("gui-select-data-note", "note #%note%");
this.put("#38", "GUI Color Name Messages");
this.put("gui-edit-data-color-red", "&cĐỏ mạnh mẽ"); this.put("gui-edit-data-color-red", "&cĐỏ mạnh mẽ");
this.put("gui-edit-data-color-orange", "&6Cam dịu dàng"); this.put("gui-edit-data-color-orange", "&6Cam dịu dàng");
this.put("gui-edit-data-color-yellow", "&eVàng yêu thương"); this.put("gui-edit-data-color-yellow", "&eVàng yêu thương");

View file

@ -61,6 +61,9 @@ public class LocaleManager extends Manager {
} else { } else {
Map<String, String> defaultLocaleStrings = locale.getDefaultLocaleStrings(); Map<String, String> defaultLocaleStrings = locale.getDefaultLocaleStrings();
for (String key : defaultLocaleStrings.keySet()) { for (String key : defaultLocaleStrings.keySet()) {
if (key.startsWith("#"))
continue;
String value = defaultLocaleStrings.get(key); String value = defaultLocaleStrings.get(key);
if (!configuration.contains(key)) if (!configuration.contains(key))
configuration.set(key, value); configuration.set(key, value);
@ -103,7 +106,6 @@ public class LocaleManager extends Manager {
public String getLocaleMessage(String messageKey, StringPlaceholders stringPlaceholders) { public String getLocaleMessage(String messageKey, StringPlaceholders stringPlaceholders) {
String message = this.locale.getString(messageKey); String message = this.locale.getString(messageKey);
System.out.println("Message: " + messageKey + " = " + message);
if (message == null) if (message == null)
return "null"; return "null";
return ChatColor.translateAlternateColorCodes('&', stringPlaceholders.apply(message)); return ChatColor.translateAlternateColorCodes('&', stringPlaceholders.apply(message));
@ -138,7 +140,7 @@ public class LocaleManager extends Manager {
* @param messageKey The message key of the Locale to send * @param messageKey The message key of the Locale to send
*/ */
public void sendMessage(CommandSender sender, String messageKey) { public void sendMessage(CommandSender sender, String messageKey) {
this.sendMessage(sender, messageKey, new StringPlaceholders()); this.sendMessage(sender, messageKey, StringPlaceholders.empty());
} }
/** /**
@ -148,7 +150,7 @@ public class LocaleManager extends Manager {
* @param messageKey The message key of the Locale to send * @param messageKey The message key of the Locale to send
*/ */
public void sendMessage(PPlayer pplayer, String messageKey) { public void sendMessage(PPlayer pplayer, String messageKey) {
this.sendMessage(pplayer, messageKey); this.sendMessage(pplayer, messageKey, StringPlaceholders.empty());
} }
/** /**