Skip to content

Commit 48ca964

Browse files
committed
Reworked the whole feature loading system to use Annotation and Reflections based on the system used in SkyHanni. Fixed inaccurate max shard amount being displayed inside attribute features.
1 parent cdaebc2 commit 48ca964

32 files changed

+811
-385
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ dependencies {
3434
modImplementation(libs.fabric.kotlin)
3535
modImplementation(libs.fabric.api)
3636
modRuntimeOnly(libs.devauth)
37+
implementation("org.reflections:reflections:0.10.2") // Might switch to something like ClassGraph
38+
include("org.reflections:reflections:0.10.2")
3739
include(libs.moulconfig)
3840
}
3941

src/main/kotlin/io/github/frostzie/skyfall/SkyFall.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.frostzie.skyfall
22

33
import io.github.frostzie.skyfall.commands.CommandManager
4+
import io.github.frostzie.skyfall.config.ConfigGuiManager
45
import io.github.frostzie.skyfall.config.ConfigManager
56
import io.github.frostzie.skyfall.config.Features
67
import io.github.frostzie.skyfall.data.RepoManager
@@ -21,20 +22,25 @@ class SkyFall : ModInitializer {
2122
configManager.firstLoad()
2223
Runtime.getRuntime().addShutdownHook(Thread {
2324
configManager.saveConfig("shutdown-hook")
25+
FeatureManager.updateFeatureStates()
2426
})
2527
IslandManager.init()
2628
CommandManager.loadCommands()
2729
StackCountRenderer.initialize()
2830
HudManager.init()
29-
FeatureManager.loadFeatures()
31+
FeatureManager.initialize()
3032

3133
ClientTickEvents.END_CLIENT_TICK.register { client ->
3234
if (screenToOpen != null) {
3335
client.setScreen(screenToOpen)
3436
screenToOpen = null
3537
}
36-
}
3738

39+
if (ConfigGuiManager.currentScreenInstance != null && client.currentScreen != ConfigGuiManager.currentScreenInstance) {
40+
feature.saveNow()
41+
ConfigGuiManager.currentScreenInstance = null
42+
}
43+
}
3844

3945
ItemTooltipCallback.EVENT.register { stack, _, _, lines ->
4046
if (!stack.isEmpty) {
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
package io.github.frostzie.skyfall.config
22

33
import io.github.frostzie.skyfall.SkyFall
4+
import io.github.frostzie.skyfall.hud.HudEditorScreen
45
import io.github.notenoughupdates.moulconfig.gui.GuiElementWrapper
56
import io.github.notenoughupdates.moulconfig.gui.MoulConfigEditor
67
import net.minecraft.client.gui.screen.Screen
78

8-
// Taken and modified from Skyhanni
99
object ConfigGuiManager {
10-
var editor: MoulConfigEditor<Features>? = null
1110

12-
fun getEditorInstance(): MoulConfigEditor<Features> {
13-
if (editor == null) {
14-
editor = MoulConfigEditor(SkyFall.Companion.configManager.processor)
11+
private var configEditor: MoulConfigEditor<Features>? = null
12+
13+
var currentScreenInstance: Screen? = null
14+
15+
private fun getConfigEditorInstance(): MoulConfigEditor<Features> {
16+
if (configEditor == null) {
17+
configEditor = MoulConfigEditor(SkyFall.configManager.processor)
1518
}
16-
return editor!!
19+
return configEditor!!
1720
}
1821

1922
fun openConfigGui(search: String? = null) {
20-
val currentEditor = getEditorInstance()
23+
val currentEditor = getConfigEditorInstance()
2124
if (search != null) {
2225
currentEditor.search(search)
2326
}
24-
SkyFall.Companion.screenToOpen = GuiElementWrapper(currentEditor)
27+
28+
val screen = GuiElementWrapper(currentEditor)
29+
currentScreenInstance = screen
30+
SkyFall.screenToOpen = screen
31+
}
32+
33+
fun openHudEditor() {
34+
val screen = HudEditorScreen()
35+
currentScreenInstance = screen
36+
SkyFall.screenToOpen = screen
2537
}
2638

2739
fun createModMenuScreen(parentScreenFromModMenu: Screen): Screen {
28-
val currentEditor = getEditorInstance()
29-
val configScreen = GuiElementWrapper(currentEditor)
30-
return configScreen
40+
val newEditor = MoulConfigEditor(SkyFall.configManager.processor)
41+
return GuiElementWrapper(newEditor)
3142
}
3243
}

src/main/kotlin/io/github/frostzie/skyfall/config/ConfigManager.kt

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
package io.github.frostzie.skyfall.config
22

3+
import com.google.gson.GsonBuilder
34
import io.github.frostzie.skyfall.SkyFall
5+
import io.github.frostzie.skyfall.features.FeatureManager
6+
import io.github.frostzie.skyfall.utils.LoggerProvider
47
import io.github.frostzie.skyfall.utils.SimpleTimeMark
5-
68
import io.github.notenoughupdates.moulconfig.observer.PropertyTypeAdapterFactory
79
import io.github.notenoughupdates.moulconfig.processor.BuiltinMoulConfigGuis
810
import io.github.notenoughupdates.moulconfig.processor.ConfigProcessorDriver
911
import io.github.notenoughupdates.moulconfig.processor.MoulConfigProcessor
10-
11-
import com.google.gson.GsonBuilder
12-
import io.github.frostzie.skyfall.utils.LoggerProvider
13-
import java.io.BufferedReader
14-
import java.io.BufferedWriter
15-
import java.io.File
16-
import java.io.FileInputStream
17-
import java.io.FileOutputStream
18-
import java.io.IOException
19-
import java.io.InputStreamReader
20-
import java.io.OutputStreamWriter
12+
import java.io.*
2113
import java.nio.charset.StandardCharsets
2214
import java.nio.file.Files
2315
import java.nio.file.StandardCopyOption
@@ -35,25 +27,22 @@ object ConfigManager {
3527

3628
lateinit var features: Features
3729

38-
var configDirectory = File("config/skyfall")
30+
private var configDirectory = File("config/skyfall")
3931
private var configFile: File? = null
4032
lateinit var processor: MoulConfigProcessor<Features>
4133

4234
fun firstLoad() {
43-
if (ConfigManager::features.isInitialized) {
44-
println("SkyFall: Loading config despite config alr being loaded?..")
45-
}
46-
4735
configDirectory.mkdirs()
4836
configFile = File(configDirectory, "config.json")
49-
println("Trying to load config from $configFile")
37+
logger.info("Trying to load config from $configFile")
38+
5039
if (configFile!!.exists()) {
5140
try {
52-
println("load-config-now")
41+
logger.info("load-config-now")
5342
val inputStreamReader = InputStreamReader(FileInputStream(configFile!!), StandardCharsets.UTF_8)
5443
val bufferedReader = BufferedReader(inputStreamReader)
5544
features = gson.fromJson(bufferedReader.readText(), Features::class.java)
56-
println("SkyFall: Loaded config file")
45+
logger.info("Loaded config File")
5746
} catch (e: Exception) {
5847
logger.error("Exception while reading config file $configFile", e)
5948
val backupFile = configFile!!.resolveSibling("config-${SimpleTimeMark.now().toMillis()}-backup.json")
@@ -66,28 +55,27 @@ object ConfigManager {
6655
}
6756
}
6857

69-
if (!ConfigManager::features.isInitialized) {
70-
println("Creating a new config file and saving it")
58+
if (!this::features.isInitialized) {
59+
logger.info("Creating a new config file and saving it")
7160
features = Features()
7261
saveConfig("blank config")
7362
}
7463

64+
logger.info("Initializing MoulConfig with the loaded configuration.")
65+
processor = MoulConfigProcessor(SkyFall.feature)
66+
BuiltinMoulConfigGuis.addProcessors(processor)
67+
val driver = ConfigProcessorDriver(processor)
68+
driver.warnForPrivateFields = false
69+
driver.processConfig(SkyFall.feature)
70+
7571
fixedRateTimer(name = "skyfall-config-auto-save", period = 60_000L, initialDelay = 60_000L) {
7672
try {
7773
saveConfig("auto-save-60s")
74+
logger.debug("60s Config Save.")
7875
} catch (e: Throwable) {
7976
logger.error("Error auto-saving config!", e)
8077
}
8178
}
82-
83-
//TODO: Add UpdateManager
84-
val features = SkyFall.feature
85-
processor = MoulConfigProcessor(SkyFall.feature)
86-
BuiltinMoulConfigGuis.addProcessors(processor)
87-
//UpdateManager.injectConfigProcessor(processor)
88-
val driver = ConfigProcessorDriver(processor)
89-
driver.warnForPrivateFields = false
90-
driver.processConfig(features)
9179
}
9280

9381
fun saveConfig(reason: String) {
@@ -106,6 +94,7 @@ object ConfigManager {
10694
StandardCopyOption.REPLACE_EXISTING,
10795
StandardCopyOption.ATOMIC_MOVE
10896
)
97+
FeatureManager.updateFeatureStates()
10998
} catch (e: IOException) {
11099
logger.error("Couldn't save config file to $file", e)
111100
}

src/main/kotlin/io/github/frostzie/skyfall/config/Features.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.frostzie.skyfall.config
22

33
import com.google.gson.annotations.Expose
4+
import io.github.frostzie.skyfall.SkyFall
45
import io.github.frostzie.skyfall.config.features.chat.ChatConfig
56
import io.github.frostzie.skyfall.config.features.dev.DevConfig
67
import io.github.frostzie.skyfall.config.features.dungeon.DungeonConfig
@@ -14,6 +15,10 @@ import io.github.notenoughupdates.moulconfig.annotations.Category
1415
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
1516

1617
class Features : Config() {
18+
override fun saveNow() {
19+
SkyFall.configManager.saveConfig("close-gui")
20+
}
21+
1722
//TODO: add version change support and color gradient title
1823
override fun getTitle(): String {
1924
return "§b§lSkyFall§r by §3Frostzie§r, config by §5Moulberry §rand §5nea89"

src/main/kotlin/io/github/frostzie/skyfall/config/features/misc/keybind/KeyBinds.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorKeybind
55
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption
66
import org.lwjgl.glfw.GLFW
77

8-
//TODO: Move all the keybinds to one class
9-
//TODO: Add mouse button support
8+
//TODO: Remove this and either recommend skytils or add new overall system for hotkeys
109

1110
class KeyBinds {
1211

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.github.frostzie.skyfall.features
2+
3+
@Target(AnnotationTarget.CLASS)
4+
@Retention(AnnotationRetention.RUNTIME)
5+
annotation class Feature(val name: String)
6+
7+
// Based on the system used by Skyhanni
8+
interface IFeature {
9+
/**
10+
* A flag to track if the feature is currently active.
11+
* This should be set to true in init() and false in terminate().
12+
*/
13+
var isRunning: Boolean
14+
15+
/**
16+
* The condition to check if this feature should be loaded.
17+
* This will now be checked both at startup and after config changes.
18+
*/
19+
fun shouldLoad(): Boolean
20+
21+
/**
22+
* The entry point of the feature. Called when the feature is enabled.
23+
* Should register listeners and set isRunning = true.
24+
*/
25+
fun init()
26+
27+
/**
28+
* A new method to clean up the feature when it's disabled.
29+
* Should unregister listeners, reset state, and set isRunning = false.
30+
*/
31+
fun terminate()
32+
}

0 commit comments

Comments
 (0)