Add migration v1

This commit is contained in:
Tomás F 2020-07-03 20:16:02 +01:00
parent 4c3906f773
commit 791f69a19d
6 changed files with 65 additions and 46 deletions

View File

@ -4,8 +4,8 @@ import com.tomff.beesplus.core.UpdateChecker;
import com.tomff.beesplus.core.gui.GuiHandler;
import com.tomff.beesplus.core.gui.GuiViewTracker;
import com.tomff.beesplus.core.items.CustomItemManager;
import com.tomff.beesplus.core.migrations.AddField;
import com.tomff.beesplus.core.migrations.Migration;
import com.tomff.beesplus.core.migrations.AddFields;
import com.tomff.beesplus.core.migrations.Field;
import com.tomff.beesplus.core.migrations.MigrationsExecutor;
import com.tomff.beesplus.handlers.BeehiveHandler;
import com.tomff.beesplus.handlers.DamageHandler;
@ -26,8 +26,6 @@ public class BeesPlus extends JavaPlugin {
private GuiViewTracker guiViewTracker;
private CustomItemManager customItemManager;
private LocalizationWrapper localizationWrapper;
@Override
public void onEnable() {
performMigrations();
@ -63,7 +61,7 @@ public class BeesPlus extends JavaPlugin {
private boolean loadLocale() {
String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag());
localizationWrapper = new LocalizationWrapper(this, "locale");
LocalizationWrapper localizationWrapper = new LocalizationWrapper(this, "locale");
try {
YamlConfiguration localeYamlFile = localizationWrapper.getLocale(locale);
@ -85,8 +83,27 @@ public class BeesPlus extends JavaPlugin {
private void performMigrations() {
MigrationsExecutor migrationsExecutor = new MigrationsExecutor(this);
migrationsExecutor.addMigration(1, new Migration()
.add(new AddField("config.yml", "beehiveupgrade.maximumpopulation", 9))
Field[] beehiveUpgradeTranslation = new Field[] {
new Field("beehive_upgrade_item_name", "&6Beehive Upgrade"),
new Field("beehive_upgrade_item_lore", "&7Bee capacity: &a+3||&8(Right click to use)"),
new Field("beehive_upgrade_success", "&aBeehive upgraded! New population: &7%beesno%&a bees"),
new Field("beehive_upgrade_max", "&cError: This beehive has reached the maximum population allowed!")
};
migrationsExecutor.addMigration(1,
new AddFields("config.yml", new Field[] {
new Field("beehiveupgrade.maximumpopulation", 9)
}),
new AddFields("locale/en.yml", beehiveUpgradeTranslation),
new AddFields("locale/fr.yml", beehiveUpgradeTranslation),
new AddFields("locale/hu.yml", beehiveUpgradeTranslation),
new AddFields("locale/zh_cn.yml", beehiveUpgradeTranslation),
new AddFields("locale/pt.yml", new Field[] {
new Field("beehive_upgrade_item_name", "&6Melhorar Colmeia"),
new Field("beehive_upgrade_item_lore", "&7População de abelhas: &a+3||&8(Clique direito para usar)"),
new Field("beehive_upgrade_success", "&aColmeia melhorada! Nova população: &7%beesno%&a abelhas"),
new Field("beehive_upgrade_max", "&cErro: Esta colmeia atingiu a população máxima permitida!")
})
);
migrationsExecutor.migrate();

View File

@ -6,21 +6,21 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
public class AddField implements Operation {
public class AddFields implements Operation {
private File file;
private Field[] fields;
private String fieldPath;
private Object fieldValue;
public AddField(String filePath, String fieldPath, Object fieldValue) {
public AddFields(String filePath, Field[] fields) {
file = new File(BASE_PATH, filePath);
this.fieldPath = fieldPath;
this.fieldValue = fieldValue;
this.fields = fields;
}
@Override
public void execute() throws IOException {
if (!file.exists()) {
return;
}
YamlConfiguration config = new YamlConfiguration();
try {
@ -29,7 +29,10 @@ public class AddField implements Operation {
e.printStackTrace();
}
config.set(fieldPath, fieldValue);
for (Field field : fields) {
config.set(field.getPath(), field.getValue());
}
config.save(file);
}
}

View File

@ -0,0 +1,19 @@
package com.tomff.beesplus.core.migrations;
public class Field {
private final String path;
private final Object value;
public Field(String path, Object value) {
this.path = path;
this.value = value;
}
public String getPath() {
return path;
}
public Object getValue() {
return value;
}
}

View File

@ -1,21 +0,0 @@
package com.tomff.beesplus.core.migrations;
import java.util.ArrayList;
import java.util.List;
public class Migration {
private List<Operation> operations;
public Migration() {
this.operations = new ArrayList<>();
}
public Migration add(Operation operation) {
operations.add(operation);
return this;
}
public List<Operation> getOperations() {
return operations;
}
}

View File

@ -12,7 +12,7 @@ import java.util.Map;
public class MigrationsExecutor {
private int latestVersion;
private Map<Integer, Migration> migrations;
private Map<Integer, Operation[]> migrations;
private File migrationFileRegistry;
private YamlConfiguration yaml;
@ -26,8 +26,8 @@ public class MigrationsExecutor {
this.latestVersion = 0;
}
public void addMigration(int version, Migration migration) {
migrations.put(version, migration);
public void addMigration(int version, Operation... operations) {
migrations.put(version, operations);
if (version > latestVersion) {
this.latestVersion = version;
@ -62,6 +62,7 @@ public class MigrationsExecutor {
public void setCurrentMigration(int version) {
yaml.set("migration", version);
try {
yaml.save(migrationFileRegistry);
} catch (IOException e) {
@ -99,8 +100,8 @@ public class MigrationsExecutor {
return true;
}
private void performMigration(Migration migration) {
for(Operation operation : migration.getOperations()) {
private void performMigration(Operation[] operations) {
for(Operation operation : operations) {
try {
operation.execute();
} catch (IOException e) {

View File

@ -3,9 +3,10 @@ package com.tomff.beesplus.items;
import com.tomff.beesplus.BeesPlus;
import com.tomff.beesplus.core.items.CustomItem;
import com.tomff.beesplus.core.items.ItemBuilder;
import com.tomff.beesplus.gui.BeeHiveInfo;
import com.tomff.beesplus.localization.Localization;
import org.bukkit.*;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.block.Beehive;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
@ -19,7 +20,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;