From d13ee3ef4df989ca60c726df961b18eceeb9ec27 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 9 Sep 2020 10:00:29 -0500 Subject: [PATCH] Added proper support for the new SongodaCore data loading system. --- .../com/songoda/epicfarming/EpicFarming.java | 117 +++++++++--------- 1 file changed, 56 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/songoda/epicfarming/EpicFarming.java b/src/main/java/com/songoda/epicfarming/EpicFarming.java index b11ab4e..916a307 100644 --- a/src/main/java/com/songoda/epicfarming/EpicFarming.java +++ b/src/main/java/com/songoda/epicfarming/EpicFarming.java @@ -124,64 +124,6 @@ public class EpicFarming extends SongodaPlugin { this.farmManager = new FarmManager(levelManager); this.boostManager = new BoostManager(); - /* - * Register Farms into FarmManger from configuration - */ - Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> { - if (storage.containsGroup("farms")) { - for (StorageRow row : storage.getRowsByGroup("farms")) { - Location location = Methods.unserializeLocation(row.getKey()); - if (location == null || location.getWorld() == null) continue; - - int level = 1; - int configLevel = row.get("level").asInt(); - if (configLevel > 0) { - level = configLevel; - } - List items = new ArrayList(); - List configItems = row.get("contents").asItemStackList(); - if (configItems != null && configItems.size() > 0) { - items = configItems; - } - UUID placedBY = null; - String configPlacedBy = row.get("placedby").asString(); - if (configPlacedBy != null) { - placedBY = UUID.fromString(configPlacedBy); - } - - FarmType farmType = FarmType.BOTH; - String farmTypeStr = row.get("farmtype").asString(); - if (farmTypeStr != null) - farmType = FarmType.valueOf(farmTypeStr); - - Farm farm = new Farm(location, levelManager.getLevel(level), placedBY); - farm.setFarmType(farmType); - farm.setItems(items); - Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> - farmManager.addFarm(location, farm)); - } - } - - // Adding in Boosts - if (storage.containsGroup("boosts")) { - for (StorageRow row : storage.getRowsByGroup("boosts")) { - - BoostData boostData = new BoostData( - row.get("amount").asInt(), - Long.parseLong(row.getKey()), - UUID.fromString(row.get("player").asString())); - - Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> { - this.boostManager.addBoostToPlayer(boostData); - }); - } - } - - // Save data initially so that if the person reloads again fast they don't lose all their data. - this.saveToFile(); - }, 10); - - // Register Listeners guiManager.init(); PluginManager pluginManager = Bukkit.getPluginManager(); @@ -207,11 +149,65 @@ public class EpicFarming extends SongodaPlugin { Bukkit.getScheduler().runTaskLater(this, () -> { if (!Bukkit.getPluginManager().isPluginEnabled("EpicHoppers")) HopperTask.startTask(this); - }, 20L); + }, 30L); // Start auto save Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::saveToFile, 6000, 6000); } + @Override + public void onDataLoad() { + if (storage.containsGroup("farms")) { + for (StorageRow row : storage.getRowsByGroup("farms")) { + Location location = Methods.unserializeLocation(row.getKey()); + if (location == null || location.getWorld() == null) continue; + + int level = 1; + int configLevel = row.get("level").asInt(); + if (configLevel > 0) { + level = configLevel; + } + List items = new ArrayList(); + List configItems = row.get("contents").asItemStackList(); + if (configItems != null && configItems.size() > 0) { + items = configItems; + } + UUID placedBY = null; + String configPlacedBy = row.get("placedby").asString(); + if (configPlacedBy != null) { + placedBY = UUID.fromString(configPlacedBy); + } + + FarmType farmType = FarmType.BOTH; + String farmTypeStr = row.get("farmtype").asString(); + if (farmTypeStr != null) + farmType = FarmType.valueOf(farmTypeStr); + + Farm farm = new Farm(location, levelManager.getLevel(level), placedBY); + farm.setFarmType(farmType); + farm.setItems(items); + Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> + farmManager.addFarm(location, farm)); + } + } + + // Adding in Boosts + if (storage.containsGroup("boosts")) { + for (StorageRow row : storage.getRowsByGroup("boosts")) { + + BoostData boostData = new BoostData( + row.get("amount").asInt(), + Long.parseLong(row.getKey()), + UUID.fromString(row.get("player").asString())); + + Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> { + this.boostManager.addBoostToPlayer(boostData); + }); + } + } + + // Save data initially so that if the person reloads again fast they don't lose all their data. + this.saveToFile(); + } @Override public void onConfigReload() { @@ -287,8 +283,7 @@ public class EpicFarming extends SongodaPlugin { } public int getLevelFromItem(ItemStack item) { - NBTCore nbt = NmsManager.getNbt(); - NBTItem nbtItem = nbt.of(item); + NBTItem nbtItem = NmsManager.getNbt().of(item); if (nbtItem.has("level")) return nbtItem.getNBTObject("level").asInt();