Skip to content

Commit 04452f0

Browse files
committed
[2.1.1] Add Options
1 parent 0a60199 commit 04452f0

File tree

14 files changed

+164
-38
lines changed

14 files changed

+164
-38
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ subprojects {
1919
install(BUKKIT_ALL, BUNGEE, VELOCITY)
2020
}
2121
version {
22-
taboolib = "6.1.1-beta18"
22+
taboolib = "6.1.1"
2323
coroutines = null
2424
}
2525
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=me.arasple.mc.trchat
2-
version=2.1.0
2+
version=2.1.1
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.incremental.useClasspathSnapshot=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package me.arasple.mc.trchat.util
2+
3+
import taboolib.common.PrimitiveIO
4+
import taboolib.common.PrimitiveSettings
5+
import taboolib.common.platform.function.getDataFolder
6+
import taboolib.library.configuration.ConfigurationSection
7+
import taboolib.module.configuration.Configuration
8+
import java.io.File
9+
10+
/**
11+
* module-starrysky
12+
* com.mcstarrysky.starrysky.config.YamlUpdater
13+
*
14+
* @author 米擦亮
15+
* @since 2023/9/6 20:50
16+
*/
17+
object YamlUpdater {
18+
19+
fun update(path: String, skipNodes: Array<String> = emptyArray(), updateExists: Boolean = true) {
20+
// 读取 Jar 包内的对应配置文件
21+
val cache = Configuration.loadFromInputStream(javaClass.classLoader.getResourceAsStream(path) ?: error("resource not found: $path"))
22+
val file = File(getDataFolder(), path)
23+
if (!file.exists()) {
24+
return
25+
}
26+
val config = Configuration.loadFromFile(file)
27+
28+
val updated = mutableListOf<String>()
29+
read(cache, config, skipNodes, updated, updateExists)
30+
if (updated.isNotEmpty()) {
31+
config.saveToFile(config.file)
32+
}
33+
34+
if (PrimitiveSettings.IS_DEBUG_MODE) {
35+
PrimitiveIO.println("Auto updated configuration: $path, with ${updated.size} elements updated.")
36+
for (node in updated) {
37+
PrimitiveIO.println("|- $node")
38+
}
39+
}
40+
}
41+
42+
private fun read(cache: ConfigurationSection, to: ConfigurationSection, skipNodes: Array<String>, updated: MutableList<String>, updateExists: Boolean) {
43+
var name = cache.name
44+
var c = cache
45+
while (c.parent != null) {
46+
name = "${c.parent!!.name}.$name"
47+
c = c.parent!!
48+
}
49+
if (name.isNotEmpty()) {
50+
name += '.'
51+
}
52+
// 遍历给定新版配置文件的所有配置项目
53+
for (key in cache.getKeys(false)) {
54+
// 白名单配置项不进行任何检查
55+
if (key in skipNodes) continue
56+
57+
// 旧版没有, 添加
58+
if (!to.contains(key)) {
59+
updated += "$name$key (+)"
60+
to[key] = cache[key]
61+
continue
62+
}
63+
64+
// 是否不更新已存在配置, 只补全缺失项
65+
if (!updateExists) continue
66+
67+
// 好像 switch case 不能判断为空, 我基础没学好
68+
if (cache[key] == null) {
69+
updated += "$name$key (${to[key]} -> null)"
70+
to[key] = null
71+
continue
72+
}
73+
74+
val read = cache[key]
75+
76+
if (read is ConfigurationSection) {
77+
val write = to[key]
78+
// 根本不是配置选区, 那肯定要覆盖掉了, 没话说了
79+
if (write == null || write !is ConfigurationSection) {
80+
updated += "$name$key (${to[key]} -> $read)"
81+
to[key] = read
82+
continue
83+
}
84+
read(read, write, skipNodes, updated, true)
85+
} else {
86+
if (read == to[key]) continue
87+
updated += "$name$key (${to[key]} -> $read)"
88+
to[key] = read
89+
}
90+
}
91+
}
92+
}

project/module-nms/src/main/kotlin/me/arasple/mc/trchat/api/nms/NMS.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class NMS {
1919
*/
2020
abstract fun rawMessageFromCraftChatMessage(component: Any): String
2121

22-
abstract fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?)
22+
abstract fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?, usePacket: Boolean = true)
2323

2424
abstract fun hoverItem(component: ComponentText, itemStack: ItemStack): ComponentText
2525

project/module-nms/src/main/kotlin/me/arasple/mc/trchat/api/nms/NMSImpl.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class NMSImpl : NMS() {
6060
}
6161
}
6262

63-
override fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?) {
64-
if (Folia.isFolia || ServerUtil.isModdedServer) {
63+
override fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?, usePacket: Boolean) {
64+
if (!usePacket || Folia.isFolia || ServerUtil.isModdedServer) {
6565
component.sendTo(adaptPlayer(receiver))
6666
return
6767
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/api/impl/BukkitComponentManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object BukkitComponentManager : ComponentManager {
7070
event.message
7171
}
7272
if (commandSender is Player) {
73-
NMS.instance.sendMessage(commandSender, newComponent, event.sender)
73+
NMS.instance.sendMessage(commandSender, newComponent, event.sender, Settings.usePackets)
7474
} else {
7575
newComponent.sendTo(adaptCommandSender(commandSender))
7676
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/api/impl/BukkitProxyManager.kt

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import me.arasple.mc.trchat.module.internal.proxy.BukkitProxyProcessor
88
import me.arasple.mc.trchat.module.internal.proxy.redis.RedisManager
99
import me.arasple.mc.trchat.util.parseString
1010
import org.bukkit.Bukkit
11+
import org.bukkit.ChatColor
1112
import org.bukkit.plugin.messaging.PluginMessageRecipient
1213
import org.spigotmc.SpigotConfig
1314
import taboolib.common.platform.Platform
@@ -38,12 +39,14 @@ object BukkitProxyManager : ClientMessageManager {
3839
override var port = 25565
3940

4041
var allPlayerNames = mapOf<String, String?>()
41-
get() = if (mode != ProxyMode.REDIS) {
42-
field
43-
} else {
42+
get() = if (mode == ProxyMode.NONE) {
43+
onlinePlayers.associate { it.name to ChatColor.stripColor(it.displayName) }
44+
} else if (mode == ProxyMode.REDIS) {
4445
val result = mutableMapOf<String, String?>()
4546
(processor as BukkitProxyProcessor.RedisSide).allNames.values.forEach { result += it }
4647
result
48+
} else {
49+
field
4750
}
4851

4952
init {
@@ -108,21 +111,24 @@ object BukkitProxyManager : ClientMessageManager {
108111
}
109112

110113
override fun getPlayerNames(): Map<String, String?> {
111-
if (mode == ProxyMode.NONE) {
112-
return onlinePlayers.associate { it.name to it.displayName }
113-
}
114114
return allPlayerNames
115115
}
116116

117+
fun getPlayerNamesMerged(): Set<String> {
118+
return allPlayerNames.let { it.keys + it.values.filterNotNull() }
119+
}
120+
117121
override fun getExactName(name: String): String? {
118122
var player = Bukkit.getPlayerExact(name)
119123
if (player == null) {
120-
player = Bukkit.getOnlinePlayers().firstOrNull { it.displayName == name }
124+
player = Bukkit.getOnlinePlayers().firstOrNull { ChatColor.stripColor(it.displayName) == name }
121125
}
122126
return if (player != null && player.isOnline) {
123127
player.name
124128
} else {
125-
getPlayerNames().keys.firstOrNull { it.equals(name, ignoreCase = true) }
129+
getPlayerNames().entries.firstOrNull {
130+
it.key.equals(name, ignoreCase = true) || it.value?.equals(name, ignoreCase = true) == true
131+
}?.key
126132
}
127133
}
128134

@@ -161,7 +167,7 @@ object BukkitProxyManager : ClientMessageManager {
161167
fun updateNames() {
162168
sendMessage(onlinePlayers.firstOrNull(), arrayOf(
163169
"UpdateNames",
164-
onlinePlayers.joinToString(",") { it.name + "-" + it.displayName },
170+
onlinePlayers.joinToString(",") { it.name + "-" + ChatColor.stripColor(it.displayName) },
165171
port.toString()
166172
))
167173
}

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/conf/file/Settings.kt

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ object Settings {
2424
lateinit var conf: Configuration
2525
private set
2626

27+
@ConfigNode("Options.Use-Packets", "settings.yml")
28+
var usePackets = true
29+
private set
30+
2731
@ConfigNode("Channel.Default", "settings.yml")
2832
var defaultChannel = "Normal"
2933
private set
@@ -43,6 +47,10 @@ object Settings {
4347
var componentMaxLength = 32700
4448
private set
4549

50+
@ConfigNode("Simple-Component.Hover", "settings.yml")
51+
var simpleHover = false
52+
private set
53+
4654
@Awake(LifeCycle.ENABLE)
4755
fun init() {
4856
conf.onReload {

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/display/channel/PrivateChannel.kt

+1-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ class PrivateChannel(
7070
}
7171
dynamic("player", optional = true) {
7272
suggest {
73-
BukkitProxyManager.getPlayerNames().flatMap { (key, value) ->
74-
if (key !in PlayerData.vanishing) {
75-
if (value == null || key == value) listOf(key) else listOf(key, value)
76-
}
77-
else emptyList()
78-
}
73+
BukkitProxyManager.getPlayerNamesMerged().filter { it !in PlayerData.vanishing }
7974
}
8075
execute<Player> { sender, _, argument ->
8176
sender.session.lastPrivateTo = BukkitProxyManager.getExactName(argument)

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/display/format/obj/Style.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package me.arasple.mc.trchat.module.display.format.obj
22

3+
import me.arasple.mc.trchat.module.conf.file.Settings
34
import me.arasple.mc.trchat.module.internal.script.Condition
45
import me.arasple.mc.trchat.util.color.colorify
56
import me.arasple.mc.trchat.util.parseInline
7+
import me.arasple.mc.trchat.util.parseSimple
68
import me.arasple.mc.trchat.util.pass
79
import me.arasple.mc.trchat.util.setPlaceholders
810
import org.bukkit.command.CommandSender
@@ -31,7 +33,11 @@ sealed interface Style {
3133

3234
data class Text(override val contents: List<Pair<String, Condition?>>) : Hover {
3335
override fun process(component: ComponentText, content: String) {
34-
component.hoverText(content)
36+
if (Settings.simpleHover) {
37+
component.hoverText(content.parseSimple())
38+
} else {
39+
component.hoverText(content.colorify())
40+
}
3541
}
3642
}
3743

@@ -90,7 +96,7 @@ sealed interface Style {
9096
}
9197
is Hover.Text -> {
9298
contents.filter { it.second.pass(sender) }.joinToString("\n") { it.first }
93-
.parseInline(sender).setPlaceholders(sender).replaceWithOrder(*vars).colorify()
99+
.parseInline(sender).setPlaceholders(sender).replaceWithOrder(*vars)
94100
}
95101
else -> {
96102
contents.firstOrNull { it.second.pass(sender) }?.first

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/TrChatBukkit.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ object TrChatBukkit : Plugin() {
2828

2929
var isGlobalMuting = false
3030

31-
@Awake(LifeCycle.CONST)
3231
internal fun detectPaperEnv() {
3332
try {
3433
// Paper 1.16.5+
@@ -40,12 +39,20 @@ object TrChatBukkit : Plugin() {
4039
}
4140
}
4241

42+
@Awake(LifeCycle.CONST)
43+
internal fun onConst() {
44+
detectPaperEnv()
45+
// registerLifeCycleTask(LifeCycle.INIT, 0) {
46+
// YamlUpdater.update("settings.yml", updateExists = false)
47+
// }
48+
}
49+
4350
override fun onLoad() {
4451
console().sendLang("Plugin-Loading", Bukkit.getBukkitVersion())
4552
}
4653

4754
override fun onEnable() {
48-
if (Folia.isFolia) {
55+
if (!Settings.usePackets || Folia.isFolia) {
4956
disablePacketListener()
5057
}
5158
BukkitProxyManager.processor

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/listener/ListenerAnvilChange.kt

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.arasple.mc.trchat.module.internal.listener
22

33
import me.arasple.mc.trchat.TrChat
4+
import me.arasple.mc.trchat.module.adventure.toAdventure
5+
import me.arasple.mc.trchat.module.internal.TrChatBukkit
46
import me.arasple.mc.trchat.util.color.MessageColors
57
import me.arasple.mc.trchat.util.parseSimple
68
import org.bukkit.event.inventory.InventoryType
@@ -30,6 +32,10 @@ object ListenerAnvilChange {
3032
var color = true
3133
private set
3234

35+
@ConfigNode("Simple-Component.Anvil", "settings.yml")
36+
var simple = false
37+
private set
38+
3339
@Suppress("Deprecation")
3440
@SubscribeEvent(priority = EventPriority.HIGHEST, ignoreCancelled = true)
3541
fun onAnvilCraft(e: PrepareAnvilEvent) {
@@ -46,12 +52,10 @@ object ListenerAnvilChange {
4652
if (filter) {
4753
setDisplayName(TrChat.api().getFilterManager().filter(displayName, adaptPlayer(p)).filtered)
4854
}
49-
if (color) {
50-
if (p.hasPermission("trchat.color.simple")) {
51-
setDisplayNameComponent(arrayOf(displayName.parseSimple().toSpigotObject()))
52-
} else {
53-
setDisplayName(MessageColors.replaceWithPermission(p, displayName, MessageColors.Type.ANVIL))
54-
}
55+
if (simple && TrChatBukkit.isPaperEnv && p.hasPermission("trchat.simple.anvil")) {
56+
displayName(displayName.parseSimple().toAdventure())
57+
} else if (color) {
58+
setDisplayName(MessageColors.replaceWithPermission(p, displayName, MessageColors.Type.ANVIL))
5559
}
5660
}
5761
e.result = result

project/runtime-bukkit/src/main/kotlin/me/arasple/mc/trchat/module/internal/listener/ListenerSignChange.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ object ListenerSignChange {
2828
var color = true
2929
private set
3030

31+
@ConfigNode("Simple-Component.Sign", "settings.yml")
32+
var simple = false
33+
private set
34+
3135
@Suppress("Deprecation")
3236
@SubscribeEvent(priority = EventPriority.HIGHEST, ignoreCancelled = true)
3337
fun onSignChange(e: SignChangeEvent) {
@@ -37,12 +41,10 @@ object ListenerSignChange {
3741
if (filter) {
3842
e.setLine(index, TrChat.api().getFilterManager().filter(e.getLine(index) ?: "", adaptPlayer(p)).filtered)
3943
}
40-
if (color) {
41-
if (TrChatBukkit.isPaperEnv && p.hasPermission("trchat.color.simple")) {
42-
e.line(index, (e.getLine(index) ?: "").parseSimple().toAdventure())
43-
} else {
44-
e.setLine(index, MessageColors.replaceWithPermission(p, e.getLine(index) ?: "", MessageColors.Type.SIGN))
45-
}
44+
if (simple && TrChatBukkit.isPaperEnv && p.hasPermission("trchat.simple.sign")) {
45+
e.line(index, (e.getLine(index) ?: "").parseSimple().toAdventure())
46+
} else if (color) {
47+
e.setLine(index, MessageColors.replaceWithPermission(p, e.getLine(index) ?: "", MessageColors.Type.SIGN))
4648
}
4749
}
4850
}

project/runtime-bukkit/src/main/resources/settings.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Options:
99
Component-Max-Length: 32700
1010
Always-Cancel-Chat-Event: false
1111
Cheat-Client-Secure-Chat: true
12+
Use-Packets: true
1213
Disabled-Commands: []
1314

1415
Channel:
@@ -42,4 +43,9 @@ Color:
4243
Chat: true
4344
Sign: true
4445
Anvil: true
45-
Book: true
46+
Book: true
47+
48+
Simple-Component:
49+
Hover: false
50+
Anvil: false
51+
Sign: false

0 commit comments

Comments
 (0)