From 97c703a45c26adcf6ededc47477180a9a3238bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MC=7E=E8=9B=9F=E9=BE=99?= <1610105206@qq.com> Date: Wed, 10 Jul 2024 20:47:56 +0800 Subject: [PATCH] Fix & Optimize --- gradle/libs.versions.toml | 5 ++ plugin/build.gradle.kts | 8 ++- .../java/fr/xephi/authme/ConsoleLogger.java | 2 +- .../message/AbstractMessageFileHandler.java | 13 ++-- .../message/HelpMessagesFileHandler.java | 16 ++--- .../fr/xephi/authme/output/Log4JFilter.java | 4 +- project/build.gradle.kts | 6 +- project/module-common/build.gradle.kts | 1 + .../authme/configruation/ConfigSection.kt | 59 ++++++++++--------- .../authme/configruation/Configuration.kt | 18 ++++++ .../fr/xephi/authme/configruation/Type.kt | 8 ++- project/module-logger/build.gradle.kts | 3 + project/module-message/build.gradle.kts | 1 + project/module-util/build.gradle.kts | 2 +- .../java/fr/xephi/authme/util/Coerce.java | 5 +- 15 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 gradle/libs.versions.toml create mode 100644 project/module-common/build.gradle.kts create mode 100644 project/module-logger/build.gradle.kts create mode 100644 project/module-message/build.gradle.kts diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 00000000..91cf1a27 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,5 @@ +[versions] +guava = "33.2.1-jre" + +[libraries] +guava = { module = "com.google.guava:guava", version.ref = "guava" } \ No newline at end of file diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index f811c88e..4bebf517 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -20,8 +20,12 @@ subprojects { } dependencies { + // Modules + implementation(project(":project:module-common")) implementation(project(":project:module-util")) + implementation(project(":project:module-logger")) implementation(project(":project:module-configuration")) + implementation(project(":project:module-message")) // Spigot API, https://www.spigotmc.org/ compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT") // Java Libraries @@ -118,7 +122,9 @@ subprojects { archiveClassifier.set("") archiveBaseName.set("AuthMe") destinationDirectory.set(file("$rootDir/outs")) - // Libraries Relocate + // Kotlin + relocate("kotlin.", "kolin200.") + // Others relocate("org.apache.http", "fr.xephi.authme.libs.org.apache.http") relocate("org.apache.commons", "fr.xephi.authme.libs.org.apache.commons") relocate("waffle", "fr.xephi.authme.libs.waffle") diff --git a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/ConsoleLogger.java b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/ConsoleLogger.java index 1b1d6788..125bf477 100644 --- a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -26,7 +26,7 @@ import java.util.logging.Logger; */ public final class ConsoleLogger { - private static final String NEW_LINE = System.getProperty("line.separator"); + private static final String NEW_LINE = System.lineSeparator(); /** Formatter which formats dates to something like "[08-16 21:18:46]" for any given LocalDateTime. */ private static final DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder() .appendLiteral('[') diff --git a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/AbstractMessageFileHandler.java b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/AbstractMessageFileHandler.java index 8211d53c..821180d3 100644 --- a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/AbstractMessageFileHandler.java +++ b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/AbstractMessageFileHandler.java @@ -2,6 +2,7 @@ package fr.xephi.authme.message; import com.google.common.annotations.VisibleForTesting; import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.configruation.Configuration; import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.output.ConsoleLoggerFactory; @@ -9,8 +10,6 @@ import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.util.FileUtils; import fr.xephi.authme.util.message.I18NUtils; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import javax.annotation.PostConstruct; import javax.inject.Inject; @@ -35,8 +34,8 @@ public abstract class AbstractMessageFileHandler implements Reloadable { private Settings settings; private String filename; - private FileConfiguration configuration; - private Map i18nConfiguration; + private Configuration configuration; + private Map i18nConfiguration; private final String defaultFile; protected AbstractMessageFileHandler() { @@ -49,7 +48,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable { String language = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE); filename = createFilePath(language); File messagesFile = initializeFile(filename); - configuration = YamlConfiguration.loadConfiguration(messagesFile); + configuration = Configuration.loadFromFile(messagesFile); i18nConfiguration = null; } @@ -117,7 +116,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable { return configuration.getString(key); } - public FileConfiguration getI18nConfiguration(String locale) { + public Configuration getI18nConfiguration(String locale) { if (i18nConfiguration == null) { i18nConfiguration = new ConcurrentHashMap<>(); } @@ -130,7 +129,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable { // Sync with reload(); String i18nFilename = createFilePath(locale); File i18nMessagesFile = initializeFile(i18nFilename); - FileConfiguration config = YamlConfiguration.loadConfiguration(i18nMessagesFile); + Configuration config = Configuration.loadFromFile(i18nMessagesFile); i18nConfiguration.put(locale, config); diff --git a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/HelpMessagesFileHandler.java b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/HelpMessagesFileHandler.java index 89dc5224..aa756fc8 100644 --- a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/HelpMessagesFileHandler.java +++ b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/message/HelpMessagesFileHandler.java @@ -1,10 +1,9 @@ package fr.xephi.authme.message; import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.configruation.Configuration; import fr.xephi.authme.output.ConsoleLoggerFactory; import fr.xephi.authme.util.FileUtils; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import javax.inject.Inject; import java.io.InputStream; @@ -19,9 +18,10 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler { private final ConsoleLogger logger = ConsoleLoggerFactory.get(HelpMessagesFileHandler.class); - private FileConfiguration defaultConfiguration; + private Configuration defaultConfiguration; - @Inject // Trigger injection in the superclass + @Inject + // Trigger injection in the superclass HelpMessagesFileHandler() { } @@ -37,7 +37,7 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler { if (message == null) { logger.warning("Error getting message with key '" + key + "'. " - + "Please update your config file '" + getFilename() + "' or run /authme messages help"); + + "Please update your config file '" + getFilename() + "' or run /authme messages help"); return getDefault(key); } return message; @@ -52,12 +52,12 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler { private String getDefault(String key) { if (defaultConfiguration == null) { InputStream stream = FileUtils.getResourceFromJar(createFilePath(DEFAULT_LANGUAGE)); - defaultConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream)); + defaultConfiguration = Configuration.loadFromReader(new InputStreamReader(stream)); } String message = defaultConfiguration.getString(key); return message == null - ? "Error retrieving message '" + key + "'" - : message; + ? "Error retrieving message '" + key + "'" + : message; } @Override diff --git a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/output/Log4JFilter.java b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/output/Log4JFilter.java index 1ebf5141..44977d24 100644 --- a/plugin/platform-bukkit/src/main/java/fr/xephi/authme/output/Log4JFilter.java +++ b/plugin/platform-bukkit/src/main/java/fr/xephi/authme/output/Log4JFilter.java @@ -7,12 +7,14 @@ import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.filter.AbstractFilter; import org.apache.logging.log4j.message.Message; +import java.io.Serializable; + /** * Implements a filter for Log4j to skip sensitive AuthMe commands. * * @author Xephi59 */ -public class Log4JFilter extends AbstractFilter { +public class Log4JFilter extends AbstractFilter implements Serializable { private static final long serialVersionUID = -5594073755007974254L; diff --git a/project/build.gradle.kts b/project/build.gradle.kts index 95b02754..281f896d 100644 --- a/project/build.gradle.kts +++ b/project/build.gradle.kts @@ -1,5 +1 @@ -repositories { -} - -dependencies { -} \ No newline at end of file +dependencies {} \ No newline at end of file diff --git a/project/module-common/build.gradle.kts b/project/module-common/build.gradle.kts new file mode 100644 index 00000000..281f896d --- /dev/null +++ b/project/module-common/build.gradle.kts @@ -0,0 +1 @@ +dependencies {} \ No newline at end of file diff --git a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/ConfigSection.kt b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/ConfigSection.kt index c84a63da..5e022d10 100644 --- a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/ConfigSection.kt +++ b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/ConfigSection.kt @@ -3,12 +3,8 @@ package fr.xephi.authme.configruation import com.electronwill.nightconfig.core.CommentedConfig import com.electronwill.nightconfig.core.Config import com.electronwill.nightconfig.core.EnumGetMethod -import org.tabooproject.reflex.Reflex.Companion.setProperty -import taboolib.common.util.decodeUnicode -import taboolib.common5.Coerce -import taboolib.library.configuration.ConfigurationSection -import taboolib.module.configuration.util.Commented -import taboolib.module.configuration.util.CommentedList +import fr.xephi.authme.util.Coerce +import fr.xephi.authme.util.StringUtil /** * ConfigSection @@ -16,7 +12,11 @@ import taboolib.module.configuration.util.CommentedList * @author Taboolib * @since 2024/7/10 19:33 */ -open class ConfigSection(var root: Config, override val name: String = "", override val parent: ConfigurationSection? = null) : ConfigurationSection { +open class ConfigSection( + var root: Config, + override val name: String = "", + override val parent: ConfigurationSection? = null +) : ConfigurationSection { private val configType = Type.getType(root.configFormat()) @@ -71,9 +71,12 @@ open class ConfigSection(var root: Config, override val name: String = "", overr // 因为在 set 方法中 Map 会被转换为 Config 类型 is Map<*, *> -> { val subConfig = root.createSubConfig() - subConfig.setProperty("map", value) + val field = subConfig::class.java.getField("map") + field.isAccessible = true + field.set(subConfig, value) ConfigSection(subConfig, name, parent) } + else -> unwrap(value) } } @@ -84,15 +87,17 @@ open class ConfigSection(var root: Config, override val name: String = "", overr value is List<*> -> root.set(path, unwrap(value, this)) value is Collection<*> && value !is List<*> -> set(path, value.toList()) value is ConfigurationSection -> set(path, value.getConfig()) - value is Map<*, *> -> set(path, value.toConfig(this)) + value is Map<*, *> -> set(path, value.asConfig(this)) value is Commented -> { set(path, value.value) setComment(path, value.comment) } + value is CommentedList -> { set(path, value.value) setComments(path, value.comment) } + else -> root.set(path, value) } } @@ -111,11 +116,11 @@ open class ConfigSection(var root: Config, override val name: String = "", overr } override fun getInt(path: String): Int { - return Coerce.toInteger(get(path)) + return Coerce.asInteger(get(path)).get() } override fun getInt(path: String, def: Int): Int { - return Coerce.toInteger(get(path) ?: def) + return Coerce.asInteger(get(path) ?: def).get() } override fun isInt(path: String): Boolean { @@ -124,11 +129,11 @@ open class ConfigSection(var root: Config, override val name: String = "", overr } override fun getBoolean(path: String): Boolean { - return Coerce.toBoolean(get(path)) + return Coerce.asBoolean(get(path)).get() } override fun getBoolean(path: String, def: Boolean): Boolean { - return Coerce.toBoolean(get(path) ?: def) + return Coerce.asBoolean(get(path) ?: def).get() } override fun isBoolean(path: String): Boolean { @@ -136,11 +141,11 @@ open class ConfigSection(var root: Config, override val name: String = "", overr } override fun getDouble(path: String): Double { - return Coerce.toDouble(get(path)) + return Coerce.asDouble(get(path)).get() } override fun getDouble(path: String, def: Double): Double { - return Coerce.toDouble(get(path) ?: def) + return Coerce.asDouble(get(path) ?: def).get() } override fun isDouble(path: String): Boolean { @@ -148,11 +153,11 @@ open class ConfigSection(var root: Config, override val name: String = "", overr } override fun getLong(path: String): Long { - return Coerce.toLong(get(path)) + return Coerce.asLong(get(path)).get() } override fun getLong(path: String, def: Long): Long { - return Coerce.toLong(get(path) ?: def) + return Coerce.asLong(get(path) ?: def).get() } override fun isLong(path: String): Boolean { @@ -176,35 +181,35 @@ open class ConfigSection(var root: Config, override val name: String = "", overr } override fun getIntegerList(path: String): List { - return getList(path)?.map { Coerce.toInteger(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asInteger(it).get() }?.toList() ?: ArrayList() } override fun getBooleanList(path: String): List { - return getList(path)?.map { Coerce.toBoolean(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asBoolean(it).get() }?.toList() ?: ArrayList() } override fun getDoubleList(path: String): List { - return getList(path)?.map { Coerce.toDouble(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asDouble(it).get() }?.toList() ?: ArrayList() } override fun getFloatList(path: String): List { - return getList(path)?.map { Coerce.toFloat(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asFloat(it).get() }?.toList() ?: ArrayList() } override fun getLongList(path: String): List { - return getList(path)?.map { Coerce.toLong(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asLong(it).get() }?.toList() ?: ArrayList() } override fun getByteList(path: String): List { - return getList(path)?.map { Coerce.toByte(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asByte(it).get() }?.toList() ?: ArrayList() } override fun getCharacterList(path: String): List { - return getList(path)?.map { Coerce.toChar(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asChar(it).get() }?.toList() ?: ArrayList() } override fun getShortList(path: String): List { - return getList(path)?.map { Coerce.toShort(it) }?.toList() ?: ArrayList() + return getList(path)?.map { Coerce.asShort(it).get() }?.toList() ?: ArrayList() } override fun getMapList(path: String): List> { @@ -289,7 +294,7 @@ open class ConfigSection(var root: Config, override val name: String = "", overr return if (this is ConfigSection) root else error("Not supported") } - private fun Map<*, *>.toConfig(parent: ConfigSection): Config { + private fun Map<*, *>.asConfig(parent: ConfigSection): Config { val section = ConfigSection(parent.root.createSubConfig()) forEach { (k, v) -> section[k.toString()] = v } return section.root @@ -321,7 +326,7 @@ open class ConfigSection(var root: Config, override val name: String = "", overr value is List<*> -> unwrap(value, parent) value is Collection<*> && value !is List<*> -> value.toList() value is ConfigurationSection -> value.getConfig() - value is Map<*, *> -> value.toConfig(parent) + value is Map<*, *> -> value.asConfig(parent) else -> value } } diff --git a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Configuration.kt b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Configuration.kt index 11e69556..e130deef 100644 --- a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Configuration.kt +++ b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Configuration.kt @@ -81,6 +81,8 @@ interface Configuration : ConfigurationSection { /** * 识别可能的 [ConfigurationSection] 类型 */ + @JvmStatic + @JvmOverloads fun parse(any: Any, type: Type = Type.YAML, concurrent: Boolean = true): ConfigurationSection { val unwrapped = ConfigSection.unwrap(any) if (unwrapped is Map<*, *>) { @@ -96,6 +98,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [Configuration] */ + @JvmStatic + @JvmOverloads fun empty(type: Type = Type.YAML, concurrent: Boolean = true): Configuration { return ConfigFile( if (concurrent) type.newFormat().createConcurrentConfig() else type.newFormat() @@ -110,6 +114,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [Configuration] */ + @JvmStatic + @JvmOverloads fun loadFromFile(file: File, type: Type? = null, concurrent: Boolean = true): Configuration { val format = (type ?: getTypeFromFile(file)).newFormat() val configFile = @@ -126,6 +132,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [Configuration] */ + @JvmStatic + @JvmOverloads fun loadFromReader(reader: Reader, type: Type = Type.YAML, concurrent: Boolean = true): Configuration { val format = type.newFormat() val configFile = @@ -142,6 +150,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [Configuration] */ + @JvmStatic + @JvmOverloads fun loadFromString(contents: String, type: Type = Type.YAML, concurrent: Boolean = true): Configuration { val format = type.newFormat() val configFile = @@ -158,6 +168,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [Configuration] */ + @JvmStatic + @JvmOverloads fun loadFromInputStream( inputStream: InputStream, type: Type = Type.YAML, @@ -178,6 +190,8 @@ interface Configuration : ConfigurationSection { * @param concurrent 是否支持并发 * @return [ConfigurationSection] */ + @JvmStatic + @JvmOverloads fun fromMap(map: Map<*, *>, type: Type = Type.YAML, concurrent: Boolean = true): ConfigurationSection { val empty = empty(type, concurrent) map.forEach { (k, v) -> empty[k.toString()] = v } @@ -191,6 +205,8 @@ interface Configuration : ConfigurationSection { * @param def 默认类型 * @return [Type] */ + @JvmStatic + @JvmOverloads fun getTypeFromFile(file: File, def: Type = Type.YAML): Type { return getTypeFromExtension(file.extension, def) } @@ -202,6 +218,8 @@ interface Configuration : ConfigurationSection { * @param def 默认类型 * @return [Type] */ + @JvmStatic + @JvmOverloads fun getTypeFromExtension(extension: String, def: Type = Type.YAML): Type { return when (extension) { "yaml", "yml" -> Type.YAML diff --git a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Type.kt b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Type.kt index eca5d1f7..797978bc 100644 --- a/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Type.kt +++ b/project/module-configuration/src/main/kotlin/fr/xephi/authme/configruation/Type.kt @@ -1,5 +1,9 @@ package fr.xephi.authme.configruation +import com.electronwill.nightconfig.core.Config +import com.electronwill.nightconfig.core.ConfigFormat +import com.electronwill.nightconfig.yaml.YamlFormat + /** * Type * @@ -8,7 +12,7 @@ package fr.xephi.authme.configruation */ enum class Type(private val format: () -> ConfigFormat) { - YAML({ YamlFormat.INSTANCE }); + YAML({ YamlFormat.defaultInstance() }); // TOML({ TomlFormat.instance() }), // @@ -25,7 +29,7 @@ enum class Type(private val format: () -> ConfigFormat) { companion object { fun getType(format: ConfigFormat<*>): Type { - return values().first { it.newFormat().javaClass == format.javaClass } + return entries.first { it.newFormat().javaClass == format.javaClass } } } } \ No newline at end of file diff --git a/project/module-logger/build.gradle.kts b/project/module-logger/build.gradle.kts new file mode 100644 index 00000000..c89e77cc --- /dev/null +++ b/project/module-logger/build.gradle.kts @@ -0,0 +1,3 @@ +dependencies { + compileOnly(libs.guava) +} \ No newline at end of file diff --git a/project/module-message/build.gradle.kts b/project/module-message/build.gradle.kts new file mode 100644 index 00000000..281f896d --- /dev/null +++ b/project/module-message/build.gradle.kts @@ -0,0 +1 @@ +dependencies {} \ No newline at end of file diff --git a/project/module-util/build.gradle.kts b/project/module-util/build.gradle.kts index 7ab8afb6..c89e77cc 100644 --- a/project/module-util/build.gradle.kts +++ b/project/module-util/build.gradle.kts @@ -1,3 +1,3 @@ dependencies { - compileOnly("com.google.guava:guava:33.2.1-jre") + compileOnly(libs.guava) } \ No newline at end of file diff --git a/project/module-util/src/main/java/fr/xephi/authme/util/Coerce.java b/project/module-util/src/main/java/fr/xephi/authme/util/Coerce.java index 529a32a2..8879506d 100644 --- a/project/module-util/src/main/java/fr/xephi/authme/util/Coerce.java +++ b/project/module-util/src/main/java/fr/xephi/authme/util/Coerce.java @@ -22,7 +22,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.common.data.builder; +package fr.xephi.authme.util; import com.google.common.primitives.Doubles; import com.google.common.primitives.Ints; @@ -44,7 +44,8 @@ public final class Coerce { /** * No subclasses for you. */ - private Coerce() {} + private Coerce() { + } /** * Gets the given object as a {@link String}.