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

Commit 8128327

Browse files
committed
[1.1.8](*include API change) fix AdvanceCodecEasyConfig clone problem, add nameTag refresh period
1 parent 29d48b3 commit 8128327

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>top.wetabq.easyapi</groupId>
99
<artifactId>EasyAPI</artifactId>
10-
<version>1.1.7</version>
10+
<version>1.1.8</version>
1111

1212

1313
<licenses>

src/main/java/top/wetabq/easyapi/api/defaults/MessageFormatAPI.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package top.wetabq.easyapi.api.defaults
22

3+
import cn.nukkit.Player
34
import top.wetabq.easyapi.api.SimpleIntegrateAPI
45

56
object MessageFormatAPI: SimpleIntegrateAPI {
67

78
private val formatters = hashMapOf<String, HashMap<Class<*>, MessageFormatter<*>>>()
89
private val simpleFormatters = arrayListOf<SimpleMessageFormatter>()
10+
private val simplePlayerFormatters = arrayListOf<(String, Player) -> String>()
911

1012
fun format(message: String, vararg data: Any): String {
1113
var msg = message
@@ -15,6 +17,11 @@ object MessageFormatAPI: SimpleIntegrateAPI {
1517
msg = fs[it.javaClass]?.parseFormat(msg, it) ?: msg
1618
}
1719
}
20+
if (it is Player) {
21+
simplePlayerFormatters.forEach { fs ->
22+
fs(msg, it)
23+
}
24+
}
1825
}
1926
simpleFormatters.forEach {
2027
msg = it.format(msg)
@@ -35,6 +42,10 @@ object MessageFormatAPI: SimpleIntegrateAPI {
3542
simpleFormatters.add(formatter)
3643
}
3744

45+
fun registerSimplePlayerFormatter(formatter: (String, Player) -> String) {
46+
simplePlayerFormatters.add(formatter)
47+
}
48+
3849
}
3950

4051
interface MessageFormatter<T> {

src/main/java/top/wetabq/easyapi/config/encoder/advance/AdvanceCodecEasyConfig.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import top.wetabq.easyapi.config.encoder.CodecEasyConfig
66

77
abstract class AdvanceCodecEasyConfig<T>(
88
configName:String,
9-
plugin: Plugin,
10-
private val defaultValue: T
9+
plugin: Plugin
1110
) : CodecEasyConfig<T>(configName, plugin),
1211
AdvanceConfigCodec<T> {
1312

@@ -29,12 +28,4 @@ abstract class AdvanceCodecEasyConfig<T>(
2928
}
3029
configSection.putAll(encodeMap)
3130
}
32-
33-
fun safeGetData(key: String): T {
34-
if (!simpleConfig.containsKey(key)) simpleConfig[key] = defaultValue
35-
return simpleConfig[key]?:defaultValue
36-
}
37-
38-
fun getDefaultValue(): T = defaultValue
39-
4031
}

src/main/java/top/wetabq/easyapi/config/encoder/advance/ReflectionConfigCodec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ReflectionConfigCodec<T>(private val clazz: Class<T>) :
149149
when {
150150
Map::class.java.isAssignableFrom(it) -> {arrayList.add(reflectDecodeMap(mapValueList[index] as LinkedHashMap<String, *>, clazz.declaredFields[index].genericType as ParameterizedType))}
151151
List::class.java.isAssignableFrom(it) -> arrayList.add(reflectDecodeList(mapValueList[index] as ArrayList<*>, (clazz.declaredFields[index].genericType as ParameterizedType)))
152-
else -> arrayList.add(it.cast(mapValueList[index]))
152+
else -> arrayList.add(mapValueList[index])
153153
}
154154
} else {
155155
arrayList.add(reflectDecode(mapValueList[index] as LinkedHashMap<String, *>, it))
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
package top.wetabq.easyapi.config.encoder.advance
22

33
import cn.nukkit.plugin.Plugin
4+
import com.google.gson.Gson
45

56
abstract class SimpleCodecEasyConfig<T>(
67
configName:String,
78
val plugin: Plugin,
89
val clazzT: Class<T>,
9-
defaultValue: T,
10+
private val defaultValue: T,
1011
private val codec: AdvanceConfigCodec<T> = ReflectionConfigCodec(
1112
clazzT
1213
)
13-
) : AdvanceCodecEasyConfig<T>(configName, plugin, defaultValue) {
14+
) : AdvanceCodecEasyConfig<T>(configName, plugin) {
1415

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

1718
override fun decode(map: LinkedHashMap<String, *>): T = codec.decode(map)
1819

1920
override fun decode(any: Any): T = codec.decode(any)
2021

22+
fun safeGetData(key: String): T {
23+
if (!simpleConfig.containsKey(key)) simpleConfig[key] = getDefaultValue()
24+
return simpleConfig[key]?:getDefaultValue()
25+
}
26+
27+
fun getDefaultValue(): T = magicClone(defaultValue)
28+
29+
private fun magicClone(obj: T): T {
30+
val stringProject = Gson().toJson(obj, clazzT)
31+
return Gson().fromJson<T>(stringProject, clazzT)
32+
}
33+
34+
2135
}

src/main/java/top/wetabq/easyapi/module/defaults/ChatNameTagFormatModule.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package top.wetabq.easyapi.module.defaults
22

33
import cn.nukkit.Player
4+
import cn.nukkit.Server
45
import cn.nukkit.event.EventHandler
56
import cn.nukkit.event.Listener
67
import cn.nukkit.event.player.PlayerChatEvent
@@ -25,6 +26,7 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() {
2526

2627
const val NAME_TAG_FORMAT_PATH = "nameTagFormat"
2728
const val CHAT_FORMAT_PATH = "chatFormat"
29+
const val REFRESH_NAME_TAG_PERIOD_PATH = "refreshNameTagPeriod"
2830

2931
const val EASY_NAME_TAG_PLACEHOLDER = "%easy_nametag%"
3032
const val PLAYER_NAME_PLACEHOLDER = "%player_name%"
@@ -46,9 +48,11 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() {
4648
val chatConfig = this.registerAPI(CHAT_CONFIG, SimpleConfigAPI(this.getModuleInfo().moduleOwner))
4749
.add(SimpleConfigEntry(NAME_TAG_FORMAT_PATH, "$PERMISSION_GROUP_PREFIX_PLACEHOLDER &r&e$PLAYER_NAME_PLACEHOLDER&r $PERMISSION_GROUP_SUFFIX_PLACEHOLDER"))
4850
.add(SimpleConfigEntry(CHAT_FORMAT_PATH, "$EASY_NAME_TAG_PLACEHOLDER &r&c≫&r &7$CHAT_MESSAGE_PLACEHOLDER"))
51+
.add(SimpleConfigEntry(REFRESH_NAME_TAG_PERIOD_PATH, 20))
4952

5053
val nameTagFormat = chatConfig.getPathValue(NAME_TAG_FORMAT_PATH) as String
5154
val chatFormat = chatConfig.getPathValue(CHAT_FORMAT_PATH) as String
55+
val refreshNameTagPeriod = (chatConfig.getPathValue(REFRESH_NAME_TAG_PERIOD_PATH) as String).toInt()
5256

5357
val nameTagFormatter = object : MessageFormatter<String> {
5458
override fun format(message: String, data: String): String {
@@ -109,6 +113,15 @@ object ChatNameTagFormatModule : SimpleEasyAPIModule() {
109113
}
110114

111115
})
116+
117+
118+
119+
SimplePluginTaskAPI.repeating(refreshNameTagPeriod) { _, _ ->
120+
getModuleInfo().moduleOwner.server.onlinePlayers.values.forEach { player ->
121+
if (player.isAlive) player.nameTag = MessageFormatAPI.format(nameTagFormat.color(), player.name)
122+
}
123+
}
124+
112125
}
113126

114127
override fun moduleDisable() {

0 commit comments

Comments
 (0)