Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.polyfrost.glintcolorizer.mixin;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.item.ItemStack;
import org.polyfrost.glintcolorizer.handler.PotionLetterHandler;
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.CallbackInfo;

@Mixin(RenderItem.class)
public class RenderItemMixin_PotionLetters {

@Inject(
method = "renderItemOverlayIntoGUI",
at = @At("TAIL")
)
private void glintColorizer$drawPotionLetter(FontRenderer font, ItemStack stack, int x, int y, String text, CallbackInfo ci) {
PotionLetterHandler.INSTANCE.render(font, stack, x, y);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.minecraft.item.ItemStack;
import org.polyfrost.glintcolorizer.handler.PotionLetterHandler;
import org.polyfrost.glintcolorizer.hook.RenderItemHook;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
Expand All @@ -13,7 +14,7 @@ public class GuiItemIconsMixin_Argentum {

@ModifyReturnValue(method = "canBake", at = @At("RETURN"), require = 0)
private static boolean glintColorizer$shinyPotsStayVanilla(boolean original, ItemStack item) {
return original && !RenderItemHook.INSTANCE.drawsShinyEffect(item);
return original && !RenderItemHook.INSTANCE.drawsShinyEffect(item) && !PotionLetterHandler.INSTANCE.drawsLetter(item);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.polyfrost.glintcolorizer.mixin.compat;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.minecraft.item.ItemStack;
import org.polyfrost.compose.render.RenderContext;
import org.polyfrost.glintcolorizer.handler.PotionLetterHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Pseudo
@Mixin(targets = "org.polyfrost.compose.minecraft.mc.McItemDecorations", remap = false)
public class McItemDecorationsMixin_PolyCompose {

@Inject(method = "draw", at = @At("TAIL"), require = 0)
private void glintColorizer$drawPotionLetter(RenderContext ctx, ItemStack stack, float x, float y, float size, String countOverride, CallbackInfo ci) {
PotionLetterHandler.INSTANCE.render(ctx, stack, x, y, size);
}

@ModifyReturnValue(method = "key", at = @At("RETURN"), require = 0)
private long glintColorizer$potionLetterKey(long original, ItemStack stack) {
return original * 31 + PotionLetterHandler.INSTANCE.decorationKey(stack);
}

}
37 changes: 37 additions & 0 deletions src/main/kotlin/org/polyfrost/glintcolorizer/config/GlintConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,43 @@ object GlintConfig : Config(
)
var potionBasedColor = false

@Switch(
title = "Potion Effect Letters",
description = "Draws a short code for the potion's effect on potion icons, e.g. S = Speed, ST = Strength, H = Instant Health.",
category = "Potion Letters"
)
var potionLetters = false

@Slider(
title = "Letter Scale",
min = 0.5f,
max = 1f,
step = 0.05f,
category = "Potion Letters"
)
var potionLetterScale = 0.75f

@Dropdown(
title = "Letter Corner",
options = ["Top Left", "Top Right", "Bottom Left", "Bottom Right"],
category = "Potion Letters"
)
var potionLetterCorner = 0

@Checkbox(
title = "Show Potion Level",
description = "Adds the level for level II and higher, e.g. S2 for Speed II.",
category = "Potion Letters"
)
var potionLetterLevel = false

@Checkbox(
title = "Potion Colored Letters",
description = "Uses the potion's color instead of white.",
category = "Potion Letters"
)
var potionLetterColored = false

private var rebound = false

override fun initialize(byConfigManager: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.polyfrost.glintcolorizer.handler

import net.minecraft.client.Minecraft
import net.minecraft.client.gui.FontRenderer
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.item.ItemPotion
import net.minecraft.item.ItemStack
import net.minecraft.potion.Potion
import net.minecraft.util.StatCollector
import org.polyfrost.glintcolorizer.config.GlintConfig
import org.polyfrost.compose.render.RenderContext
import org.polyfrost.glintcolorizer.hook.RenderItemHook
import java.util.Objects

object PotionLetterHandler {

private val abbreviations: Map<Int, String> = mapOf(
Potion.moveSpeed.id to "S",
Potion.moveSlowdown.id to "SL",
Potion.digSpeed.id to "HA",
Potion.digSlowdown.id to "MF",
Potion.damageBoost.id to "ST",
Potion.heal.id to "H",
Potion.harm.id to "D",
Potion.jump.id to "J",
Potion.confusion.id to "N",
Potion.regeneration.id to "R",
Potion.resistance.id to "RE",
Potion.fireResistance.id to "F",
Potion.waterBreathing.id to "WB",
Potion.invisibility.id to "I",
Potion.blindness.id to "B",
Potion.nightVision.id to "NV",
Potion.hunger.id to "HU",
Potion.weakness.id to "W",
Potion.poison.id to "P",
Potion.wither.id to "WI",
Potion.healthBoost.id to "HB",
Potion.absorption.id to "A",
Potion.saturation.id to "SA",
)

private val labels = HashMap<Int, String>()

val isEnabled: Boolean
get() = GlintConfig.enabled && GlintConfig.potionLetters

fun drawsLetter(stack: ItemStack?): Boolean = isEnabled && labelFor(stack) != null

fun labelFor(stack: ItemStack?): String? {
val item = stack?.item as? ItemPotion ?: return null
val effect = item.getEffects(stack)?.firstOrNull() ?: return null
val id = effect.potionID
val showLevel = GlintConfig.potionLetterLevel && effect.amplifier > 0
val key = (id shl 9) or ((effect.amplifier and 0xFF) shl 1) or (if (showLevel) 1 else 0)
return labels.getOrPut(key) {
val base = abbreviations[id] ?: fallbackAbbreviation(id)
if (showLevel) base + (effect.amplifier + 1) else base
}
}

private fun fallbackAbbreviation(id: Int): String {
val potion = Potion.potionTypes.getOrNull(id) ?: return "?"
return StatCollector.translateToLocal(potion.name).take(2).uppercase()
}

fun render(font: FontRenderer, stack: ItemStack?, x: Int, y: Int) {
if (!isEnabled) return
val label = labelFor(stack) ?: return
val scale = GlintConfig.potionLetterScale

GlStateManager.disableLighting()
GlStateManager.disableDepth()
GlStateManager.disableBlend()
GlStateManager.pushMatrix()
GlStateManager.translate(x + offsetX(font, label), y + offsetY(font), 0f)
GlStateManager.scale(scale, scale, 1f)
font.drawStringWithShadow(label, 0f, 0f, color(stack!!))
GlStateManager.popMatrix()
GlStateManager.color(1f, 1f, 1f, 1f)
GlStateManager.enableLighting()
GlStateManager.enableDepth()
}

fun render(ctx: RenderContext, stack: ItemStack, x: Float, y: Float, size: Float) {
if (!isEnabled) return
val label = labelFor(stack) ?: return
val font = Minecraft.getMinecraft().fontRendererObj
val unit = size / 16f
ctx.mcText(label, x + offsetX(font, label) * unit, y + offsetY(font) * unit, color(stack), true, GlintConfig.potionLetterScale * unit)
}

fun decorationKey(stack: ItemStack): Int {
if (!isEnabled) return 0
val label = labelFor(stack) ?: return 0
return Objects.hash(label, GlintConfig.potionLetterScale, GlintConfig.potionLetterCorner, color(stack))
}

private fun offsetX(font: FontRenderer, label: String): Float {
val corner = GlintConfig.potionLetterCorner
return if (corner == 1 || corner == 3) 16f - font.getStringWidth(label) * GlintConfig.potionLetterScale else 0f
}

private fun offsetY(font: FontRenderer): Float {
val scale = GlintConfig.potionLetterScale
return if (GlintConfig.potionLetterCorner >= 2) 16f - font.FONT_HEIGHT * scale + scale else 0f
}

private fun color(stack: ItemStack): Int =
if (GlintConfig.potionLetterColored) RenderItemHook.getPotionColor(stack) else 0xFFFFFFFF.toInt()
}
2 changes: 2 additions & 0 deletions src/main/resources/mixins.glintcolorizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mixins": [
"LayerArmorBaseMixin",
"RenderItemMixin_GlintCustomizer",
"RenderItemMixin_PotionLetters",
"RenderItemMixin_ShinyEffect",
"accessor.RenderItemAccessor"
],
Expand All @@ -13,6 +14,7 @@
"compat.GuiItemGlintsMixin_Argentum",
"compat.GuiItemIconsMixin_Argentum",
"compat.LegacyGlintMixin_OverflowAnimations",
"compat.McItemDecorationsMixin_PolyCompose",
"compat.McItemGlintMixin_PolyCompose",
"ItemEntityRendererMixin_HandleCameraTransforms",
"ItemRendererMixin_HandleCameraTransforms"
Expand Down