From 914a50efa5c500348366154750ab8c3483a657e3 Mon Sep 17 00:00:00 2001 From: "Erymanthus[#5074] | (u/)RayDeeUx" <51521765+RayDeeUx@users.noreply.github.com> Date: Tue, 12 Dec 2023 10:18:29 -0500 Subject: [PATCH] Feature: Held Item Lore (#22) Co-authored-by: Wyvest --- .../mixins/GuiIngameAccessor.java | 11 ++ .../polyfrost/evergreenhud/EvergreenHUD.kt | 1 + .../evergreenhud/hud/HeldItemLore.kt | 177 ++++++++++++++++++ .../evergreenhud/utils/ItemStackUtils.kt | 15 ++ src/main/resources/mixins.evergreenhud.json | 1 + 5 files changed, 205 insertions(+) create mode 100644 src/main/java/org/polyfrost/evergreenhud/mixins/GuiIngameAccessor.java create mode 100644 src/main/kotlin/org/polyfrost/evergreenhud/hud/HeldItemLore.kt create mode 100644 src/main/kotlin/org/polyfrost/evergreenhud/utils/ItemStackUtils.kt diff --git a/src/main/java/org/polyfrost/evergreenhud/mixins/GuiIngameAccessor.java b/src/main/java/org/polyfrost/evergreenhud/mixins/GuiIngameAccessor.java new file mode 100644 index 0000000..7740c27 --- /dev/null +++ b/src/main/java/org/polyfrost/evergreenhud/mixins/GuiIngameAccessor.java @@ -0,0 +1,11 @@ +package org.polyfrost.evergreenhud.mixins; + +import net.minecraft.client.gui.GuiIngame; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(GuiIngame.class) +public interface GuiIngameAccessor { + @Accessor() + int getRemainingHighlightTicks(); +} diff --git a/src/main/kotlin/org/polyfrost/evergreenhud/EvergreenHUD.kt b/src/main/kotlin/org/polyfrost/evergreenhud/EvergreenHUD.kt index 1cfc3e8..81192b9 100644 --- a/src/main/kotlin/org/polyfrost/evergreenhud/EvergreenHUD.kt +++ b/src/main/kotlin/org/polyfrost/evergreenhud/EvergreenHUD.kt @@ -30,6 +30,7 @@ class EvergreenHUD { GameMode() GameType() HeightLimit() + HeldItemLore() InGameTime() Inventory() Map() diff --git a/src/main/kotlin/org/polyfrost/evergreenhud/hud/HeldItemLore.kt b/src/main/kotlin/org/polyfrost/evergreenhud/hud/HeldItemLore.kt new file mode 100644 index 0000000..6ede486 --- /dev/null +++ b/src/main/kotlin/org/polyfrost/evergreenhud/hud/HeldItemLore.kt @@ -0,0 +1,177 @@ +package org.polyfrost.evergreenhud.hud + +import cc.polyfrost.oneconfig.config.Config +import cc.polyfrost.oneconfig.config.annotations.* +import cc.polyfrost.oneconfig.config.annotations.Number +import cc.polyfrost.oneconfig.config.core.OneColor +import cc.polyfrost.oneconfig.config.data.Mod +import cc.polyfrost.oneconfig.config.data.ModType +import cc.polyfrost.oneconfig.hud.TextHud +import cc.polyfrost.oneconfig.libs.universal.UGraphics +import cc.polyfrost.oneconfig.libs.universal.UMinecraft +import cc.polyfrost.oneconfig.renderer.NanoVGHelper +import cc.polyfrost.oneconfig.renderer.TextRenderer +import cc.polyfrost.oneconfig.utils.color.ColorUtils +import cc.polyfrost.oneconfig.utils.dsl.mc +import org.polyfrost.evergreenhud.mixins.GuiIngameAccessor +import org.polyfrost.evergreenhud.utils.ItemStackUtils.getLore +import kotlin.math.min + +class HeldItemLore : Config(Mod("Held Item Lore", ModType.HUD), "evergreenhud/helditemlore.json", false) { + @HUD(name = "Main") + var hud = HeldItemLoreHud() + + init { + initialize() + } + + class HeldItemLoreHud : TextHud(true, 50, 50) { + + @Switch(name = "Fade Out", description = "If disabled, the held item lore HUD will persist indefinitely.") + var fadeOut: Boolean = false + + @Switch(name = "Remove Empty Lines") + var removeEmptyLines: Boolean = false + + @Switch(name = "Skip Item Name") + var skipItemName: Boolean = false + + @Number(name = "Stop After Line", min = 0F, max = 100F, description = "The HUD will stop rendering lore lines after this amount. Leave at 0 to render all lines in an item lore.\nThis setting won't count the item's display name as a line.") + var stopAfterLine: Int = 0 + + // @Number(name = "Extra Seconds", min = 0F, max = 60F, description = "The number of extra seconds the tooltip will remain on screen before fading out.") + // var extraSeconds: Int = 0 + // above line is my attempt at extending the time a tooltip had before fading out -ery + + @Exclude private var opacity = 0 + + @Exclude private val TOOLTIP: MutableList = mutableListOf( + "§bExample Item Lore §7(Left click and hold to drag this!)", + "", + "§7If you can read this, you're spending", + "§cWAY §7too much time in §2Minecraft§7.", + "", + "§7You need to §bget out of your house", + "§7and earn some §7§osocial points.", + "", + "§c§lGet out there §r§7and live your life.", + "§7Make friends. Go on a hike. §7§nDo both at once!", + ) + + override fun drawLine(line: String?, x: Float, y: Float, scale: Float) { + val color = OneColor( + ColorUtils.setAlpha( + this.color.rgb, + min(this.color.alpha.toDouble(), opacity.toDouble()).toInt() + ) or (this.opacity shl 24) + ) + UGraphics.enableBlend() + TextRenderer.drawScaledString(line, x, y, color.rgb, TextRenderer.TextType.toType(textType), scale) + UGraphics.disableBlend() + } + + override fun drawBackground(x: Float, y: Float, width: Float, height: Float, scale: Float) { + val nanoVGHelper = NanoVGHelper.INSTANCE + nanoVGHelper.setupAndDraw(true) { vg: Long -> + val bgColor = + ColorUtils.setAlpha( + bgColor.rgb, + min(bgColor.alpha.toDouble(), opacity.toDouble()) + .toInt() + ) + val borderColor = + ColorUtils.setAlpha( + borderColor.rgb, + min( + borderColor.alpha.toDouble(), + opacity.toDouble() + ).toInt() + ) + if (rounded) { + nanoVGHelper.drawRoundedRect(vg, x, y, width, height, bgColor, cornerRadius * scale) + if (border) nanoVGHelper.drawHollowRoundRect( + vg, + x - borderSize * scale, + y - borderSize * scale, + width + borderSize * scale, + height + borderSize * scale, + borderColor, + cornerRadius * scale, + borderSize * scale + ) + } else { + nanoVGHelper.drawRect(vg, x, y, width, height, bgColor) + if (border) nanoVGHelper.drawHollowRoundRect( + vg, + x - borderSize * scale, + y - borderSize * scale, + width + borderSize * scale, + height + borderSize * scale, + borderColor, + 0f, + borderSize * scale + ) + } + } + } + + override fun shouldShow(): Boolean { + val inGameGUI: GuiIngameAccessor = UMinecraft.getMinecraft().ingameGUI as GuiIngameAccessor + + // val ticksPerSecond = 20 + // val extraTicks = (extraSeconds * ticksPerSecond) + val remainingTicks = inGameGUI.getRemainingHighlightTicks() // + extraTicks + var o: Int = + if (fadeOut) (remainingTicks * 256 / 10) + else 255 + if (o > 255) o = 255 + opacity = o + return o > 0 && super.shouldShow() + } + + override fun getLines(lines: MutableList, example: Boolean) { + if ( + //#if MC>=11202 + //$$ mc.player + //#else + mc.thePlayer + //#endif + == null) { + lines.add("Unknown") + return + } + + if (example) { + lines.clear() + addTextToHUD(TOOLTIP, lines) + return + } + + if (!shouldShow()) return + + val theHeldItem = + //#if MC>=11202 + //$$ mc.player.heldItemMainhand + //#else + mc.thePlayer.heldItem + //#endif + val itemName = theHeldItem.displayName + val itemLore = theHeldItem.getLore() + if (itemName.isNotEmpty() && !skipItemName) lines.add("§r$itemName§r") // §r to avoid coloring either the inventory or enderchest huds + if (itemLore.isNotEmpty()) { + addTextToHUD(itemLore, lines) + } + } + + private fun addTextToHUD(theListToAdd: List, lines: MutableList) { + var index = 0 + for (line in theListToAdd) { + if (line.isEmpty() && removeEmptyLines) continue + lines.add(line) + index++ + if (stopAfterLine in 1..index) break + } + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/polyfrost/evergreenhud/utils/ItemStackUtils.kt b/src/main/kotlin/org/polyfrost/evergreenhud/utils/ItemStackUtils.kt new file mode 100644 index 0000000..4a52967 --- /dev/null +++ b/src/main/kotlin/org/polyfrost/evergreenhud/utils/ItemStackUtils.kt @@ -0,0 +1,15 @@ +package org.polyfrost.evergreenhud.utils + +import net.minecraft.item.ItemStack + +object ItemStackUtils { + fun ItemStack.getLore(): List { + var list: MutableList = ArrayList() + val theTagCompound = this.tagCompound ?: return list + val theTagList = theTagCompound.getCompoundTag("display").getTagList("Lore", 8) + for (i in 0..(theTagList.tagCount() - 1)) { + list.add(theTagList.getStringTagAt(i)) + } + return list + } +} \ No newline at end of file diff --git a/src/main/resources/mixins.evergreenhud.json b/src/main/resources/mixins.evergreenhud.json index 9a4e0e3..2d84e9e 100644 --- a/src/main/resources/mixins.evergreenhud.json +++ b/src/main/resources/mixins.evergreenhud.json @@ -10,6 +10,7 @@ "client": [ "EntityPlayerMixin", "ForgeHooksMixin", + "GuiIngameAccessor", "LevelheadAboveHeadRenderMixin", "MainMixin", "RendererLivingEntityMixin"