Skip to content

Commit

Permalink
feat: make getTownConfigAt public
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Aug 29, 2023
1 parent 50b621b commit d3d88f4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.6.10"
version = "6.6.11"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}に設定しました。")
Expand Down Expand Up @@ -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")
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/github/mori01231/lifecore/config/TownConfig.kt
Original file line number Diff line number Diff line change
@@ -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() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}この町での透明化は許可されていません。")
Expand All @@ -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<LivingEntity>()
e.affectedEntities.forEach { entity ->
Expand All @@ -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は許可されていません。")
Expand Down

0 comments on commit d3d88f4

Please sign in to comment.