Skip to content

Commit

Permalink
Update Discord Libraries + Fix pause menu
Browse files Browse the repository at this point in the history
  • Loading branch information
AsoDesu committed Apr 9, 2023
1 parent 9144728 commit 2daff33
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {
modApi include("me.shedaniel.cloth:cloth-config-fabric:10.0.96") {
exclude(group: "net.fabricmc.fabric-api")
}
modApi("com.terraformersmc:modmenu:5.0.1")
modApi("com.terraformersmc:modmenu:6.1.0-rc.4")

implementation include('com.github.JnCrMx:discord-game-sdk4j:v0.5.5')
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.19.4
yarn_mappings=1.19.4+build.1
loader_version=0.14.17
# Mod Properties
mod_version=1.3.1
mod_version=1.3.2
maven_group=net.asodev
archives_base_name=islandutils
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class DiscordPresence {

static String os = System.getProperty("os.name").toLowerCase();

public static void init() {
if (initalised) return;
public static boolean init() {
if (initalised) return true;

File discordLibrary;
try {
Expand All @@ -26,7 +26,7 @@ public static void init() {
System.load(discordJNI.getAbsolutePath());
} catch (Exception e) {
System.out.println("Failed to grab Natives: " + e.getMessage());
return;
return false;
}

Core.initDiscordNative(discordLibrary.getAbsolutePath());
Expand All @@ -40,16 +40,17 @@ public static void init() {
initalised = true;
} catch (Exception e) {
System.out.println("Failed to Initialise Discord Presence.");
return;
return false;
}

new Thread(() -> {
while (core.isOpen()) {
while (core != null && core.isOpen()) {
core.runCallbacks();
try { Thread.sleep(16); }
catch (Exception e) { e.printStackTrace(); }
}
}).start();
}, "IslandUtils - Discord Callbacks").start();
return true;
}

public static void clear() {
Expand All @@ -60,6 +61,7 @@ public static void clear() {
catch (Exception e) { e.printStackTrace(); }

initalised = false;
core = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ public class DiscordPresenceUpdator {
public static UUID timeLeftBossbar = null;
public static Instant started;
public static void create() {
if (!IslandOptions.getOptions().discordPresence) return;

try {
DiscordPresence.init();
boolean didInit = DiscordPresence.init();
if (!didInit) {
System.out.println("Failed to start discord presence");
return;
}

activity = new Activity();
activity.setType(ActivityType.PLAYING);
Expand Down Expand Up @@ -139,9 +145,12 @@ public static void clear() {

public static void updateFromConfig(IslandOptions options) {
try {
if (activity == null || DiscordPresence.core == null) return;
if (!MccIslandState.isOnline()) { clear(); return; }

if (options.discordPresence) create();
else { clear(); return; }

if (activity == null) activity = new Activity();

if (!options.showTimeElapsed) activity.timestamps().setStart(Instant.MAX);
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.asodev.islandutils.mixins.discord;

import net.asodev.islandutils.discord.DiscordPresenceUpdator;
import net.asodev.islandutils.state.GAME;
import net.asodev.islandutils.state.MccIslandState;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -15,6 +17,8 @@ public class ConnectionMixin {
private void disconnect(Component component, CallbackInfo ci) {
DiscordPresenceUpdator.started = null;
DiscordPresenceUpdator.clear();

MccIslandState.setGame(GAME.HUB);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,39 @@
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(PauseScreen.class)
public class PauseScreenMixin extends Screen {

@Final @Shadow private static Component DISCONNECT;
@Final @Shadow private static Component RETURN_TO_MENU;
@Mutable @Shadow private Button disconnectButton;
@Mutable @Shadow private void onDisconnect() {}

protected PauseScreenMixin(Component component) {
super(component);
}

@Inject(method = "createPauseMenu",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;isLocalServer()Z", shift = At.Shift.AFTER),
cancellable = true,
locals = LocalCapture.CAPTURE_FAILHARD
@Redirect(
method = "createPauseMenu",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/components/Button;builder(Lnet/minecraft/network/chat/Component;Lnet/minecraft/client/gui/components/Button$OnPress;)Lnet/minecraft/client/gui/components/Button$Builder;"
)
)
private void createPause(CallbackInfo ci, GridLayout gridLayout, GridLayout.RowHelper rowHelper) {
if (!MccIslandState.isOnline()) return;
ci.cancel();
private Button.Builder createPause(Component component, Button.OnPress onPress) {
if (!MccIslandState.isOnline() && IslandOptions.getOptions().isPauseConfirm()) return Button.builder(component, onPress);
if (component == DISCONNECT) {
Component message = Component.literal("Are you sure you want to leave?").withStyle(ChatFormatting.AQUA);
Component no = Component.literal("Cancel");
Component yes = Component.literal("Disconnect").withStyle(ChatFormatting.RED);

Component message = Component.literal("Are you sure you want to leave?").withStyle(ChatFormatting.AQUA);
Component no = Component.literal("Cancel");
Component yes = Component.literal("Disconnect").withStyle(ChatFormatting.RED);

Component component = this.minecraft.isLocalServer() ? RETURN_TO_MENU : DISCONNECT;
this.disconnectButton = rowHelper.addChild(Button.builder(component, (button) -> {
if (!IslandOptions.getOptions().isPauseConfirm()) disconnect(button);
else {
return Button.builder(component, (button) -> {
ConfirmScreen screen = new ConfirmScreen((bool) -> callback(bool, button), Component.literal("Leave the server"), message, yes, no);
Minecraft.getInstance().setScreen(screen);
}
}).width(204).build(), 2);
gridLayout.arrangeElements();
FrameLayout.alignInRectangle(gridLayout, 0, 0, this.width, this.height, 0.5F, 0.25F);
gridLayout.visitWidgets(this::addRenderableWidget);
});
}
return Button.builder(component, onPress);
}

void callback(boolean confirm, Button buttonx) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"homepage": "https://modrinth.com/mod/island-utils"
},
"license": "MIT",
"icon": "icon.png",
"icon": "assets/island/icon.png",
"environment": "client",
"entrypoints": {
"client": [ "net.asodev.islandutils.IslandutilsClient" ],
Expand All @@ -23,7 +23,7 @@
"custom": {
"modmenu": {
"links": {
"islandutils.website": "https://modrinth.com/mod/island-utils"
"website": "https://modrinth.com/mod/island-utils"
}
}
},
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86/discord_game_sdk.dll
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86/discord_game_sdk.dll.lib
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86_64/discord_game_sdk.bundle
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86_64/discord_game_sdk.dll
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86_64/discord_game_sdk.dll.lib
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86_64/discord_game_sdk.dylib
Binary file not shown.
Binary file modified src/main/resources/native/lib/x86_64/discord_game_sdk.so
Binary file not shown.

0 comments on commit 2daff33

Please sign in to comment.