Skip to content

Commit

Permalink
Backend: Improves performance of item backgrounds and borders (#1497)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Co-authored-by: Cal <cwolfson58@gmail.com>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent 0eed4b0 commit 44b86cc
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 123 deletions.
2 changes: 0 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import at.hannibal2.skyhanni.data.HighlightOnHoverSlot
import at.hannibal2.skyhanni.data.HypixelData
import at.hannibal2.skyhanni.data.ItemAddManager
import at.hannibal2.skyhanni.data.ItemClickData
import at.hannibal2.skyhanni.data.ItemRenderBackground
import at.hannibal2.skyhanni.data.ItemTipHelper
import at.hannibal2.skyhanni.data.LocationFixData
import at.hannibal2.skyhanni.data.MaxwellAPI
Expand Down Expand Up @@ -478,7 +477,6 @@ class SkyHanniMod {
loadModule(ScoreboardData())
loadModule(SeaCreatureFeatures())
loadModule(SeaCreatureManager())
loadModule(ItemRenderBackground())
loadModule(EntityData())
loadModule(MobData())
loadModule(MobDetection())
Expand Down
67 changes: 0 additions & 67 deletions src/main/java/at/hannibal2/skyhanni/data/ItemRenderBackground.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package at.hannibal2.skyhanni.events

import net.minecraft.item.ItemStack

class RenderRealOverlayEvent(
class RenderGuiItemOverlayEvent(
val stack: ItemStack?,
val x: Int,
val y: Int,
) : LorenzEvent()
) : LorenzEvent()
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package at.hannibal2.skyhanni.features.garden.visitor

import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.background
import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.borderLine
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.visitor.VisitorAPI.ACCEPT_SLOT
Expand All @@ -13,8 +11,10 @@ import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.RenderUtils.drawBorder
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.item.ItemStack
import net.minecraft.inventory.Slot
import net.minecraftforge.event.entity.player.ItemTooltipEvent
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand All @@ -23,26 +23,29 @@ class VisitorRewardWarning {
private val config get() = VisitorAPI.config.rewardWarning

@SubscribeEvent
fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) {
fun onBackgroundDrawn(event: GuiContainerEvent.ForegroundDrawnEvent) {
if (!VisitorAPI.inInventory) return
if (!config.preventRefusing && !config.preventRefusingCopper && !config.preventAcceptingCopper) return

val visitor = VisitorAPI.getVisitor(lastClickedNpc) ?: return
val refuseOfferStack = event.gui.inventorySlots.getSlot(REFUSE_SLOT).stack
val acceptOfferStack = event.gui.inventorySlots.getSlot(ACCEPT_SLOT).stack
val refuseOfferSlot = event.gui.inventorySlots.getSlot(REFUSE_SLOT)
val acceptOfferSlot = event.gui.inventorySlots.getSlot(ACCEPT_SLOT)
val blockReason = visitor.blockReason ?: return

if (blockReason.blockRefusing) {
renderColor(refuseOfferStack, acceptOfferStack, LorenzColor.GREEN)
renderColor(refuseOfferSlot, acceptOfferSlot, LorenzColor.GREEN)
} else {
renderColor(acceptOfferStack, refuseOfferStack, LorenzColor.RED)
renderColor(acceptOfferSlot, refuseOfferSlot, LorenzColor.RED)
}
}

private fun renderColor(backgroundStack: ItemStack?, outlineStack: ItemStack?, outlineColor: LorenzColor) {
if (!config.bypassKey.isKeyHeld()) backgroundStack?.background =
LorenzColor.DARK_GRAY.addOpacity(config.opacity).rgb
if (config.optionOutline) outlineStack?.borderLine = outlineColor.addOpacity(200).rgb
private fun renderColor(backgroundSlot: Slot?, outlineSlot: Slot?, outlineColor: LorenzColor) {
if (!config.bypassKey.isKeyHeld() && backgroundSlot != null) {
backgroundSlot highlight LorenzColor.DARK_GRAY.addOpacity(config.opacity)
}
if (config.optionOutline && outlineSlot != null) {
outlineSlot drawBorder outlineColor.addOpacity(200)
}
}

@SubscribeEvent(priority = EventPriority.HIGH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.background
import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.borderLine
import at.hannibal2.skyhanni.data.jsonobjects.repo.HideNotClickableItemsJson
import at.hannibal2.skyhanni.data.jsonobjects.repo.HideNotClickableItemsJson.SalvageFilter
import at.hannibal2.skyhanni.events.GuiContainerEvent
Expand Down Expand Up @@ -33,6 +31,8 @@ import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.MultiFilter
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.RenderUtils.drawBorder
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isMuseumDonated
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRiftExportable
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRiftTransferable
Expand Down Expand Up @@ -93,7 +93,7 @@ class HideNotClickableItems {
}

@SubscribeEvent
fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) {
fun onBackgroundDrawn(event: GuiContainerEvent.ForegroundDrawnEvent) {
if (!LorenzUtils.inSkyBlock) return
if (isDisabled()) return
if (bypasssActive()) return
Expand All @@ -102,14 +102,11 @@ class HideNotClickableItems {
val chest = guiChest.inventorySlots as ContainerChest
val chestName = chest.getInventoryName()

for ((_, stack) in chest.getLowerItems()) {
for ((slot, stack) in chest.getLowerItems()) {
if (hide(chestName, stack)) {
val opacity = config.opacity
val color = LorenzColor.DARK_GRAY.addOpacity(opacity)
stack.background = color.rgb
slot highlight LorenzColor.DARK_GRAY.addOpacity(config.opacity)
} else if (showGreenLine && config.itemsGreenLine) {
val color = LorenzColor.GREEN.addOpacity(200)
stack.borderLine = color.rgb
slot drawBorder LorenzColor.GREEN.addOpacity(200)
}
}
}
Expand Down Expand Up @@ -386,7 +383,6 @@ class HideNotClickableItems {
if (!chestName.startsWith("Sack of Sacks")) return false
if (ItemUtils.isSkyBlockMenuItem(stack)) return false

val name = stack.cleanName()
showGreenLine = true
if (ItemUtils.isSack(stack)) return false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.background
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
Expand All @@ -11,6 +10,7 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
Expand Down Expand Up @@ -56,7 +56,7 @@ class QuickCraftFeatures {
}

@SubscribeEvent
fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) {
fun onBackgroundDrawn(event: GuiContainerEvent.ForegroundDrawnEvent) {
val inventoryType = getInventoryType() ?: return
if (KeyboardManager.isModifierKeyDown()) return
if (event.gui !is GuiChest) return
Expand All @@ -66,8 +66,7 @@ class QuickCraftFeatures {
if (inventoryType.ignoreSlot(slot.slotNumber)) continue
if (stack.name == "§cQuick Crafting Slot") continue
if (needsQuickCraftConfirmation(stack)) {
val color = LorenzColor.DARK_GRAY.addOpacity(180)
stack.background = color.rgb
slot highlight LorenzColor.DARK_GRAY.addOpacity(180)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package at.hannibal2.skyhanni.features.itemabilities.abilitycooldown

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.ItemRenderBackground.Companion.background
import at.hannibal2.skyhanni.events.ActionBarUpdateEvent
import at.hannibal2.skyhanni.events.ItemClickEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.PlaySoundEvent
import at.hannibal2.skyhanni.events.RenderGuiItemOverlayEvent
import at.hannibal2.skyhanni.events.RenderItemTipEvent
import at.hannibal2.skyhanni.events.RenderObject
import at.hannibal2.skyhanni.features.itemabilities.abilitycooldown.ItemAbility.Companion.getMultiplier
import at.hannibal2.skyhanni.features.nether.ashfang.AshfangFreezeCooldown
import at.hannibal2.skyhanni.utils.CollectionUtils.equalsOneOf
import at.hannibal2.skyhanni.utils.CollectionUtils.mapKeysNotNull
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils
import at.hannibal2.skyhanni.utils.ItemUtils.cleanName
Expand All @@ -23,6 +24,7 @@ import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.between
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getAbilityScrolls
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getItemId
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getItemUuid
Expand All @@ -48,7 +50,7 @@ class ItemAbilityCooldown {
)

private var lastAbility = ""
private var items = mapOf<ItemStack, List<ItemText>>()
private var items = mapOf<String, List<ItemText>>()
private var abilityItems = mapOf<ItemStack, MutableList<ItemAbility>>()
private val WEIRD_TUBA = "WEIRD_TUBA".asInternalName()
private val WEIRDER_TUBA = "WEIRDER_TUBA".asInternalName()
Expand Down Expand Up @@ -254,7 +256,12 @@ class ItemAbilityCooldown {
abilityItems = ItemUtils.getItemsInInventory(true).associateWith { hasAbility(it) }
}

items = abilityItems.mapValues { kp -> kp.value.map { createItemText(it) } }
items = abilityItems.entries.associateByTo(
mutableMapOf(),
{ it.key.getIdentifier() },
{ kp -> kp.value.map { createItemText(it) } }
).mapKeysNotNull { it.key }

}

private fun createItemText(ability: ItemAbility): ItemText {
Expand Down Expand Up @@ -291,8 +298,7 @@ class ItemAbilityCooldown {

val guiOpen = Minecraft.getMinecraft().currentScreen != null
val uuid = stack.getIdentifier() ?: return
val list = items.filter { (it.key.getIdentifier()) == uuid }
.firstNotNullOfOrNull { it.value } ?: return
val list = items[uuid] ?: return

for (itemText in list) {
if (guiOpen && !itemText.onCooldown) continue
Expand All @@ -303,16 +309,31 @@ class ItemAbilityCooldown {
renderObject.offsetY = -10
}
event.renderObjects.add(renderObject)
}
}

@SubscribeEvent
fun onRenderItem(event: RenderGuiItemOverlayEvent) {
if (!isEnabled()) return
if (!config.itemAbilityCooldownBackground) return

val guiOpen = Minecraft.getMinecraft().currentScreen != null
val stack = event.stack

val uuid = stack?.getIdentifier() ?: return
val list = items[uuid] ?: return

for (itemText in list) {
if (guiOpen && !itemText.onCooldown) continue
val color = itemText.color

// fix multiple problems when having multiple abilities
if (config.itemAbilityCooldownBackground) {
var opacity = 130
if (color == LorenzColor.GREEN) {
opacity = 80
if (!config.itemAbilityShowWhenReady) return
}
stack.background = color.addOpacity(opacity).rgb
var opacity = 130
if (color == LorenzColor.GREEN) {
opacity = 80
if (!config.itemAbilityShowWhenReady) return
}
event highlight color.addOpacity(opacity)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package at.hannibal2.skyhanni.mixins.hooks

import at.hannibal2.skyhanni.events.GuiRenderItemEvent
import at.hannibal2.skyhanni.events.RenderRealOverlayEvent
import at.hannibal2.skyhanni.events.RenderGuiItemOverlayEvent
import at.hannibal2.skyhanni.test.SkyHanniDebugsAndTests
import net.minecraft.client.gui.FontRenderer
import net.minecraft.item.ItemStack
Expand All @@ -25,5 +25,5 @@ fun renderItemOverlayPost(

fun renderItemReturn(stack: ItemStack, x: Int, y: Int) {
if (!SkyHanniDebugsAndTests.globalRender) return
RenderRealOverlayEvent(stack, x, y).postAndCatch()
RenderGuiItemOverlayEvent(stack, x, y).postAndCatch()
}
11 changes: 11 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,15 @@ object CollectionUtils {
addString("§a]")
}))
}

inline fun <K, V, R : Any> Map<K, V>.mapKeysNotNull(transform: (Map.Entry<K, V>) -> R?): Map<R, V> {
val destination = LinkedHashMap<R, V>()
for (element in this) {
val newKey = transform(element)
if (newKey != null) {
destination[newKey] = element.value
}
}
return destination
}
}
Loading

0 comments on commit 44b86cc

Please sign in to comment.