Skip to content

Commit

Permalink
refine yacl rendering & behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
LucunJi committed Jun 23, 2024
1 parent 3bd6eb9 commit 0af3627
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/main/java/github/io/lucunji/explayerenderer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,10 +38,10 @@ public class PlayerHUDRenderer {
new DataBackupEntry<LivingEntity, Boolean>(Entity::isInSneakingPose, (e, flag) -> {
if (e instanceof ClientPlayerEntity) ((ClientPlayerEntityAccessor) e).setInSneakingPose(flag);
}),
new DataBackupEntry<LivingEntity, Float>(e -> ((LivingEntityMixin) e).getLeaningPitch(), (e, pitch) -> ((LivingEntityMixin) e).setLeaningPitch(pitch)),
new DataBackupEntry<LivingEntity, Float>(e -> ((LivingEntityMixin) e).getLastLeaningPitch(), (e, pitch) -> ((LivingEntityMixin) e).setLastLeaningPitch(pitch)),
new DataBackupEntry<LivingEntity, Float>(e -> ((LivingEntityAccessor) e).getLeaningPitch(), (e, pitch) -> ((LivingEntityAccessor) e).setLeaningPitch(pitch)),
new DataBackupEntry<LivingEntity, Float>(e -> ((LivingEntityAccessor) e).getLastLeaningPitch(), (e, pitch) -> ((LivingEntityAccessor) e).setLastLeaningPitch(pitch)),
new DataBackupEntry<LivingEntity, Boolean>(LivingEntity::isFallFlying, (e, flag) -> ((EntityMixin) e).callSetFlag(7, flag)),
new DataBackupEntry<LivingEntity, Integer>(LivingEntity::getFallFlyingTicks, (e, ticks) -> ((LivingEntityMixin) e).setFallFlyingTicks(ticks)),
new DataBackupEntry<LivingEntity, Integer>(LivingEntity::getFallFlyingTicks, (e, ticks) -> ((LivingEntityAccessor) e).setFallFlyingTicks(ticks)),

new DataBackupEntry<LivingEntity, Entity>(LivingEntity::getVehicle, (e, vehicle) -> ((EntityMixin) e).setVehicle(vehicle)),

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -21,7 +20,4 @@ public interface LivingEntityMixin {

@Accessor
void setFallFlyingTicks(int fallFlyingTicks);

@Invoker
float callGetScaleFactor();
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
5 changes: 4 additions & 1 deletion src/main/resources/explayerenderer.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"ClientPlayerEntityAccessor",
"EntityMixin",
"InGameHudMixin",
"LivingEntityMixin"
"LivingEntityAccessor",
"yacl.YACLScreenMixin",
"yacl.AbstractWidgetMixin",
"yacl.ControllerWidgetAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 0af3627

Please sign in to comment.