Skip to content

Commit

Permalink
✨ reload webhook + client
Browse files Browse the repository at this point in the history
  • Loading branch information
SerenModz21 committed Jul 28, 2022
1 parent 0dad828 commit 1ee985c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .github/changelog.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 2 additions & 18 deletions src/main/java/me/seren/Events.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/me/seren/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,13 +62,42 @@ 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();
presence.setActivity(Utils.activityOf(Utils.ActivityChoices.STARTED));
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<ServerCommandSource> requirePermission(String permission, int defaultLevel) {
if (isFabricPermissionsAPILoaded()) return Permissions.require(permission, defaultLevel);
return source -> source.hasPermissionLevel(defaultLevel);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/me/seren/command/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,7 +28,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
}

private static int reload(CommandContext<ServerCommandSource> ctx) {
modConfig.reload();
Utils.reloadModConfig(ctx.getSource().getServer());
ctx.getSource().sendFeedback(Text.literal("The config has been reloaded"), true);
return 1;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/me/seren/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public ModConfig(String file) {
config.load();
}

public void reload() {
public void reloadFile() {
config.load();
Utils.setStartedPresence();
}

public String getWebhookUrl() {
Expand Down

0 comments on commit 1ee985c

Please sign in to comment.