Skip to content

Commit

Permalink
Make it work at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Dec 6, 2023
1 parent f5f0d3c commit 7a2b9e8
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ public class WorldHost
private static int delayIndex = 0;
private static Future<Void> connectingFuture;

public static boolean shareWorldOnLoadUi;
public static boolean shareWorldOnLoadReal;
public static boolean shareWorldOnLoad;

//#if FABRIC
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public class WorldHostComponents {
public static final Component FRIENDS = Components.translatable("world-host.friends");
public static final Component SERVERS = Components.translatable("world-host.servers");
public static final Component PLAY_TEXT = Component.translatable("world-host.play_world");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.gaming32.worldhost.ext;

public interface SelectWorldScreenExt {
boolean wh$shareButtonPressed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ private void serverIsClosed(boolean waitForServer, CallbackInfo ci) {

@Inject(method = "setUUID", at = @At("TAIL"))
private void shareWorldOnLoad(UUID uuid, CallbackInfo ci) {
if (!WorldHost.shareWorldOnLoadReal) return;
WorldHost.shareWorldOnLoadReal = false;
if (!WorldHost.shareWorldOnLoad) return;
WorldHost.shareWorldOnLoad = false;
final Component message;
if (publishServer(worldData.getGameType(), worldData.getAllowCommands(), HttpUtil.getAvailablePort())) {
message = wh$getOpenedMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.gaming32.worldhost.mixin;

import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.WorldHostComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.storage.LevelSummary;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LevelSummary.class)
public class MixinLevelSummary {
//#if MC >= 1.20.3
@Inject(method = "primaryActionMessage", at = @At("HEAD"), cancellable = true)
private void shareButton(CallbackInfoReturnable<Component> cir) {
if (WorldHost.CONFIG.isShareButton()) {
cir.setReturnValue(WorldHostComponents.PLAY_TEXT);
}
}
//#endif
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
package io.github.gaming32.worldhost.mixin;

import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.WorldHostComponents;
import io.github.gaming32.worldhost.ext.SelectWorldScreenExt;
import io.github.gaming32.worldhost.gui.screen.WorldHostScreen;
import io.github.gaming32.worldhost.versions.Components;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.worldselection.SelectWorldScreen;
import net.minecraft.client.gui.screens.worldselection.WorldSelectionList;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.storage.LevelSummary;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(SelectWorldScreen.class)
public class MixinSelectWorldScreen extends Screen {
public class MixinSelectWorldScreen extends Screen implements SelectWorldScreenExt {
@Shadow private WorldSelectionList list;

@Unique
private Button wh$shareButton;

@Unique
private boolean wh$shareButtonPressed;

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

@ModifyConstant(method = "init()V", constant = @Constant(stringValue = "selectWorld.select"))
private String changePlayButtonText(String constant) {
return WorldHost.CONFIG.isShareButton() ? "world-host.play_world" : constant;
//#if MC >= 1.20.3
@ModifyArg(
method = "init()V",
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;",
ordinal = 0
)
)
private Component changePlayButtonText(Component original) {
return WorldHost.CONFIG.isShareButton() ? WorldHostComponents.PLAY_TEXT : original;
}
//#else
//$$ @ModifyConstant(method = "init()V", constant = @Constant(stringValue = "selectWorld.select"))
//$$ private String changePlayButtonText(String constant) {
//$$ return WorldHost.CONFIG.isShareButton() ? "world-host.play_world" : constant;
//$$ }
//#endif

@ModifyConstant(method = "init()V", constant = @Constant(intValue = 150, ordinal = 0))
private int shrinkPlayButton(int constant) {
Expand Down Expand Up @@ -62,8 +79,10 @@ private void addShareWorldButton(CallbackInfo ci) {
//$$ wh$shareButton = addButton(
//#endif
WorldHostScreen.button(Components.translatable("world-host.share_world"), b -> {
WorldHost.shareWorldOnLoadUi = true;
list.getSelectedOpt().ifPresent(WorldSelectionList.WorldListEntry::joinWorld);
list.getSelectedOpt().ifPresent(worldListEntry -> {
wh$shareButtonPressed = true;
worldListEntry.joinWorld();
});
}).pos(width / 2 - 50, height - 52)
.width(100)
.build()
Expand All @@ -85,16 +104,43 @@ private String changeCreateButtonText(String constant) {
return WorldHost.CONFIG.isShareButton() ? "world-host.create_world" : constant;
}

//#if MC >= 1.20.3
@ModifyArg(
method = "updateButtonStatus",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/components/Button;setMessage(Lnet/minecraft/network/chat/Component;)V",
ordinal = 0
)
)
private Component changePlayButtonTextOnUpdate(Component original) {
return WorldHost.CONFIG.isShareButton() ? WorldHostComponents.PLAY_TEXT : original;
}
//#endif

@Inject(method = "updateButtonStatus", at = @At("TAIL"))
private void updateShareButtonStatus(
boolean active,
//#if MC > 1.19.2
boolean bl2,
//#endif
CallbackInfo ci
) {
//#if MC >= 1.20.3
private void updateShareButtonStatus(LevelSummary levelSummary, CallbackInfo ci) {
if (wh$shareButton != null) {
wh$shareButton.active = active;
wh$shareButton.active = levelSummary != null && levelSummary.primaryActionActive();
}
}
//#else
//$$ private void updateShareButtonStatus(
//$$ boolean active,
//$$ //#if MC > 1.19.2
//$$ boolean bl2,
//$$ //#endif
//$$ CallbackInfo ci
//$$ ) {
//$$ if (wh$shareButton != null) {
//$$ wh$shareButton.active = active;
//$$ }
//$$ }
//#endif

@Override
public boolean wh$shareButtonPressed() {
return wh$shareButtonPressed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.platform.InputConstants;
import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.ext.SelectWorldScreenExt;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.worldselection.WorldSelectionList;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -19,26 +20,25 @@
public class MixinWorldSelectionList_WorldListEntry {
@Shadow @Final private Minecraft minecraft;

@Shadow @Final WorldSelectionList this$0;

@Inject(method = "joinWorld", at = @At("HEAD"))
private void shareWorldOnShift(CallbackInfo ci) {
if (
!WorldHost.CONFIG.isShareButton() ||
WorldHost.shareWorldOnLoadUi ||
!InputConstants.isKeyDown(
if (WorldHost.CONFIG.isShareButton()) {
if (((SelectWorldScreenExt)this$0.getScreen()).wh$shareButtonPressed()) {
WorldHost.shareWorldOnLoad = true;
} else {
WorldHost.shareWorldOnLoad = InputConstants.isKeyDown(
minecraft.getWindow().getWindow(),
//#if MC > 1.16.5
InputConstants.KEY_LSHIFT
//#else
//$$ GLFW.GLFW_KEY_LEFT_SHIFT
//#endif
)
) return;
WorldHost.shareWorldOnLoadUi = true;
}

@Inject(method = "loadWorld", at = @At("HEAD"))
private void setupShareWorld(CallbackInfo ci) {
WorldHost.shareWorldOnLoadReal = WorldHost.shareWorldOnLoadUi;
WorldHost.shareWorldOnLoadUi = false;
);
}
} else {
WorldHost.shareWorldOnLoad = false;
}
}
}
5 changes: 3 additions & 2 deletions src/main/resources/world-host.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compatibilityLevel": "$java_version",
"mixins": [
"MixinCommands",
"MixinLevelSummary",
"MixinPublishCommand",
"ServerConnectionListenerAccessor"
],
Expand All @@ -23,9 +24,9 @@
"MixinTitleScreen",
"MixinWorldSelectionList_WorldListEntry",
"PlainTextButtonAccessor",
"ServerStatusPingerAccessor"
"ServerStatusPingerAccessor",
//#if FABRIC && MC > 1.16.1
, "modmenu.MixinModMenuEventHandler"
"modmenu.MixinModMenuEventHandler"
//#endif
],
"injectors": {
Expand Down
5 changes: 4 additions & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ dependencies {
}
}

modRuntimeOnly("me.djtheredstoner:DevAuth-${if (isFabric) "fabric" else "forge-latest"}:1.1.2")
// TODO: Remove this if when DevAuth gets Neo support on Maven
if (!isNeoForge) {
modRuntimeOnly("me.djtheredstoner:DevAuth-${if (isFabric) "fabric" else "forge-latest"}:1.1.2")
}

if (isFabric) {
when (mcVersion) {
Expand Down

0 comments on commit 7a2b9e8

Please sign in to comment.