Skip to content

Commit

Permalink
feat(bukkit): add `/sayanvanish feature <feature> update <option> <va…
Browse files Browse the repository at this point in the history
…lue>` command
  • Loading branch information
Syrent committed Jun 2, 2024
1 parent cbba680 commit 96f3002
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sayandev.sayanvanish.api.feature

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class Configurable
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.sayandev.sayanvanish.api.SayanVanishAPI
import org.sayandev.sayanvanish.api.VanishOptions
import org.sayandev.sayanvanish.api.database.DatabaseConfig
import org.sayandev.sayanvanish.api.database.databaseConfig
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.Features
import org.sayandev.sayanvanish.api.feature.RegisteredFeatureHandler
import org.sayandev.sayanvanish.api.utils.Paste
Expand All @@ -33,11 +34,13 @@ import org.sayandev.stickynote.lib.incendo.cloud.bukkit.parser.OfflinePlayerPars
import org.sayandev.stickynote.lib.incendo.cloud.component.CommandComponent
import org.sayandev.stickynote.lib.incendo.cloud.parser.flag.CommandFlag
import org.sayandev.stickynote.lib.incendo.cloud.parser.standard.IntegerParser
import org.sayandev.stickynote.lib.incendo.cloud.parser.standard.StringArrayParser
import org.sayandev.stickynote.lib.incendo.cloud.parser.standard.StringParser
import org.sayandev.stickynote.lib.incendo.cloud.suggestion.Suggestion
import org.sayandev.stickynote.lib.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import java.io.File
import java.util.concurrent.CompletableFuture
import kotlin.reflect.full.memberProperties

class SayanVanishCommand : StickyCommand("sayanvanish", "vanish", "v") {

Expand Down Expand Up @@ -246,6 +249,69 @@ class SayanVanishCommand : StickyCommand("sayanvanish", "vanish", "v") {
sender.sendMessage(language.feature.featureEnabled.component(Placeholder.unparsed("feature", feature.id)))
}
.build())

manager.command(featureLiteral
.literal("update")
.permission(constructBasePermission("feature.update"))
.required(
"option",
CommandComponent.builder<SenderExtension, String>("state", StringParser.stringParser())
.suggestionProvider { context, _ ->
val feature = Features.features.find { it.id == context.get<String>("feature") } ?: return@suggestionProvider CompletableFuture.completedFuture(emptyList())
CompletableFuture.completedFuture(feature::class.java.declaredFields.filter { it.isAnnotationPresent(Configurable::class.java) }.map { Suggestion.suggestion(it.name) })
}
)
.required("value",
CommandComponent.builder<SenderExtension, String>("value", StringParser.stringParser(StringParser.StringMode.QUOTED))
.suggestionProvider { context, _ ->
val feature = Features.features.find { it.id == context.get<String>("feature") } ?: let {
return@suggestionProvider CompletableFuture.completedFuture(emptyList())
}

val field = feature::class.java.declaredFields.find { it.name == context.get("option") } ?: let {
return@suggestionProvider CompletableFuture.completedFuture(emptyList())
}
field.isAccessible = true

when (field.type) {
Boolean::class.java -> CompletableFuture.completedFuture(listOf("true", "false").map { Suggestion.suggestion(it) })
Int::class.java -> CompletableFuture.completedFuture(listOf("0", "1", "2", "3", "4", "5").map { Suggestion.suggestion(it) })
Double::class.java -> CompletableFuture.completedFuture(listOf("0.0", "0.1", "0.2", "0.3", "0.4", "0.5").map { Suggestion.suggestion(it) })
Float::class.java -> CompletableFuture.completedFuture(listOf("0.0", "0.1", "0.2", "0.3", "0.4", "0.5").map { Suggestion.suggestion(it) })
else -> CompletableFuture.completedFuture(listOf(field.get(feature).toString()).map { Suggestion.suggestion(it) })
}
}
)
.handler { context ->
val sender = context.sender().bukkitSender()
val feature = Features.features.find { it.id == context.get<String>("feature") } ?: let {
sender.sendMessage(language.feature.featureNotFound.component())
return@handler
}

val field = feature::class.java.declaredFields.find { it.name == context.get("option") }
if (field == null) {
sender.sendMessage(language.feature.invalidOption.component(Placeholder.unparsed("options", feature::class.memberProperties.joinToString(", ") { it.name })))
return@handler
}

val value = context.get<String>("value")
field.isAccessible = true
try {
field.set(feature, value)
} catch (_: Exception) {
sender.sendMessage(language.feature.invalidValue.component(Placeholder.unparsed("values", field.type.simpleName ?: "N/A")))
return@handler
}
feature.save()

sender.sendMessage(language.feature.updated.component(
Placeholder.unparsed("feature", feature.id),
Placeholder.unparsed("option", field.name),
Placeholder.unparsed("state", value)
))
}
.build())
}

private fun sendPasteError(sender: CommandSender, error: Throwable?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ data class LanguageConfig(
val featureDisabled: String = "<gray><gold><feature></gold> has been disabled.",
val alreadyDisabled: String = "<gray><gold><feature></gold> is already disabled.",
val alreadyEnabled: String = "<gray><gold><feature></gold> is already enabled.",
val updated: String = "<gray><gold><feature> <option></gold> has been updated to <gold><state></gold>.",
val invalidOption: String = "<red>Invalid option, valid options are <gold><options></gold>.",
val invalidValue: String = "<red>Invalid value, valid values are <gold><values></gold>.",
)

@ConfigSerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sayandev.sayanvanish.bukkit.feature.features

import org.bukkit.event.EventHandler
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI.Companion.user
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
Expand All @@ -16,9 +17,9 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureActionbar(
val content: String = "<gray>You are currently vanished!",
val delay: Long = 20,
val period: Long = 20,
@Configurable val content: String = "<gray>You are currently vanished!",
@Configurable val delay: Long = 20,
@Configurable val period: Long = 20,
) : ListenedFeature("actionbar") {

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.sayandev.sayanvanish.bukkit.feature.features
import org.bukkit.event.EventHandler
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserVanishEvent
Expand All @@ -17,12 +18,12 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureFakeMessage(
val sendFakeJoinMessage: Boolean = false,
val sendFakeQuitMessage: Boolean = false,
val fakeJoinMessage: String = "<yellow><player> joined the game",
val fakeQuitMessage: String = "<yellow><player> left the game",
val disableJoinMessageIfVanished: Boolean = true,
val disableQuitMessageIfVanished: Boolean = true,
@Configurable val sendFakeJoinMessage: Boolean = false,
@Configurable val sendFakeQuitMessage: Boolean = false,
@Configurable val fakeJoinMessage: String = "<yellow><player> joined the game",
@Configurable val fakeQuitMessage: String = "<yellow><player> left the game",
@Configurable val disableJoinMessageIfVanished: Boolean = true,
@Configurable val disableQuitMessageIfVanished: Boolean = true,
) : ListenedFeature("fake_message") {

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sayandev.sayanvanish.bukkit.feature.features

import org.bukkit.event.EventHandler
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserVanishEvent
Expand All @@ -11,7 +12,7 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureFly(
val disableOnReappear: Boolean = true
@Configurable val disableOnReappear: Boolean = true
) : ListenedFeature("fly") {

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sayandev.sayanvanish.bukkit.feature.features

import org.bukkit.event.EventHandler
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserVanishEvent
Expand All @@ -11,7 +12,7 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureInvulnerability(
val disableOnReappear: Boolean = true
@Configurable val disableOnReappear: Boolean = true
) : ListenedFeature("invulnerability") {

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.bukkit.event.EventHandler
import org.bukkit.event.player.PlayerGameModeChangeEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.sayandev.sayanvanish.api.Permission
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI
import org.sayandev.sayanvanish.bukkit.api.SayanVanishBukkitAPI.Companion.user
Expand All @@ -17,7 +18,7 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureLevel(
val seeAsSpectator: Boolean = true
@Configurable val seeAsSpectator: Boolean = true
): ListenedFeature("level") {

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sayandev.sayanvanish.bukkit.feature.features

import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.Feature
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.api.feature.category.FeatureCategories
Expand All @@ -15,7 +16,7 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureProxyVanishQueue(
val checkEvery: Long = 100
@Configurable val checkEvery: Long = 100
) : Feature("proxy_vanish_queue", category = FeatureCategories.PROXY) {

override fun enable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.event.EventHandler
import org.bukkit.event.player.PlayerJoinEvent
import org.sayandev.sayanvanish.api.feature.Configurable
import org.sayandev.sayanvanish.api.feature.RegisteredFeature
import org.sayandev.sayanvanish.bukkit.api.event.BukkitUserUnVanishEvent
import org.sayandev.sayanvanish.bukkit.feature.ListenedFeature
Expand All @@ -21,9 +22,9 @@ import org.sayandev.stickynote.lib.spongepowered.configurate.objectmapping.Confi
@RegisteredFeature
@ConfigSerializable
class FeatureUpdateChecker(
val checkEveryXMinutes: Int = 60,
val notifyPermission: String = "sayanvanish.feature.updatechecker.notify",
val notifyOnJoin: Boolean = true,
@Configurable val checkEveryXMinutes: Int = 60,
@Configurable val notifyPermission: String = "sayanvanish.feature.updatechecker.notify",
@Configurable val notifyOnJoin: Boolean = true,
val content: List<String> = listOf(
"<green>A new version of <white>SayanVanish</white> is available!",
"<gold> - Latest release: <white><latest_release_name>",
Expand Down

0 comments on commit 96f3002

Please sign in to comment.