-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Set Highlighting use lore rather than ItemAbility data
Showing
2 changed files
with
72 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 42 additions & 1 deletion
43
src/main/java/net/oxyopia/vice/features/itemabilities/SetHighlighting.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,59 @@ | ||
package net.oxyopia.vice.features.itemabilities | ||
|
||
import gg.essential.elementa.utils.withAlpha | ||
import net.fabricmc.fabric.api.item.v1.EnchantingContext | ||
import net.minecraft.enchantment.Enchantments | ||
import net.minecraft.item.ItemStack | ||
import net.oxyopia.vice.Vice | ||
import net.oxyopia.vice.data.Set | ||
import net.oxyopia.vice.events.ContainerRenderSlotEvent | ||
import net.oxyopia.vice.events.core.SubscribeEvent | ||
import net.oxyopia.vice.utils.HudUtils.highlight | ||
import net.oxyopia.vice.utils.ItemUtils.cleanName | ||
import net.oxyopia.vice.utils.ItemUtils.getLore | ||
import net.oxyopia.vice.utils.ItemUtils.isPlayerHeadWithArmor | ||
|
||
object SetHighlighting { | ||
private val cache = hashMapOf<String, Set?>() | ||
private val loreRegex by lazy { | ||
Regex("♦ Set: (.+)") | ||
} | ||
|
||
@SubscribeEvent | ||
fun onInventorySlotRender(event: ContainerRenderSlotEvent) { | ||
if (!Vice.config.INVENTORY_SET_COLORS) return | ||
val item = event.slot.stack | ||
|
||
if (!Vice.config.INCLUDE_ARMOR_IN_SET_COLORS && | ||
(item.canBeEnchantedWith(Enchantments.PROTECTION, EnchantingContext.ANVIL) || item.isPlayerHeadWithArmor()) | ||
) return | ||
|
||
val set = ItemAbility.getByName(event.slot.stack.cleanName())?.set ?: return | ||
val set = event.slot.stack.getSet(event) ?: return | ||
event.highlight(set.color.withAlpha(Vice.config.INVENTORY_SET_COLORS_OPACITY)) | ||
} | ||
|
||
private fun ItemStack.getSet(event: ContainerRenderSlotEvent): Set? { | ||
val name = cleanName() | ||
|
||
return cache[name] ?: run { | ||
val lore = getLore() | ||
if (lore.isEmpty()) return null | ||
|
||
lore.forEach { | ||
loreRegex.find(it)?.apply { | ||
val matchedSet = Set.getByName(groupValues[1]) | ||
|
||
cache[name] = matchedSet | ||
return@run matchedSet | ||
} | ||
} | ||
|
||
if (event.shouldCacheEmpty()) cache[name] = null | ||
return null | ||
} | ||
} | ||
|
||
private fun ContainerRenderSlotEvent.shouldCacheEmpty(): Boolean { | ||
return chestName.contains("Crafting") || chestName.contains("Backpack") | ||
} | ||
} |