mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Fix reading and writing UTF-8 characters from storage files - closes #129
This commit is contained in:
parent
f1a1612f5d
commit
7d24e748b7
@ -43,9 +43,12 @@ import me.lucko.luckperms.common.utils.ThrowingFunction;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@ -76,8 +79,9 @@ public class JSONBacking extends FlatfileBacking {
|
||||
private boolean fileToWriter(File file, ThrowingFunction<JsonWriter, Boolean> writeOperation) {
|
||||
boolean success = false;
|
||||
try {
|
||||
try (FileWriter fileWriter = new FileWriter(file)) {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
|
||||
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
|
||||
try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) {
|
||||
jsonWriter.setIndent(" ");
|
||||
success = writeOperation.apply(jsonWriter);
|
||||
@ -85,6 +89,7 @@ public class JSONBacking extends FlatfileBacking {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -94,13 +99,15 @@ public class JSONBacking extends FlatfileBacking {
|
||||
private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) {
|
||||
boolean success = false;
|
||||
try {
|
||||
try (FileReader fileReader = new FileReader(file)) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
|
||||
try (FileInputStream fileInput = new FileInputStream(file)) {
|
||||
try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
|
||||
try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
|
||||
success = readOperation.apply(jsonReader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -43,9 +43,12 @@ import org.yaml.snakeyaml.Yaml;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -62,6 +65,7 @@ import static me.lucko.luckperms.common.core.model.PermissionHolder.exportToLega
|
||||
public class YAMLBacking extends FlatfileBacking {
|
||||
private static Yaml getYaml() {
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setAllowUnicode(true);
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
return new Yaml(options);
|
||||
}
|
||||
@ -79,35 +83,39 @@ public class YAMLBacking extends FlatfileBacking {
|
||||
super(plugin, "YAML", pluginDir);
|
||||
}
|
||||
|
||||
private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) {
|
||||
boolean success = false;
|
||||
try {
|
||||
try (FileReader fileReader = new FileReader(file)) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
|
||||
success = readOperation.apply((Map<String, Object>) getYaml().load(bufferedReader));
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
private boolean writeMapToFile(File file, Map<String, Object> values) {
|
||||
try {
|
||||
try (FileWriter fileWriter = new FileWriter(file)) {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
|
||||
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
|
||||
getYaml().dump(values, bufferedWriter);
|
||||
bufferedWriter.flush();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) {
|
||||
boolean success = false;
|
||||
try {
|
||||
try (FileInputStream fileInput = new FileInputStream(file)) {
|
||||
try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
|
||||
success = readOperation.apply((Map<String, Object>) getYaml().load(bufferedReader));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loadUser(UUID uuid, String username) {
|
||||
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
|
||||
|
Loading…
Reference in New Issue
Block a user