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

Commit

Permalink
[1.1.12.0] fix Kotlin data class constructor params not match, ChatNa…
Browse files Browse the repository at this point in the history
…meTagFormatModule task not cancel when disabled. fix ReflectionConfigCodec can not deep decode.

[1.1.10.2] fix EconomyAPI only use Player getMoney, path add `.`
  • Loading branch information
WetABQ committed Apr 5, 2020
1 parent 687048f commit 970a208
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 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.10</version>
<version>1.1.12.0</version>


<licenses>
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/top/wetabq/easyapi/api/defaults/EconomyAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,51 @@ 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)
}
))
}

fun addMoney(player: Player, amount: Double) {
fun addMoney(player: String, amount: Double) {
compatibilityCheck.doCompatibilityAction(mapOf(
ECONOMY_API to {
EconomyAPI.getInstance().addMoney(player, amount)
}
))
}

fun reduceMoney(player: Player, amount: Double) {
fun reduceMoney(player: String, amount: Double) {
compatibilityCheck.doCompatibilityAction(mapOf(
ECONOMY_API to {
EconomyAPI.getInstance().reduceMoney(player, amount)
}
))
}

fun setMoney(player: Player, amount: Double) {
fun setMoney(player: String, amount: Double) {
compatibilityCheck.doCompatibilityAction(mapOf(
ECONOMY_API to {
EconomyAPI.getInstance().setMoney(player, amount)
}
))
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ object SimpleConfig : CodecEasyConfig<Any>(

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"

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ReflectionConfigCodec<T>(private val clazz: Class<T>) :
when {
valueType.actualTypeArguments[1] is ParameterizedType && (valueType.actualTypeArguments[1] as ParameterizedType).typeName.contains("LinkedHashMap") -> decodeMap[s] = reflectDecodeMap(any as LinkedHashMap<String, *>, 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<String, *>, valueType.actualTypeArguments[1] as Class<*>)
/*any.javaClass.declaredFields.forEach { field ->
Expand Down Expand Up @@ -142,14 +142,37 @@ class ReflectionConfigCodec<T>(private val clazz: Class<T>) :

private fun reflectDecode(map: LinkedHashMap<String, *>, clazz: Class<out Any>): Any {
val constructor = clazz.constructors[0]
val arrayList = arrayListOf<Any>()
val arrayList = arrayListOf<Any?>()
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<String, *>, 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<String, *>,
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<String, *>, it))
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/top/wetabq/easyapi/gui/ConfigGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ConfigGUI<T>(
.forEach { value -> params.add(value) }
try {
val clazz = simpleCodecEasyConfig.clazzT
val arrayParam = arrayListOf<Any>()
val arrayParam = arrayListOf<Any?>()
params.forEach { param -> arrayParam.add(autoType(param.toString())) }
if (translatedMap.isNotEmpty()) {
translatedMap.forEach { (key, value) ->
Expand All @@ -69,6 +69,10 @@ class ConfigGUI<T>(
}
}
}
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -125,7 +128,7 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() {
}

override fun moduleDisable() {

nameTagChangeTask.cancel()
}


Expand Down

0 comments on commit 970a208

Please sign in to comment.