9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-27 18:59:10 +00:00

Disabled saving of data if managers are null to prevent exceptions being thrown on plugin disable.

This commit is contained in:
SamB440
2021-01-13 20:27:39 +00:00
parent 625eee56ed
commit 50fb25ca3e
2 changed files with 58 additions and 68 deletions

View File

@@ -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();

View File

@@ -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<Path> 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);