Skip to content

Commit

Permalink
Add hud element config links. (#1383)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
nea89o and hannibal002 authored Apr 8, 2024
1 parent ff6dd29 commit 878f8ee
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 16 deletions.
29 changes: 29 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package at.hannibal2.skyhanni.config

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.core.config.Position
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson
import at.hannibal2.skyhanni.data.jsonobjects.local.JacobContestsJson
import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson
import at.hannibal2.skyhanni.data.jsonobjects.local.VisualWordsJson
import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelApiTrophyFish
import at.hannibal2.skyhanni.events.LorenzEvent
import at.hannibal2.skyhanni.features.fishing.trophy.TrophyRarity
import at.hannibal2.skyhanni.features.misc.update.UpdateManager
import at.hannibal2.skyhanni.utils.FeatureTogglesByDefaultAdapter
import at.hannibal2.skyhanni.utils.IdentityCharacteristics
import at.hannibal2.skyhanni.utils.KotlinTypeAdapterFactory
import at.hannibal2.skyhanni.utils.LorenzLogger
import at.hannibal2.skyhanni.utils.LorenzRarity
Expand All @@ -29,6 +32,7 @@ import com.google.gson.TypeAdapterFactory
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import io.github.notenoughupdates.moulconfig.annotations.ConfigLink
import io.github.notenoughupdates.moulconfig.observer.PropertyTypeAdapterFactory
import io.github.notenoughupdates.moulconfig.processor.BuiltinMoulConfigGuis
import io.github.notenoughupdates.moulconfig.processor.ConfigProcessorDriver
Expand Down Expand Up @@ -214,6 +218,31 @@ class ConfigManager {
BuiltinMoulConfigGuis.addProcessors(processor)
UpdateManager.injectConfigProcessor(processor)
ConfigProcessorDriver(processor).processConfig(features)

try {
findPositionLinks(features, mutableSetOf())
} catch (e: Exception) {
if (LorenzEvent.isInGuardedEventHandler)
throw e
}
}

private fun findPositionLinks(obj: Any?, slog: MutableSet<IdentityCharacteristics<Any>>) {
if (obj == null) return
if (!obj.javaClass.name.startsWith("at.hannibal2.skyhanni.")) return
val ic = IdentityCharacteristics(obj)
if (ic in slog) return
slog.add(ic)
for (field in obj.javaClass.fields) {
field.isAccessible = true
if (field.type != Position::class.java) {
findPositionLinks(field.get(obj), slog)
continue
}
val configLink = field.getAnnotation(ConfigLink::class.java) ?: continue
val position = field.get(obj) as Position
position.setLink(configLink)
}
}

private fun firstLoadFile(file: File?, fileType: ConfigFileType, defaultValue: Any): Any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
package at.hannibal2.skyhanni.config.core.config;

import at.hannibal2.skyhanni.SkyHanniMod;
import at.hannibal2.skyhanni.config.ConfigGuiManager;
import at.hannibal2.skyhanni.config.Features;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.ConfigLink;
import io.github.notenoughupdates.moulconfig.gui.GuiScreenElementWrapper;
import io.github.notenoughupdates.moulconfig.gui.MoulConfigEditor;
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;

public class Position {
@Expose
Expand All @@ -39,6 +48,8 @@ public class Position {
@Expose
private boolean centerY;

public transient Field linkField;

private boolean clicked = false;
public String internalName = null;

Expand Down Expand Up @@ -203,4 +214,22 @@ public int moveY(int deltaY, int objHeight) {
}
return deltaY;
}

public boolean canJumpToConfigOptions() {
return linkField != null && ConfigGuiManager.INSTANCE.getEditorInstance().getProcessedConfig().getOptionFromField(linkField) != null;
}

public void jumpToConfigOptions() {
MoulConfigEditor<Features> editor = ConfigGuiManager.INSTANCE.getEditorInstance();
if (linkField == null) return;
ProcessedOption option = editor.getProcessedConfig().getOptionFromField(linkField);
if (option == null) return;
editor.search("");
if (!editor.goToOption(option)) return;
SkyHanniMod.Companion.setScreenToOpen(new GuiScreenElementWrapper(editor));
}

public void setLink(@NotNull ConfigLink configLink) throws NoSuchFieldException {
linkField = configLink.owner().getField(configLink.field());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class GuiPositionEditor(private val positions: List<Position>, private val borde
val location = "§7x: §e${pos.rawX}§7, y: §e${pos.rawY}§7, scale: §e${pos.scale.round(2)}"
GuiRenderUtils.drawStringCentered("§b" + pos.internalName, getScaledWidth() / 2, 18)
GuiRenderUtils.drawStringCentered(location, getScaledWidth() / 2, 28)
if (pos.canJumpToConfigOptions())
GuiRenderUtils.drawStringCentered(
"§aRight-Click to open associated config options",
getScaledWidth() / 2,
38
)
}

private fun renderRectangles(): Int {
Expand Down Expand Up @@ -142,7 +148,6 @@ class GuiPositionEditor(private val positions: List<Position>, private val borde
override fun mouseClicked(originalX: Int, priginalY: Int, mouseButton: Int) {
super.mouseClicked(originalX, priginalY, mouseButton)

if (mouseButton != 0) return

val mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth
val mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1
Expand All @@ -152,19 +157,20 @@ class GuiPositionEditor(private val positions: List<Position>, private val borde
val elementHeight = position.getDummySize().y
val x = position.getAbsX()
val y = position.getAbsY()
if (!position.clicked &&
GuiRenderUtils.isPointInRect(

mouseX,
mouseY,
x - border,
y - border,
elementWidth + border * 2,
elementHeight + border * 2

)

) {
val isHovered = GuiRenderUtils.isPointInRect(
mouseX,
mouseY,
x - border,
y - border,
elementWidth + border * 2,
elementHeight + border * 2
)
if (!isHovered) continue
if (mouseButton == 1) {
position.jumpToConfigOptions()
break
}
if (!position.clicked && mouseButton == 0) {
clickedPos = i
position.clicked = true
grabbedX = mouseX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorDropdown;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;
import io.github.notenoughupdates.moulconfig.annotations.ConfigLink;

public class ComposterConfig {
@Expose
Expand Down Expand Up @@ -86,9 +87,11 @@ public String toString() {
}

@Expose
@ConfigLink(owner = ComposterConfig.class, field = "overlay")
public Position overlayOrganicMatterPos = new Position(140, 152, false, true);

@Expose
@ConfigLink(owner = ComposterConfig.class, field = "overlay")
public Position overlayFuelExtrasPos = new Position(-320, 152, false, true);

@Expose
Expand Down Expand Up @@ -158,8 +161,10 @@ public String toString() {
public NotifyLowConfig notifyLow = new NotifyLowConfig();

@Expose
@ConfigLink(owner = ComposterConfig.class, field = "displayEnabled")
public Position displayPos = new Position(-390, 10, false, true);

@Expose
@ConfigLink(owner = ComposterConfig.class, field = "displayEnabled")
public Position outsideGardenPos = new Position(-363, 13, false, true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import at.hannibal2.skyhanni.config.core.config.Position;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigLink;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;

public class SprayConfig {
Expand All @@ -24,6 +25,7 @@ public class SprayConfig {
public boolean drawPlotsBorderWhenInHands = true;

@Expose
@ConfigLink(owner = SprayConfig.class, field = "pestWhenSelector")
public Position position = new Position(315, -200, 2.3f);

@Expose
Expand Down Expand Up @@ -54,5 +56,6 @@ public class SprayConfig {
public boolean expiryNotification = true;

@Expose
@ConfigLink(owner = SprayConfig.class, field = "displayEnabled")
public Position displayPosition = new Position(390, 75, false, true);
}
4 changes: 3 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import at.hannibal2.skyhanni.mixins.hooks.setValue
import at.hannibal2.skyhanni.mixins.transformers.AccessorEventBus
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import net.minecraft.launchwrapper.Launch
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.Event
import net.minecraftforge.fml.common.eventhandler.IEventListener
Expand All @@ -25,7 +27,7 @@ abstract class LorenzEvent : Event() {
return 0
}
}
val isInGuardedEventHandler get() = eventHandlerDepth > 0
val isInGuardedEventHandler get() = eventHandlerDepth > 0 || LorenzUtils.isInDevEnvironment()
}

fun postAndCatchAndBlock(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ object LorenzUtils {
enumValues<T>().joinToString("|", transform = transform)

// TODO move to val by lazy
fun isInDevEnvironment() = Launch.blackboard["fml.deobfuscatedEnvironment"] as Boolean
fun isInDevEnvironment() = ((Launch.blackboard ?: mapOf())["fml.deobfuscatedEnvironment"] as Boolean?) ?: true

fun shutdownMinecraft(reason: String? = null) {
System.err.println("SkyHanni-${SkyHanniMod.version} forced the game to shutdown.")
Expand Down

0 comments on commit 878f8ee

Please sign in to comment.