From d3d88f467ef42bca82cea3dc3e87f846aaa5260e Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Wed, 30 Aug 2023 00:39:51 +0900 Subject: [PATCH] feat: make getTownConfigAt public --- build.gradle.kts | 2 +- .../mori01231/lifecore/command/TownConfigCommand.kt | 12 +++++++++++- .../github/mori01231/lifecore/config/TownConfig.kt | 12 +++++++++++- .../lifecore/listener/TownSpecificListener.kt | 13 +++---------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 817d85a..80513e2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.azisaba" -version = "6.6.10" +version = "6.6.11" java { toolchain.languageVersion.set(JavaLanguageVersion.of(8)) diff --git a/src/main/java/com/github/mori01231/lifecore/command/TownConfigCommand.kt b/src/main/java/com/github/mori01231/lifecore/command/TownConfigCommand.kt index eba6193..3b03f46 100644 --- a/src/main/java/com/github/mori01231/lifecore/command/TownConfigCommand.kt +++ b/src/main/java/com/github/mori01231/lifecore/command/TownConfigCommand.kt @@ -30,7 +30,11 @@ class TownConfigCommand(private val plugin: LifeCore) : PlayerTabExecutor() { } val valueString = args[1] val field = TownConfig::class.java.getField(args[0]) - val value = toValue(field.type, valueString) + val value = try { + toValue(field.type, valueString) + } catch (e: Exception) { + player.sendMessage("${ChatColor.RED}${e.message}") + } field.set(townConfig, value) plugin.lifeCoreConfig.save(plugin) player.sendMessage("${ChatColor.GOLD}${args[0]}${ChatColor.GREEN}を${ChatColor.WHITE}$value${ChatColor.GREEN}に設定しました。") @@ -71,7 +75,13 @@ class TownConfigCommand(private val plugin: LifeCore) : PlayerTabExecutor() { return type.enumConstants.first { (it as Enum<*>).name.lowercase() == value.lowercase() } } return when (type) { + String::class.java -> value Boolean::class.java -> value.toBoolean() + Int::class.java -> value.toInt() + Float::class.java -> value.toFloat() + Byte::class.java -> value.toByte() + Double::class.java -> value.toDouble() + Class::class.java -> Class.forName(value) else -> error("Unknown type: $type") } } diff --git a/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt b/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt index 9dee687..980236f 100644 --- a/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt +++ b/src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt @@ -1,9 +1,19 @@ package com.github.mori01231.lifecore.config +import com.github.mori01231.lifecore.listener.TownyOutlawListener import kotlinx.serialization.Serializable +import org.bukkit.Location @Serializable data class TownConfig( @JvmField var allowInvisibility: Boolean = true, @JvmField var allowSit: Boolean = true, -) +) { + companion object { + fun getTownConfigAt(lifeCoreConfig: LifeCoreConfig, location: Location): TownConfig? { + val town = TownyOutlawListener.getTownAt(location) ?: return null + val townName = TownyOutlawListener.getNameTownyObject(town) + return lifeCoreConfig.townConfig.computeIfAbsent(townName) { TownConfig() } + } + } +} diff --git a/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt b/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt index ceeb031..72f9ecb 100644 --- a/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt +++ b/src/main/java/com/github/mori01231/lifecore/listener/TownSpecificListener.kt @@ -5,7 +5,6 @@ import com.github.mori01231.lifecore.config.TownConfig import com.github.mori01231.lifecore.util.runTaskTimer import org.bukkit.Bukkit import org.bukkit.ChatColor -import org.bukkit.Location import org.bukkit.entity.LivingEntity import org.bukkit.event.EventHandler import org.bukkit.event.Listener @@ -14,19 +13,13 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent import org.bukkit.potion.PotionEffectType class TownSpecificListener(private val plugin: LifeCore) : Listener { - private fun getTownConfigAt(location: Location): TownConfig? { - val town = TownyOutlawListener.getTownAt(location) ?: return null - val townName = TownyOutlawListener.getNameTownyObject(town) - return plugin.lifeCoreConfig.townConfig.computeIfAbsent(townName) { TownConfig() } - } - fun startTask() { Bukkit.getScheduler().runTaskTimer(plugin, 20, 20) { Bukkit.getOnlinePlayers().forEach { player -> if (player.hasPermission("lifecore.bypass-town-restrictions")) { return@forEach } - val config = getTownConfigAt(player.location) ?: return@forEach + val config = TownConfig.getTownConfigAt(plugin.lifeCoreConfig, player.location) ?: return@forEach if (!config.allowInvisibility && player.hasPotionEffect(PotionEffectType.INVISIBILITY)) { player.removePotionEffect(PotionEffectType.INVISIBILITY) player.sendMessage("${ChatColor.RED}この町での透明化は許可されていません。") @@ -37,7 +30,7 @@ class TownSpecificListener(private val plugin: LifeCore) : Listener { @EventHandler fun onPotionSplash(e: PotionSplashEvent) { - val config = getTownConfigAt(e.potion.location) ?: return + val config = TownConfig.getTownConfigAt(plugin.lifeCoreConfig, e.potion.location) ?: return if (!config.allowInvisibility) { val toRemove = mutableListOf() e.affectedEntities.forEach { entity -> @@ -57,7 +50,7 @@ class TownSpecificListener(private val plugin: LifeCore) : Listener { val command = if (e.message.contains(' ')) e.message.substring(0, e.message.indexOf(' ')) else e.message val commandName = if (command.contains(':')) command.substring(command.indexOf(':') + 1) else command.substring(1) if (commandName == "sit" || commandName == "gsit") { - val config = getTownConfigAt(e.player.location) ?: return + val config = TownConfig.getTownConfigAt(plugin.lifeCoreConfig, e.player.location) ?: return if (!config.allowSit) { e.isCancelled = true e.player.sendMessage("${ChatColor.RED}この町での/sitは許可されていません。")