Skip to content

Commit

Permalink
Fix incompatibility with Night Vision Flash Be Gone
Browse files Browse the repository at this point in the history
This was caused by the origins night vision effect compatibility code.

Fixes IrisShaders#1115
  • Loading branch information
coderbot16 committed Dec 21, 2021
1 parent f615e21 commit 812954b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
13 changes: 0 additions & 13 deletions src/main/java/net/coderbot/iris/mixin/MixinGameRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import net.coderbot.iris.Iris;
import net.coderbot.iris.pipeline.FixedFunctionWorldRenderingPipeline;

import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -24,7 +22,6 @@
import net.minecraft.client.renderer.RenderBuffers;
import net.minecraft.client.renderer.MultiBufferSource.BufferSource;
import net.minecraft.server.packs.resources.ResourceManager;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(GameRenderer.class)
@Environment(EnvType.CLIENT)
Expand All @@ -50,14 +47,4 @@ private void disableVanillaHandRendering(ItemInHandRenderer itemInHandRenderer,
itemInHandRenderer.renderHandsWithItems(tickDelta, poseStack, bufferSource, localPlayer, light);
}

// Origins compatibility: Allows us to call getNightVisionScale even if the entity does not have night vision.
// This injection gives a chance for mods injecting at HEAD to return a modified night vision value.
@Inject(method = "getNightVisionScale", at = @At(value = "INVOKE",
target = "Lnet/minecraft/world/effect/MobEffectInstance;getDuration()I"), cancellable = true)
private static void iris$safecheckNightvisionStrength(LivingEntity livingEntity, float partialTicks,
CallbackInfoReturnable<Float> cir){
if (livingEntity.getEffect(MobEffects.NIGHT_VISION) == null) {
cir.setReturnValue(0.0f);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.coderbot.iris.mixin;

import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
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;

// Priority of 1010 (> 1000) to run after other mod mixins. In particular, Night Vision Flash Be Gone overwrites this
// method, so we need to run after it so that our injection silently fails instead of crashing the game.
@Mixin(value = GameRenderer.class, priority = 1010)
public class MixinGameRenderer_NightVisionCompat {
// Origins compatibility: Allows us to call getNightVisionScale even if the entity does not have night vision.
// This injection gives a chance for mods injecting at HEAD to return a modified night vision value.
//
// It's optional because of Night Vision Flash Be Gone overwriting this method, but having this injection
// succeed avoids a lot of spurious (but silently caught) NullPointerExceptions.
@Inject(method = "getNightVisionScale", at = @At(value = "INVOKE",
target = "Lnet/minecraft/world/effect/MobEffectInstance;getDuration()I"), cancellable = true,
require = 0)
private static void iris$safecheckNightvisionStrength(LivingEntity livingEntity, float partialTicks,
CallbackInfoReturnable<Float> cir){
if (livingEntity.getEffect(MobEffects.NIGHT_VISION) == null) {
cir.setReturnValue(0.0f);
}
}
}
29 changes: 18 additions & 11 deletions src/main/java/net/coderbot/iris/uniforms/CommonUniforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,24 @@ private static float getNightVision() {
if (cameraEntity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) cameraEntity;

// See MixinGameRenderer#iris$safecheckNightvisionStrength.
//
// We modify the behavior of getNightVisionScale so that it's safe for us to call it even on entities that
// don't have the effect, allowing us to pick up modified night vision strength values from mods like Origins.
//
// See: https://github.com/apace100/apoli/blob/320b0ef547fbbf703de7154f60909d30366f6500/src/main/java/io/github/apace100/apoli/mixin/GameRendererMixin.java#L153
float nightVisionStrength =
GameRenderer.getNightVisionScale(livingEntity, CapturedRenderingState.INSTANCE.getTickDelta());

if (nightVisionStrength > 0) {
return nightVisionStrength;
try {
// See MixinGameRenderer#iris$safecheckNightvisionStrength.
//
// We modify the behavior of getNightVisionScale so that it's safe for us to call it even on entities
// that don't have the effect, allowing us to pick up modified night vision strength values from mods
// like Origins.
//
// See: https://github.com/apace100/apoli/blob/320b0ef547fbbf703de7154f60909d30366f6500/src/main/java/io/github/apace100/apoli/mixin/GameRendererMixin.java#L153
float nightVisionStrength =
GameRenderer.getNightVisionScale(livingEntity, CapturedRenderingState.INSTANCE.getTickDelta());

if (nightVisionStrength > 0) {
return nightVisionStrength;
}
} catch (NullPointerException e) {
// If our injection didn't get applied, a NullPointerException will occur from calling that method if
// the entity doesn't currently have night vision. This isn't pretty but it's functional.
return 0.0F;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.iris.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"MixinEntityRenderDispatcher",
"MixinFogRenderer",
"MixinGameRenderer",
"MixinGameRenderer_NightVisionCompat",
"MixinScreenEffectRenderer",
"MixinGlStateManager",
"MixinGlStateManager_AmdCrashFix",
Expand Down

0 comments on commit 812954b

Please sign in to comment.