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

Commit

Permalink
1.1.5 add SimpleAsyncTaskAPI add AsyncListener
Browse files Browse the repository at this point in the history
  • Loading branch information
WetABQ committed Mar 19, 2020
1 parent b5f445b commit a9a9bea
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>top.wetabq.easyapi</groupId>
<artifactId>EasyAPI</artifactId>
<version>1.1.3</version>
<version>1.1.5</version>


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


import cn.nukkit.event.EventHandler
import cn.nukkit.event.Listener
import cn.nukkit.event.block.BlockBreakEvent
import cn.nukkit.event.block.BlockPlaceEvent
import cn.nukkit.event.entity.EntityArmorChangeEvent
import cn.nukkit.event.entity.EntityDamageByEntityEvent
import cn.nukkit.event.entity.EntityDamageEvent
import cn.nukkit.event.entity.EntityDeathEvent
import cn.nukkit.event.inventory.CraftItemEvent
import cn.nukkit.event.inventory.InventoryPickupItemEvent
import cn.nukkit.event.inventory.InventoryTransactionEvent
import cn.nukkit.event.level.WeatherChangeEvent
import cn.nukkit.event.player.*
import top.wetabq.easyapi.EasyAPI
import top.wetabq.easyapi.api.SimpleIntegrateAPI
import top.wetabq.easyapi.listener.AsyncListener
import top.wetabq.easyapi.module.defaults.asyncTaskCallEvent

object AsyncListenerAPI : SimpleIntegrateAPI, Listener, AsyncListener {

private val asyncListenerList = arrayListOf<AsyncListener>()

init {
EasyAPI.server.pluginManager.registerEvents(this, EasyAPI.INSTANCE)
}

fun add(t: AsyncListener): AsyncListenerAPI {
asyncListenerList.add(t)
return this
}

fun remove(t: AsyncListener): AsyncListenerAPI {
asyncListenerList.remove(t)
return this
}

@EventHandler
override fun onPlayerChatEvent(event: PlayerChatEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerChatEvent(event) }
}
}

@EventHandler
override fun onPlayerCommandPreprocessEvent(event: PlayerCommandPreprocessEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerCommandPreprocessEvent(event) }
}
}

@EventHandler
override fun onPlayerDeathEvent(event: PlayerDeathEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerDeathEvent(event) }
}
}

@EventHandler
override fun onPlayerBucketEmptyEvent(event: PlayerBucketEmptyEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerBucketEmptyEvent(event) }
}
}

@EventHandler
override fun onPlayerBucketFillEvent(event: PlayerBucketFillEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerBucketFillEvent(event) }
}
}

@EventHandler
override fun onPlayerDropItemEvent(event: PlayerDropItemEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerDropItemEvent(event) }
}
}

@EventHandler
override fun onPlayerEatFoodEvent(event: PlayerEatFoodEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerEatFoodEvent(event) }
}
}

@EventHandler
override fun onPlayerFoodLevelChangeEvent(event: PlayerFoodLevelChangeEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerFoodLevelChangeEvent(event) }
}
}

@EventHandler
override fun onPlayerFormRespondedEvent(event: PlayerFormRespondedEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerFormRespondedEvent(event) }
}
}

@EventHandler
override fun onPlayerGameModeChangeEvent(event: PlayerGameModeChangeEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerGameModeChangeEvent(event) }
}
}

@EventHandler
override fun onPlayerInteractEntityEvent(event: PlayerInteractEntityEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerInteractEntityEvent(event) }
}
}

@EventHandler
override fun onPlayerInteractEvent(event: PlayerInteractEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerInteractEvent(event) }
}
}

@EventHandler
override fun onPlayerItemHeldEvent(event: PlayerItemHeldEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerItemHeldEvent(event) }
}
}

@EventHandler
override fun onPlayerJoinEvent(event: PlayerJoinEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerJoinEvent(event) }
}
}

@EventHandler
override fun onPlayerLoginEvent(event: PlayerLoginEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerLoginEvent(event) }
}
}

@EventHandler
override fun onPlayerMoveEvent(event: PlayerMoveEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerMoveEvent(event) }
}
}

@EventHandler
override fun onPlayerPreLoginEvent(event: PlayerPreLoginEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerPreLoginEvent(event) }
}
}

@EventHandler
override fun onPlayerQuitEvent(event: PlayerQuitEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerQuitEvent(event) }
}
}

@EventHandler
override fun onPlayerRespawnEvent(event: PlayerRespawnEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerRespawnEvent(event) }
}
}

@EventHandler
override fun onPlayerTeleportEvent(event: PlayerTeleportEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onPlayerTeleportEvent(event) }
}
}

@EventHandler
override fun onBlockBreakEvent(event: BlockBreakEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onBlockBreakEvent(event) }
}
}

@EventHandler
override fun onBlockPlaceEvent(event: BlockPlaceEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onBlockPlaceEvent(event) }
}
}

@EventHandler
override fun onEntityArmorChangeEvent(event: EntityArmorChangeEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onEntityArmorChangeEvent(event) }
}
}

@EventHandler
override fun onEntityDamageByEntityEvent(event: EntityDamageByEntityEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onEntityDamageByEntityEvent(event) }
}
}

@EventHandler
override fun onEntityDamageEvent(event: EntityDamageEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onEntityDamageEvent(event) }
}
}

@EventHandler
override fun onEntityDeathEvent(event: EntityDeathEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onEntityDeathEvent(event) }
}
}

@EventHandler
override fun onCraftItemEvent(event: CraftItemEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onCraftItemEvent(event) }
}
}

@EventHandler
override fun onInventoryPickupItemEvent(event: InventoryPickupItemEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onInventoryPickupItemEvent(event) }
}
}

@EventHandler
override fun onInventoryTransactionEvent(event: InventoryTransactionEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onInventoryTransactionEvent(event) }
}
}

@EventHandler
override fun onWeatherChangeEvent(event: WeatherChangeEvent) {
asyncTaskCallEvent(event, EasyAPI.INSTANCE) {
asyncListenerList.forEach { it.onWeatherChangeEvent(event) }
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package top.wetabq.easyapi.api.defaults

import cn.nukkit.scheduler.AsyncTask
import top.wetabq.easyapi.EasyAPI
import top.wetabq.easyapi.api.SimpleIntegrateAPI

object SimpleAsyncTaskAPI : SimpleIntegrateAPI {

fun add(action: () -> Unit): AsyncTask {
val task =
object: AsyncTask() {
override fun onRun() {
action()
}
}
EasyAPI.server.scheduler.scheduleAsyncTask(EasyAPI.INSTANCE, task)
return task
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package top.wetabq.easyapi.api.defaults

import cn.nukkit.scheduler.PluginTask
import top.wetabq.easyapi.EasyAPI
import top.wetabq.easyapi.api.SimpleIntegrateAPI

object SimplePluginTaskAPI : SimpleIntegrateAPI {

fun delay(delay: Int, action: (Int) -> Unit): PluginTask<EasyAPI> {
val task =
object: PluginTask<EasyAPI>(EasyAPI.INSTANCE) {

override fun onRun(currentTick: Int) {
action(currentTick)
}
}
EasyAPI.server.scheduler.scheduleDelayedTask(task, delay)
return task
}

fun repeating(period: Int, action: (Int) -> Unit): PluginTask<EasyAPI> {
val task =
object: PluginTask<EasyAPI>(EasyAPI.INSTANCE) {

override fun onRun(currentTick: Int) {
action(currentTick)
}
}
EasyAPI.server.scheduler.scheduleRepeatingTask(task, period)
return task
}

fun delayRepeating(delay: Int, period: Int, action: (Int) -> Unit): PluginTask<EasyAPI> {
val task =
object: PluginTask<EasyAPI>(EasyAPI.INSTANCE) {

override fun onRun(currentTick: Int) {
action(currentTick)
}
}
EasyAPI.server.scheduler.scheduleDelayedRepeatingTask(task, delay, period)
return task
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import top.wetabq.easyapi.config.encoder.CodecEasyConfig

abstract class AdvanceCodecEasyConfig<T>(
configName:String,
plugin: Plugin
plugin: Plugin,
private val defaultValue: T
) : CodecEasyConfig<T>(configName, plugin),
AdvanceConfigCodec<T> {

Expand All @@ -29,4 +30,9 @@ abstract class AdvanceCodecEasyConfig<T>(
configSection.putAll(encodeMap)
}

fun safeGetData(key: String): T {
if (!simpleConfig.containsKey(key)) simpleConfig[key] = defaultValue
return simpleConfig[key]?:defaultValue
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ abstract class SimpleCodecEasyConfig<T>(
configName:String,
val plugin: Plugin,
val clazzT: Class<T>,
defaultValue: T,
private val codec: AdvanceConfigCodec<T> = ReflectionConfigCodec(
clazzT
)
) : AdvanceCodecEasyConfig<T>(configName, plugin) {
) : AdvanceCodecEasyConfig<T>(configName, plugin, defaultValue) {

override fun encode(obj: T): LinkedHashMap<String, *> = codec.encode(obj)

Expand Down
Loading

0 comments on commit a9a9bea

Please sign in to comment.