From 1bf31b7bcd40397ee348eaf8d1e1a609f7bf1de4 Mon Sep 17 00:00:00 2001 From: Markil3 <75867393+Markil3@users.noreply.github.com> Date: Mon, 1 Mar 2021 17:18:41 -0700 Subject: [PATCH] Makes potion icons fade over time --- .../markil3/immersive_hud/TimerUtils.java | 79 +++++++++++++++++ .../markil3/immersive_hud/mixin/HudHook.java | 84 ++++++++++++++++++- 2 files changed, 159 insertions(+), 4 deletions(-) diff --git a/src/main/java/markil3/immersive_hud/TimerUtils.java b/src/main/java/markil3/immersive_hud/TimerUtils.java index d71669f..302975f 100644 --- a/src/main/java/markil3/immersive_hud/TimerUtils.java +++ b/src/main/java/markil3/immersive_hud/TimerUtils.java @@ -23,6 +23,8 @@ import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.effect.StatusEffect; +import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BowItem; @@ -41,7 +43,13 @@ import net.minecraft.item.TridentItem; import net.minecraft.util.Hand; import net.minecraft.util.hit.EntityHitResult; import net.minecraft.util.hit.HitResult; +import net.minecraft.util.math.MathHelper; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Optional; @@ -92,6 +100,8 @@ public class TimerUtils */ private static int mapLock; + private static HashMap effectTime = new HashMap<>(); + /** * How many more ticks the hotbar will be onscreen. Every tick is 1/20th of * a second. @@ -427,6 +437,75 @@ public class TimerUtils return true; } + /** + * Updates the timings of some of the potions currently affecting the + * player. + * + * @param player - The player being affected. + */ + public static void updatePotions(ClientPlayerEntity player) + { + for (StatusEffect effect : + new HashSet<>(effectTime.keySet())) + { + if (!player.hasStatusEffect(effect)) + { + effectTime.remove(effect); + } + } + } + + /** + * Updates the timings of some of the potions currently affecting the + * player. + * + * @param effectinstance - The effect to update + */ + public static boolean updatePotion(StatusEffectInstance effectinstance) + { + Integer time = effectTime.get(effectinstance.getEffectType()); + if (time == null) + { + effectTime.put(effectinstance.getEffectType(), + (time = VISUAL_TIME)); + } + else + { + effectTime.put(effectinstance.getEffectType(), (time -= 1)); + } + return Main.getAlpha(time) > 0; + } + + /** + * Obtains how transparent + * + * @param effectinstance + * + * @return + */ + public static float getPotionAlpha(StatusEffectInstance effectinstance) + { + final int BLINK_TIME = 200; + float effectAlpha = 0.0F; + if (effectinstance.getDuration() <= BLINK_TIME) + { + effectAlpha = + MathHelper.sin(7000F / (effectinstance.getDuration() + 16F * (float) Math.PI)) * 50F / (effectinstance + .getDuration() + 100F) + 0.5F; + } + else if (effectinstance.getDuration() <= BLINK_TIME + 10) + { + effectAlpha = -(effectinstance.getDuration() - 200) / 22F + 0.454F; + } + else + { + effectAlpha = + Main.getAlpha(effectTime.getOrDefault(effectinstance.getEffectType(), + 0)); + } + return effectAlpha; + } + /** * Determines whether or not to draw the armor bar, adjusting the alpha as * needed. diff --git a/src/main/java/markil3/immersive_hud/mixin/HudHook.java b/src/main/java/markil3/immersive_hud/mixin/HudHook.java index 32808f7..1839305 100644 --- a/src/main/java/markil3/immersive_hud/mixin/HudHook.java +++ b/src/main/java/markil3/immersive_hud/mixin/HudHook.java @@ -16,12 +16,17 @@ */ package markil3.immersive_hud.mixin; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.hud.InGameHud; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.effect.StatusEffectInstance; 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.ModifyVariable; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import markil3.immersive_hud.TimerUtils; @@ -36,6 +41,8 @@ import markil3.immersive_hud.TimerUtils; @Mixin(InGameHud.class) public class HudHook { + private StatusEffectInstance currentEffect; + /** * Determines whether or not to draw the crosshair. * @@ -83,8 +90,7 @@ public class HudHook * @since 0.1-1.16.4-fabric */ @Inject(method = "renderMountJumpBar", at = @At(value = "HEAD"), - cancellable = - true) + cancellable = true) public void startJumpbar(MatrixStack stack, int x, CallbackInfo callbackInfo) @@ -166,8 +172,7 @@ public class HudHook * @since 0.1-1.16.4-fabric */ @Inject(method = "renderMountHealth", at = @At(value = "HEAD"), - cancellable = - true) + cancellable = true) public void startMountHealth(MatrixStack stack, CallbackInfo callbackInfo) { if (TimerUtils.drawMountHealth()) @@ -194,6 +199,77 @@ public class HudHook TimerUtils.resetAlpha(); } + /** + * Makes the actual adjustment to the hotbar as needed. + * + * @version 0.2-1.16.4-fabric + * @since 0.1-1.16.4-fabric + */ + @Inject(method = "renderStatusEffectOverlay" + + "(Lnet/minecraft/client/util/math/MatrixStack;)V", at = + @At(value = "HEAD", shift = At.Shift.AFTER)) + public void startPotion(MatrixStack effect, CallbackInfo info) + { + TimerUtils.updatePotions(MinecraftClient.getInstance().cameraEntity instanceof ClientPlayerEntity ? + ((ClientPlayerEntity) MinecraftClient.getInstance().cameraEntity) : + null); + } + + /** + * Makes the actual adjustment to the hotbar as needed. + * + * @version 0.2-1.16.4-fabric + * @since 0.1-1.16.4-fabric + */ + @Redirect(method = "renderStatusEffectOverlay" + + "(Lnet/minecraft/client/util/math/MatrixStack;)V", at = + @At(value = "INVOKE", + target = + "Lnet/minecraft/entity/effect/StatusEffectInstance;" + + "shouldShowIcon()Z")) + public boolean shouldRenderPotion(StatusEffectInstance effect) + { + currentEffect = effect; + return TimerUtils.updatePotion(effect); + } + + /** + * Makes the actual adjustment to the hotbar as needed. + * + * @param matrices - The matrix drawing stack. + * @param callbackInfo + * + * @version 0.2-1.16.4-fabric + * @since 0.1-1.16.4-fabric + */ + @ModifyVariable(method = "renderStatusEffectOverlay" + + "(Lnet/minecraft/client/util/math/MatrixStack;)V", at = + @At(value = "STORE"), ordinal = 1) + public float updatePotion(float f, + MatrixStack stack) + { + return TimerUtils.getPotionAlpha(currentEffect); + } + + /** + * Resets any changes from drawing the potion effects. + * + * @param matrices - The matrix drawing stack. + * @param callbackInfo + * + * @version 0.2-1.16.4-fabric + * @since 0.2-1.16.4-fabric + */ + @Inject(method = "renderStatusEffectOverlay", at = @At(value = "TAIL")) + public void finishPotion(MatrixStack matrices, + CallbackInfo callbackInfo) + { + /* + * Resets the color so that nothing else is bothered. + */ + TimerUtils.resetAlpha(); + } + /** * Determines whether or not to draw the hotbar. *