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

Commit

Permalink
1.1.0 add task api, fix screen show bug
Browse files Browse the repository at this point in the history
  • Loading branch information
WetABQ committed Mar 17, 2020
1 parent 2bc3552 commit 634c274
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>top.wetabq.easyapi</groupId>
<artifactId>EasyAPI</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>


<licenses>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/top/wetabq/easyapi/api/CommonDynamicIntegrateAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package top.wetabq.easyapi.api


abstract class CommonDynamicIntegrateAPI<T, out I: DynamicIntegrateAPI<*, I>> : DynamicIntegrateAPI<T, I> {

protected val interfaceList = arrayListOf<T>()

override fun add(t: T): I {
interfaceList.add(t)
return addInterface(t)
}

override fun remove(t: T): I {
interfaceList.remove(t)
return removeInterface(t)
}

abstract fun addInterface(t: T): I

abstract fun removeInterface(t: T): I

override fun getAll(): Collection<T> = interfaceList

override fun removeAll() {
interfaceList.forEach { remove(it) }
}

}
21 changes: 21 additions & 0 deletions src/main/java/top/wetabq/easyapi/api/default/AsyncTaskAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import cn.nukkit.plugin.Plugin
import cn.nukkit.scheduler.AsyncTask
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI

class AsyncTaskAPI(private val plugin: Plugin) : CommonDynamicIntegrateAPI<AsyncTask, AsyncTaskAPI>() {

override fun addInterface(t: AsyncTask): AsyncTaskAPI {
Server.getInstance().scheduler.scheduleAsyncTask(plugin, t)
return this
}

override fun removeInterface(t: AsyncTask): AsyncTaskAPI {
Server.getInstance().scheduler.scheduleAsyncTask(plugin, t)
return this
}


}
20 changes: 4 additions & 16 deletions src/main/java/top/wetabq/easyapi/api/default/CommandAPI.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import top.wetabq.easyapi.api.DynamicIntegrateAPI
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import top.wetabq.easyapi.command.EasyCommand

class CommandAPI: DynamicIntegrateAPI<EasyCommand, CommandAPI> {
class CommandAPI: CommonDynamicIntegrateAPI<EasyCommand, CommandAPI>() {

private val commandList = arrayListOf<EasyCommand>()

override fun add(t: EasyCommand): CommandAPI {
commandList.add(t)
override fun addInterface(t: EasyCommand): CommandAPI {
Server.getInstance().commandMap.register( "", t)
return this
}

override fun remove(t: EasyCommand): CommandAPI {
override fun removeInterface(t: EasyCommand): CommandAPI {
// NOT SUPPORT ACTUALLY REMOVE
commandList.remove(t)
return this
}

override fun getAll(): Collection<EasyCommand> {
return commandList
}

override fun removeAll() {
commandList.forEach { t -> remove(t) }
}


}
16 changes: 4 additions & 12 deletions src/main/java/top/wetabq/easyapi/api/default/ConfigAPI.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package top.wetabq.easyapi.api.default

import top.wetabq.easyapi.api.DynamicIntegrateAPI
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import top.wetabq.easyapi.config.EasyConfig

class ConfigAPI: DynamicIntegrateAPI<EasyConfig, ConfigAPI> {
class ConfigAPI: CommonDynamicIntegrateAPI<EasyConfig, ConfigAPI>() {

private val configList = arrayListOf<EasyConfig>()

override fun add(t: EasyConfig): ConfigAPI {
configList.add(t)
override fun addInterface(t: EasyConfig): ConfigAPI {
return this
}

override fun remove(t: EasyConfig): ConfigAPI {
override fun removeInterface(t: EasyConfig): ConfigAPI {
t.save()
return this
}

override fun getAll(): Collection<EasyConfig> = configList

override fun removeAll() {
configList.forEach { t -> remove(t) }
}

}
17 changes: 4 additions & 13 deletions src/main/java/top/wetabq/easyapi/api/default/NukkitListenerAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import cn.nukkit.event.HandlerList
import cn.nukkit.event.Listener
import cn.nukkit.plugin.Plugin
import cn.nukkit.utils.Utils
import top.wetabq.easyapi.api.DynamicIntegrateAPI
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import java.lang.reflect.Method
import java.util.*

class NukkitListenerAPI(private val plugin: Plugin) : DynamicIntegrateAPI<Listener, NukkitListenerAPI> {
class NukkitListenerAPI(private val plugin: Plugin) : CommonDynamicIntegrateAPI<Listener, NukkitListenerAPI>() {

private val listeners = arrayListOf<Listener>()

override fun add(t: Listener): NukkitListenerAPI {
listeners.add(t)
override fun addInterface(t: Listener): NukkitListenerAPI {
Server.getInstance().pluginManager.registerEvents(t, plugin)
return this
}

override fun remove(t: Listener): NukkitListenerAPI {
override fun removeInterface(t: Listener): NukkitListenerAPI {
var methods: Set<Method> = setOf()
try {
val publicMethods: Array<Method> = t.javaClass.methods
Expand Down Expand Up @@ -81,10 +78,4 @@ class NukkitListenerAPI(private val plugin: Plugin) : DynamicIntegrateAPI<Listen
}
}

override fun removeAll() {
listeners.forEach { t -> remove(t) }
}

override fun getAll() : Collection<Listener> = listeners

}
21 changes: 21 additions & 0 deletions src/main/java/top/wetabq/easyapi/api/default/PluginTaskAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import cn.nukkit.plugin.Plugin
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import top.wetabq.easyapi.task.PluginTaskEntry

class PluginTaskAPI<T: Plugin>(private val plugin: Plugin) : CommonDynamicIntegrateAPI<PluginTaskEntry<T>, PluginTaskAPI<T>>() {

override fun addInterface(t: PluginTaskEntry<T>): PluginTaskAPI<T> {
Server.getInstance().scheduler.scheduleDelayedRepeatingTask(plugin, t.pluginTask, t.delay, t.period)
return this
}

override fun removeInterface(t: PluginTaskEntry<T>): PluginTaskAPI<T> {
Server.getInstance().scheduler.cancelTask(t.pluginTask.taskId)
return this
}


}
16 changes: 4 additions & 12 deletions src/main/java/top/wetabq/easyapi/api/default/SimpleCommandAPI.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
package top.wetabq.easyapi.api.default

import top.wetabq.easyapi.api.DynamicIntegrateAPI
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import top.wetabq.easyapi.command.EasySubCommand
import top.wetabq.easyapi.command.default.EasyAPICommand

/**
* Use EasyAPIModule build-in command to add sub-command
* Usage: /eapi
*/
class SimpleCommandAPI : DynamicIntegrateAPI<EasySubCommand, SimpleCommandAPI> {
class SimpleCommandAPI : CommonDynamicIntegrateAPI<EasySubCommand, SimpleCommandAPI>() {

private val commandList = arrayListOf<EasySubCommand>()

override fun add(t: EasySubCommand): SimpleCommandAPI {
override fun addInterface(t: EasySubCommand): SimpleCommandAPI {
EasyAPICommand.subCommand.add(t)
EasyAPICommand.loadCommandBase()
return this
}

override fun remove(t: EasySubCommand): SimpleCommandAPI {
override fun removeInterface(t: EasySubCommand): SimpleCommandAPI {
EasyAPICommand.subCommand.remove(t)
EasyAPICommand.loadCommandBase()
return this
}

override fun getAll(): Collection<EasySubCommand> = commandList

override fun removeAll() {
commandList.forEach { t -> remove(t) }
}

}
15 changes: 4 additions & 11 deletions src/main/java/top/wetabq/easyapi/api/default/SimpleConfigAPI.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.plugin.Plugin
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import top.wetabq.easyapi.api.DisableNotRemoveAll
import top.wetabq.easyapi.api.DynamicIntegrateAPI
import top.wetabq.easyapi.config.default.SimpleConfig
import top.wetabq.easyapi.config.default.SimpleConfigEntry

@DisableNotRemoveAll
class SimpleConfigAPI(private val plugin: Plugin): DynamicIntegrateAPI<SimpleConfigEntry<*>, SimpleConfigAPI> {
class SimpleConfigAPI(private val plugin: Plugin): CommonDynamicIntegrateAPI<SimpleConfigEntry<*>, SimpleConfigAPI>() {

private val simpleConfigEntryList = arrayListOf<SimpleConfigEntry<*>>()

override fun add(t: SimpleConfigEntry<*>): SimpleConfigAPI {
override fun addInterface(t: SimpleConfigEntry<*>): SimpleConfigAPI {
SimpleConfig.addPath(t.path, plugin, t.value?:"")
SimpleConfig.save()
return this
}

override fun remove(t: SimpleConfigEntry<*>): SimpleConfigAPI {
override fun removeInterface(t: SimpleConfigEntry<*>): SimpleConfigAPI {
SimpleConfig.removePath(t.path, plugin)
SimpleConfig.save()
return this
Expand All @@ -39,11 +37,6 @@ class SimpleConfigAPI(private val plugin: Plugin): DynamicIntegrateAPI<SimpleCon
SimpleConfig.setPathValue(key.path, plugin, key.value?:"")
}

override fun getAll(): Collection<SimpleConfigEntry<*>> = simpleConfigEntryList

override fun removeAll() {
simpleConfigEntryList.forEach { t -> remove(t) }
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package top.wetabq.easyapi.module.default

import cn.nukkit.Player
import cn.nukkit.scheduler.PluginTask
import cn.nukkit.utils.TextFormat
import top.wetabq.easyapi.EasyAPI
import top.wetabq.easyapi.api.default.PluginTaskAPI
import top.wetabq.easyapi.module.ModuleInfo
import top.wetabq.easyapi.module.ModuleVersion
import top.wetabq.easyapi.module.SimpleEasyAPIModule
import top.wetabq.easyapi.screen.ScreenShow
import top.wetabq.easyapi.screen.ShowType
import top.wetabq.easyapi.task.PluginTaskEntry
import kotlin.math.floor

object ScreenShowModule : SimpleEasyAPIModule() {
Expand All @@ -27,6 +30,8 @@ object ScreenShowModule : SimpleEasyAPIModule() {
private val screenShowList = arrayListOf<ScreenShow>()
private val alternateList = arrayListOf<ScreenShow>()

const val SHOW_TASK = "showTask"

override fun getModuleInfo(): ModuleInfo = ModuleInfo(
EasyAPI,
EasyBaseModule.MODULE_NAME,
Expand All @@ -35,11 +40,22 @@ object ScreenShowModule : SimpleEasyAPIModule() {
)

override fun moduleRegister() {
this.registerAPI(SHOW_TASK, PluginTaskAPI<EasyAPI>(EasyAPI))
.add(PluginTaskEntry(
object : PluginTask<EasyAPI>(EasyAPI) {

override fun onRun(p0: Int) {
sendAll()
}

},
5
))
}

override fun moduleDisable() {

screenShowList.clear()
alternateList.clear()
}

/*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/top/wetabq/easyapi/task/PluginTaskEntry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package top.wetabq.easyapi.task

import cn.nukkit.plugin.Plugin
import cn.nukkit.scheduler.PluginTask

data class PluginTaskEntry<T: Plugin> (
var pluginTask: PluginTask<T>,
var period: Int,
var delay: Int = 0
)

0 comments on commit 634c274

Please sign in to comment.