Finalize persistent data implementation

This commit is contained in:
Gabriel Tofvesson 2021-05-02 21:58:35 +02:00
parent 05640e87b2
commit f545ce86c8

View File

@ -2,9 +2,12 @@ package dev.w1zzrd.invtweaks;
import dev.w1zzrd.invtweaks.command.MagnetCommandExecutor;
import dev.w1zzrd.invtweaks.command.SortCommandExecutor;
import dev.w1zzrd.invtweaks.config.MagnetConfig;
import dev.w1zzrd.invtweaks.listener.MagnetismListener;
import dev.w1zzrd.invtweaks.serialization.MagnetConfig;
import dev.w1zzrd.invtweaks.listener.SortListener;
import dev.w1zzrd.invtweaks.listener.StackReplaceListener;
import dev.w1zzrd.invtweaks.serialization.MagnetData;
import dev.w1zzrd.invtweaks.serialization.UUIDList;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.event.HandlerList;
@ -23,23 +26,20 @@ public final class InvTweaksPlugin extends JavaPlugin {
* Plugin logging tag. This should be prepended to any log messages sent by this plugin
*/
public static final String LOG_PLUGIN_NAME = "[InventoryTweaks]";
private static final String PERSISTENT_DATA_NAME = "data";
private final Logger logger = Bukkit.getLogger();
// Command executor references in case I need them or something idk
private SortCommandExecutor sortCommandExecutor;
private MagnetCommandExecutor magnetCommandExecutor;
private DataStore data;
@Override
public void onEnable() {
logger.fine(LOG_PLUGIN_NAME + " Plugin enabled");
registerSerializers();
getConfig().options().copyDefaults(true);
saveConfig();
enablePersistentData();
initCommands();
initEvents();
}
@ -50,10 +50,7 @@ public final class InvTweaksPlugin extends JavaPlugin {
disableEvents();
disableCommands();
saveConfig();
unregisterSerializers();
disablePersistentData();
}
@Override
@ -66,12 +63,16 @@ public final class InvTweaksPlugin extends JavaPlugin {
magnetCommandExecutor.reloadConfig();
}
public DataStore getPersistentData() {
return data;
}
/**
* Initialize commands registered by this plugin
*/
private void initCommands() {
sortCommandExecutor = new SortCommandExecutor();
magnetCommandExecutor = new MagnetCommandExecutor(this);
magnetCommandExecutor = new MagnetCommandExecutor(this, getPersistentData());
// TODO: Bind command by annotation
Objects.requireNonNull(getCommand("sort")).setExecutor(sortCommandExecutor);
@ -86,12 +87,15 @@ public final class InvTweaksPlugin extends JavaPlugin {
pluginManager.registerEvents(new StackReplaceListener(), this);
pluginManager.registerEvents(new SortListener(), this);
pluginManager.registerEvents(new MagnetismListener(magnetCommandExecutor), this);
}
/**
* Do whatever is necessary to disable commands and their execution
*/
private void disableCommands() {
magnetCommandExecutor.onDisable();
sortCommandExecutor = null;
magnetCommandExecutor = null;
}
@ -106,9 +110,33 @@ public final class InvTweaksPlugin extends JavaPlugin {
private void registerSerializers() {
ConfigurationSerialization.registerClass(MagnetConfig.class);
ConfigurationSerialization.registerClass(MagnetData.class);
ConfigurationSerialization.registerClass(UUIDList.class);
}
private void unregisterSerializers() {
ConfigurationSerialization.unregisterClass(MagnetConfig.class);
ConfigurationSerialization.unregisterClass(MagnetData.class);
ConfigurationSerialization.unregisterClass(UUIDList.class);
}
private void enablePersistentData() {
registerSerializers();
getConfig().options().copyDefaults(true);
saveConfig();
// Implicit load
data = new DataStore(PERSISTENT_DATA_NAME, this);
}
private void disablePersistentData() {
data.saveData();
data = null;
saveConfig();
unregisterSerializers();
}
}