From db37d6ce9a3d30539002e03e316c771088179795 Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Mon, 5 Apr 2021 13:35:44 +0200 Subject: [PATCH 1/2] add Map for TemporaryData and shutdown handler --- .idea/misc.xml | 2 +- src/main/java/Controll/Controller.java | 11 ++++++++ src/main/java/Controll/GuildController.java | 8 ++++++ src/main/java/Controll/SuperModule.java | 31 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 7122aeb..2dbd315 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,5 +4,5 @@ - + \ No newline at end of file diff --git a/src/main/java/Controll/Controller.java b/src/main/java/Controll/Controller.java index 0324de1..3202212 100644 --- a/src/main/java/Controll/Controller.java +++ b/src/main/java/Controll/Controller.java @@ -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"); + } } diff --git a/src/main/java/Controll/GuildController.java b/src/main/java/Controll/GuildController.java index 93445d3..5a47e29 100644 --- a/src/main/java/Controll/GuildController.java +++ b/src/main/java/Controll/GuildController.java @@ -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()); diff --git a/src/main/java/Controll/SuperModule.java b/src/main/java/Controll/SuperModule.java index 88135e8..03c3c89 100644 --- a/src/main/java/Controll/SuperModule.java +++ b/src/main/java/Controll/SuperModule.java @@ -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 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)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 getTemporaryData(){ + return temporaryData; + } } From 1a8fe6c8ada071568f684a475431fcf26725d049 Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Mon, 5 Apr 2021 13:46:02 +0200 Subject: [PATCH 2/2] edit VoiceLobby Module to support tempData --- src/main/java/Modules/VoiceLobby.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/Modules/VoiceLobby.java b/src/main/java/Modules/VoiceLobby.java index 0c234dc..e21780f 100644 --- a/src/main/java/Modules/VoiceLobby.java +++ b/src/main/java/Modules/VoiceLobby.java @@ -31,7 +31,6 @@ public class VoiceLobby extends SuperModule { private long groupCategory; private long launchpadChannel; - private HashSet tmpChannel = new HashSet<>(); private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -98,7 +97,7 @@ public class VoiceLobby extends SuperModule { // move User event.getGuild().moveVoiceMember(((GuildMessageReceivedEvent) event).getMember(), newChannel).queue(); - tmpChannel.add(newChannel.getIdLong()); + ((HashSet)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong()); } else { thisEvent.getChannel().sendMessage("Please Join a Voice Channel first!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); @@ -185,7 +184,7 @@ public class VoiceLobby extends SuperModule { //check if user left a temp channel if ((thisEvent.getChannelLeft() != null && thisEvent.getChannelLeft().getParent().getIdLong() == groupCategory)) { //check if channel is now empty - if (thisEvent.getChannelLeft().getMembers().size() < 1 && tmpChannel.contains(thisEvent.getChannelLeft().getIdLong())) { + if (thisEvent.getChannelLeft().getMembers().size() < 1 && ((HashSet)getTemporaryData().get("tmpChannel")).contains(thisEvent.getChannelLeft().getIdLong())) { logger.debug("may remove channel: " + thisEvent.getChannelLeft().getId()); //wait for 30 seconds and check again @@ -197,7 +196,7 @@ public class VoiceLobby extends SuperModule { logger.debug("remove channel: " + thisEvent.getChannelLeft().getId()); thisEvent.getChannelLeft().delete().queue(); - tmpChannel.remove(thisEvent.getChannelLeft().getIdLong()); + ((HashSet)getTemporaryData().get("tmpChannel")).remove(thisEvent.getChannelLeft().getIdLong()); } else { logger.debug("dont remove channel: " + thisEvent.getChannelLeft().getId()); } @@ -215,7 +214,7 @@ public class VoiceLobby extends SuperModule { // move User event.getGuild().moveVoiceMember(thisEvent.getEntity(), newChannel).queue(); - tmpChannel.add(newChannel.getIdLong()); + ((HashSet)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong()); } } }