Advertisement
jayhillx

clientlifeevents

Jan 8th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package com.xlife.client;
  2.  
  3. import com.mojang.blaze3d.systems.RenderSystem;
  4. import com.xlife.XLife;
  5. import net.minecraft.Util;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.gui.Gui;
  8. import net.minecraft.client.gui.GuiGraphics;
  9. import net.minecraft.resources.ResourceLocation;
  10. import net.minecraft.world.entity.ai.attributes.Attributes;
  11. import net.minecraft.world.entity.player.Player;
  12. import net.minecraftforge.api.distmarker.Dist;
  13. import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
  14. import net.minecraftforge.client.event.RenderGuiOverlayEvent;
  15. import net.minecraftforge.client.gui.overlay.ForgeGui;
  16. import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
  17. import net.minecraftforge.eventbus.api.SubscribeEvent;
  18. import net.minecraftforge.fml.common.Mod;
  19.  
  20. import java.util.HashMap;
  21. import java.util.Map;
  22.  
  23. @Mod.EventBusSubscriber(modid = XLife.modId, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
  24. public class ClientLifeEvents {
  25.     private static final Map<Integer, ResourceLocation> HEART_COLORS = Util.make(new HashMap<>(), textures -> {
  26.         textures.put(1, new ResourceLocation("textures/gui/icons.png"));
  27.         textures.put(2, XLife.modLoc("textures/gui/hearts/blue.png"));
  28.         textures.put(3, XLife.modLoc("textures/gui/hearts/green.png"));
  29.         textures.put(4, XLife.modLoc("textures/gui/hearts/orange.png"));
  30.         textures.put(5, XLife.modLoc("textures/gui/hearts/pink.png"));
  31.         textures.put(6, XLife.modLoc("textures/gui/hearts/purple.png"));
  32.         textures.put(7, XLife.modLoc("textures/gui/hearts/yellow.png"));
  33.         textures.put(8, XLife.modLoc("textures/gui/hearts/cyan.png"));
  34.         textures.put(9, XLife.modLoc("textures/gui/hearts/lime.png"));
  35.         textures.put(10, XLife.modLoc("textures/gui/hearts/black.png"));
  36.     });
  37.  
  38.     @SubscribeEvent(receiveCanceled = true)
  39.     public static void handleRenderHearts(RenderGuiOverlayEvent.Pre event) {
  40.         Minecraft mc = Minecraft.getInstance();
  41.         int health = Math.round(mc.player.getMaxHealth());
  42.  
  43.         if (!mc.gameMode.canHurtPlayer()) return;
  44.  
  45.         if (event.getOverlay() == VanillaGuiOverlay.PLAYER_HEALTH.type()) {
  46.             event.setCanceled(true);
  47.             mc.getProfiler().push("renderHealth");
  48.             RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
  49.             RenderSystem.setShaderTexture(0, HEART_COLORS.get(health > 20.0F ? 1 : health / 2));
  50.             mc.gui.renderPlayerHealth(event.getGuiGraphics());
  51.             RenderSystem.disableBlend();
  52.             mc.getProfiler().pop();
  53.         }
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement