Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
[new] Plugin System (kami-blue#1773)
Browse files Browse the repository at this point in the history
Co-authored-by: Dewy <[email protected]>
Co-authored-by: lv <[email protected]>
  • Loading branch information
3 people authored Mar 13, 2021
1 parent fe13e53 commit fc7fc34
Show file tree
Hide file tree
Showing 78 changed files with 1,447 additions and 351 deletions.
23 changes: 23 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ processResources {
}
}

task sourceJar(type: Jar) { // Generate sources
group 'build'
description 'Assemble API library source archive'

archiveClassifier.set('api-source')
from sourceSets.main.allSource
}

task apiJar(type: Jar) {
group 'build'
description 'Assemble API library archive'

archiveClassifier.set('api')
from sourceSets.main.output
}

// Don't put baritone mixin here please c:
jar {
manifest.attributes(
Expand All @@ -200,3 +216,10 @@ jar {
}
}
}

task buildApi {
group 'build'
dependsOn sourceJar
dependsOn apiJar
description 'Assemble API library archives'
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.kamiblue.client.mixin.client.accessor.player.AccessorPlayerControllerMP;
import org.kamiblue.client.module.modules.combat.CrystalAura;
import org.kamiblue.client.module.modules.player.BlockInteraction;
import org.kamiblue.client.plugin.PluginError;
import org.kamiblue.client.util.Wrapper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -29,9 +30,6 @@
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
* Created by 086 on 17/11/2017.
*/
@Mixin(Minecraft.class)
public abstract class MixinMinecraft {

Expand Down Expand Up @@ -162,6 +160,7 @@ public void init(CallbackInfo info) {
if (KamiGuiUpdateNotification.Companion.getLatest() != null && !KamiGuiUpdateNotification.Companion.isLatest()) {
Wrapper.getMinecraft().displayGuiScreen(new KamiGuiUpdateNotification());
}
PluginError.Companion.displayErrors();
}

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/kamiblue/client/Loader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.kamiblue.client.command.CommandManager
import org.kamiblue.client.gui.GuiManager
import org.kamiblue.client.manager.ManagerLoader
import org.kamiblue.client.module.ModuleManager
import org.kamiblue.client.plugin.PluginManager
import org.kamiblue.client.util.threads.mainScope

internal object LoaderWrapper {
Expand All @@ -17,6 +18,7 @@ internal object LoaderWrapper {
loaderList.add(CommandManager)
loaderList.add(ManagerLoader)
loaderList.add(GuiManager)
loaderList.add(PluginManager)
}

@JvmStatic
Expand All @@ -32,7 +34,7 @@ internal object LoaderWrapper {
}
}

interface AsyncLoader<T> {
internal interface AsyncLoader<T> {
var deferred: Deferred<T>?

fun preLoad() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/kamiblue/client/command/Args.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.minecraft.item.Item
import net.minecraft.util.math.BlockPos
import org.kamiblue.capeapi.PlayerProfile
import org.kamiblue.client.gui.GuiManager
import org.kamiblue.client.gui.hudgui.HudElement
import org.kamiblue.client.gui.hudgui.AbstractHudElement
import org.kamiblue.client.manager.managers.UUIDManager
import org.kamiblue.client.module.AbstractModule
import org.kamiblue.client.module.ModuleManager
Expand Down Expand Up @@ -40,8 +40,8 @@ class ModuleArg(

class HudElementArg(
override val name: String
) : AbstractArg<HudElement>(), AutoComplete by DynamicPrefixMatch(::allAlias) {
override suspend fun convertToType(string: String?): HudElement? {
) : AbstractArg<AbstractHudElement>(), AutoComplete by DynamicPrefixMatch(::allAlias) {
override suspend fun convertToType(string: String?): AbstractHudElement? {
return GuiManager.getHudElementOrNull(string)
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/kamiblue/client/command/ClientCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.minecraft.util.math.BlockPos
import org.kamiblue.capeapi.PlayerProfile
import org.kamiblue.client.event.ClientExecuteEvent
import org.kamiblue.client.event.SafeExecuteEvent
import org.kamiblue.client.gui.hudgui.HudElement
import org.kamiblue.client.gui.hudgui.AbstractHudElement
import org.kamiblue.client.module.AbstractModule
import org.kamiblue.client.module.modules.client.CommandConfig
import org.kamiblue.client.util.Wrapper
Expand Down Expand Up @@ -38,7 +38,7 @@ abstract class ClientCommand(
@CommandBuilder
protected inline fun AbstractArg<*>.hudElement(
name: String,
block: BuilderBlock<HudElement>
block: BuilderBlock<AbstractHudElement>
) {
arg(HudElementArg(name), block)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ abstract class ClientCommand(

protected companion object {
val mc = Wrapper.minecraft
val prefix: String get() = CommandConfig.prefix.value
val prefix: String get() = CommandConfig.prefix
}

}
19 changes: 18 additions & 1 deletion src/main/kotlin/org/kamiblue/client/command/CommandManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import kotlinx.coroutines.*
import org.kamiblue.client.AsyncLoader
import org.kamiblue.client.KamiMod
import org.kamiblue.client.event.ClientExecuteEvent
import org.kamiblue.client.event.KamiEventBus
import org.kamiblue.client.module.modules.client.CommandConfig
import org.kamiblue.client.util.StopTimer
import org.kamiblue.client.util.text.MessageSendHelper
import org.kamiblue.client.util.text.formatValue
import org.kamiblue.client.util.threads.defaultScope
import org.kamiblue.client.util.threads.onMainThread
import org.kamiblue.command.AbstractCommandManager
import org.kamiblue.command.Command
import org.kamiblue.command.CommandBuilder
import org.kamiblue.command.utils.CommandNotFoundException
import org.kamiblue.command.utils.SubCommandNotFoundException
import org.kamiblue.commons.utils.ClassUtils
import org.kamiblue.commons.utils.ClassUtils.instance

object CommandManager : AbstractCommandManager<ClientExecuteEvent>(), AsyncLoader<List<Class<out ClientCommand>>> {
override var deferred: Deferred<List<Class<out ClientCommand>>>? = null
val prefix: String get() = CommandConfig.prefix.value
val prefix: String get() = CommandConfig.prefix

override fun preLoad0(): List<Class<out ClientCommand>> {
val stopTimer = StopTimer()
Expand All @@ -42,6 +45,20 @@ object CommandManager : AbstractCommandManager<ClientExecuteEvent>(), AsyncLoade
KamiMod.LOG.info("${input.size} commands loaded, took ${time}ms")
}

override fun register(builder: CommandBuilder<ClientExecuteEvent>): Command<ClientExecuteEvent> {
synchronized(lockObject) {
KamiEventBus.subscribe(builder)
return super.register(builder)
}
}

override fun unregister(builder: CommandBuilder<ClientExecuteEvent>): Command<ClientExecuteEvent>? {
synchronized(lockObject) {
KamiEventBus.unsubscribe(builder)
return super.unregister(builder)
}
}

fun runCommand(string: String) {
defaultScope.launch {
val args = tryParseArgument(string) ?: return@launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ object BindCommand : ClientCommand(
}
}

literal("modifiers") {
boolean("enabled") { modifiersArg ->
execute("Disallow binds while holding a modifier") {
val modifiers = modifiersArg.value

CommandConfig.modifierEnabled.value = modifiers
MessageSendHelper.sendChatMessage(
"Modifiers ${if (modifiers) " ${TextFormatting.GREEN format "enabled"}" else " ${TextFormatting.RED format "disabled"}"}"
)
}
}
}

module("module") { moduleArg ->
string("bind") { bindArg ->
execute("Bind a module to a key") {
Expand Down
179 changes: 179 additions & 0 deletions src/main/kotlin/org/kamiblue/client/command/commands/PluginCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package org.kamiblue.client.command.commands

import org.kamiblue.client.command.ClientCommand
import org.kamiblue.client.plugin.PluginError
import org.kamiblue.client.plugin.PluginLoader
import org.kamiblue.client.plugin.PluginManager
import org.kamiblue.client.plugin.api.Plugin
import org.kamiblue.client.util.ConfigUtils
import org.kamiblue.client.util.text.MessageSendHelper
import org.kamiblue.client.util.text.formatValue
import java.io.File

object PluginCommand : ClientCommand(
name = "plugin",
description = "Manage plugins"
) {
init {
literal("load") {
string("jar name") { nameArg ->
execute {
val name = "${nameArg.value.removeSuffix(".jar")}.jar"
val file = File("${PluginManager.pluginPath}$name")

if (!file.exists()) {
MessageSendHelper.sendErrorMessage("${formatValue(name)} is not a valid jar file name!")
}

val time = System.currentTimeMillis()
MessageSendHelper.sendChatMessage("Loading plugin ${formatValue(name)}...")

ConfigUtils.saveAll()
val loader = PluginLoader(file)

if (PluginManager.loadedPlugins.containsName(loader.info.name)) {
MessageSendHelper.sendWarningMessage("Plugin ${formatValue(name)} is already loaded!")
return@execute
}

PluginManager.load(loader)
ConfigUtils.loadAll()

val stopTime = System.currentTimeMillis() - time
MessageSendHelper.sendChatMessage("Loaded plugin ${formatValue(name)}, took $stopTime ms!")

PluginError.displayErrors()
}
}
}

literal("reload") {
string("plugin name") { nameArg ->
execute {
val name = nameArg.value
val plugin = PluginManager.loadedPlugins[name]

if (plugin == null) {
MessageSendHelper.sendErrorMessage("Plugin ${formatValue(name)} is not loaded")
return@execute
}

val time = System.currentTimeMillis()
MessageSendHelper.sendChatMessage("Reloading plugin ${formatValue(name)}...")

ConfigUtils.saveAll()

val file = PluginManager.loadedPluginLoader[plugin.name]!!.file
PluginManager.unload(plugin)
PluginManager.load(PluginLoader(file))
ConfigUtils.loadAll()

val stopTime = System.currentTimeMillis() - time
MessageSendHelper.sendChatMessage("Reloaded plugin ${formatValue(name)}, took $stopTime ms!")

PluginError.displayErrors()
}
}

execute {
val time = System.currentTimeMillis()
MessageSendHelper.sendChatMessage("Reloading all plugins...")

ConfigUtils.saveAll()
PluginManager.unloadAll()
PluginManager.loadAll(PluginManager.getLoaders())
ConfigUtils.loadAll()

val stopTime = System.currentTimeMillis() - time
MessageSendHelper.sendChatMessage("Reloaded all plugins, took $stopTime ms!")

PluginError.displayErrors()
}
}

literal("unload") {
string("plugin name") { nameArg ->
execute {
val name = nameArg.value
val plugin = PluginManager.loadedPlugins[name]

if (plugin == null) {
MessageSendHelper.sendErrorMessage("Plugin ${formatValue(name)} is not loaded")
return@execute
}

val time = System.currentTimeMillis()
MessageSendHelper.sendChatMessage("Unloading plugin ${formatValue(name)}...")

ConfigUtils.saveAll()
PluginManager.unload(plugin)
ConfigUtils.loadAll()

val stopTime = System.currentTimeMillis() - time
MessageSendHelper.sendChatMessage("Unloaded plugin ${formatValue(name)}, took $stopTime ms!")
}
}

execute {
val time = System.currentTimeMillis()
MessageSendHelper.sendChatMessage("Unloading all plugins...")

ConfigUtils.saveAll()
PluginManager.unloadAll()
ConfigUtils.loadAll()

val stopTime = System.currentTimeMillis() - time
MessageSendHelper.sendChatMessage("Unloaded all plugins, took $stopTime ms!")
}
}

literal("list") {
execute {
MessageSendHelper.sendChatMessage("Loaded plugins: ${formatValue(PluginManager.loadedPlugins.size)}")

if (PluginManager.loadedPlugins.isEmpty()) {
MessageSendHelper.sendRawChatMessage("No plugin loaded")
} else {
for ((index, plugin) in PluginManager.loadedPlugins.withIndex()) {
MessageSendHelper.sendRawChatMessage("${formatValue(index)}. ${formatValue(plugin.name)}")
}
}
}
}

literal("info") {
int("index") { indexArg ->
execute {
val index = indexArg.value
val plugin = PluginManager.loadedPlugins.toList().getOrNull(index)
?: run {
MessageSendHelper.sendChatMessage("No plugin found for index: ${formatValue(index)}")
return@execute
}
val loader = PluginManager.loadedPluginLoader[plugin.name]!!

sendPluginInfo(plugin, loader)
}
}

string( "plugin name") { nameArg ->
execute {
val name = nameArg.value
val plugin = PluginManager.loadedPlugins[name]
?: run {
MessageSendHelper.sendChatMessage("No plugin found for name: ${formatValue(name)}")
return@execute
}
val loader = PluginManager.loadedPluginLoader[plugin.name]!!

sendPluginInfo(plugin, loader)
}
}
}
}

private fun sendPluginInfo(plugin: Plugin, loader: PluginLoader) {
MessageSendHelper.sendChatMessage("Info for plugin: $loader")
MessageSendHelper.sendRawChatMessage(plugin.toString())
}
}
Loading

0 comments on commit fc7fc34

Please sign in to comment.