From 8fffdf110f800162c370f1d4bc2e68196d72362b Mon Sep 17 00:00:00 2001 From: Markil3 <75867393+Markil3@users.noreply.github.com> Date: Sat, 27 Feb 2021 11:00:20 -0700 Subject: [PATCH] Makes potion icons fade over time. --- .../java/markil3/immersive_hud/EventBus.java | 18 +++ .../markil3/immersive_hud/RenderUtils.java | 130 ++++++++++++++++-- .../markil3/immersive_hud/TimerUtils.java | 66 +++++++++ 3 files changed, 202 insertions(+), 12 deletions(-) diff --git a/src/main/java/markil3/immersive_hud/EventBus.java b/src/main/java/markil3/immersive_hud/EventBus.java index c9e89c3..7d23460 100644 --- a/src/main/java/markil3/immersive_hud/EventBus.java +++ b/src/main/java/markil3/immersive_hud/EventBus.java @@ -140,6 +140,24 @@ public class EventBus resetAlpha(); } break; + case POTION_ICONS: + if (event instanceof RenderGameOverlayEvent.Pre) + { + TimerUtils.updatePotions(mc.player); + event.setCanceled(true); + RenderUtils.renderPotionIcons(mc, + mc.ingameGUI, + event.getMatrixStack()); + resetAlpha(); + } + /* + * Reset the transparency. + */ + else if (event instanceof RenderGameOverlayEvent.Post) + { + resetAlpha(); + } + break; case HOTBAR: if (event instanceof RenderGameOverlayEvent.Pre) { diff --git a/src/main/java/markil3/immersive_hud/RenderUtils.java b/src/main/java/markil3/immersive_hud/RenderUtils.java index 03520f5..1f41ab9 100644 --- a/src/main/java/markil3/immersive_hud/RenderUtils.java +++ b/src/main/java/markil3/immersive_hud/RenderUtils.java @@ -1,32 +1,36 @@ /** * Copyright (C) 2009-2021 Mojang Studios - * - * Players are free to create, build, and mod within Minecraft if the - * branding usage guidelines are followed. Fan art, logos, videos, and screen - * shots are acceptable as long as they are not used to falsely represent - * Mojang Minecraft assets. If you intend to use any portion of the name in - * relation to services, products, or distribution, you must adhere to the - * following requirements. - * - * Mojang's terms of service and brand guidelines are designed to let you - * know in plain terms what you can and cannot do with your game, the Mojang - * brand or Mojang assets. - * https://account.mojang.com/documents/minecraft_eula + *

+ * Players are free to create, build, and mod within Minecraft if the branding + * usage guidelines are followed. Fan art, logos, videos, and screen shots are + * acceptable as long as they are not used to falsely represent Mojang Minecraft + * assets. If you intend to use any portion of the name in relation to + * services, products, or distribution, you must adhere to the following + * requirements. + *

+ * Mojang's terms of service and brand guidelines are designed to let you know + * in plain terms what you can and cannot do with your game, the Mojang brand or + * Mojang assets. https://account.mojang.com/documents/minecraft_eula */ package markil3.immersive_hud; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.AbstractGui; import net.minecraft.client.gui.IngameGui; +import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.model.IBakedModel; import net.minecraft.client.renderer.model.ItemCameraTransforms; import net.minecraft.client.renderer.texture.AtlasTexture; import net.minecraft.client.renderer.texture.OverlayTexture; +import net.minecraft.client.renderer.texture.PotionSpriteUploader; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.settings.AttackIndicatorStatus; import net.minecraft.crash.CrashReport; @@ -35,10 +39,16 @@ import net.minecraft.crash.ReportedException; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.potion.Effect; +import net.minecraft.potion.EffectInstance; import net.minecraft.util.HandSide; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; +import java.util.Collection; +import java.util.List; + /** * Contains methods for rendering various HUD elements. Most of these methods * come from {@link IngameGui} and @@ -50,6 +60,102 @@ import net.minecraft.world.World; */ public class RenderUtils { + public static void renderPotionIcons(Minecraft mc, + IngameGui gui, + MatrixStack matrixStack) + { + int scaledWidth = mc.getMainWindow().getScaledWidth(); + int scaledHeight = mc.getMainWindow().getScaledHeight(); + + Collection collection = + mc.player.getActivePotionEffects(); + if (!collection.isEmpty()) + { + RenderSystem.enableBlend(); + int i = 0; + int j = 0; + PotionSpriteUploader potionspriteuploader = + mc.getPotionSpriteUploader(); + List list = + Lists.newArrayListWithExpectedSize(collection.size()); + mc.getTextureManager() + .bindTexture(ContainerScreen.INVENTORY_BACKGROUND); + + for (EffectInstance effectinstance : Ordering.natural() + .reverse() + .sortedCopy(collection)) + { + Effect effect = effectinstance.getPotion(); + float effectAlpha = TimerUtils.getPotionAlpha(mc.player, effectinstance); + if (!effectinstance.shouldRenderHUD() || effectAlpha < 0.01F) + { + continue; + } + // Rebind in case previous renderHUDEffect changed texture + mc.getTextureManager() + .bindTexture(ContainerScreen.INVENTORY_BACKGROUND); + if (effectinstance.isShowIcon()) + { + int k = scaledWidth; + int l = 1; + if (mc.isDemo()) + { + l += 15; + } + + if (effect.isBeneficial()) + { + ++i; + k = k - 25 * i; + } + else + { + ++j; + k = k - 25 * j; + l += 26; + } + + RenderSystem.color4f(1.0F, 1.0F, 1.0F, effectAlpha); + if (effectinstance.isAmbient()) + { + gui.blit(matrixStack, k, l, 165, 166, 24, 24); + } + else + { + gui.blit(matrixStack, k, l, 141, 166, 24, 24); + } + + TextureAtlasSprite textureatlassprite = + potionspriteuploader.getSprite(effect); + int j1 = k; + int k1 = l; + float f1 = effectAlpha; + list.add(() -> { + mc.getTextureManager() + .bindTexture(textureatlassprite.getAtlasTexture() + .getTextureLocation()); + RenderSystem.color4f(1.0F, 1.0F, 1.0F, f1); + gui.blit(matrixStack, + j1 + 3, + k1 + 3, + gui.getBlitOffset(), + 18, + 18, + textureatlassprite); + }); + effectinstance.renderHUDEffect(gui, + matrixStack, + k, + l, + gui.getBlitOffset(), + effectAlpha); + } + } + + list.forEach(Runnable::run); + } + } + /** * Renders the horse jump bar. * diff --git a/src/main/java/markil3/immersive_hud/TimerUtils.java b/src/main/java/markil3/immersive_hud/TimerUtils.java index 7d11774..7f76d2e 100644 --- a/src/main/java/markil3/immersive_hud/TimerUtils.java +++ b/src/main/java/markil3/immersive_hud/TimerUtils.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.item.BowItem; @@ -19,10 +20,16 @@ import net.minecraft.item.ShootableItem; import net.minecraft.item.SnowballItem; import net.minecraft.item.ThrowablePotionItem; import net.minecraft.item.TridentItem; +import net.minecraft.potion.Effect; +import net.minecraft.potion.EffectInstance; import net.minecraft.potion.Effects; import net.minecraft.util.Hand; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.Optional; public class TimerUtils @@ -66,6 +73,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. @@ -310,6 +319,63 @@ public class TimerUtils return canceled; } + /** + * 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 (Effect effect : Collections.checkedSet(effectTime.keySet(), + Effect.class)) + { + if (!player.isPotionActive(effect)) + { + effectTime.remove(effect); + } + } + } + + /** + * Obtains how transparent + * + * @param player + * @param effectinstance + * + * @return + */ + public static float getPotionAlpha(ClientPlayerEntity player, + EffectInstance effectinstance) + { + final int BLINK_TIME = 200; + float effectAlpha = 0.0F; + Integer time = effectTime.get(effectinstance.getPotion()); + if (time == null) + { + effectTime.put(effectinstance.getPotion(), (time = VISUAL_TIME)); + } + else + { + effectTime.put(effectinstance.getPotion(), (time -= 1)); + } + 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(time); + } + return effectAlpha; + } + /** * Updates several fields relating to what items are being held by the * player.