|
| 1 | +package dev.ryanhcode.sable.mixin.compatibility.shouldersurfing; |
| 2 | + |
| 3 | +import com.github.exopandora.shouldersurfing.api.client.IClientConfig; |
| 4 | +import com.github.exopandora.shouldersurfing.api.model.CrosshairVisibility; |
| 5 | +import com.github.exopandora.shouldersurfing.api.model.Perspective; |
| 6 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 7 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 8 | +import com.llamalad7.mixinextras.lib.apache.commons.ArrayUtils; |
| 9 | +import com.llamalad7.mixinextras.sugar.Local; |
| 10 | +import dev.ryanhcode.sable.mixinhelpers.camera.new_camera_types.SableCameraTypes; |
| 11 | +import dev.ryanhcode.sable.mixinhelpers.compatibility.shouldersurfing.SablePerspectives; |
| 12 | +import net.minecraft.client.CameraType; |
| 13 | +import org.spongepowered.asm.mixin.Final; |
| 14 | +import org.spongepowered.asm.mixin.Mixin; |
| 15 | +import org.spongepowered.asm.mixin.Mutable; |
| 16 | +import org.spongepowered.asm.mixin.Shadow; |
| 17 | +import org.spongepowered.asm.mixin.gen.Invoker; |
| 18 | +import org.spongepowered.asm.mixin.injection.At; |
| 19 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 20 | +import org.spongepowered.asm.mixin.injection.ModifyVariable; |
| 21 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 22 | + |
| 23 | +@Mixin(Perspective.class) |
| 24 | +public class PerspectiveMixin { |
| 25 | + |
| 26 | + @Shadow |
| 27 | + @Final |
| 28 | + @Mutable |
| 29 | + private static Perspective[] $VALUES; |
| 30 | + |
| 31 | + static { |
| 32 | + final var subLevelView = create("SUB_LEVEL_VIEW", $VALUES.length, SableCameraTypes.SUB_LEVEL_VIEW, CrosshairVisibility.NEVER); |
| 33 | + |
| 34 | + $VALUES = ArrayUtils.add($VALUES, subLevelView); |
| 35 | + |
| 36 | + final var subLevelViewUnlocked = create("SUB_LEVEL_VIEW_UNLOCKED", $VALUES.length, SableCameraTypes.SUB_LEVEL_VIEW_UNLOCKED, CrosshairVisibility.NEVER); |
| 37 | + |
| 38 | + $VALUES = ArrayUtils.add($VALUES, subLevelViewUnlocked); |
| 39 | + } |
| 40 | + |
| 41 | + @SuppressWarnings("SameParameterValue") |
| 42 | + @Invoker(value = "<init>") |
| 43 | + private static Perspective create(final String name, final int ordinal, final CameraType cameraType, final CrosshairVisibility defaultCrosshairVisibility) { |
| 44 | + throw new AssertionError("Unreachable"); |
| 45 | + } |
| 46 | + |
| 47 | + @SuppressWarnings("ConstantValue") |
| 48 | + @WrapOperation(method = "next", at = @At(value = "INVOKE", target = "Lcom/github/exopandora/shouldersurfing/api/client/IClientConfig;replaceDefaultPerspective()Z")) |
| 49 | + public boolean nextPerspective(final IClientConfig instance, final Operation<Boolean> original) { |
| 50 | + if ((Object) this == SablePerspectives.SUB_LEVEL_VIEW || (Object) this == SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + return original.call(instance); |
| 54 | + } |
| 55 | + |
| 56 | + @ModifyVariable(method = "next", at = @At(value = "STORE"), name = "next") |
| 57 | + public Perspective next(final Perspective next, @Local(argsOnly = true) final IClientConfig config) { |
| 58 | + if (config.replaceDefaultPerspective()) { |
| 59 | + if ((Object) this == Perspective.SHOULDER_SURFING) { |
| 60 | + return SablePerspectives.SUB_LEVEL_VIEW; |
| 61 | + } |
| 62 | + } else { |
| 63 | + // The normal logic will try to wrap around to our new values, but the next one should be first person |
| 64 | + if ((Object) this == Perspective.SHOULDER_SURFING) { |
| 65 | + return Perspective.FIRST_PERSON; |
| 66 | + } |
| 67 | + |
| 68 | + if ((Object) this == Perspective.THIRD_PERSON_BACK) { |
| 69 | + return SablePerspectives.SUB_LEVEL_VIEW; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ((Object) this == SablePerspectives.SUB_LEVEL_VIEW) { |
| 74 | + return SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED; |
| 75 | + } |
| 76 | + if ((Object) this == SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED) { |
| 77 | + return Perspective.THIRD_PERSON_FRONT; |
| 78 | + } |
| 79 | + |
| 80 | + return next; |
| 81 | + } |
| 82 | + |
| 83 | + @Inject(method = "next", at = @At("TAIL"), cancellable = true) |
| 84 | + public void getNext(final CallbackInfoReturnable<Perspective> cir, @Local(name = "next") final Perspective next) { |
| 85 | + if (next == SablePerspectives.SUB_LEVEL_VIEW) { |
| 86 | + cir.setReturnValue(SablePerspectives.SUB_LEVEL_VIEW); |
| 87 | + } |
| 88 | + if (next == SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED) { |
| 89 | + cir.setReturnValue(SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings("ConstantValue") |
| 94 | + @Inject(method = "isEnabled", at = @At("HEAD"), cancellable = true) |
| 95 | + public void isEnabled(final CallbackInfoReturnable<Boolean> cir) { |
| 96 | + if ((Object) this == SablePerspectives.SUB_LEVEL_VIEW || (Object) this == SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED) { |
| 97 | + cir.setReturnValue(true); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Inject(method = "of", at = @At("HEAD"), cancellable = true) |
| 102 | + private static void of(final CameraType cameraType, final boolean shoulderSurfing, final CallbackInfoReturnable<Perspective> cir) { |
| 103 | + if (cameraType == SableCameraTypes.SUB_LEVEL_VIEW) { |
| 104 | + cir.setReturnValue(SablePerspectives.SUB_LEVEL_VIEW); |
| 105 | + } |
| 106 | + if (cameraType == SableCameraTypes.SUB_LEVEL_VIEW_UNLOCKED) { |
| 107 | + cir.setReturnValue(SablePerspectives.SUB_LEVEL_VIEW_UNLOCKED); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments