Cleaned up code

Added delapy when stopping to ensure that the server-stopped message was sent
This commit is contained in:
Jonas_Jones 2023-10-23 20:53:19 +02:00
parent 5bfb195326
commit 48761085c4
4 changed files with 18 additions and 13 deletions

View file

@ -17,7 +17,7 @@ public class ModConfigs {
//public static Boolean PLAYER_COMMAND_MSG; //public static Boolean PLAYER_COMMAND_MSG;
public static Boolean SERVER_START_STOP_MSG; public static Boolean SERVER_START_STOP_MSG;
public static String BOT_STATUS; //public static String BOT_STATUS;
public static void registerConfigs() { public static void registerConfigs() {
configs = new ModConfigProvider(); configs = new ModConfigProvider();
@ -37,7 +37,7 @@ public class ModConfigs {
configs.addKeyValuePair(new Pair<>("bot.player_chat_msgs", true), "Should the bot send a message when a player sends a chat message"); configs.addKeyValuePair(new Pair<>("bot.player_chat_msgs", true), "Should the bot send a message when a player sends a chat message");
//configs.addKeyValuePair(new Pair<>("bot.player_command_msgs", true), "Should the bot send a message when a player sends a command"); //configs.addKeyValuePair(new Pair<>("bot.player_command_msgs", true), "Should the bot send a message when a player sends a command");
configs.addKeyValuePair(new Pair<>("bot.server_start_stop_msgs", true), "Should the bot send a message when the server starts or stops"); configs.addKeyValuePair(new Pair<>("bot.server_start_stop_msgs", true), "Should the bot send a message when the server starts or stops");
configs.addKeyValuePair(new Pair<>("bot.status", "PlayerCount"), "What status should the bot display [None, PlayerCount, IP, Uptime]"); //configs.addKeyValuePair(new Pair<>("bot.status", "PlayerCount"), "What status should the bot display [None, PlayerCount, IP, Uptime]");
} }
private static void assignConfigs() { private static void assignConfigs() {
@ -49,7 +49,7 @@ public class ModConfigs {
PLAYER_CHAT_MSG = CONFIG.getOrDefault("bot.player_chat_msgs", true); PLAYER_CHAT_MSG = CONFIG.getOrDefault("bot.player_chat_msgs", true);
//PLAYER_COMMAND_MSG = CONFIG.getOrDefault("bot.player_command_msgs", true); //PLAYER_COMMAND_MSG = CONFIG.getOrDefault("bot.player_command_msgs", true);
SERVER_START_STOP_MSG = CONFIG.getOrDefault("bot.server_start_stop_msgs", true); SERVER_START_STOP_MSG = CONFIG.getOrDefault("bot.server_start_stop_msgs", true);
BOT_STATUS = CONFIG.getOrDefault("bot.status", "PlayerCount"); //BOT_STATUS = CONFIG.getOrDefault("bot.status", "PlayerCount");
YetAnotherDiscordChatLink.LOGGER.info("All " + configs.getConfigsList().size() + " Configs have been set properly"); YetAnotherDiscordChatLink.LOGGER.info("All " + configs.getConfigsList().size() + " Configs have been set properly");
} }

View file

@ -45,7 +45,7 @@ public class DiscordBot {
} }
}); });
// Set the bot status // Set the bot status
if (ModConfigs.BOT_STATUS.equals("Uptime")) { /*if (ModConfigs.BOT_STATUS.equals("Uptime")) {
new Thread(() -> { new Thread(() -> {
while (isBotRunning) { while (isBotRunning) {
botStatus(ModConfigs.BOT_STATUS); botStatus(ModConfigs.BOT_STATUS);
@ -58,7 +58,7 @@ public class DiscordBot {
}).start(); }).start();
} else { } else {
botStatus(ModConfigs.BOT_STATUS); botStatus(ModConfigs.BOT_STATUS);
} }*/
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Failed to start Discord bot. Check the provided discord token in the config file."); LOGGER.error("Failed to start Discord bot. Check the provided discord token in the config file.");
return; return;
@ -104,11 +104,17 @@ public class DiscordBot {
ServerLifecycleEvents.SERVER_STOPPED.register(server -> { ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
sendToDiscord("Server is stopped!"); sendToDiscord("Server is stopped!");
//wait for 2 seconds to make sure the message is sent
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopBot(); stopBot();
}); });
} }
public static void botStatus(String status) { /*public static void botStatus(String status) {
if (!isBotRunning) { if (!isBotRunning) {
return; return;
} }
@ -150,5 +156,5 @@ public class DiscordBot {
} }
return duration.toString(); return duration.toString();
} }*/
} }

View file

@ -10,7 +10,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static dev.jonasjones.yadcl.dcbot.DiscordBot.botStatus; //import static dev.jonasjones.yadcl.dcbot.DiscordBot.botStatus;
import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord; import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord;
@Mixin(PlayerManager.class) @Mixin(PlayerManager.class)
@ -19,9 +19,9 @@ public class PlayerManagerMixin {
public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) { public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) {
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) { if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
sendToDiscord(player.getDisplayName().getString() + " joined the game"); sendToDiscord(player.getDisplayName().getString() + " joined the game");
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) { /*if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
botStatus(ModConfigs.BOT_STATUS); botStatus(ModConfigs.BOT_STATUS);
} }*/
} }
} }
@ -29,9 +29,9 @@ public class PlayerManagerMixin {
public void remove(ServerPlayerEntity player, CallbackInfo ci) { public void remove(ServerPlayerEntity player, CallbackInfo ci) {
if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) { if (ModConfigs.PLAYER_JOIN_LEAVE_MSG) {
sendToDiscord(player.getDisplayName().getString() + " left the game"); sendToDiscord(player.getDisplayName().getString() + " left the game");
if (ModConfigs.BOT_STATUS.equals("PlayerCount")) { /*if (ModConfigs.BOT_STATUS.equals("PlayerCount")) {
botStatus(ModConfigs.BOT_STATUS); botStatus(ModConfigs.BOT_STATUS);
} }*/
} }
} }
} }

View file

@ -8,7 +8,6 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord; import static dev.jonasjones.yadcl.dcbot.DiscordBot.sendToDiscord;
import static dev.jonasjones.yadcl.dcbot.DiscordBot.stopBot;
@Mixin(MinecraftServer.class) @Mixin(MinecraftServer.class)
public class ServerStopMixin { public class ServerStopMixin {