feat: migrate custom locale
This commit is contained in:
parent
c78ba0c84a
commit
548d210a95
11 changed files with 196 additions and 73 deletions
|
@ -13,7 +13,10 @@ import xyz.ineanto.nicko.config.ConfigurationManager;
|
||||||
import xyz.ineanto.nicko.event.PlayerJoinListener;
|
import xyz.ineanto.nicko.event.PlayerJoinListener;
|
||||||
import xyz.ineanto.nicko.event.PlayerQuitListener;
|
import xyz.ineanto.nicko.event.PlayerQuitListener;
|
||||||
import xyz.ineanto.nicko.i18n.Locale;
|
import xyz.ineanto.nicko.i18n.Locale;
|
||||||
import xyz.ineanto.nicko.i18n.LocaleFileManager;
|
import xyz.ineanto.nicko.i18n.CustomLocale;
|
||||||
|
import xyz.ineanto.nicko.migration.ConfigurationMigrator;
|
||||||
|
import xyz.ineanto.nicko.migration.CustomLocaleMigrator;
|
||||||
|
import xyz.ineanto.nicko.migration.Migrator;
|
||||||
import xyz.ineanto.nicko.mojang.MojangAPI;
|
import xyz.ineanto.nicko.mojang.MojangAPI;
|
||||||
import xyz.ineanto.nicko.placeholder.NickoExpansion;
|
import xyz.ineanto.nicko.placeholder.NickoExpansion;
|
||||||
import xyz.ineanto.nicko.storage.PlayerDataStore;
|
import xyz.ineanto.nicko.storage.PlayerDataStore;
|
||||||
|
@ -24,9 +27,9 @@ import xyz.xenondevs.invui.gui.structure.Structure;
|
||||||
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
||||||
import xyz.xenondevs.invui.item.impl.SimpleItem;
|
import xyz.xenondevs.invui.item.impl.SimpleItem;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.util.List;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
|
|
||||||
public class NickoBukkit extends JavaPlugin {
|
public class NickoBukkit extends JavaPlugin {
|
||||||
private static NickoBukkit plugin;
|
private static NickoBukkit plugin;
|
||||||
|
@ -37,7 +40,7 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
private PlayerDataStore dataStore;
|
private PlayerDataStore dataStore;
|
||||||
private ConfigurationManager configurationManager;
|
private ConfigurationManager configurationManager;
|
||||||
private Configuration configuration;
|
private Configuration configuration;
|
||||||
private LocaleFileManager localeFileManager;
|
private CustomLocale customLocale;
|
||||||
private PlayerNameStore nameStore;
|
private PlayerNameStore nameStore;
|
||||||
private RandomNameFetcher nameFetcher;
|
private RandomNameFetcher nameFetcher;
|
||||||
private Metrics metrics;
|
private Metrics metrics;
|
||||||
|
@ -95,19 +98,11 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!unitTesting) {
|
if (!unitTesting) {
|
||||||
// Migrate configuration (1.0.8-RC1)
|
final List<Migrator> migrations = List.of(
|
||||||
if (configuration.getVersion() == null
|
new ConfigurationMigrator(this),
|
||||||
|| configuration.getVersion().isEmpty()
|
new CustomLocaleMigrator(this)
|
||||||
|| configuration.getVersionObject().compareTo(Configuration.VERSION) != 0) {
|
);
|
||||||
getLogger().info("Migrating configuration file from older version...");
|
migrations.forEach(Migrator::migrate);
|
||||||
try {
|
|
||||||
Files.copy(configurationManager.getFile().toPath(), configurationManager.getBackupFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
Files.delete(configurationManager.getFile().toPath());
|
|
||||||
configurationManager.saveDefaultConfig();
|
|
||||||
} catch (IOException e) {
|
|
||||||
getLogger().severe("Failed to migrate your configuration!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class.forName("io.papermc.paper.threadedregions.RegionizedServerInitEvent");
|
Class.forName("io.papermc.paper.threadedregions.RegionizedServerInitEvent");
|
||||||
|
@ -116,11 +111,15 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
} catch (ClassNotFoundException ignored) { }
|
} catch (ClassNotFoundException ignored) { }
|
||||||
|
|
||||||
if (configuration.isCustomLocale()) {
|
if (configuration.isCustomLocale()) {
|
||||||
localeFileManager = new LocaleFileManager();
|
try {
|
||||||
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
|
customLocale = new CustomLocale(this);
|
||||||
getLogger().info("Successfully loaded custom language file.");
|
if (customLocale.dumpIntoFile(Locale.ENGLISH)) {
|
||||||
} else {
|
getLogger().info("Successfully loaded custom language file.");
|
||||||
getLogger().severe("Failed to load custom language file!");
|
} else {
|
||||||
|
getLogger().severe("Failed to load custom language file!");
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,6 +188,10 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
return dataStore;
|
return dataStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConfigurationManager getConfigurationManager() {
|
||||||
|
return configurationManager;
|
||||||
|
}
|
||||||
|
|
||||||
public PlayerNameStore getNameStore() {
|
public PlayerNameStore getNameStore() {
|
||||||
return nameStore;
|
return nameStore;
|
||||||
}
|
}
|
||||||
|
@ -197,7 +200,7 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
return mojangAPI;
|
return mojangAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocaleFileManager getLocaleFileManager() {
|
public CustomLocale getCustomLocale() {
|
||||||
return localeFileManager;
|
return customLocale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
77
src/main/java/xyz/ineanto/nicko/i18n/CustomLocale.java
Normal file
77
src/main/java/xyz/ineanto/nicko/i18n/CustomLocale.java
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
package xyz.ineanto.nicko.i18n;
|
||||||
|
|
||||||
|
import com.github.jsixface.YamlConfig;
|
||||||
|
import xyz.ineanto.nicko.NickoBukkit;
|
||||||
|
import xyz.ineanto.nicko.version.Version;
|
||||||
|
import xyz.xenondevs.invui.util.IOUtils;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class CustomLocale {
|
||||||
|
private final Logger logger = Logger.getLogger("CustomLocale");
|
||||||
|
private final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
|
||||||
|
|
||||||
|
private final File file;
|
||||||
|
private final File backupFile;
|
||||||
|
private final NickoBukkit instance;
|
||||||
|
private final String version;
|
||||||
|
private final Version versionObject;
|
||||||
|
private final BufferedInputStream inputStream;
|
||||||
|
private final BufferedOutputStream outputStream;
|
||||||
|
private final YamlConfig yamlFile;
|
||||||
|
|
||||||
|
public CustomLocale(NickoBukkit instance) throws FileNotFoundException {
|
||||||
|
this.instance = instance;
|
||||||
|
this.file = new File(directory, "lang.yml");
|
||||||
|
final String date = Instant.now()
|
||||||
|
.atZone(ZoneId.systemDefault())
|
||||||
|
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
|
||||||
|
this.backupFile = new File(directory, "lang.old-" + date + ".yml");
|
||||||
|
this.inputStream = new BufferedInputStream(new FileInputStream(file));
|
||||||
|
this.outputStream = new BufferedOutputStream(new FileOutputStream(file));
|
||||||
|
this.yamlFile = new YamlConfig(inputStream);
|
||||||
|
this.version = yamlFile.getString("version");
|
||||||
|
this.versionObject = Version.fromString(version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean dumpIntoFile(Locale locale) {
|
||||||
|
if (locale == Locale.CUSTOM) return true;
|
||||||
|
if (file.exists()) return true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (directory.mkdirs()) {
|
||||||
|
if (file.createNewFile()) {
|
||||||
|
IOUtils.copy(inputStream, outputStream, 8192);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.severe("Unable to dump Locale: " + locale.getCode() + "!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Version getVersionObject() {
|
||||||
|
return versionObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public YamlConfig getYamlFile() {
|
||||||
|
return yamlFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getBackupFile() {
|
||||||
|
return backupFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getFile() {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
|
@ -145,7 +145,7 @@ public class I18N {
|
||||||
|
|
||||||
private YamlConfig getYamlConfig() {
|
private YamlConfig getYamlConfig() {
|
||||||
if (playerLocale == Locale.CUSTOM) {
|
if (playerLocale == Locale.CUSTOM) {
|
||||||
return instance.getLocaleFileManager().getYamlFile();
|
return instance.getCustomLocale().getYamlFile();
|
||||||
} else {
|
} else {
|
||||||
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
||||||
return new YamlConfig(resource);
|
return new YamlConfig(resource);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package xyz.ineanto.nicko.i18n;
|
package xyz.ineanto.nicko.i18n;
|
||||||
|
|
||||||
|
import xyz.ineanto.nicko.version.Version;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public enum Locale implements Serializable {
|
public enum Locale implements Serializable {
|
||||||
|
@ -7,6 +9,8 @@ public enum Locale implements Serializable {
|
||||||
FRENCH("fr", "Français"),
|
FRENCH("fr", "Français"),
|
||||||
CUSTOM("cm", "Server Custom");
|
CUSTOM("cm", "Server Custom");
|
||||||
|
|
||||||
|
public static final Version VERSION = new Version(1, 1, 0);
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private transient final String name;
|
private transient final String name;
|
||||||
|
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
package xyz.ineanto.nicko.i18n;
|
|
||||||
|
|
||||||
import com.github.jsixface.YamlConfig;
|
|
||||||
import xyz.ineanto.nicko.NickoBukkit;
|
|
||||||
import xyz.xenondevs.invui.util.IOUtils;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class LocaleFileManager {
|
|
||||||
private final Logger logger = Logger.getLogger("LocaleFileManager");
|
|
||||||
private final File folder = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
|
|
||||||
private final File file = new File(folder, "lang.yml");
|
|
||||||
private YamlConfig yamlFile;
|
|
||||||
|
|
||||||
public boolean dumpFromLocale(Locale locale) {
|
|
||||||
if (locale == Locale.CUSTOM) return true;
|
|
||||||
if (file.exists()) return true;
|
|
||||||
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(locale.getCode() + ".yml");
|
|
||||||
try {
|
|
||||||
if (folder.mkdirs()) {
|
|
||||||
if (file.createNewFile()) {
|
|
||||||
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
|
||||||
IOUtils.copy(inputStream, outputStream, 8192);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.severe("Unable to dump Locale: " + locale.getCode() + "!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public YamlConfig getYamlFile() {
|
|
||||||
if (yamlFile == null) {
|
|
||||||
try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
|
|
||||||
yamlFile = new YamlConfig(inputStream);
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return yamlFile;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package xyz.ineanto.nicko.migration;
|
||||||
|
|
||||||
|
import xyz.ineanto.nicko.NickoBukkit;
|
||||||
|
import xyz.ineanto.nicko.config.Configuration;
|
||||||
|
import xyz.ineanto.nicko.config.ConfigurationManager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
|
public class ConfigurationMigrator implements Migrator {
|
||||||
|
private final NickoBukkit instance;
|
||||||
|
|
||||||
|
public ConfigurationMigrator(NickoBukkit instance) {
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate() {
|
||||||
|
final Configuration configuration = instance.getNickoConfig();
|
||||||
|
final ConfigurationManager configurationManager = instance.getConfigurationManager();
|
||||||
|
|
||||||
|
// Migrate configuration (1.0.8-RC1)
|
||||||
|
if (configuration.getVersion() == null
|
||||||
|
|| configuration.getVersion().isEmpty()
|
||||||
|
|| configuration.getVersionObject().compareTo(Configuration.VERSION) != 0) {
|
||||||
|
instance.getLogger().info("Migrating configuration file to match the current version...");
|
||||||
|
try {
|
||||||
|
Files.copy(configurationManager.getFile().toPath(), configurationManager.getBackupFile().toPath(), StandardCopyOption.ATOMIC_MOVE);
|
||||||
|
configurationManager.saveDefaultConfig();
|
||||||
|
} catch (IOException e) {
|
||||||
|
instance.getLogger().severe("Failed to migrate your configuration!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package xyz.ineanto.nicko.migration;
|
||||||
|
|
||||||
|
import xyz.ineanto.nicko.NickoBukkit;
|
||||||
|
import xyz.ineanto.nicko.i18n.CustomLocale;
|
||||||
|
import xyz.ineanto.nicko.i18n.Locale;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
|
public class CustomLocaleMigrator implements Migrator {
|
||||||
|
private final NickoBukkit instance;
|
||||||
|
|
||||||
|
public CustomLocaleMigrator(NickoBukkit instance) {
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate() {
|
||||||
|
final CustomLocale customLanguageFile = instance.getCustomLocale();
|
||||||
|
|
||||||
|
// Migrate custom locale (1.1.0-RC1)
|
||||||
|
if (customLanguageFile.getVersionObject() == null
|
||||||
|
|| customLanguageFile.getVersion().isEmpty()
|
||||||
|
|| customLanguageFile.getVersionObject().compareTo(Locale.VERSION) != 0) {
|
||||||
|
instance.getLogger().info("Migrating custom locale to match the current version...");
|
||||||
|
try {
|
||||||
|
Files.copy(customLanguageFile.getFile().toPath(), customLanguageFile.getBackupFile().toPath(), StandardCopyOption.ATOMIC_MOVE);
|
||||||
|
customLanguageFile.dumpIntoFile(Locale.ENGLISH);
|
||||||
|
} catch (IOException e) {
|
||||||
|
instance.getLogger().severe("Failed to migrate your custom locale!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
src/main/java/xyz/ineanto/nicko/migration/Migrator.java
Normal file
5
src/main/java/xyz/ineanto/nicko/migration/Migrator.java
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package xyz.ineanto.nicko.migration;
|
||||||
|
|
||||||
|
public interface Migrator {
|
||||||
|
void migrate();
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
# Nicko ${version} - Config:
|
# Nicko ${version} - Config:
|
||||||
|
|
||||||
# Specifies the configuration version.
|
# Specifies the configuration version. (Don't change!)
|
||||||
# Do NOT modify this field.
|
|
||||||
version: "1.0.8"
|
version: "1.0.8"
|
||||||
|
|
||||||
############
|
############
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
# Nicko ${version} - Language File:
|
||||||
|
|
||||||
|
# Specifies the configuration version, don't change.
|
||||||
|
version: "1.1.0"
|
||||||
|
|
||||||
error:
|
error:
|
||||||
generic: "An unknown error occurred."
|
generic: "An unknown error occurred."
|
||||||
permission: "§cYou do not have the required permission."
|
permission: "§cYou do not have the required permission."
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
# Nicko ${version} - Config:
|
||||||
|
|
||||||
|
# Specifies the configuration version, don't change.
|
||||||
|
version: "1.1.0"
|
||||||
|
|
||||||
error:
|
error:
|
||||||
generic: "Une erreur inconnue c'est produite."
|
generic: "Une erreur inconnue c'est produite."
|
||||||
permission: "§cVous ne possédez pas la permission."
|
permission: "§cVous ne possédez pas la permission."
|
||||||
|
|
Loading…
Reference in a new issue