Skip to content

Commit

Permalink
Feature: Plot Menu Highlighting (hannibal002#1181)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
2 people authored and superhize committed Apr 8, 2024
1 parent 4215baf commit 4ee82be
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -621,6 +622,7 @@ class SkyHanniMod {
loadModule(TpsCounter())
loadModule(ParticleHider())
loadModule(MiscFeatures())
loadModule(GardenPlotMenuHighlighting())
loadModule(SkyMartCopperPrice())
loadModule(GardenVisitorFeatures())
loadModule(NPCVisitorFix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PlotStatusType> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package at.hannibal2.skyhanni.features.garden.inventory
package at.hannibal2.skyhanni.features.garden.inventory.plots

import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package at.hannibal2.skyhanni.features.garden.inventory
package at.hannibal2.skyhanni.features.garden.inventory.plots

import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GardenPlotAPI.Plot, PlotStatusType>()

@SubscribeEvent
fun onInventoryOpen(event: InventoryOpenEvent) {
if (!isEnabled()) return

for (slot in InventoryUtils.getItemsInOpenChest()) {
val list = mutableListOf<PlotStatusType>()
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<PlotStatusType>): 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
}

0 comments on commit 4ee82be

Please sign in to comment.