Skip to content

Commit

Permalink
fix: Correct generation of properties file in legacy version
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Oct 12, 2023
1 parent 6a8ee6f commit 1c3c322
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/blank038/servermarket/ServerMarket.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void loadConfig(boolean start) {
this.getConsoleLogger().log(false, " ");
this.saveDefaultConfig();
this.reloadConfig();
// Initialize I18n
new I18n(this.getConfig().getString("language", "zh_CN"));
// Run legacy converter
LegacyBackup.check();
if (this.isEnabled()) {
// Initialize I18n
new I18n(this.getConfig().getString("language", "zh_CN"));
// Initialize IStorageHandler
AbstractStorageHandler.check();
storageHandler.reload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.blank038.servermarket.ServerMarket;
import com.blank038.servermarket.i18n.I18n;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;

/**
Expand All @@ -25,7 +26,8 @@ public static void check() {
// backups data
try {
String fileName = "ServerMarket-" + System.currentTimeMillis();
FileUtils.copyDirectory(ServerMarket.getInstance().getDataFolder(), new File(backupFolder, fileName));
Files.createDirectories(Paths.get("./backups", fileName));
LegacyBackup.copyFile(ServerMarket.getInstance().getDataFolder(), new File(backupFolder, fileName));
ServerMarket.getInstance().saveResource("history.yml", "history.yml");
ServerMarket.getInstance().getConsoleLogger().log(false,
I18n.getProperties().getProperty("backup-completed").replace("%s", fileName));
Expand All @@ -36,4 +38,28 @@ public static void check() {
}
}
}

private static boolean copyFile(File in, File out) throws IOException {
if (in.exists()) {
if (in.isDirectory()) {
out.mkdir();
for (File i : in.listFiles()) {
LegacyBackup.copyFile(new File(in, i.getName()), new File(out, i.getName()));
}
} else if (in.isFile()) {
InputStreamReader isr = new InputStreamReader(Files.newInputStream(in.toPath()), StandardCharsets.UTF_8);
OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(out.toPath()),StandardCharsets.UTF_8);
char[] bytes = new char[1024];
int len;
while ((len = isr.read(bytes)) > 0) {
osw.write(bytes, 0, len);
}
isr.close();
osw.flush();
osw.close();
}
return true;
}
return false;
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/blank038/servermarket/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,8 +70,8 @@ public void init(String language) {
this.header = "prefix";
// initialize properties
properties = new Properties();
try {
properties.load(ServerMarket.getInstance().getResource("properties/" + language + ".properties"));
try (InputStream resourceInputStream = ServerMarket.getInstance().getResource("properties/" + language + ".properties")) {
properties.load(new InputStreamReader(resourceInputStream, StandardCharsets.UTF_8));
} catch (IOException e) {
ServerMarket.getInstance().getLogger().log(Level.WARNING, e, () -> "Failed to load properties file.");
}
Expand Down

0 comments on commit 1c3c322

Please sign in to comment.