add Map for TemporaryData and shutdown handler

This commit is contained in:
Hiajen Hiajen 2021-04-05 13:35:44 +02:00
parent 221c930a32
commit db37d6ce9a
4 changed files with 51 additions and 1 deletions

View file

@ -4,5 +4,5 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="adopt-openj9-1.8" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="adopt-openj9-1.8" project-jdk-type="JavaSDK" />
</project>

View file

@ -4,6 +4,7 @@ import IO.JSON;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.ShutdownEvent;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
@ -133,6 +134,7 @@ public class Controller extends ListenerAdapter {
reload(event.getJDA());
}
@Override
public void onGuildJoin(@NotNull GuildJoinEvent event){
//Check if guild exists
for (GuildController guildController : guilds){
@ -156,6 +158,7 @@ public class Controller extends ListenerAdapter {
}
@Override
public void onGuildLeave(@NotNull GuildLeaveEvent event){
JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE);
@ -170,4 +173,12 @@ public class Controller extends ListenerAdapter {
}
JSON.saveJson(guilds, GUILD_CONFIG_FILE);
}
@Override
public void onShutdown(@NotNull ShutdownEvent shutdownEvent){
for (GuildController guildController: guilds){
guildController.shutdown();
}
logger.debug("Shutdown complete");
}
}

View file

@ -141,6 +141,14 @@ public class GuildController {
modules.add(new Help(this));
}
protected void shutdown(){
for (Module module: modules){
if (module instanceof SuperModule)
((SuperModule) module).shutdown();
}
logger.debug("Shutdown Modules for Guild: " + getGUILD_ID());
}
private boolean checkForActivate(String moduleName){
if (moduleConf.containsKey(moduleName)){
logger.info("activate " + moduleName + " in " +getGUILD_ID());

View file

@ -5,6 +5,9 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import org.json.simple.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -13,6 +16,10 @@ public abstract class SuperModule implements Module {
private GuildController guildController;
private JSONObject config;
private final String command;
/**
* May temporary data but save worthy (active timer etc)
*/
private Map<String, Object> temporaryData;
/**
* Construcker of a Module
@ -23,12 +30,24 @@ public abstract class SuperModule implements Module {
this.command = command;
this.guildController = guildController;
this.config = config;
temporaryData = (Map<String, Object>)config.get("TemporaryData");
if (temporaryData != null) {
this.config.remove("TemporaryData");
safeConfig();
} else {
temporaryData = new HashMap<>();
}
}
public SuperModule(String command, GuildController guildController){
this.command = command;
this.guildController = guildController;
this.config = null;
temporaryData = new HashMap<>();
}
/**
@ -118,4 +137,16 @@ public abstract class SuperModule implements Module {
protected void safeConfig(){
safeConfig(getConfig());
}
/**
* On shutdown safe temporary data what should be saved
*/
protected void shutdown(){
getConfig().put("TemporaryData", temporaryData);
safeConfig();
}
protected Map<String, Object> getTemporaryData(){
return temporaryData;
}
}