From f307d45a23a319fb48c3af8c7fbd657e07df78b4 Mon Sep 17 00:00:00 2001 From: ILike2WatchMemes Date: Sat, 23 Mar 2024 18:59:01 +0100 Subject: Feature: Plot Menu Highlighting (#1181) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 6 +- .../config/features/garden/GardenConfig.java | 5 + .../config/features/garden/PlotIconConfig.java | 2 +- .../garden/PlotMenuHighlightingConfig.java | 45 ++++++ .../garden/inventory/GardenNextPlotPrice.kt | 55 ------- .../features/garden/inventory/GardenPlotIcon.kt | 158 --------------------- .../garden/inventory/plots/GardenNextPlotPrice.kt | 55 +++++++ .../garden/inventory/plots/GardenPlotIcon.kt | 158 +++++++++++++++++++++ .../inventory/plots/GardenPlotMenuHighlighting.kt | 81 +++++++++++ 9 files changed, 349 insertions(+), 216 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotMenuHighlightingConfig.java delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenNextPlotPrice.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenPlotIcon.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenNextPlotPrice.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotIcon.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotMenuHighlighting.kt (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index c55aa910d..125f3f0a7 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -197,8 +197,9 @@ import at.hannibal2.skyhanni.features.garden.fortuneguide.CaptureFarmingGear import at.hannibal2.skyhanni.features.garden.inventory.AnitaExtraFarmingFortune import at.hannibal2.skyhanni.features.garden.inventory.GardenCropMilestoneInventory import at.hannibal2.skyhanni.features.garden.inventory.GardenInventoryNumbers -import at.hannibal2.skyhanni.features.garden.inventory.GardenNextPlotPrice -import at.hannibal2.skyhanni.features.garden.inventory.GardenPlotIcon +import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenNextPlotPrice +import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenPlotIcon +import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenPlotMenuHighlighting import at.hannibal2.skyhanni.features.garden.inventory.SkyMartCopperPrice import at.hannibal2.skyhanni.features.garden.pests.PestAPI import at.hannibal2.skyhanni.features.garden.pests.PestFinder @@ -621,6 +622,7 @@ class SkyHanniMod { loadModule(TpsCounter()) loadModule(ParticleHider()) loadModule(MiscFeatures()) + loadModule(GardenPlotMenuHighlighting()) loadModule(SkyMartCopperPrice()) loadModule(GardenVisitorFeatures()) loadModule(NPCVisitorFix) diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/garden/GardenConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/garden/GardenConfig.java index 9fa7e2bcc..9d941c095 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/garden/GardenConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/garden/GardenConfig.java @@ -119,6 +119,11 @@ public class GardenConfig { @Accordion public CropStartLocationConfig cropStartLocation = new CropStartLocationConfig(); + @Expose + @ConfigOption(name = "Plot Menu Highlighting", desc = "") + @Accordion + public PlotMenuHighlightingConfig plotMenuHighlighting = new PlotMenuHighlightingConfig(); + @Expose @ConfigOption(name = "Garden Plot Icon", desc = "") @Accordion diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotIconConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotIconConfig.java index 2801d2bf6..aca099417 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotIconConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotIconConfig.java @@ -1,7 +1,7 @@ package at.hannibal2.skyhanni.config.features.garden; import at.hannibal2.skyhanni.config.FeatureToggle; -import at.hannibal2.skyhanni.features.garden.inventory.GardenPlotIcon; +import at.hannibal2.skyhanni.features.garden.inventory.plots.GardenPlotIcon; import at.hannibal2.skyhanni.utils.ChatUtils; import com.google.gson.annotations.Expose; import io.github.moulberry.moulconfig.annotations.ConfigEditorBoolean; diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotMenuHighlightingConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotMenuHighlightingConfig.java new file mode 100644 index 000000000..acf10ec5d --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/config/features/garden/PlotMenuHighlightingConfig.java @@ -0,0 +1,45 @@ +package at.hannibal2.skyhanni.config.features.garden; + +import at.hannibal2.skyhanni.config.FeatureToggle; +import at.hannibal2.skyhanni.utils.LorenzColor; +import com.google.gson.annotations.Expose; +import io.github.moulberry.moulconfig.annotations.ConfigEditorBoolean; +import io.github.moulberry.moulconfig.annotations.ConfigEditorDraggableList; +import io.github.moulberry.moulconfig.annotations.ConfigOption; + +import java.util.ArrayList; +import java.util.List; + +public class PlotMenuHighlightingConfig { + @Expose + @ConfigOption(name = "Enabled", desc = "Highlights plots based on their status.") + @ConfigEditorBoolean + @FeatureToggle + public boolean enabled = false; + + @Expose + @ConfigOption(name = "Statuses", desc = "Change which statuses are enabled, and the hierarchy of them.") + @ConfigEditorDraggableList + public List deskPlotStatusTypes = new ArrayList<>(); + + public enum PlotStatusType { + PESTS("§cPests", LorenzColor.RED), + SPRAYS("§eSprays", LorenzColor.YELLOW), + LOCKED("§7Locked", LorenzColor.DARK_GRAY), + CURRENT("§aCurrent plot", LorenzColor.GREEN) + ; + + public final String name; + public final LorenzColor highlightColor; + + PlotStatusType(String name, LorenzColor highlightColor) { + this.name = name; + this.highlightColor = highlightColor; + } + + @Override + public String toString() { + return name; + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenNextPlotPrice.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenNextPlotPrice.kt deleted file mode 100644 index 54414d211..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenNextPlotPrice.kt +++ /dev/null @@ -1,55 +0,0 @@ -package at.hannibal2.skyhanni.features.garden.inventory - -import at.hannibal2.skyhanni.events.LorenzToolTipEvent -import at.hannibal2.skyhanni.features.garden.GardenAPI -import at.hannibal2.skyhanni.test.command.ErrorManager -import at.hannibal2.skyhanni.utils.InventoryUtils -import at.hannibal2.skyhanni.utils.ItemUtils -import at.hannibal2.skyhanni.utils.ItemUtils.name -import at.hannibal2.skyhanni.utils.NEUInternalName -import at.hannibal2.skyhanni.utils.NEUItems.getPrice -import at.hannibal2.skyhanni.utils.NumberUtil -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class GardenNextPlotPrice { - - @SubscribeEvent - fun onTooltip(event: LorenzToolTipEvent) { - if (!GardenAPI.inGarden()) return - if (!GardenAPI.config.plotPrice) return - - if (InventoryUtils.openInventoryName() != "Configure Plots") return - - if (!event.itemStack.name.startsWith("§ePlot")) return - - var next = false - val list = event.toolTip - var i = -1 - for (line in event.toolTipRemovedPrefix()) { - i++ - if (line.contains("Cost")) { - next = true - continue - } - - if (next) { - val readItemAmount = ItemUtils.readItemAmount(line) - readItemAmount?.let { - val (itemName, amount) = it - val lowestBin = NEUInternalName.fromItemName(itemName).getPrice() - val price = lowestBin * amount - val format = NumberUtil.format(price) - list[i] = list[i] + " §7(§6$format§7)" - } ?: run { - ErrorManager.logErrorStateWithData( - "Garden Next Plot Price error", - "Could not read item amount from line", - "line" to line, - "event.toolTip" to event.toolTip, - ) - } - break - } - } - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenPlotIcon.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenPlotIcon.kt deleted file mode 100644 index 6b7389079..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/GardenPlotIcon.kt +++ /dev/null @@ -1,158 +0,0 @@ -package at.hannibal2.skyhanni.features.garden.inventory - -import at.hannibal2.skyhanni.events.GuiContainerEvent -import at.hannibal2.skyhanni.events.InventoryCloseEvent -import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent -import at.hannibal2.skyhanni.events.LorenzToolTipEvent -import at.hannibal2.skyhanni.features.garden.GardenAPI -import at.hannibal2.skyhanni.utils.ChatUtils -import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName -import at.hannibal2.skyhanni.utils.ItemUtils.getLore -import at.hannibal2.skyhanni.utils.NEUItems.getItemStack -import io.github.moulberry.notenoughupdates.events.ReplaceItemEvent -import io.github.moulberry.notenoughupdates.util.Utils -import net.minecraft.client.player.inventory.ContainerLocalMenu -import net.minecraft.init.Items -import net.minecraft.item.ItemStack -import net.minecraftforge.fml.common.eventhandler.EventPriority -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -object GardenPlotIcon { - - private val config get() = GardenAPI.config.plotIcon - private val plotList get() = GardenAPI.storage?.plotIcon?.plotList - private var inInventory = false - private var copyStack: ItemStack? = null - - // TODO replace with enum - private var editMode = 0 // 0 = off, 1 = on, 2 = reset - private var lastClickedSlotId = -1 - private var originalStack = mutableMapOf() - private var cachedStack = mutableMapOf() - private val editStack = ItemStack(Items.wooden_axe) - private val whitelistedSlot = - listOf(2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 20, 21, 23, 24, 29, 30, 31, 32, 33, 38, 39, 40, 41, 42) - - var hardReset = false - - fun isEnabled() = GardenAPI.inGarden() && config.enabled && inInventory - - @SubscribeEvent(priority = EventPriority.HIGHEST) - fun onInventoryOpen(event: InventoryFullyOpenedEvent) { - inInventory = event.inventoryName == "Configure Plots" - if (!isEnabled()) return - - for ((index, stack) in event.inventoryItems) { - originalStack[index] = stack - } - val plotList = plotList ?: return - for ((index, internalName) in plotList) { - val old = originalStack[index]!! - val new = internalName.getItemStack() - cachedStack[index] = Utils.editItemStackInfo(new, old.displayName, true, *old.getLore().toTypedArray()) - } - } - - @SubscribeEvent - fun onInventoryClose(event: InventoryCloseEvent) { - inInventory = false - editMode = 0 - } - - @SubscribeEvent(priority = EventPriority.HIGH) - fun replaceItem(event: ReplaceItemEvent) { - if (!isEnabled()) return - val plotList = plotList ?: return - if (hardReset) { - plotList.clear() - hardReset = false - return - } - - if (event.inventory is ContainerLocalMenu) { - if (event.slotNumber == 53) { - event.replaceWith(editStack) - } - if (plotList.isNotEmpty() && plotList.contains(event.slotNumber)) { - if (lastClickedSlotId == event.slotNumber) { - lastClickedSlotId = -1 - return - } - event.replaceWith(cachedStack[event.slotNumber]) - } - } - } - - @SubscribeEvent(priority = EventPriority.HIGH) - fun onStackClick(event: GuiContainerEvent.SlotClickEvent) { - if (!isEnabled()) return - lastClickedSlotId = event.slotId - if (event.slotId == 53) { - event.isCanceled = true - if (event.clickedButton == 0) { - if (editMode == 2) - editMode = 0 - else - editMode++ - } else if (event.clickedButton == 1) { - if (editMode == 0) - editMode = 2 - else - editMode-- - } - return - } - if (editMode != 0) { - if (event.slotId in 54..89) { - event.isCanceled = true - copyStack = event.slot?.stack?.copy()?.also { - it.stackSize = 1 - } ?: return - // TODO different format, not bold or show not in chat at all. - ChatUtils.chat("§6§lClick an item in the desk menu to replace it with that item!") - return - } - if (event.slotId != 53) { - val plotList = plotList ?: return - if (!whitelistedSlot.contains(event.slotId)) return - event.isCanceled = true - if (editMode == 2) { - plotList.remove(event.slotId) - return - } - val copyStack = copyStack ?: return - plotList[event.slotId] = copyStack.getInternalName() - cachedStack[event.slotId] = copyStack - } - } - } - - @SubscribeEvent - fun onTooltip(event: LorenzToolTipEvent) { - if (!isEnabled()) return - val plotList = plotList ?: return - val list = event.toolTip - val index = event.slot.slotNumber - if (index == 53) { - list.clear() - list.add("§6Edit Mode") - list.add("") - list.add("${if (editMode == 0) "§6► " else ""}§cOFF§7: §bNothing change, behave like normal.") - list.add("${if (editMode == 1) "§6► " else ""}§aON§7: §bClick an item in your inventory then click again") - list.add("${if (editMode == 1) "§6► " else ""}§bin the plot menu to change it to that item.") - list.add("${if (editMode == 2) "§6► " else ""}§9RESET§7: §bClick an item in the menu to reset it to default.") - list.add("") - list.add("§eClick to switch Edit Mode !") - list.add("") - } - if (plotList.contains(index)) { - val stack = originalStack[index] ?: return - val lore = stack.getLore() - list.clear() - list.add(0, stack.displayName) - for (i in lore.indices) { - list.add(i + 1, stack.getLore()[i]) - } - } - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenNextPlotPrice.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenNextPlotPrice.kt new file mode 100644 index 000000000..1391f5706 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenNextPlotPrice.kt @@ -0,0 +1,55 @@ +package at.hannibal2.skyhanni.features.garden.inventory.plots + +import at.hannibal2.skyhanni.events.LorenzToolTipEvent +import at.hannibal2.skyhanni.features.garden.GardenAPI +import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.ItemUtils +import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.NEUInternalName +import at.hannibal2.skyhanni.utils.NEUItems.getPrice +import at.hannibal2.skyhanni.utils.NumberUtil +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class GardenNextPlotPrice { + + @SubscribeEvent + fun onTooltip(event: LorenzToolTipEvent) { + if (!GardenAPI.inGarden()) return + if (!GardenAPI.config.plotPrice) return + + if (InventoryUtils.openInventoryName() != "Configure Plots") return + + if (!event.itemStack.name.startsWith("§ePlot")) return + + var next = false + val list = event.toolTip + var i = -1 + for (line in event.toolTipRemovedPrefix()) { + i++ + if (line.contains("Cost")) { + next = true + continue + } + + if (next) { + val readItemAmount = ItemUtils.readItemAmount(line) + readItemAmount?.let { + val (itemName, amount) = it + val lowestBin = NEUInternalName.fromItemName(itemName).getPrice() + val price = lowestBin * amount + val format = NumberUtil.format(price) + list[i] = list[i] + " §7(§6$format§7)" + } ?: run { + ErrorManager.logErrorStateWithData( + "Garden Next Plot Price error", + "Could not read item amount from line", + "line" to line, + "event.toolTip" to event.toolTip, + ) + } + break + } + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotIcon.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotIcon.kt new file mode 100644 index 000000000..32a75c425 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotIcon.kt @@ -0,0 +1,158 @@ +package at.hannibal2.skyhanni.features.garden.inventory.plots + +import at.hannibal2.skyhanni.events.GuiContainerEvent +import at.hannibal2.skyhanni.events.InventoryCloseEvent +import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent +import at.hannibal2.skyhanni.events.LorenzToolTipEvent +import at.hannibal2.skyhanni.features.garden.GardenAPI +import at.hannibal2.skyhanni.utils.ChatUtils +import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import io.github.moulberry.notenoughupdates.events.ReplaceItemEvent +import io.github.moulberry.notenoughupdates.util.Utils +import net.minecraft.client.player.inventory.ContainerLocalMenu +import net.minecraft.init.Items +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.EventPriority +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +object GardenPlotIcon { + + private val config get() = GardenAPI.config.plotIcon + private val plotList get() = GardenAPI.storage?.plotIcon?.plotList + private var inInventory = false + private var copyStack: ItemStack? = null + + // TODO replace with enum + private var editMode = 0 // 0 = off, 1 = on, 2 = reset + private var lastClickedSlotId = -1 + private var originalStack = mutableMapOf() + private var cachedStack = mutableMapOf() + private val editStack = ItemStack(Items.wooden_axe) + private val whitelistedSlot = + listOf(2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 20, 21, 23, 24, 29, 30, 31, 32, 33, 38, 39, 40, 41, 42) + + var hardReset = false + + fun isEnabled() = GardenAPI.inGarden() && config.enabled && inInventory + + @SubscribeEvent(priority = EventPriority.HIGHEST) + fun onInventoryOpen(event: InventoryFullyOpenedEvent) { + inInventory = event.inventoryName == "Configure Plots" + if (!isEnabled()) return + + for ((index, stack) in event.inventoryItems) { + originalStack[index] = stack + } + val plotList = plotList ?: return + for ((index, internalName) in plotList) { + val old = originalStack[index]!! + val new = internalName.getItemStack() + cachedStack[index] = Utils.editItemStackInfo(new, old.displayName, true, *old.getLore().toTypedArray()) + } + } + + @SubscribeEvent + fun onInventoryClose(event: InventoryCloseEvent) { + inInventory = false + editMode = 0 + } + + @SubscribeEvent(priority = EventPriority.HIGH) + fun replaceItem(event: ReplaceItemEvent) { + if (!isEnabled()) return + val plotList = plotList ?: return + if (hardReset) { + plotList.clear() + hardReset = false + return + } + + if (event.inventory is ContainerLocalMenu) { + if (event.slotNumber == 53) { + event.replaceWith(editStack) + } + if (plotList.isNotEmpty() && plotList.contains(event.slotNumber)) { + if (lastClickedSlotId == event.slotNumber) { + lastClickedSlotId = -1 + return + } + event.replaceWith(cachedStack[event.slotNumber]) + } + } + } + + @SubscribeEvent(priority = EventPriority.HIGH) + fun onStackClick(event: GuiContainerEvent.SlotClickEvent) { + if (!isEnabled()) return + lastClickedSlotId = event.slotId + if (event.slotId == 53) { + event.isCanceled = true + if (event.clickedButton == 0) { + if (editMode == 2) + editMode = 0 + else + editMode++ + } else if (event.clickedButton == 1) { + if (editMode == 0) + editMode = 2 + else + editMode-- + } + return + } + if (editMode != 0) { + if (event.slotId in 54..89) { + event.isCanceled = true + copyStack = event.slot?.stack?.copy()?.also { + it.stackSize = 1 + } ?: return + // TODO different format, not bold or show not in chat at all. + ChatUtils.chat("§6§lClick an item in the desk menu to replace it with that item!") + return + } + if (event.slotId != 53) { + val plotList = plotList ?: return + if (!whitelistedSlot.contains(event.slotId)) return + event.isCanceled = true + if (editMode == 2) { + plotList.remove(event.slotId) + return + } + val copyStack = copyStack ?: return + plotList[event.slotId] = copyStack.getInternalName() + cachedStack[event.slotId] = copyStack + } + } + } + + @SubscribeEvent + fun onTooltip(event: LorenzToolTipEvent) { + if (!isEnabled()) return + val plotList = plotList ?: return + val list = event.toolTip + val index = event.slot.slotNumber + if (index == 53) { + list.clear() + list.add("§6Edit Mode") + list.add("") + list.add("${if (editMode == 0) "§6► " else ""}§cOFF§7: §bNothing change, behave like normal.") + list.add("${if (editMode == 1) "§6► " else ""}§aON§7: §bClick an item in your inventory then click again") + list.add("${if (editMode == 1) "§6► " else ""}§bin the plot menu to change it to that item.") + list.add("${if (editMode == 2) "§6► " else ""}§9RESET§7: §bClick an item in the menu to reset it to default.") + list.add("") + list.add("§eClick to switch Edit Mode !") + list.add("") + } + if (plotList.contains(index)) { + val stack = originalStack[index] ?: return + val lore = stack.getLore() + list.clear() + list.add(0, stack.displayName) + for (i in lore.indices) { + list.add(i + 1, stack.getLore()[i]) + } + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotMenuHighlighting.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotMenuHighlighting.kt new file mode 100644 index 000000000..165980a9f --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/inventory/plots/GardenPlotMenuHighlighting.kt @@ -0,0 +1,81 @@ +package at.hannibal2.skyhanni.features.garden.inventory.plots + +import at.hannibal2.skyhanni.config.features.garden.PlotMenuHighlightingConfig.PlotStatusType +import at.hannibal2.skyhanni.events.GuiContainerEvent +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.features.garden.GardenAPI +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.currentSpray +import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.pests +import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.RenderUtils.highlight +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class GardenPlotMenuHighlighting { + + private val config get() = GardenAPI.config.plotMenuHighlighting + + private var highlightedPlots = mutableMapOf() + + @SubscribeEvent + fun onInventoryOpen(event: InventoryOpenEvent) { + if (!isEnabled()) return + + for (slot in InventoryUtils.getItemsInOpenChest()) { + val list = mutableListOf() + val plot = GardenPlotAPI.plots.find { it.inventorySlot == slot.slotIndex } ?: continue + + val (pestsEnabled, spraysEnabled, locksEnabled, currentEnabled) = PlotStatusType.entries.map { it in config.deskPlotStatusTypes } + + if (plot.pests >= 1 && pestsEnabled) list.add(PlotStatusType.PESTS) + if (plot.currentSpray != null && spraysEnabled) list.add(PlotStatusType.SPRAYS) + if (!plot.unlocked && locksEnabled) list.add(PlotStatusType.LOCKED) + if (plot == GardenPlotAPI.getCurrentPlot() && currentEnabled) list.add(PlotStatusType.CURRENT) + + getLowestIndexItem(list)?.let { index -> + val status = config.deskPlotStatusTypes[index] + handleCurrent(plot, status) + } ?: highlightedPlots.remove(plot) + } + } + + @SubscribeEvent + fun onBackgroundDrawn(event: GuiContainerEvent.BackgroundDrawnEvent) { + if (!isEnabled() || highlightedPlots.isEmpty()) return + + for (plot in highlightedPlots) { + val items = InventoryUtils.getItemsInOpenChest() + if (plot.key.inventorySlot in items.indices) { + val slot = items[plot.key.inventorySlot] + slot.stack.stackSize = handleStackSize(plot.key, plot.value) + slot highlight plot.value.highlightColor + } + } + } + + private fun handleStackSize(plot: GardenPlotAPI.Plot, status: PlotStatusType): Int { + return when (status.name) { + "§cPests" -> return plot.pests + "§eSprays" -> return plot.currentSpray?.expiry?.timeUntil()?.inWholeMinutes?.toInt() ?: 1 + else -> 1 + } + } + + private fun handleCurrent(plot: GardenPlotAPI.Plot, status: PlotStatusType) { + val isHighlighted = highlightedPlots.containsKey(plot) + val isCurrent = highlightedPlots[plot] == status + if (!isHighlighted || isCurrent) { + if (!isHighlighted) highlightedPlots[plot] = status + } else { + highlightedPlots[plot] = status + } + } + + private fun getLowestIndexItem(array: MutableList): Int? { + return array.mapNotNull { status -> config.deskPlotStatusTypes.find { it == status } } + .minOfOrNull { config.deskPlotStatusTypes.indexOf(it) } + } + + private fun isEnabled() = + GardenAPI.inGarden() && InventoryUtils.openInventoryName() == "Configure Plots" && config.enabled +} -- cgit