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

Commit

Permalink
Try to start adapting Nukkit2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WetABQ committed Mar 18, 2020
1 parent dd723b7 commit d2cc344
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/top/wetabq/easyapi/api/CompatibilityCheck.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package top.wetabq.easyapi.api

import cn.nukkit.Server
import top.wetabq.easyapi.EasyAPI

class CompatibilityCheck(private val providers: List<String>) {

Expand All @@ -9,7 +9,7 @@ class CompatibilityCheck(private val providers: List<String>) {
fun check() {
// TODO: Check server started
providers.forEach { plugin ->
if (Server.getInstance().pluginManager.getPlugin(plugin) != null) {
if (EasyAPI.server.pluginManager.getPlugin(plugin) != null) {
finalKey = plugin
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/top/wetabq/easyapi/api/default/AsyncTaskAPI.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import cn.nukkit.plugin.Plugin
import cn.nukkit.scheduler.AsyncTask
import top.wetabq.easyapi.EasyAPI
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)
EasyAPI.server.scheduler.scheduleAsyncTask(plugin, t)
return this
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/top/wetabq/easyapi/api/default/CommandAPI.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package top.wetabq.easyapi.api.default

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import cn.nukkit.event.Event
import cn.nukkit.event.HandlerList
import cn.nukkit.event.Listener
import cn.nukkit.plugin.Plugin
import cn.nukkit.utils.Utils
import top.wetabq.easyapi.EasyAPI
import top.wetabq.easyapi.api.CommonDynamicIntegrateAPI
import java.lang.reflect.Method
import java.util.*

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/top/wetabq/easyapi/api/default/PluginTaskAPI.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package top.wetabq.easyapi.api.default

import cn.nukkit.Server
import cn.nukkit.plugin.Plugin
import top.wetabq.easyapi.EasyAPI
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)
EasyAPI.server.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)
EasyAPI.server.scheduler.cancelTask(t.pluginTask.taskId)
return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package top.wetabq.easyapi.module.default

import cn.nukkit.Server
import cn.nukkit.event.Event
import cn.nukkit.plugin.Plugin
import cn.nukkit.scheduler.AsyncTask
Expand All @@ -25,7 +24,7 @@ suspend fun <T: Event> coroutineCallEvent(event: T, syncAction: (T) -> Unit = {}

fun <T: Event> asyncTaskCallEvent(event: T, plugin: Plugin, syncAction: (T) -> Unit = {}, asyncAction: (T) -> Unit) {
syncAction(event)
Server.getInstance().scheduler.scheduleAsyncTask(plugin, object: AsyncTask() {
EasyAPI.server.scheduler.scheduleAsyncTask(plugin, object: AsyncTask() {
override fun onRun() {
asyncAction(event)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package top.wetabq.easyapi.module.default

import cn.nukkit.Player
import cn.nukkit.Server
import cn.nukkit.event.EventHandler
import cn.nukkit.event.Listener
import cn.nukkit.event.player.PlayerChatEvent
Expand Down Expand Up @@ -56,7 +55,7 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() {
override fun format(message: String, data: String): String {
//IF data IS PLAYER NAME
var final = message
if (Server.getInstance().getPlayer(data) is Player) {
if (EasyAPI.server.getPlayer(data) is Player) {
if (data.contains(EASY_NAME_TAG_PLACEHOLDER)) {
final = final.replace(EASY_NAME_TAG_PLACEHOLDER, nameTagFormat)
}
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/top/wetabq/easyapi/utils/MerticsLite.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package top.wetabq.easyapi.utils

import cn.nukkit.Server
import cn.nukkit.plugin.Plugin
import cn.nukkit.plugin.service.NKServiceManager
import cn.nukkit.plugin.service.RegisteredServiceProvider
Expand All @@ -10,13 +9,13 @@ import com.google.common.base.Preconditions
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import top.wetabq.easyapi.EasyAPI
import java.io.*
import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Modifier
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.function.Consumer
import java.util.zip.GZIPOutputStream
import javax.net.ssl.HttpsURLConnection

Expand All @@ -43,11 +42,6 @@ class MerticsLite(var plugin: Plugin) {
// The uuid of the server
private var serverUUID: String? = null

/**
* Class constructor.
*
* @param plugin The plugin which stats should be submitted.
*/
init {
Preconditions.checkNotNull(plugin)
// Get the config file
Expand Down Expand Up @@ -93,7 +87,7 @@ class MerticsLite(var plugin: Plugin) {
if (enabled) {
var found = false
// Search for all other bStats Metrics classes to see if we are the first one
for (service in Server.getInstance().serviceManager.knownService) {
for (service in EasyAPI.server.serviceManager.knownService) {
try {
service.getField("B_STATS_VERSION") // Our identifier :)
found = true // We aren't the first
Expand All @@ -102,7 +96,7 @@ class MerticsLite(var plugin: Plugin) {
}
}
// Register our service
Server.getInstance().serviceManager.register(MerticsLite::class.java, this, plugin, ServicePriority.NORMAL)
EasyAPI.server.serviceManager.register(MerticsLite::class.java, this, plugin, ServicePriority.NORMAL)
if (!found) { // We are the first!
startSubmitting()
}
Expand Down Expand Up @@ -131,7 +125,7 @@ class MerticsLite(var plugin: Plugin) {
}
// Nevertheless we want our code to run in the Nukkit main thread, so we have to use the Nukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Server.getInstance().scheduler.scheduleTask(plugin) { submitData() }
EasyAPI.server.scheduler.scheduleTask(plugin) { submitData() }
}
}, 1000 * 60 * 5.toLong(), 1000 * 60 * 30.toLong())
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
Expand Down Expand Up @@ -162,10 +156,10 @@ class MerticsLite(var plugin: Plugin) {
* @return The server specific data.
*/
private fun getServerData(): JsonObject { // Minecraft specific data
val playerAmount = Server.getInstance().onlinePlayers.size
val onlineMode = if (Server.getInstance().getPropertyBoolean("xbox-auth", false)) 1 else 0
val minecraftVersion = Server.getInstance().version
val softwareName = Server.getInstance().name
val playerAmount = EasyAPI.server.onlinePlayers.size
val onlineMode = if (EasyAPI.server.getPropertyBoolean("xbox-auth", false)) 1 else 0
val minecraftVersion = EasyAPI.server.version
val softwareName = EasyAPI.server.name
// OS/Java specific data
val javaVersion = System.getProperty("java.version")
val osName = System.getProperty("os.name")
Expand Down Expand Up @@ -193,7 +187,7 @@ class MerticsLite(var plugin: Plugin) {
val data = getServerData()
val pluginData = JsonArray()
// Search for all other bStats Metrics classes to get their plugin data
Server.getInstance().serviceManager.knownService.forEach { service ->
EasyAPI.server.serviceManager.knownService.forEach { service ->
try {
service.getField("B_STATS_VERSION") // Our identifier :)
var providers: List<RegisteredServiceProvider<*>>? = null
Expand All @@ -203,7 +197,7 @@ class MerticsLite(var plugin: Plugin) {
val handle = NKServiceManager::class.java.getDeclaredField("handle")
field.setInt(handle, handle.modifiers and Modifier.FINAL.inv())
handle.isAccessible = true
providers = (handle[Server.getInstance().serviceManager] as Map<Class<*>?, List<RegisteredServiceProvider<*>>?>)[service]
providers = (handle[EasyAPI.server.serviceManager] as Map<Class<*>?, List<RegisteredServiceProvider<*>>?>)[service]
} catch (e: IllegalAccessException) { // Something went wrong! :(
if (logFailedRequests) {
plugin.logger.warning("Failed to link to metrics class " + service.name, e)
Expand Down Expand Up @@ -258,7 +252,7 @@ class MerticsLite(var plugin: Plugin) {
@Throws(Exception::class)
private fun sendData(plugin: Plugin, data: JsonObject) {
Preconditions.checkNotNull(data)
if (Server.getInstance().isPrimaryThread) {
if (EasyAPI.server.isPrimaryThread) {
throw IllegalAccessException("This method must not be called from the main thread!")
}
if (logSentData) {
Expand Down

0 comments on commit d2cc344

Please sign in to comment.