Skip to content

Commit

Permalink
[2.1.1] Add Options
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsFlicker committed Apr 21, 2024
1 parent 0a60199 commit 04452f0
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ subprojects {
install(BUKKIT_ALL, BUNGEE, VELOCITY)
}
version {
taboolib = "6.1.1-beta18"
taboolib = "6.1.1"
coroutines = null
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=me.arasple.mc.trchat
version=2.1.0
version=2.1.1
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.incremental.useClasspathSnapshot=true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package me.arasple.mc.trchat.util

import taboolib.common.PrimitiveIO
import taboolib.common.PrimitiveSettings
import taboolib.common.platform.function.getDataFolder
import taboolib.library.configuration.ConfigurationSection
import taboolib.module.configuration.Configuration
import java.io.File

/**
* module-starrysky
* com.mcstarrysky.starrysky.config.YamlUpdater
*
* @author 米擦亮
* @since 2023/9/6 20:50
*/
object YamlUpdater {

fun update(path: String, skipNodes: Array<String> = emptyArray(), updateExists: Boolean = true) {
// 读取 Jar 包内的对应配置文件
val cache = Configuration.loadFromInputStream(javaClass.classLoader.getResourceAsStream(path) ?: error("resource not found: $path"))
val file = File(getDataFolder(), path)
if (!file.exists()) {
return
}
val config = Configuration.loadFromFile(file)

val updated = mutableListOf<String>()
read(cache, config, skipNodes, updated, updateExists)
if (updated.isNotEmpty()) {
config.saveToFile(config.file)
}

if (PrimitiveSettings.IS_DEBUG_MODE) {
PrimitiveIO.println("Auto updated configuration: $path, with ${updated.size} elements updated.")
for (node in updated) {
PrimitiveIO.println("|- $node")
}
}
}

private fun read(cache: ConfigurationSection, to: ConfigurationSection, skipNodes: Array<String>, updated: MutableList<String>, updateExists: Boolean) {
var name = cache.name
var c = cache
while (c.parent != null) {
name = "${c.parent!!.name}.$name"
c = c.parent!!
}
if (name.isNotEmpty()) {
name += '.'
}
// 遍历给定新版配置文件的所有配置项目
for (key in cache.getKeys(false)) {
// 白名单配置项不进行任何检查
if (key in skipNodes) continue

// 旧版没有, 添加
if (!to.contains(key)) {
updated += "$name$key (+)"
to[key] = cache[key]
continue
}

// 是否不更新已存在配置, 只补全缺失项
if (!updateExists) continue

// 好像 switch case 不能判断为空, 我基础没学好
if (cache[key] == null) {
updated += "$name$key (${to[key]} -> null)"
to[key] = null
continue
}

val read = cache[key]

if (read is ConfigurationSection) {
val write = to[key]
// 根本不是配置选区, 那肯定要覆盖掉了, 没话说了
if (write == null || write !is ConfigurationSection) {
updated += "$name$key (${to[key]} -> $read)"
to[key] = read
continue
}
read(read, write, skipNodes, updated, true)
} else {
if (read == to[key]) continue
updated += "$name$key (${to[key]} -> $read)"
to[key] = read
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class NMS {
*/
abstract fun rawMessageFromCraftChatMessage(component: Any): String

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class NMSImpl : NMS() {
}
}

override fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?) {
if (Folia.isFolia || ServerUtil.isModdedServer) {
override fun sendMessage(receiver: Player, component: ComponentText, sender: UUID?, usePacket: Boolean) {
if (!usePacket || Folia.isFolia || ServerUtil.isModdedServer) {
component.sendTo(adaptPlayer(receiver))
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object BukkitComponentManager : ComponentManager {
event.message
}
if (commandSender is Player) {
NMS.instance.sendMessage(commandSender, newComponent, event.sender)
NMS.instance.sendMessage(commandSender, newComponent, event.sender, Settings.usePackets)
} else {
newComponent.sendTo(adaptCommandSender(commandSender))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import me.arasple.mc.trchat.module.internal.proxy.BukkitProxyProcessor
import me.arasple.mc.trchat.module.internal.proxy.redis.RedisManager
import me.arasple.mc.trchat.util.parseString
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.plugin.messaging.PluginMessageRecipient
import org.spigotmc.SpigotConfig
import taboolib.common.platform.Platform
Expand Down Expand Up @@ -38,12 +39,14 @@ object BukkitProxyManager : ClientMessageManager {
override var port = 25565

var allPlayerNames = mapOf<String, String?>()
get() = if (mode != ProxyMode.REDIS) {
field
} else {
get() = if (mode == ProxyMode.NONE) {
onlinePlayers.associate { it.name to ChatColor.stripColor(it.displayName) }
} else if (mode == ProxyMode.REDIS) {
val result = mutableMapOf<String, String?>()
(processor as BukkitProxyProcessor.RedisSide).allNames.values.forEach { result += it }
result
} else {
field
}

init {
Expand Down Expand Up @@ -108,21 +111,24 @@ object BukkitProxyManager : ClientMessageManager {
}

override fun getPlayerNames(): Map<String, String?> {
if (mode == ProxyMode.NONE) {
return onlinePlayers.associate { it.name to it.displayName }
}
return allPlayerNames
}

fun getPlayerNamesMerged(): Set<String> {
return allPlayerNames.let { it.keys + it.values.filterNotNull() }
}

override fun getExactName(name: String): String? {
var player = Bukkit.getPlayerExact(name)
if (player == null) {
player = Bukkit.getOnlinePlayers().firstOrNull { it.displayName == name }
player = Bukkit.getOnlinePlayers().firstOrNull { ChatColor.stripColor(it.displayName) == name }
}
return if (player != null && player.isOnline) {
player.name
} else {
getPlayerNames().keys.firstOrNull { it.equals(name, ignoreCase = true) }
getPlayerNames().entries.firstOrNull {
it.key.equals(name, ignoreCase = true) || it.value?.equals(name, ignoreCase = true) == true
}?.key
}
}

Expand Down Expand Up @@ -161,7 +167,7 @@ object BukkitProxyManager : ClientMessageManager {
fun updateNames() {
sendMessage(onlinePlayers.firstOrNull(), arrayOf(
"UpdateNames",
onlinePlayers.joinToString(",") { it.name + "-" + it.displayName },
onlinePlayers.joinToString(",") { it.name + "-" + ChatColor.stripColor(it.displayName) },
port.toString()
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ object Settings {
lateinit var conf: Configuration
private set

@ConfigNode("Options.Use-Packets", "settings.yml")
var usePackets = true
private set

@ConfigNode("Channel.Default", "settings.yml")
var defaultChannel = "Normal"
private set
Expand All @@ -43,6 +47,10 @@ object Settings {
var componentMaxLength = 32700
private set

@ConfigNode("Simple-Component.Hover", "settings.yml")
var simpleHover = false
private set

@Awake(LifeCycle.ENABLE)
fun init() {
conf.onReload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ class PrivateChannel(
}
dynamic("player", optional = true) {
suggest {
BukkitProxyManager.getPlayerNames().flatMap { (key, value) ->
if (key !in PlayerData.vanishing) {
if (value == null || key == value) listOf(key) else listOf(key, value)
}
else emptyList()
}
BukkitProxyManager.getPlayerNamesMerged().filter { it !in PlayerData.vanishing }
}
execute<Player> { sender, _, argument ->
sender.session.lastPrivateTo = BukkitProxyManager.getExactName(argument)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.arasple.mc.trchat.module.display.format.obj

import me.arasple.mc.trchat.module.conf.file.Settings
import me.arasple.mc.trchat.module.internal.script.Condition
import me.arasple.mc.trchat.util.color.colorify
import me.arasple.mc.trchat.util.parseInline
import me.arasple.mc.trchat.util.parseSimple
import me.arasple.mc.trchat.util.pass
import me.arasple.mc.trchat.util.setPlaceholders
import org.bukkit.command.CommandSender
Expand Down Expand Up @@ -31,7 +33,11 @@ sealed interface Style {

data class Text(override val contents: List<Pair<String, Condition?>>) : Hover {
override fun process(component: ComponentText, content: String) {
component.hoverText(content)
if (Settings.simpleHover) {
component.hoverText(content.parseSimple())
} else {
component.hoverText(content.colorify())
}
}
}

Expand Down Expand Up @@ -90,7 +96,7 @@ sealed interface Style {
}
is Hover.Text -> {
contents.filter { it.second.pass(sender) }.joinToString("\n") { it.first }
.parseInline(sender).setPlaceholders(sender).replaceWithOrder(*vars).colorify()
.parseInline(sender).setPlaceholders(sender).replaceWithOrder(*vars)
}
else -> {
contents.firstOrNull { it.second.pass(sender) }?.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ object TrChatBukkit : Plugin() {

var isGlobalMuting = false

@Awake(LifeCycle.CONST)
internal fun detectPaperEnv() {
try {
// Paper 1.16.5+
Expand All @@ -40,12 +39,20 @@ object TrChatBukkit : Plugin() {
}
}

@Awake(LifeCycle.CONST)
internal fun onConst() {
detectPaperEnv()
// registerLifeCycleTask(LifeCycle.INIT, 0) {
// YamlUpdater.update("settings.yml", updateExists = false)
// }
}

override fun onLoad() {
console().sendLang("Plugin-Loading", Bukkit.getBukkitVersion())
}

override fun onEnable() {
if (Folia.isFolia) {
if (!Settings.usePackets || Folia.isFolia) {
disablePacketListener()
}
BukkitProxyManager.processor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.arasple.mc.trchat.module.internal.listener

import me.arasple.mc.trchat.TrChat
import me.arasple.mc.trchat.module.adventure.toAdventure
import me.arasple.mc.trchat.module.internal.TrChatBukkit
import me.arasple.mc.trchat.util.color.MessageColors
import me.arasple.mc.trchat.util.parseSimple
import org.bukkit.event.inventory.InventoryType
Expand Down Expand Up @@ -30,6 +32,10 @@ object ListenerAnvilChange {
var color = true
private set

@ConfigNode("Simple-Component.Anvil", "settings.yml")
var simple = false
private set

@Suppress("Deprecation")
@SubscribeEvent(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onAnvilCraft(e: PrepareAnvilEvent) {
Expand All @@ -46,12 +52,10 @@ object ListenerAnvilChange {
if (filter) {
setDisplayName(TrChat.api().getFilterManager().filter(displayName, adaptPlayer(p)).filtered)
}
if (color) {
if (p.hasPermission("trchat.color.simple")) {
setDisplayNameComponent(arrayOf(displayName.parseSimple().toSpigotObject()))
} else {
setDisplayName(MessageColors.replaceWithPermission(p, displayName, MessageColors.Type.ANVIL))
}
if (simple && TrChatBukkit.isPaperEnv && p.hasPermission("trchat.simple.anvil")) {
displayName(displayName.parseSimple().toAdventure())
} else if (color) {
setDisplayName(MessageColors.replaceWithPermission(p, displayName, MessageColors.Type.ANVIL))
}
}
e.result = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ object ListenerSignChange {
var color = true
private set

@ConfigNode("Simple-Component.Sign", "settings.yml")
var simple = false
private set

@Suppress("Deprecation")
@SubscribeEvent(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onSignChange(e: SignChangeEvent) {
Expand All @@ -37,12 +41,10 @@ object ListenerSignChange {
if (filter) {
e.setLine(index, TrChat.api().getFilterManager().filter(e.getLine(index) ?: "", adaptPlayer(p)).filtered)
}
if (color) {
if (TrChatBukkit.isPaperEnv && p.hasPermission("trchat.color.simple")) {
e.line(index, (e.getLine(index) ?: "").parseSimple().toAdventure())
} else {
e.setLine(index, MessageColors.replaceWithPermission(p, e.getLine(index) ?: "", MessageColors.Type.SIGN))
}
if (simple && TrChatBukkit.isPaperEnv && p.hasPermission("trchat.simple.sign")) {
e.line(index, (e.getLine(index) ?: "").parseSimple().toAdventure())
} else if (color) {
e.setLine(index, MessageColors.replaceWithPermission(p, e.getLine(index) ?: "", MessageColors.Type.SIGN))
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion project/runtime-bukkit/src/main/resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Options:
Component-Max-Length: 32700
Always-Cancel-Chat-Event: false
Cheat-Client-Secure-Chat: true
Use-Packets: true
Disabled-Commands: []

Channel:
Expand Down Expand Up @@ -42,4 +43,9 @@ Color:
Chat: true
Sign: true
Anvil: true
Book: true
Book: true

Simple-Component:
Hover: false
Anvil: false
Sign: false

0 comments on commit 04452f0

Please sign in to comment.