From 6c2e06afaf58ad4801d3c10aaf00f30000d88e24 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 10 May 2021 16:15:56 +0200 Subject: [PATCH] Changed data.yml format and added save-method --- .../ruakij/LinkedBeaconTeleporters/Main.java | 13 +++++++ .../LinkedBeaconTeleporterManager.java | 36 +++++++++++++++---- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java index b02d945..e1f9ecf 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java @@ -47,6 +47,8 @@ public class Main extends JavaPlugin { public void onDisable() { + LinkedBeaconTeleporterManager.saveData(); + log.info("Plugin deactivated"); } @@ -68,4 +70,15 @@ public class Main extends JavaPlugin { Bukkit.getPluginManager().disablePlugin(this); } } + + public static void saveData() { + // data.yml + try{ + File dataFile = new File("plugins/"+ plugin.getName() +"/data.yml"); + data.save(dataFile); + }catch (Exception ex){ + log.severe("Could not save data.yml!"); + ex.printStackTrace(); + } + } } diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java index d5a20e0..e615ad5 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java @@ -7,7 +7,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.block.Block; -import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.meta.ItemMeta; @@ -39,18 +39,17 @@ public class LinkedBeaconTeleporterManager { // Load teleporters for(String teleporterId : Main.data.getKeys(false)){ - ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); + List serialisedLocs = Main.data.getStringList(teleporterId); // New list for this id List linkedBeaconTeleporters = new ArrayList(); - for(String serializedLoc : teleportersData.getKeys(false)) { - ConfigurationSection teleporterData = teleportersData.getConfigurationSection(serializedLoc); + for(String serializedLoc : serialisedLocs) { // Load location Location loc; try { - loc = Function.deserializeBlockLocation(teleporterData.getString("loc")); + loc = Function.deserializeBlockLocation(serializedLoc); } catch (InvalidPropertiesFormatException e) { Main.log.severe("Could not load location='"+ serializedLoc +"' from "+ teleporterId); continue; @@ -80,6 +79,31 @@ public class LinkedBeaconTeleporterManager { Main.log.info("All done!"); } + public static void saveData(){ + Main.log.info("Saving data.."); + + Main.data = new YamlConfiguration(); + for(String teleporterId : placedLBTsById.keySet()){ + List LBTs = placedLBTsById.get(teleporterId); + + // Generate clean array with data we want + List dataList = new ArrayList<>(); + for(LinkedBeaconTeleporter lbt : LBTs){ + dataList.add( + Function.serialiseBlockLocation( + ((LinkedBeaconTeleporterBlock)lbt).block().getLocation() + ) + ); + } + + Main.data.set(teleporterId, dataList); + } + + Main.saveData(); + + Main.log.info("done"); + } + static void constructAndAddRecipes(){ // Create result-item ItemStack item = getItemStackFromData("-"); @@ -160,7 +184,7 @@ public class LinkedBeaconTeleporterManager { return placedLBTsById.get(teleportId); } - String constructRegex(String name, List lore){ + static String constructRegex(String name, List lore){ // Serialize String regex = serialiseBeaconItem(name, lore);