From 1ee985cfeae432dfc6f498643507bdb245e374b6 Mon Sep 17 00:00:00 2001 From: Seren_Modz 21 Date: Thu, 28 Jul 2022 06:27:47 +0100 Subject: [PATCH] :sparkles: reload webhook + client --- .github/changelog.md | 7 ++++ gradle.properties | 2 +- src/main/java/me/seren/Events.java | 20 ++--------- src/main/java/me/seren/Utils.java | 33 +++++++++++++++++++ .../java/me/seren/command/MainCommand.java | 3 +- src/main/java/me/seren/config/ModConfig.java | 3 +- 6 files changed, 45 insertions(+), 23 deletions(-) diff --git a/.github/changelog.md b/.github/changelog.md index 349ed19..ae18033 100644 --- a/.github/changelog.md +++ b/.github/changelog.md @@ -1,3 +1,10 @@ +# 1.0.0-beta1.7 + +- Reload webhook from config + - Close the old webhook if one exists +- Reload client from config + - Shutdown the old client if one exists + # 1.0.0-beta1.6 - Changed configuration file from `kingsworld.properties` to `kingsworld/config.yml` diff --git a/gradle.properties b/gradle.properties index b68b731..aec7bc2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ loader_version = 0.14.8 # Mod Properties mod_name = Kings World -mod_version = 1.0.0-beta1.6 +mod_version = 1.0.0-beta1.7 maven_group = me.seren archives_base_name = kings-world diff --git a/src/main/java/me/seren/Events.java b/src/main/java/me/seren/Events.java index 6849b91..9cfef83 100644 --- a/src/main/java/me/seren/Events.java +++ b/src/main/java/me/seren/Events.java @@ -1,7 +1,5 @@ package me.seren; -import club.minnced.discord.webhook.WebhookClient; -import me.seren.discord.Client; import net.minecraft.entity.Entity; import net.minecraft.network.message.MessageType; import net.minecraft.network.message.SignedMessage; @@ -11,27 +9,13 @@ import net.minecraft.text.Text; import net.minecraft.util.registry.RegistryKey; -import javax.security.auth.login.LoginException; - import static me.seren.KingsWorld.*; public final class Events { public static void serverStarting(MinecraftServer server) { loadConfig(); - - logger.info("Creating webhook client..."); - try { - webhook = WebhookClient.withUrl(modConfig.getWebhookUrl()); - } catch (IllegalArgumentException e) { - logger.error(e.getMessage()); - } - - logger.info("Creating discord client..."); - try { - client = new Client(server); - } catch (LoginException | InterruptedException e) { - logger.error(e.getMessage()); - } + Utils.initializeWebhook(); + Utils.initializeClient(server); } public static void serverStarted(MinecraftServer server) { diff --git a/src/main/java/me/seren/Utils.java b/src/main/java/me/seren/Utils.java index c94dcfb..b4743a4 100644 --- a/src/main/java/me/seren/Utils.java +++ b/src/main/java/me/seren/Utils.java @@ -1,16 +1,20 @@ package me.seren; +import club.minnced.discord.webhook.WebhookClient; import club.minnced.discord.webhook.send.AllowedMentions; import club.minnced.discord.webhook.send.WebhookMessageBuilder; import me.lucko.fabric.api.permissions.v0.Permissions; +import me.seren.discord.Client; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.managers.Presence; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.entity.Entity; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; +import javax.security.auth.login.LoginException; import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; @@ -58,6 +62,24 @@ public static void sendEntityWebhook(Entity entity, String content) { .build()); } + public static void initializeWebhook() { + logger.info("Creating webhook client..."); + try { + webhook = WebhookClient.withUrl(modConfig.getWebhookUrl()); + } catch (IllegalArgumentException e) { + logger.error(e.getMessage()); + } + } + + public static void initializeClient(MinecraftServer server) { + logger.info("Creating discord client..."); + try { + client = new Client(server); + } catch (LoginException | InterruptedException e) { + logger.error(e.getMessage()); + } + } + public static void setStartedPresence() { if (client == null) return; Presence presence = client.jda.getPresence(); @@ -65,6 +87,17 @@ public static void setStartedPresence() { presence.setStatus(modConfig.getStartedActivityStatus()); } + public static void reloadModConfig(MinecraftServer server) { + modConfig.reloadFile(); + + if (webhook != null) webhook.close(); + initializeWebhook(); + + if (client != null) client.jda.shutdown(); + initializeClient(server); + setStartedPresence(); + } + public static Predicate requirePermission(String permission, int defaultLevel) { if (isFabricPermissionsAPILoaded()) return Permissions.require(permission, defaultLevel); return source -> source.hasPermissionLevel(defaultLevel); diff --git a/src/main/java/me/seren/command/MainCommand.java b/src/main/java/me/seren/command/MainCommand.java index 64421ca..3d32cb4 100644 --- a/src/main/java/me/seren/command/MainCommand.java +++ b/src/main/java/me/seren/command/MainCommand.java @@ -8,7 +8,6 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; -import static me.seren.KingsWorld.modConfig; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -29,7 +28,7 @@ public static void register(CommandDispatcher dispatcher) { } private static int reload(CommandContext ctx) { - modConfig.reload(); + Utils.reloadModConfig(ctx.getSource().getServer()); ctx.getSource().sendFeedback(Text.literal("The config has been reloaded"), true); return 1; } diff --git a/src/main/java/me/seren/config/ModConfig.java b/src/main/java/me/seren/config/ModConfig.java index 6a1fd86..77149bf 100644 --- a/src/main/java/me/seren/config/ModConfig.java +++ b/src/main/java/me/seren/config/ModConfig.java @@ -13,9 +13,8 @@ public ModConfig(String file) { config.load(); } - public void reload() { + public void reloadFile() { config.load(); - Utils.setStartedPresence(); } public String getWebhookUrl() {