Skip to content

Commit

Permalink
Merge pull request #5 from Tom-The-Geek/fix/dedicated
Browse files Browse the repository at this point in the history
Fix crash on dedicated server
  • Loading branch information
Meredith Espinosa authored Jul 12, 2021
2 parents 585b6b1 + ab45160 commit de6ee6a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
21 changes: 13 additions & 8 deletions src/main/java/net/modfest/utilities/ModFestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.text.LiteralText;
import net.modfest.utilities.config.Config;
Expand All @@ -21,53 +22,56 @@

import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.util.Collections;

public class ModFestUtilities implements ModInitializer {

public static final Logger LOGGER = LogManager.getLogger();
public static final OkHttpClient client = new OkHttpClient();
public static final OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
public static final Gson GSON = new GsonBuilder().create();
private static JDA discord;

@Override
public void onInitialize() {
Config.getInstance().load();
restart();

CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(CommandManager.literal("modfest")
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(4))
.executes(context -> 0)
.then(CommandManager.literal("reload").executes(context -> {
Config.getInstance().load();
restart();
restart(context.getSource().getMinecraftServer());
context.getSource().sendFeedback(new LiteralText("Reloaded ModFestChat config."), true);
return 0;
}))
)
);

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
restart(server);
WebHookUtil.send(WebHookJson.createSystem("The server is starting..."));
});
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
WebHookUtil.send(WebHookJson.createSystem("The server has started."));
});

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
WebHookUtil.send(WebHookJson.createSystem("The server is starting..."));
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
ModFestUtilities.shutdown();
WebHookUtil.send(WebHookJson.createSystem("The server has shutdown."));
});
}

public static void restart() {
public static void restart(MinecraftServer server) {
if (discord != null) {
shutdown();
}
if (!Config.getInstance().getChannel().isEmpty() && !Config.getInstance().getToken().isEmpty()) {
try {
discord = JDABuilder.createDefault(Config.getInstance().getToken(), GatewayIntent.getIntents(GatewayIntent.DEFAULT | GatewayIntent.getRaw(GatewayIntent.GUILD_MEMBERS)))
.addEventListeners(new ChannelListener())
.addEventListeners(new ChannelListener(server))
.build();
} catch (LoginException e) {
e.printStackTrace();
Expand All @@ -78,6 +82,7 @@ public static void restart() {
public static void shutdown() {
if (discord != null) {
discord.shutdown();
discord = null; // allow garbage collection, as the event listener has a reference to the MinecraftServer.
}
}

Expand Down
44 changes: 19 additions & 25 deletions src/main/java/net/modfest/utilities/discord/ChannelListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,35 @@
import java.util.UUID;

public class ChannelListener extends ListenerAdapter {
private final MinecraftServer server;

public ChannelListener(MinecraftServer server) {
this.server = server;
}

@Override
public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) {
if (!event.getChannel().getId().equals(Config.getInstance().getChannel())
|| event.getAuthor().isBot()) return;

Object gameInstance = FabricLoader.getInstance().getGameInstance();
MinecraftServer server = null;
if (gameInstance instanceof MinecraftClient) {
server = ((MinecraftClient) gameInstance).getServer();
} else if (gameInstance instanceof MinecraftServer) {
server = (MinecraftServer) gameInstance;
}

if (server != null) {
PlayerManager playerManager = server.getPlayerManager();
if (playerManager != null) {
PlayerManager playerManager = server.getPlayerManager();
if (playerManager != null) {
LiteralText hoverText = (LiteralText) new LiteralText(event.getAuthor().getName()).formatted(Formatting.GRAY)
.append(new LiteralText("#" + event.getAuthor().getDiscriminator()).formatted(Formatting.DARK_GRAY));

LiteralText hoverText = (LiteralText) new LiteralText(event.getAuthor().getName()).formatted(Formatting.GRAY)
.append(new LiteralText("#" + event.getAuthor().getDiscriminator()).formatted(Formatting.DARK_GRAY));

if (event.getMember() != null && event.getMember().getRoles().size() != 0) {
hoverText.append(new LiteralText("\n\nRoles:").formatted(Formatting.GRAY));
for (Role role : event.getMember().getRoles()) {
hoverText.append(new LiteralText("\n" + role.getName()).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(role.getColorRaw()))));
}
if (event.getMember() != null && event.getMember().getRoles().size() != 0) {
hoverText.append(new LiteralText("\n\nRoles:").formatted(Formatting.GRAY));
for (Role role : event.getMember().getRoles()) {
hoverText.append(new LiteralText("\n" + role.getName()).setStyle(Style.EMPTY.withColor(TextColor.fromRgb(role.getColorRaw()))));
}
}

LiteralText text = (LiteralText) new LiteralText("<" + event.getAuthor().getName() + ">")
.setStyle(Style.EMPTY.withHoverEvent(
new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)
).withColor(Formatting.BLUE)).append(new LiteralText(" " + event.getMessage().getContentStripped()).formatted(Formatting.GRAY));
LiteralText text = (LiteralText) new LiteralText("<@" + event.getAuthor().getName() + ">")
.setStyle(Style.EMPTY.withHoverEvent(
new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)
).withColor(Formatting.BLUE)).append(new LiteralText(" " + event.getMessage().getContentStripped()).formatted(Formatting.GRAY));

playerManager.broadcastChatMessage(text, MessageType.CHAT, UUID.randomUUID());
}
playerManager.broadcastChatMessage(text, MessageType.CHAT, UUID.randomUUID());
}
}

Expand Down

0 comments on commit de6ee6a

Please sign in to comment.