Skip to content

Commit

Permalink
Merge pull request #522 from TabooLib/dev/6.2.0-sky
Browse files Browse the repository at this point in the history
Dev/6.2.0 sky
  • Loading branch information
Bkm016 authored Dec 27, 2024
2 parents 22254d8 + 511410b commit df22fb1
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 38 deletions.
1 change: 1 addition & 0 deletions common-legacy-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependencies {
compileOnly(project(":common-env"))
compileOnly(project(":common-platform-api"))
compileOnly(project(":common-util"))
testImplementation(project(":common"))
testImplementation(project(":common-platform-api"))
testImplementation(project(":common-util"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ static class FileListener {
final WatchKey watchKey;

FileListener(File file, Consumer<File> callback, FileWatcher fileWatcher) throws IOException {
this.file = file;
this.file = file.getCanonicalFile();
this.callback = callback;
this.fileWatcher = fileWatcher;
Path path;
if (file.isDirectory()) {
path = file.toPath();
if (this.file.isDirectory()) {
path = this.file.toPath();
} else {
path = file.getParentFile().toPath();
path = this.file.getParentFile().toPath();
}
watchKey = path.register(
fileWatcher.watchService,
Expand Down
8 changes: 8 additions & 0 deletions common-legacy-api/src/test/kotlin/TestFileWatcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import taboolib.common5.FileWatcher
import java.io.File

fun main() {
FileWatcher.INSTANCE.addSimpleListener(File("test.txt")) {
println("change")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ abstract class ThrottleFunction<K : Any>(
addThrottleFunction(this)
}

fun canExecute(delay: Long = 0L): Boolean {
return canExecute(Unit, delay)
}

override fun canExecute(key: Unit, delay: Long): Boolean {
val currentTime = System.currentTimeMillis()
return if (currentTime - lastExecuteTime >= delay) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class ConfigLoader : ClassVisitor(1) {
field.set(findInstance(owner), conf)
// 自动重载
if (configAnno.property("autoReload", false)) {
PrimitiveIO.debug("Listening config file changes: ${file.absolutePath}")
FileWatcher.INSTANCE.addSimpleListener(file) {
PrimitiveIO.debug("Config file changed: ${file.absolutePath}")
if (file.exists()) {
conf.loadFromFile(file)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ interface Configuration : ConfigurationSection {

/**
* 注册重载回调
*
*
* @param runnable 回调
*/
fun onReload(runnable: Runnable)

/**
* 变更类型
*
*
* @param type 类型
*/
fun changeType(type: Type)
Expand All @@ -97,7 +97,7 @@ interface Configuration : ConfigurationSection {

/**
* 创建空配置
*
*
* @param type 类型
* @param concurrent 是否支持并发
* @return [Configuration]
Expand All @@ -108,7 +108,7 @@ interface Configuration : ConfigurationSection {

/**
* 从文件加载
*
*
* @param file 文件
* @param type 类型
* @param concurrent 是否支持并发
Expand All @@ -123,7 +123,7 @@ interface Configuration : ConfigurationSection {

/**
* 从 [Reader] 加载
*
*
* @param reader Reader
* @param type 类型
* @param concurrent 是否支持并发
Expand All @@ -138,7 +138,7 @@ interface Configuration : ConfigurationSection {

/**
* 从字符串加载
*
*
* @param contents 字符串
* @param type 类型
* @param concurrent 是否支持并发
Expand Down Expand Up @@ -168,7 +168,7 @@ interface Configuration : ConfigurationSection {

/**
* 从另一个含有 "saveToString" 方法的配置文件对象加载
*
*
* @param otherConfig 对象
* @param type 类型
* @param concurrent 是否支持并发
Expand All @@ -188,7 +188,7 @@ interface Configuration : ConfigurationSection {

/**
* 反序列化
*
*
* @param ignoreConstructor 是否忽略构造函数
* @return T
*/
Expand All @@ -198,7 +198,7 @@ interface Configuration : ConfigurationSection {

/**
* 获取值并反序列化
*
*
* @param key 键
* @param ignoreConstructor 是否忽略构造函数
* @return T
Expand All @@ -209,7 +209,7 @@ interface Configuration : ConfigurationSection {

/**
* 获取值并反序列化
*
*
* @param key 键
* @param obj 原始对象
* @param ignoreConstructor 是否忽略构造函数
Expand All @@ -221,7 +221,7 @@ interface Configuration : ConfigurationSection {

/**
* 序列化并写入配置文件
*
*
* @param key 键
* @param obj 对象
*/
Expand All @@ -231,7 +231,7 @@ interface Configuration : ConfigurationSection {

/**
* 序列化
*
*
* @param obj 对象
* @param type 类型
* @param concurrent 是否支持并发
Expand All @@ -246,7 +246,7 @@ interface Configuration : ConfigurationSection {

/**
* 反序列化
*
*
* @param section [ConfigurationSection]
* @param ignoreConstructor 是否忽略构造函数
* @return T
Expand Down Expand Up @@ -303,12 +303,22 @@ interface Configuration : ConfigurationSection {
* @return [Type]
*/
fun getTypeFromExtension(extension: String, def: Type = Type.YAML): Type {
return getTypeFromExtensionOrNull(extension) ?: def
}

/**
* 从文件扩展名获取类型
*
* @param extension 扩展名
* @return [Type]
*/
fun getTypeFromExtensionOrNull(extension: String): Type? {
return when (extension) {
"yaml", "yml" -> Type.YAML
"toml", "tml" -> Type.TOML
"json" -> Type.JSON
"conf" -> Type.HOCON
else -> def
else -> null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package taboolib.module.lang

import taboolib.common.platform.ProxyCommandSender
import taboolib.module.chat.HexColor
import taboolib.module.chat.colored

/**
* 颜色转换
*/
object ColorTransfer : TextTransfer {

/**
* 是否启用颜色模块
*/
val isSupported = try {
HexColor.translate("")
true
} catch (_: NoClassDefFoundError) {
false
}

override fun translate(sender: ProxyCommandSender, source: String, vararg args: Any): String {
return source.colored()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package taboolib.module.lang
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer

/**
* 向命令发送者发送语言节点对应的消息
*
* @param node 语言节点
* @param args 参数
*/
fun ProxyCommandSender.sendLang(node: String, vararg args: Any) {
val file = getLocaleFile()
if (file == null) {
Expand All @@ -17,10 +23,24 @@ fun ProxyCommandSender.sendLang(node: String, vararg args: Any) {
}
}

/**
* 获取语言节点对应的文本
*
* @param node 语言节点
* @param args 参数
* @return 文本内容,若节点不存在则返回 "{$node}"
*/
fun ProxyCommandSender.asLangText(node: String, vararg args: Any): String {
return asLangTextOrNull(node, *args) ?: "{$node}"
}

/**
* 获取语言节点对应的文本
*
* @param node 语言节点
* @param args 参数
* @return 文本内容,若节点不存在则返回 null
*/
fun ProxyCommandSender.asLangTextOrNull(node: String, vararg args: Any): String? {
val file = getLocaleFile()
if (file != null) {
Expand All @@ -29,6 +49,13 @@ fun ProxyCommandSender.asLangTextOrNull(node: String, vararg args: Any): String?
return null
}

/**
* 获取语言节点对应的文本列表
*
* @param node 语言节点
* @param args 参数
* @return 文本列表
*/
fun ProxyCommandSender.asLangTextList(node: String, vararg args: Any): List<String> {
val file = getLocaleFile()
return if (file == null) {
Expand All @@ -49,17 +76,32 @@ fun ProxyCommandSender.asLangTextList(node: String, vararg args: Any): List<Stri
}
}

/**
* 获取命令发送者的语言设置
*
* @return 语言代码
*/
fun ProxyCommandSender.getLocale(): String {
return if (this is ProxyPlayer) Language.getLocale(this) else Language.getLocale()
}

/**
* 获取命令发送者对应的语言文件
*
* @return 语言文件,若不存在则返回默认语言文件或第一个可用的语言文件
*/
fun ProxyCommandSender.getLocaleFile(): LanguageFile? {
val locale = getLocale()
return Language.languageFile.entries.firstOrNull { it.key.equals(locale, true) }?.value
?: Language.languageFile[Language.default]
?: Language.languageFile.values.firstOrNull()
}

/**
* 注册语言
*
* @param code 语言代码
*/
fun registerLanguage(vararg code: String) {
Language.addLanguage(*code)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package taboolib.module.lang

import taboolib.common.Inject
import taboolib.common.LifeCycle
import taboolib.common.OpenListener
import taboolib.common.OpenResult
import taboolib.common.io.runningResourcesInJar
import taboolib.common.platform.Awake
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
import taboolib.module.chat.HexColor
import taboolib.module.chat.colored
import taboolib.module.configuration.Configuration
import taboolib.module.configuration.Type
import taboolib.module.lang.event.PlayerSelectLocaleEvent
import taboolib.module.lang.event.SystemSelectLocaleEvent
import taboolib.module.lang.gameside.TypeActionBar
Expand All @@ -25,8 +23,9 @@ import java.util.*
* @author sky
* @since 2021/6/18 10:43 下午
*/
@Awake
@Inject
object Language {
object Language : OpenListener {

/** 是否完成了首次加载 */
private var isFirstLoaded = false
Expand Down Expand Up @@ -96,6 +95,7 @@ object Language {
/** 添加新的语言文件 */
fun addLanguage(vararg code: String) {
languageCode += code
// 如果已经完成了首次加载,则立刻重载语言文件
if (isFirstLoaded) {
reload()
}
Expand All @@ -120,25 +120,31 @@ object Language {

@Awake(LifeCycle.INIT)
fun reload() {
// 加载语言文件类型
runningResourcesInJar.keys.filter { it.startsWith("$path/") }.filter {
Configuration.getTypeFromExtension(it.substringAfterLast('.')) in Type.values()
}.forEach {
languageCode += it.substringAfterLast('/').substringBeforeLast('.')
}
// 加载颜色字符模块
try {
HexColor.translate("")
textTransfer += object : TextTransfer {
override fun translate(sender: ProxyCommandSender, source: String, vararg args: Any): String {
return source.colored()
if (!isFirstLoaded) {
// 加载语言文件类型
runningResourcesInJar.keys
.filter { it.startsWith("$path/") }
.filter { Configuration.getTypeFromExtensionOrNull(it.substringAfterLast('.')) != null }
.forEach {
languageCode += it.substringAfterLast('/').substringBeforeLast('.')
}
// 加载颜色字符模块
if (ColorTransfer.isSupported && !textTransfer.contains(ColorTransfer)) {
textTransfer += ColorTransfer
}
} catch (_: NoClassDefFoundError) {
}
// 加载语言文件
isFirstLoaded = true
languageFile.clear()
languageFile.putAll(ResourceReader(Language::class.java).files)
}

override fun call(name: String, data: Array<out Any>?): OpenResult {
return when (name) {
"taboolib:language_reload" -> OpenResult.successful(reload())
"taboolib:language_code" -> OpenResult.successful(languageCode)
"taboolib:language_file" -> OpenResult.successful(languageFile.keys)
else -> OpenResult.failed()
}
}
}
Loading

0 comments on commit df22fb1

Please sign in to comment.