From 50fb25ca3e2a81a4080cdca8676fcba0a8d2667e Mon Sep 17 00:00:00 2001 From: SamB440 Date: Wed, 13 Jan 2021 20:27:39 +0000 Subject: [PATCH] Disabled saving of data if managers are null to prevent exceptions being thrown on plugin disable. --- .../islandearth/rpgregions/RPGRegions.java | 33 +++---- .../managers/RPGRegionsManagers.java | 93 ++++++++----------- 2 files changed, 58 insertions(+), 68 deletions(-) diff --git a/rpgregions/src/main/java/net/islandearth/rpgregions/RPGRegions.java b/rpgregions/src/main/java/net/islandearth/rpgregions/RPGRegions.java index 09513ce..833c6fe 100644 --- a/rpgregions/src/main/java/net/islandearth/rpgregions/RPGRegions.java +++ b/rpgregions/src/main/java/net/islandearth/rpgregions/RPGRegions.java @@ -68,7 +68,14 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu plugin = this; this.createConfig(); this.generateLang(); - this.managers = new RPGRegionsManagers(this); + try { + this.managers = new RPGRegionsManagers(this); + } catch (ReflectiveOperationException | IOException e) { + getLogger().severe("Error starting managers: "); + e.printStackTrace(); + Bukkit.getPluginManager().disablePlugin(this); + return; + } this.registerListeners(); this.registerCommands(); this.hook(this); @@ -79,22 +86,16 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu public void onDisable() { if (managers == null || managers.getRegionsCache() == null || managers.getStorageManager() == null) { getLogger().warning("Unable to save data as managers were null"); - } - - // Save all player data (quit event not called for shutdown) - Bukkit.getOnlinePlayers().forEach(player -> { - if (managers.getStorageManager().getCachedAccounts().containsKey(player.getUniqueId())) - this.getManagers().getStorageManager().removeCachedAccount(player.getUniqueId()); - }); + } else { + // Save all player data (quit event not called for shutdown) + Bukkit.getOnlinePlayers().forEach(player -> { + if (managers.getStorageManager().getCachedAccounts().containsKey(player.getUniqueId())) + this.getManagers().getStorageManager().removeCachedAccount(player.getUniqueId()); + }); - // Save all region configs - managers.getRegionsCache().getConfiguredRegions().forEach((id, region) -> { - try { - region.save(this); - } catch (IOException e) { - e.printStackTrace(); - } - }); + // Save all region configs + managers.getRegionsCache().getConfiguredRegions().forEach((id, region) -> region.save(this)); + } // Close database connection DB.close(); diff --git a/rpgregions/src/main/java/net/islandearth/rpgregions/managers/RPGRegionsManagers.java b/rpgregions/src/main/java/net/islandearth/rpgregions/managers/RPGRegionsManagers.java index 795ea44..8d53c16 100644 --- a/rpgregions/src/main/java/net/islandearth/rpgregions/managers/RPGRegionsManagers.java +++ b/rpgregions/src/main/java/net/islandearth/rpgregions/managers/RPGRegionsManagers.java @@ -36,34 +36,31 @@ import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import java.util.stream.Collectors; +import java.util.stream.Stream; public class RPGRegionsManagers { private StorageManager storageManager; private IntegrationManager integrationManager; - private RPGRegionsCache regionsCache; - private RegenerationManager regenerationManager; + private final RPGRegionsCache regionsCache; + private final RegenerationManager regenerationManager; - public RPGRegionsManagers(RPGRegions plugin) { + public RPGRegionsManagers(RPGRegions plugin) throws ReflectiveOperationException, IllegalStateException, IOException { StorageType.valueOf(plugin.getConfig().getString("settings.storage.mode").toUpperCase()) .get() .ifPresent(storageManager1 -> storageManager = storageManager1); if (storageManager == null) throw new IllegalStateException("Could not find StorageManager!"); - try { - IntegrationType.valueOf(plugin.getConfig().getString("settings.integration.name").toUpperCase()) - .get() - .ifPresent(integrationManager1 -> integrationManager = integrationManager1); - } catch (ClassNotFoundException e) { - plugin.getLogger().log(Level.SEVERE, "Could not find IntegrationManager!", e); - } + IntegrationType.valueOf(plugin.getConfig().getString("settings.integration.name").toUpperCase()) + .get() + .ifPresent(integrationManager1 -> integrationManager = integrationManager1); this.regionsCache = new RPGRegionsCache(plugin); @@ -97,50 +94,42 @@ public class RPGRegionsManagers { Material.PURPUR_BLOCK, Material.PURPUR_PILLAR), 5, 30)))); configuredRegion.getIconCommand().add(new IconCommand("say", IconCommand.CommandClickType.DISCOVERED, 0)); - try { - configuredRegion.save(plugin); - } catch (IOException e) { - e.printStackTrace(); - } + configuredRegion.save(plugin); - try { - Files.walk(Paths.get(folder.getPath())) - .filter(Files::isRegularFile) - .collect(Collectors.toList()) - .forEach(path -> { - File file = path.toFile(); - if (regionsCache.getConfiguredRegions().containsKey(file.getName().replace(".json", ""))) { - plugin.getLogger().severe("Duplicate region files have been found for " + file.getName() + ". " + - "In order to protect your data, the plugin will NOT load the duplicate region config."); - return; - } - // Exclude non-json files - if (file.getName().endsWith(".json")) { - try { - Reader reader = new FileReader(file); - ConfiguredRegion region = plugin.getGson().fromJson(reader, ConfiguredRegion.class); - if (!region.getId().equals("exampleconfig")) regionsCache.addConfiguredRegion(region); - if (region.getRequirements() != null) { - region.getRequirements().forEach(regionRequirement -> { - if (regionRequirement instanceof PlaceholderRequirement) { - if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) { - plugin.getLogger().severe("Region " + region.getId() + " has PlaceholderRequirement but PlaceholderAPI is not installed. It will not be loaded."); - regionsCache.removeConfiguredRegion(region.getId()); - } - } - plugin.getLogger().warning("Warning: Region " + region.getId() + " uses requirements. These are highly experimental and there may be bypasses."); - }); + Stream files = Files.walk(Paths.get(folder.getPath())); + files.filter(Files::isRegularFile) + .collect(Collectors.toList()) + .forEach(path -> { + File file = path.toFile(); + if (regionsCache.getConfiguredRegions().containsKey(file.getName().replace(".json", ""))) { + plugin.getLogger().severe("Duplicate region files have been found for " + file.getName() + ". " + + "In order to protect your data, the plugin will NOT load the duplicate region config."); + return; + } + // Exclude non-json files + if (file.getName().endsWith(".json")) { + try { + Reader reader = new FileReader(file); + ConfiguredRegion region = plugin.getGson().fromJson(reader, ConfiguredRegion.class); + if (!region.getId().equals("exampleconfig")) regionsCache.addConfiguredRegion(region); + if (region.getRequirements() != null) { + region.getRequirements().forEach(regionRequirement -> { + if (regionRequirement instanceof PlaceholderRequirement + && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) { + plugin.getLogger().severe("Region " + region.getId() + " has PlaceholderRequirement but PlaceholderAPI is not installed. It will not be loaded."); + regionsCache.removeConfiguredRegion(region.getId()); } - reader.close(); - } catch (Exception e) { - plugin.getLogger().severe("Error loading region config " + file.getName() + ":"); - e.printStackTrace(); - } + plugin.getLogger().warning("Warning: Region " + region.getId() + " uses requirements. These are highly experimental and there may be bypasses."); + }); } - }); - } catch (IOException e) { - e.printStackTrace(); - } + reader.close(); + } catch (Exception e) { + plugin.getLogger().severe("Error loading region config " + file.getName() + ":"); + e.printStackTrace(); + } + } + }); + files.close(); this.regenerationManager = new RegenerationManager(plugin);