Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

Commit b5f445b

Browse files
committed
fix some problem, add module switch
1 parent 9931669 commit b5f445b

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

src/main/java/top/wetabq/easyapi/api/defaults/AsyncTaskAPI.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AsyncTaskAPI(private val plugin: Plugin) : CommonDynamicIntegrateAPI<Async
1313
}
1414

1515
override fun removeInterface(t: AsyncTask): AsyncTaskAPI {
16-
EasyAPI.server.scheduler.scheduleAsyncTask(plugin, t)
16+
if (!t.isFinished) EasyAPI.server.scheduler.cancelTask(t.taskId)
1717
return this
1818
}
1919

src/main/java/top/wetabq/easyapi/command/EasyCommand.kt

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,78 @@ abstract class EasyCommand(private val command: String, description: String = "E
1212
val subCommand = ArrayList<EasySubCommand>()
1313

1414
init {
15+
this.setDescription(description)
1516
loadCommandBase()
17+
addHelp()
1618
}
1719

1820
fun loadCommandBase() {
19-
this.setDescription(description)
2021
this.setCommandParameters(object : HashMap<String, Array<CommandParameter>>() {
2122
init {
2223
var i = 1
2324
subCommand.forEach { sc ->
2425
val strs = StringBuilder()
25-
strs.let{ sc.getAliases().forEach { aliases -> it.append("$aliases/") }}
26+
strs.let{ sc.getAliases()?.forEach { aliases -> it.append("$aliases/") }}
27+
if (strs.toString() != "") strs.insert(0, "(").append(")")
2628
if (sc.getArguments() != null) {
27-
val commandParameters = arrayOf(CommandParameter("${sc.subCommandName}($strs)", false, sc.getAliases()))
29+
val commandParameters = arrayOf(CommandParameter("${sc.subCommandName}$strs", false, sc.getAliases()))
2830
sc.getArguments()?.forEach { argSetting -> commandParameters.plus(CommandParameter(argSetting.argName,argSetting.argType,argSetting.optional)) }
2931
put("${i}arg", commandParameters)
30-
} else put("${i}arg", arrayOf(CommandParameter("${sc.subCommandName}(${strs})", false, sc.getAliases())))
32+
} else put("${i}arg", arrayOf(CommandParameter("${sc.subCommandName}${strs}", false, sc.getAliases())))
3133
i++
3234
}
3335
}
3436
})
3537
this.usage = "/${command} <subcommand> [args]"
3638
}
3739

40+
fun addHelp() {
41+
this.subCommand.add(object: EasySubCommand("help") {
42+
43+
override fun getArguments(): Array<CommandArgument>? = null
44+
45+
override fun getAliases(): Array<String>? = null
46+
47+
override fun getDescription(): String = "View help"
48+
49+
override fun execute(sender: CommandSender, label: String, args: Array<out String>): Boolean {
50+
sendHelp(sender)
51+
return true
52+
}
53+
54+
})
55+
}
56+
3857
override fun execute(sender: CommandSender, label: String, args: Array<out String>): Boolean {
3958
if (args.isEmpty()) {
4059
sendHelp(sender)
4160
return true
4261
}
4362
subCommand.forEach { sc ->
4463
if (args[0] == sc.subCommandName) {
45-
return sc.execute(sender, label, args)
64+
val result = sc.safeExecute(sender, label, args)
65+
if (!result) sendHelp(sender)
66+
return result
4667
}
47-
for (aliases in sc.getAliases()) {
48-
if (args[0] == aliases) return sc.execute(sender, label, args)
68+
sc.getAliases()?.forEach { aliases ->
69+
if (args[0] == aliases) {
70+
val result = sc.safeExecute(sender, label, args)
71+
if (!result) sendHelp(sender)
72+
return result
73+
}
4974
}
5075
}
5176
sendHelp(sender)
5277
return true
5378
}
5479

55-
fun sendHelp(sender: CommandSender) {
80+
open fun sendHelp(sender: CommandSender) {
5681
sender.sendMessage(TextFormat.GOLD.toString() + "----Help----")
5782
subCommand.forEach { sc ->
5883
val strs = StringBuilder()
59-
sc.getAliases().forEach {aliases -> strs.append(aliases) }
60-
sender.sendMessage("${TextFormat.AQUA}/${command} ${sc.subCommandName}($strs) - ${sc.getDescription()}")
84+
sc.getAliases()?.forEach {aliases -> strs.append(aliases) }
85+
if (strs.toString() != "") strs.insert(0, "(").append(")")
86+
sender.sendMessage("${TextFormat.AQUA}/${command} ${sc.subCommandName}$strs - ${sc.getDescription()}")
6187
}
6288
}
6389

src/main/java/top/wetabq/easyapi/command/EasySubCommand.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ abstract class EasySubCommand(val subCommandName: String) {
66

77
abstract fun getArguments(): Array<CommandArgument>?
88

9-
abstract fun getAliases(): Array<String>
9+
abstract fun getAliases(): Array<String>?
1010

1111
abstract fun getDescription() : String
1212

1313
abstract fun execute(sender: CommandSender, label: String, args: Array<out String>): Boolean
1414

15+
fun safeExecute(sender: CommandSender, label: String, args: Array<out String>): Boolean {
16+
if ((getArguments()?:arrayOf()).size == args.size - 1) return false
17+
return execute(sender, label, args)
18+
}
19+
1520
}

src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class ReflectionConfigCodec<T>(private val clazz: Class<T>) :
4545
return constructor.newInstance(*arrayList.toArray())
4646
}
4747

48-
private fun isJavaClass(clz: Class<*>?): Boolean {
49-
return clz != null && clz.classLoader == null
48+
private fun isJavaClass(clz: Class<*>): Boolean {
49+
return (clz != null && clz.classLoader == null) || clz.packageName.startsWith("kotlin") || clz.packageName.startsWith("java")
5050
}
5151

5252

src/main/java/top/wetabq/easyapi/module/EasyAPIModuleManager.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ object EasyAPIModuleManager {
2828

2929
fun disable(module: IEasyAPIModule) {
3030
module.disable()
31+
modules.remove(module.getModuleInfo())
3132
module.getAllIntegrateAPI().forEach { api ->
3233
if (!api.javaClass.isAnnotationPresent(DisableNotRemoveAll::class.java)) api.removeAll()
3334
}
35+
val info = module.getModuleInfo()
36+
EasyAPI.INSTANCE.logger.warning("Module ${info.name}_${info.moduleVersion} from ${info.moduleOwner.name} by ${info.author} disabled")
3437
}
3538

3639
fun disableAll() {
37-
modules.forEach { (info, module) ->
38-
disable(module)
39-
EasyAPI.INSTANCE.logger.warning("Module ${info.name}_${info.moduleVersion} from ${info.moduleOwner.name} by ${info.author} disabled")
40-
}
40+
modules.values.forEach { disable(it) }
4141
}
4242

4343
fun getModule(moduleInfo: ModuleInfo) : IEasyAPIModule? = modules[moduleInfo]
4444

45+
fun getAllModule(): Map<ModuleInfo, IEasyAPIModule> = modules
46+
4547

4648

4749
}

src/main/java/top/wetabq/easyapi/module/defaults/EasyBaseModule.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package top.wetabq.easyapi.module.defaults
22

3+
import cn.nukkit.scheduler.AsyncTask
34
import top.wetabq.easyapi.EasyAPI
5+
import top.wetabq.easyapi.api.defaults.AsyncTaskAPI
46
import top.wetabq.easyapi.api.defaults.CommandAPI
57
import top.wetabq.easyapi.api.defaults.ConfigAPI
68
import top.wetabq.easyapi.api.defaults.SimpleConfigAPI
79
import top.wetabq.easyapi.command.defaults.EasyAPICommand
810
import top.wetabq.easyapi.config.defaults.SimpleConfig
911
import top.wetabq.easyapi.config.defaults.SimpleConfigEntry
12+
import top.wetabq.easyapi.module.EasyAPIModuleManager
1013
import top.wetabq.easyapi.module.ModuleInfo
1114
import top.wetabq.easyapi.module.ModuleVersion
1215
import top.wetabq.easyapi.module.SimpleEasyAPIModule
@@ -20,8 +23,10 @@ object EasyBaseModule: SimpleEasyAPIModule() {
2023
const val SIMPLE_COMMAND = "simpleCommand"
2124
const val SIMPLE_CONFIG = "simpleConfig"
2225
const val EASY_API_CONFIG = "easyAPIConfig"
26+
const val UNREGISTER_MODULE_TASK = "unregisterModuleTask"
2327

2428
const val TITLE_PATH = "title"
29+
const val MODULE_PATH = "module"
2530

2631
override fun getModuleInfo(): ModuleInfo = ModuleInfo(
2732
EasyAPI.INSTANCE,
@@ -40,6 +45,17 @@ object EasyBaseModule: SimpleEasyAPIModule() {
4045
val easyAPIConfig = this.registerAPI(EASY_API_CONFIG, SimpleConfigAPI(this.getModuleInfo().moduleOwner))
4146
.add(SimpleConfigEntry(TITLE_PATH, "&c[&eEasy&aAPI&c]".color()))
4247

48+
this.registerAPI(UNREGISTER_MODULE_TASK, AsyncTaskAPI(this.getModuleInfo().moduleOwner))
49+
.add(object: AsyncTask() {
50+
override fun onRun() {
51+
EasyAPIModuleManager.getAllModule().forEach { (info, module) ->
52+
val moduleSwitchPath = "$MODULE_PATH.${info.moduleOwner.name}.${info.name}"
53+
easyAPIConfig.add(SimpleConfigEntry(moduleSwitchPath, true))
54+
if (!(easyAPIConfig.getPathValue(moduleSwitchPath) as String).toBoolean()) EasyAPIModuleManager.disable(module)
55+
}
56+
}
57+
})
58+
4359
EasyAPI.TITLE = easyAPIConfig.getPathValue(TITLE_PATH) as String
4460

4561
}

0 commit comments

Comments
 (0)