Skip to content

Commit

Permalink
temporarily switch selected controller when opening player selection …
Browse files Browse the repository at this point in the history
…menu
  • Loading branch information
Jab125 committed Oct 17, 2024
1 parent 7ad3cfe commit 85e289a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.User;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;

import java.util.UUID;

Expand All @@ -21,4 +22,5 @@ public interface ILegacy4JModCompat extends IModCompat {
void connectionEstablished(HotJoinS2CThread thread, AlohaPayload payload, UUID uuid);
void receivedSdlNatives(SdlNativesPayload payload);
void renderUsername(GuiGraphics graphics);
void onBeginScreenSet(Screen previousScreen, Screen newScreen);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

public record Legacy4JData(String controllerName, int controllerIndex, int selectedControllerHandler) {
public record Legacy4JData(String controllerName, int oldControllerIndex, int controllerIndex, int selectedControllerHandler) {
public static final Codec<Legacy4JData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("controllerName").forGetter(Legacy4JData::controllerName),
Codec.INT.fieldOf("oldControllerIndex").forGetter(Legacy4JData::oldControllerIndex),
Codec.INT.fieldOf("controllerIndex").forGetter(Legacy4JData::controllerIndex),
Codec.INT.fieldOf("selectedControllerHandler").forGetter(Legacy4JData::selectedControllerHandler)
).apply(instance, Legacy4JData::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.client.User;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.LoadingOverlay;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
Expand All @@ -25,6 +26,7 @@
import wily.legacy.client.LegacyTip;
import wily.legacy.client.controller.SDLControllerHandler;
import wily.legacy.client.screen.ChooseUserScreen;
import wily.legacy.client.screen.ConfirmationScreen;
import wily.legacy.util.MCAccount;
import wily.legacy.util.ScreenUtil;

Expand Down Expand Up @@ -65,6 +67,7 @@ public static void openLegacy4JUserPicker() {
}

public static void openLegacy4JUserPicker(Legacy4JData data) {
ScreenUtil.getLegacyOptions().selectedController().set(data.controllerIndex());
ChooseUserScreen chooseUserScreen = new ChooseUserScreen(null);
((AuthCallback)chooseUserScreen).hotjoin$authResponse(s -> {
Minecraft.getInstance().getToasts().addToast(new LegacyTip(Component.literal("Success, joining world...")));
Expand Down Expand Up @@ -145,4 +148,16 @@ public void renderUsername(GuiGraphics graphics) {
graphics.drawString(Minecraft.getInstance().font, username, graphics.guiWidth() - 33 - Minecraft.getInstance().font.width(username), graphics.guiHeight() - 27, 16777215);
}
}

@Override
public void onBeginScreenSet(Screen previousScreen, Screen newScreen) {
if (previousScreen instanceof ChooseUserScreen screen) {
Legacy4JData o = (Legacy4JData) ((ChooseUserScreen & AuthCallback) screen).hotjoin$legacy4jData();
if (newScreen instanceof ConfirmationScreen) {

} else {
ScreenUtil.getLegacyOptions().selectedController().set(o.oldControllerIndex());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void interceptRun(CallbackInfo ci) {
int finalI = i;
Minecraft.getInstance().tell(() -> {
if (Minecraft.getInstance().screen != null) return;
Legacy4JModCompat.openLegacy4JUserPicker(new Legacy4JData(controller.getName(), finalI,ScreenUtil.getLegacyOptions().selectedControllerHandler().get()));
Legacy4JModCompat.openLegacy4JUserPicker(new Legacy4JData(controller.getName(), ScreenUtil.getLegacyOptions().selectedController().get(), finalI, ScreenUtil.getLegacyOptions().selectedControllerHandler().get()));
});
}
//System.out.println(controller.getName() + " is holding down the guide button!");
Expand All @@ -65,7 +65,7 @@ void interceptRun(CallbackInfo ci) {
int finalI = i;
Minecraft.getInstance().tell(() -> {
if (Minecraft.getInstance().screen != null) return;
Legacy4JModCompat.openLegacy4JUserPicker(new Legacy4JData(controller.getName(), finalI,ScreenUtil.getLegacyOptions().selectedControllerHandler().get()));
Legacy4JModCompat.openLegacy4JUserPicker(new Legacy4JData(controller.getName(), ScreenUtil.getLegacyOptions().selectedController().get(), finalI, ScreenUtil.getLegacyOptions().selectedControllerHandler().get()));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.jab125.hotjoin.mixin.legacy4j;

import dev.jab125.hotjoin.HotJoin;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
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.CallbackInfo;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Inject(method = "setScreen", at = @At("HEAD"))
void startSetScreen(Screen screen, CallbackInfo ci) {
if (HotJoin.legacy4JModCompat != null) {
HotJoin.legacy4JModCompat.onBeginScreenSet(Minecraft.getInstance().screen, screen);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/hot-join.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"authme.MicrosoftAuthScreenMixin",
"legacy4j.ChooseUserScreenMixin",
"legacy4j.LoadingOverlayMixin",
"legacy4j.MinecraftMixin",
"legacy4j.RealmsAvailabilityMixin"
],
"mixins": [
Expand Down

0 comments on commit 85e289a

Please sign in to comment.