From 0af3627f1dcb583b80ed451c5ca4d125db85a5aa Mon Sep 17 00:00:00 2001 From: lucunji Date: Sun, 23 Jun 2024 04:33:49 -0400 Subject: [PATCH] refine yacl rendering & behavior --- .../io/lucunji/explayerenderer/Main.java | 5 +- .../client/render/PlayerHUDRenderer.java | 14 ++-- ...tyMixin.java => LivingEntityAccessor.java} | 6 +- .../mixin/yacl/AbstractWidgetMixin.java | 23 ++++++ .../mixin/yacl/ControllerWidgetAccessor.java | 12 +++ .../mixin/yacl/YACLScreenMixin.java | 79 +++++++++++++++++++ .../resources/explayerenderer.mixins.json | 5 +- 7 files changed, 127 insertions(+), 17 deletions(-) rename src/main/java/github/io/lucunji/explayerenderer/mixin/{LivingEntityMixin.java => LivingEntityAccessor.java} (79%) create mode 100644 src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/AbstractWidgetMixin.java create mode 100644 src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/ControllerWidgetAccessor.java create mode 100644 src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/YACLScreenMixin.java diff --git a/src/main/java/github/io/lucunji/explayerenderer/Main.java b/src/main/java/github/io/lucunji/explayerenderer/Main.java index e364cfe..b44dd9f 100644 --- a/src/main/java/github/io/lucunji/explayerenderer/Main.java +++ b/src/main/java/github/io/lucunji/explayerenderer/Main.java @@ -22,10 +22,7 @@ public void onInitializeClient() { "key." + MOD_ID + ".category")); ClientTickEvents.END_CLIENT_TICK.register(client -> { while (configKey.wasPressed()) { - if (Configs.isConfigScreen(client.currentScreen)) - client.currentScreen.close(); - else - client.setScreen(Configs.HANDLER.generateGui().generateScreen(client.currentScreen)); + client.setScreen(Configs.HANDLER.generateGui().generateScreen(client.currentScreen)); } }); } diff --git a/src/main/java/github/io/lucunji/explayerenderer/client/render/PlayerHUDRenderer.java b/src/main/java/github/io/lucunji/explayerenderer/client/render/PlayerHUDRenderer.java index da544c3..329e1e9 100644 --- a/src/main/java/github/io/lucunji/explayerenderer/client/render/PlayerHUDRenderer.java +++ b/src/main/java/github/io/lucunji/explayerenderer/client/render/PlayerHUDRenderer.java @@ -7,7 +7,7 @@ import github.io.lucunji.explayerenderer.config.PoseOffsetMethod; import github.io.lucunji.explayerenderer.mixin.ClientPlayerEntityAccessor; import github.io.lucunji.explayerenderer.mixin.EntityMixin; -import github.io.lucunji.explayerenderer.mixin.LivingEntityMixin; +import github.io.lucunji.explayerenderer.mixin.LivingEntityAccessor; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.InventoryScreen; import net.minecraft.client.network.ClientPlayerEntity; @@ -38,10 +38,10 @@ public class PlayerHUDRenderer { new DataBackupEntry(Entity::isInSneakingPose, (e, flag) -> { if (e instanceof ClientPlayerEntity) ((ClientPlayerEntityAccessor) e).setInSneakingPose(flag); }), - new DataBackupEntry(e -> ((LivingEntityMixin) e).getLeaningPitch(), (e, pitch) -> ((LivingEntityMixin) e).setLeaningPitch(pitch)), - new DataBackupEntry(e -> ((LivingEntityMixin) e).getLastLeaningPitch(), (e, pitch) -> ((LivingEntityMixin) e).setLastLeaningPitch(pitch)), + new DataBackupEntry(e -> ((LivingEntityAccessor) e).getLeaningPitch(), (e, pitch) -> ((LivingEntityAccessor) e).setLeaningPitch(pitch)), + new DataBackupEntry(e -> ((LivingEntityAccessor) e).getLastLeaningPitch(), (e, pitch) -> ((LivingEntityAccessor) e).setLastLeaningPitch(pitch)), new DataBackupEntry(LivingEntity::isFallFlying, (e, flag) -> ((EntityMixin) e).callSetFlag(7, flag)), - new DataBackupEntry(LivingEntity::getFallFlyingTicks, (e, ticks) -> ((LivingEntityMixin) e).setFallFlyingTicks(ticks)), + new DataBackupEntry(LivingEntity::getFallFlyingTicks, (e, ticks) -> ((LivingEntityAccessor) e).setFallFlyingTicks(ticks)), new DataBackupEntry(LivingEntity::getVehicle, (e, vehicle) -> ((EntityMixin) e).setVehicle(vehicle)), @@ -169,11 +169,11 @@ private void transformEntity(LivingEntity targetEntity, float partialTicks, bool } ((EntityMixin) targetEntity).setVehicle(null); - ((LivingEntityMixin) targetEntity).setLeaningPitch(0); - ((LivingEntityMixin) targetEntity).setLastLeaningPitch(0); + ((LivingEntityAccessor) targetEntity).setLeaningPitch(0); + ((LivingEntityAccessor) targetEntity).setLastLeaningPitch(0); ((EntityMixin) targetEntity).callSetFlag(7, false); - ((LivingEntityMixin) targetEntity).setFallFlyingTicks(0); + ((LivingEntityAccessor) targetEntity).setFallFlyingTicks(0); } // FIXME: NEVERFIX - glitch when the mouse moves too fast, caused by lerping a warped value, it is possibly wrapped in LivingEntity#tick or LivingEntity#turnHead diff --git a/src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityMixin.java b/src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityAccessor.java similarity index 79% rename from src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityMixin.java rename to src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityAccessor.java index 78721a2..cda9efd 100644 --- a/src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityMixin.java +++ b/src/main/java/github/io/lucunji/explayerenderer/mixin/LivingEntityAccessor.java @@ -3,10 +3,9 @@ import net.minecraft.entity.LivingEntity; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; -import org.spongepowered.asm.mixin.gen.Invoker; @Mixin(LivingEntity.class) -public interface LivingEntityMixin { +public interface LivingEntityAccessor { @Accessor float getLeaningPitch(); @@ -21,7 +20,4 @@ public interface LivingEntityMixin { @Accessor void setFallFlyingTicks(int fallFlyingTicks); - - @Invoker - float callGetScaleFactor(); } diff --git a/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/AbstractWidgetMixin.java b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/AbstractWidgetMixin.java new file mode 100644 index 0000000..29c9ea4 --- /dev/null +++ b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/AbstractWidgetMixin.java @@ -0,0 +1,23 @@ +package github.io.lucunji.explayerenderer.mixin.yacl; + +import dev.isxander.yacl3.gui.AbstractWidget; +import dev.isxander.yacl3.gui.controllers.ControllerWidget; +import github.io.lucunji.explayerenderer.config.Configs; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Element; +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(AbstractWidget.class) +public class AbstractWidgetMixin { + @Inject(method = "drawButtonRect", at = @At(value = "HEAD"), cancellable = true) + public void skipDrawingButtonBackground(DrawContext graphics, int x1, int y1, int x2, int y2, boolean hovered, boolean enabled, CallbackInfo ci) { + //noinspection ConstantValue + if (((Element) this) instanceof ControllerWidget controllerWidget + && Configs.isConfigScreen(((ControllerWidgetAccessor) controllerWidget).getScreen())) { + ci.cancel(); + } + } +} diff --git a/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/ControllerWidgetAccessor.java b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/ControllerWidgetAccessor.java new file mode 100644 index 0000000..768727c --- /dev/null +++ b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/ControllerWidgetAccessor.java @@ -0,0 +1,12 @@ +package github.io.lucunji.explayerenderer.mixin.yacl; + +import dev.isxander.yacl3.gui.YACLScreen; +import dev.isxander.yacl3.gui.controllers.ControllerWidget; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(ControllerWidget.class) +public interface ControllerWidgetAccessor { + @Accessor + YACLScreen getScreen(); +} \ No newline at end of file diff --git a/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/YACLScreenMixin.java b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/YACLScreenMixin.java new file mode 100644 index 0000000..9014c28 --- /dev/null +++ b/src/main/java/github/io/lucunji/explayerenderer/mixin/yacl/YACLScreenMixin.java @@ -0,0 +1,79 @@ +package github.io.lucunji.explayerenderer.mixin.yacl; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.YetAnotherConfigLib; +import dev.isxander.yacl3.api.utils.OptionUtils; +import dev.isxander.yacl3.gui.YACLScreen; +import dev.isxander.yacl3.impl.utils.YACLConstants; +import github.io.lucunji.explayerenderer.config.Configs; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.tab.TabManager; +import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +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.callback.CallbackInfo; + +@Mixin(YACLScreen.class) +public abstract class YACLScreenMixin extends Screen { + @Shadow + public abstract boolean pendingChanges(); + + @Shadow + @Final + public YetAnotherConfigLib config; + + @Shadow + @Final + public TabManager tabManager; + + @Shadow private boolean pendingChanges; + + protected YACLScreenMixin(Text title) { + super(title); + } + + @Inject(method = "renderBackground", at = @At("HEAD"), cancellable = true) + public void renderBackground(DrawContext guiGraphics, int mouseX, int mouseY, float partialTick, CallbackInfo ci) { + if (!Configs.isConfigScreen(this)) return; + + renderDarkening(guiGraphics); + ci.cancel(); + } + + /** + * Instantly apply changes, without saving to file or triggering any flag. + * Force {@link #pendingChanges} to {@code true} + */ + @Inject(method = "onOptionChanged", at = @At("RETURN"), remap = false) + public void applyOnOptionChanged(Option opt, CallbackInfo ci) { + if (!Configs.isConfigScreen(this)) return; + + pendingChanges = true; + + OptionUtils.forEachOptions(config, Option::applyValue); + OptionUtils.forEachOptions(config, option -> { + if (option.changed()) { + option.forgetPendingValue(); + YACLConstants.LOGGER.error("Option '{}' value mismatch after applying! Reset to binding's getter.", option.name().getString()); + } + }); + + if (tabManager.getCurrentTab() instanceof YACLScreen.CategoryTab categoryTab) { + categoryTab.updateButtons(); + categoryTab.undoButton.active = false; + } + } + + @Inject(method = "cancelOrReset", at = @At("HEAD"), remap = false) + public void reloadOnCancelled(CallbackInfo ci) { + if (Configs.isConfigScreen(this)) return; + + if (pendingChanges()) { + Configs.HANDLER.load(); + } + } +} diff --git a/src/main/resources/explayerenderer.mixins.json b/src/main/resources/explayerenderer.mixins.json index 0c05e70..d45d613 100644 --- a/src/main/resources/explayerenderer.mixins.json +++ b/src/main/resources/explayerenderer.mixins.json @@ -8,7 +8,10 @@ "ClientPlayerEntityAccessor", "EntityMixin", "InGameHudMixin", - "LivingEntityMixin" + "LivingEntityAccessor", + "yacl.YACLScreenMixin", + "yacl.AbstractWidgetMixin", + "yacl.ControllerWidgetAccessor" ], "injectors": { "defaultRequire": 1