package net.minecraft.client.gui.screens.inventory; import com.google.common.collect.Ordering; import java.util.Collection; import java.util.List; import java.util.Optional; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.resources.MobEffectTextureManager; import net.minecraft.core.Holder; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.effect.MobEffectUtil; @Environment(EnvType.CLIENT) public class EffectsInInventory { private static final ResourceLocation EFFECT_BACKGROUND_LARGE_SPRITE = ResourceLocation.withDefaultNamespace("container/inventory/effect_background_large"); private static final ResourceLocation EFFECT_BACKGROUND_SMALL_SPRITE = ResourceLocation.withDefaultNamespace("container/inventory/effect_background_small"); private final AbstractContainerScreen screen; private final Minecraft minecraft; public EffectsInInventory(AbstractContainerScreen screen) { this.screen = screen; this.minecraft = Minecraft.getInstance(); } public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) { this.renderEffects(guiGraphics, mouseX, mouseY); } public boolean canSeeEffects() { int i = this.screen.leftPos + this.screen.imageWidth + 2; int j = this.screen.width - i; return j >= 32; } private void renderEffects(GuiGraphics guiGraphics, int mouseX, int mouseY) { int i = this.screen.leftPos + this.screen.imageWidth + 2; int j = this.screen.width - i; Collection collection = this.minecraft.player.getActiveEffects(); if (!collection.isEmpty() && j >= 32) { boolean bl = j >= 120; int k = 33; if (collection.size() > 5) { k = 132 / (collection.size() - 1); } Iterable iterable = Ordering.natural().sortedCopy(collection); this.renderBackgrounds(guiGraphics, i, k, iterable, bl); this.renderIcons(guiGraphics, i, k, iterable, bl); if (bl) { this.renderLabels(guiGraphics, i, k, iterable); } else if (mouseX >= i && mouseX <= i + 33) { int l = this.screen.topPos; MobEffectInstance mobEffectInstance = null; for (MobEffectInstance mobEffectInstance2 : iterable) { if (mouseY >= l && mouseY <= l + k) { mobEffectInstance = mobEffectInstance2; } l += k; } if (mobEffectInstance != null) { List list = List.of( this.getEffectName(mobEffectInstance), MobEffectUtil.formatDuration(mobEffectInstance, 1.0F, this.minecraft.level.tickRateManager().tickrate()) ); guiGraphics.renderTooltip(this.screen.getFont(), list, Optional.empty(), mouseX, mouseY); } } } } private void renderBackgrounds(GuiGraphics guiGraphics, int x, int y, Iterable activeEffects, boolean large) { int i = this.screen.topPos; for (MobEffectInstance mobEffectInstance : activeEffects) { if (large) { guiGraphics.blitSprite(RenderType::guiTextured, EFFECT_BACKGROUND_LARGE_SPRITE, x, i, 120, 32); } else { guiGraphics.blitSprite(RenderType::guiTextured, EFFECT_BACKGROUND_SMALL_SPRITE, x, i, 32, 32); } i += y; } } private void renderIcons(GuiGraphics guiGraphics, int x, int y, Iterable activeEffects, boolean large) { MobEffectTextureManager mobEffectTextureManager = this.minecraft.getMobEffectTextures(); int i = this.screen.topPos; for (MobEffectInstance mobEffectInstance : activeEffects) { Holder holder = mobEffectInstance.getEffect(); TextureAtlasSprite textureAtlasSprite = mobEffectTextureManager.get(holder); guiGraphics.blitSprite(RenderType::guiTextured, textureAtlasSprite, x + (large ? 6 : 7), i + 7, 18, 18); i += y; } } private void renderLabels(GuiGraphics guiGraphics, int x, int y, Iterable activeEffects) { int i = this.screen.topPos; for (MobEffectInstance mobEffectInstance : activeEffects) { Component component = this.getEffectName(mobEffectInstance); guiGraphics.drawString(this.screen.getFont(), component, x + 10 + 18, i + 6, 16777215); Component component2 = MobEffectUtil.formatDuration(mobEffectInstance, 1.0F, this.minecraft.level.tickRateManager().tickrate()); guiGraphics.drawString(this.screen.getFont(), component2, x + 10 + 18, i + 6 + 10, 8355711); i += y; } } private Component getEffectName(MobEffectInstance effect) { MutableComponent mutableComponent = effect.getEffect().value().getDisplayName().copy(); if (effect.getAmplifier() >= 1 && effect.getAmplifier() <= 9) { mutableComponent.append(CommonComponents.SPACE).append(Component.translatable("enchantment.level." + (effect.getAmplifier() + 1))); } return mutableComponent; } }