This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
forked from kami-blue/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[new] Plugin System (kami-blue#1773)
Co-authored-by: Dewy <[email protected]> Co-authored-by: lv <[email protected]>
- Loading branch information
1 parent
fe13e53
commit fc7fc34
Showing
78 changed files
with
1,447 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
src/main/kotlin/org/kamiblue/client/command/commands/PluginCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
Oops, something went wrong.