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