From 970a208a046e84aa2a47efb467469cd834689e3e Mon Sep 17 00:00:00 2001 From: wetabq Date: Sun, 5 Apr 2020 23:57:20 +0800 Subject: [PATCH] [1.1.12.0] fix Kotlin data class constructor params not match, ChatNameTagFormatModule task not cancel when disabled. fix ReflectionConfigCodec can not deep decode. [1.1.10.2] fix EconomyAPI only use Player getMoney, path add `.` --- pom.xml | 2 +- .../wetabq/easyapi/api/defaults/EconomyAPI.kt | 23 ++++++++++--- .../easyapi/config/defaults/SimpleConfig.kt | 2 +- .../encoder/advance/ReflectionConfigCodec.kt | 33 ++++++++++++++++--- .../java/top/wetabq/easyapi/gui/ConfigGUI.kt | 6 +++- .../defaults/ChatNameTagFormatModule.kt | 7 ++-- 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index c7cac8d..f64493f 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ top.wetabq.easyapi EasyAPI - 1.1.10 + 1.1.12.0 diff --git a/src/main/java/top/wetabq/easyapi/api/defaults/EconomyAPI.kt b/src/main/java/top/wetabq/easyapi/api/defaults/EconomyAPI.kt index e53fba5..5267633 100644 --- a/src/main/java/top/wetabq/easyapi/api/defaults/EconomyAPI.kt +++ b/src/main/java/top/wetabq/easyapi/api/defaults/EconomyAPI.kt @@ -18,7 +18,21 @@ object EconomyAPI : SimpleIntegrateAPI { } } - fun getMoney(player: Player): Double? { + fun getMoney(player: Player): Double? = getMoney(player.name) + + fun addMoney(player: Player, amount: Double) { + addMoney(player.name, amount) + } + + fun reduceMoney(player: Player, amount: Double) { + reduceMoney(player.name, amount) + } + + fun setMoney(player: Player, amount: Double) { + setMoney(player.name, amount) + } + + fun getMoney(player: String): Double? { return compatibilityCheck.doCompatibilityAction(mapOf( ECONOMY_API to { EconomyAPI.getInstance().myMoney(player) @@ -26,7 +40,7 @@ object EconomyAPI : SimpleIntegrateAPI { )) } - fun addMoney(player: Player, amount: Double) { + fun addMoney(player: String, amount: Double) { compatibilityCheck.doCompatibilityAction(mapOf( ECONOMY_API to { EconomyAPI.getInstance().addMoney(player, amount) @@ -34,7 +48,7 @@ object EconomyAPI : SimpleIntegrateAPI { )) } - fun reduceMoney(player: Player, amount: Double) { + fun reduceMoney(player: String, amount: Double) { compatibilityCheck.doCompatibilityAction(mapOf( ECONOMY_API to { EconomyAPI.getInstance().reduceMoney(player, amount) @@ -42,7 +56,7 @@ object EconomyAPI : SimpleIntegrateAPI { )) } - fun setMoney(player: Player, amount: Double) { + fun setMoney(player: String, amount: Double) { compatibilityCheck.doCompatibilityAction(mapOf( ECONOMY_API to { EconomyAPI.getInstance().setMoney(player, amount) @@ -50,4 +64,5 @@ object EconomyAPI : SimpleIntegrateAPI { )) } + } \ No newline at end of file diff --git a/src/main/java/top/wetabq/easyapi/config/defaults/SimpleConfig.kt b/src/main/java/top/wetabq/easyapi/config/defaults/SimpleConfig.kt index 5a51b0b..9cf12fd 100644 --- a/src/main/java/top/wetabq/easyapi/config/defaults/SimpleConfig.kt +++ b/src/main/java/top/wetabq/easyapi/config/defaults/SimpleConfig.kt @@ -36,6 +36,6 @@ object SimpleConfig : CodecEasyConfig( fun directlyGetPathValue(path: String): Any? = simpleConfig[path] - fun getPath(path: String, plugin: Plugin) : String = plugin.description.name + path + fun getPath(path: String, plugin: Plugin) : String = plugin.description.name + if(path.startsWith(".")) path else ".$path" } \ No newline at end of file diff --git a/src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt b/src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt index 216c8ae..0cb0b1d 100644 --- a/src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt +++ b/src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt @@ -95,7 +95,7 @@ class ReflectionConfigCodec(private val clazz: Class) : when { valueType.actualTypeArguments[1] is ParameterizedType && (valueType.actualTypeArguments[1] as ParameterizedType).typeName.contains("LinkedHashMap") -> decodeMap[s] = reflectDecodeMap(any as LinkedHashMap, valueType.actualTypeArguments[1] as ParameterizedType) valueType.actualTypeArguments[1] is ParameterizedType && (valueType.actualTypeArguments[1] as ParameterizedType).typeName.contains("ArrayList") -> decodeMap[s] = reflectDecodeList(any as ArrayList<*>, valueType.actualTypeArguments[1] as ParameterizedType) - isJavaClass(valueType.actualTypeArguments[0] as Class<*>) -> decodeMap[s] = any + isJavaClass(valueType.actualTypeArguments[1] as Class<*>) -> decodeMap[s] = any else -> { decodeMap[s] = reflectDecode(any as LinkedHashMap, valueType.actualTypeArguments[1] as Class<*>) /*any.javaClass.declaredFields.forEach { field -> @@ -142,14 +142,37 @@ class ReflectionConfigCodec(private val clazz: Class) : private fun reflectDecode(map: LinkedHashMap, clazz: Class): Any { val constructor = clazz.constructors[0] - val arrayList = arrayListOf() + val arrayList = arrayListOf() val mapValueList = map.values.toList() constructor.parameterTypes.forEachIndexed { index, it -> if (isJavaClass(it)) { when { - Map::class.java.isAssignableFrom(it) -> arrayList.add(reflectDecodeMap(mapValueList[index] as LinkedHashMap, clazz.declaredFields[index].genericType as ParameterizedType)) - List::class.java.isAssignableFrom(it) -> arrayList.add(reflectDecodeList(mapValueList[index] as ArrayList<*>, (clazz.declaredFields[index].genericType as ParameterizedType))) - else -> arrayList.add(mapValueList[index]) + Map::class.java.isAssignableFrom(it) -> arrayList.add( + reflectDecodeMap( + mapValueList[index] as LinkedHashMap, + clazz.declaredFields[index].genericType as ParameterizedType + ) + ) + List::class.java.isAssignableFrom(it) -> arrayList.add( + reflectDecodeList( + mapValueList[index] as ArrayList<*>, + (clazz.declaredFields[index].genericType as ParameterizedType) + ) + ) + else -> { + if (index < mapValueList.size) { + arrayList.add(mapValueList[index]) + } else { + if (it.name.contains("DefaultConstructorMarker")) { + /*val sbKotlin = Class.forName("kotlin.jvm.internal.DefaultConstructorMarker") + .getDeclaredConstructor() + sbKotlin.isAccessible = true*/ + arrayList.add(null) + } else { + arrayList.add(1) + } + } + } } } else { arrayList.add(reflectDecode(mapValueList[index] as LinkedHashMap, it)) diff --git a/src/main/java/top/wetabq/easyapi/gui/ConfigGUI.kt b/src/main/java/top/wetabq/easyapi/gui/ConfigGUI.kt index d965da8..3d77e4d 100644 --- a/src/main/java/top/wetabq/easyapi/gui/ConfigGUI.kt +++ b/src/main/java/top/wetabq/easyapi/gui/ConfigGUI.kt @@ -57,7 +57,7 @@ class ConfigGUI( .forEach { value -> params.add(value) } try { val clazz = simpleCodecEasyConfig.clazzT - val arrayParam = arrayListOf() + val arrayParam = arrayListOf() params.forEach { param -> arrayParam.add(autoType(param.toString())) } if (translatedMap.isNotEmpty()) { translatedMap.forEach { (key, value) -> @@ -69,6 +69,10 @@ class ConfigGUI( } } } + if (arrayParam.toArray().size < clazz.constructors[0].parameterTypes.size) { //sbKotlin + arrayParam.add(1) + arrayParam.add(null) + } val newDataObj = clazz.constructors[0].newInstance(*arrayParam.toArray()) as T if (id is String) { player.sendMessage("&e${simpleCodecEasyConfig.plugin.name} > &aSave successfully".color()) diff --git a/src/main/java/top/wetabq/easyapi/module/defaults/ChatNameTagFormatModule.kt b/src/main/java/top/wetabq/easyapi/module/defaults/ChatNameTagFormatModule.kt index f09910e..4933c8c 100644 --- a/src/main/java/top/wetabq/easyapi/module/defaults/ChatNameTagFormatModule.kt +++ b/src/main/java/top/wetabq/easyapi/module/defaults/ChatNameTagFormatModule.kt @@ -8,6 +8,7 @@ import cn.nukkit.event.player.PlayerChatEvent import cn.nukkit.event.player.PlayerDeathEvent import cn.nukkit.event.player.PlayerJoinEvent import cn.nukkit.event.player.PlayerRespawnEvent +import cn.nukkit.scheduler.PluginTask import top.wetabq.easyapi.EasyAPI import top.wetabq.easyapi.api.defaults.* import top.wetabq.easyapi.config.defaults.SimpleConfigEntry @@ -37,6 +38,8 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() { const val NAME_TAG_FORMATTER = "nameTagFormatter" const val CHAT_FORMATTER = "chatFormatter" + lateinit var nameTagChangeTask: PluginTask<*> + override fun getModuleInfo(): ModuleInfo = ModuleInfo( EasyAPI.INSTANCE, MODULE_NAME, @@ -116,7 +119,7 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() { - SimplePluginTaskAPI.repeating(refreshNameTagPeriod) { _, _ -> + nameTagChangeTask = SimplePluginTaskAPI.repeating(refreshNameTagPeriod) { _, _ -> getModuleInfo().moduleOwner.server.onlinePlayers.values.forEach { player -> if (player.isAlive) player.nameTag = MessageFormatAPI.format(nameTagFormat.color(), player.name) } @@ -125,7 +128,7 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() { } override fun moduleDisable() { - + nameTagChangeTask.cancel() }