Skip to content

Commit

Permalink
Add extensions API
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzeva committed Jul 21, 2023
1 parent f8898f2 commit 3c00f2e
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 33 deletions.
1 change: 1 addition & 0 deletions run
5 changes: 5 additions & 0 deletions src/main/kotlin/pm/n2/tangerine/extension/Extension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pm.n2.tangerine.extension

import net.fabricmc.loader.api.ModContainer

class Extension(val container: ModContainer, val entrypoint: TangerineEntrypoint)
15 changes: 15 additions & 0 deletions src/main/kotlin/pm/n2/tangerine/extension/TangerineEntrypoint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pm.n2.tangerine.extension

import pm.n2.tangerine.commands.TangerineCommand
import pm.n2.tangerine.modules.Module
import pm.n2.tangerine.modules.ModuleCategory

abstract class TangerineEntrypoint {
abstract fun onInitialize()

open fun getModules(): List<Module> = listOf()

open fun getCommands(): List<TangerineCommand> = listOf()

open fun getCategories(): List<ModuleCategory> = listOf()
}
30 changes: 6 additions & 24 deletions src/main/kotlin/pm/n2/tangerine/gui/renderables/MenuBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,12 @@ object MenuBar : TangerineRenderable("MenuBar") {
ImGui.endMenu()
}

drawMenuTab(
I18n.translate("tangerine.category.movement"),
ModuleManager.getModulesByCategory(ModuleCategory.MOVEMENT)
)

drawMenuTab(
I18n.translate("tangerine.category.combat"),
ModuleManager.getModulesByCategory(ModuleCategory.COMBAT)
)

drawMenuTab(
I18n.translate("tangerine.category.visuals"),
ModuleManager.getModulesByCategory(ModuleCategory.VISUALS)
)

drawMenuTab(
I18n.translate("tangerine.category.player"),
ModuleManager.getModulesByCategory(ModuleCategory.PLAYER)
)

drawMenuTab(
I18n.translate("tangerine.category.misc"),
ModuleManager.getModulesByCategory(ModuleCategory.MISC)
)
for (category in ModuleManager.categories) {
drawMenuTab(
I18n.translate("tangerine.category.${category.id}"),
ModuleManager.getModulesByCategory(category)
)
}

ImGui.endMainMenuBar()
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/pm/n2/tangerine/managers/CommandManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import pm.n2.tangerine.commands.PaperTPCommand
import pm.n2.tangerine.core.Manager

object CommandManager : Manager {
private val commands = listOf(PaperTPCommand, CarpetTPCommand, ClipCommand, PacketsCommand)
private val commands = mutableListOf(PaperTPCommand, CarpetTPCommand, ClipCommand, PacketsCommand)

override fun init() {
// lets add extensions commands
ExtensionManager.extensions.forEach { commands.addAll(it.entrypoint.getCommands()) }

commands.forEach { cmd -> cmd.register(ClientCommandManager.DISPATCHER) }
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/pm/n2/tangerine/managers/ExtensionManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pm.n2.tangerine.managers

import net.fabricmc.loader.api.FabricLoader
import org.slf4j.LoggerFactory
import pm.n2.tangerine.core.Manager
import pm.n2.tangerine.extension.Extension
import pm.n2.tangerine.extension.TangerineEntrypoint

object ExtensionManager: Manager {
private val logger = LoggerFactory.getLogger("Tangerine ExtensionManager")
val extensions = findExtensions().toMutableList()

override fun init() {
logger.debug("Found ${extensions.size} extensions")

extensions.forEach {
it.entrypoint.onInitialize()
}
}

fun findExtensions(): List<Extension> {
val mods = FabricLoader.getInstance().getEntrypointContainers("tangerine", TangerineEntrypoint::class.java)
return mods.map { Extension(it.provider, it.entrypoint) }
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/pm/n2/tangerine/managers/ManagerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ object ManagerManager : Manager {
ImGuiManager,
KeyboardManager,
ModuleManager,
OverlayManager
OverlayManager,
ExtensionManager
)

override fun init() {
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/pm/n2/tangerine/managers/ModuleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import pm.n2.tangerine.modules.visuals.StorageESPModule
import pm.n2.tangerine.modules.visuals.TracersModule

object ModuleManager : Manager {
val modules = listOf(
val categories = mutableListOf(
ModuleCategory.MOVEMENT,
ModuleCategory.PLAYER,
ModuleCategory.COMBAT,
ModuleCategory.MISC,
ModuleCategory.VISUALS
)

val modules = mutableListOf(
CritsModule,
KillAuraModule,

Expand All @@ -45,6 +53,10 @@ object ModuleManager : Manager {
)

override fun init() {
// Let extensions add their modules before doing our magic
ExtensionManager.extensions.forEach { categories.addAll(it.entrypoint.getCategories()) }
ExtensionManager.extensions.forEach { modules.addAll(it.entrypoint.getModules()) }

// Setup config
for (module in modules) {
val options = mutableListOf<ConfigOption<*>>(module.enabled)
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/pm/n2/tangerine/modules/ModuleCategory.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pm.n2.tangerine.modules

enum class ModuleCategory {
MOVEMENT,
VISUALS,
PLAYER,
COMBAT,
MISC
class ModuleCategory(val id: String) {
companion object {
val MOVEMENT = ModuleCategory("movement")
val VISUALS = ModuleCategory("visuals")
val PLAYER = ModuleCategory("player")
val COMBAT = ModuleCategory("combat")
val MISC = ModuleCategory("misc")
}
}
6 changes: 6 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"value": "pm.n2.tangerine.compat.TangerineModMenu",
"adapter": "kotlin"
}
],
"tangerine": [
{
"value": "pm.n2.tangerine.extension.impl.TangerineTestExtension",
"adapter": "kotlin"
}
]
},
"depends": {
Expand Down

0 comments on commit 3c00f2e

Please sign in to comment.