Persistent Store Command Tweaks

- delete-all-entities command.
- Basic tab completion.
This commit is contained in:
Kenneth Endfinger 2021-12-22 22:06:27 -05:00
parent b91602d719
commit ecdb6a2898
No known key found for this signature in database
GPG Key ID: C4E68E5647420E10
3 changed files with 48 additions and 18 deletions

View File

@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import org.bukkit.GameMode
import org.bukkit.command.CommandExecutor
import org.bukkit.command.TabCompleter
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin
@ -81,6 +82,9 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
for (name in names) {
val command = getCommand(name) ?: throw Exception("Failed to get $name command")
command.setExecutor(executor)
if (executor is TabCompleter) {
command.tabCompleter = executor
}
}
}

View File

@ -1,13 +1,14 @@
package cloud.kubelet.foundation.core.command
import cloud.kubelet.foundation.core.FoundationCorePlugin
import jetbrains.exodus.entitystore.Entity
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import java.util.*
import org.bukkit.command.TabCompleter
class PersistentStoreCommand(private val plugin: FoundationCorePlugin) : CommandExecutor, TabCompleter {
private val allSubCommands = mutableListOf("stats", "sample", "delete-all-entities")
class PersistentStoreCommand(private val plugin: FoundationCorePlugin) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (args.isEmpty()) {
sender.sendMessage("Invalid Command Usage.")
@ -36,32 +37,54 @@ class PersistentStoreCommand(private val plugin: FoundationCorePlugin) : Command
val storeName = args[1]
val entityTypeName = args[2]
val store = plugin.getPersistentStore(storeName)
val random = Random()
store.transact {
val entities = getAll(entityTypeName)
val results = mutableListOf<Entity>()
val entities = getAll(entityTypeName).take(3)
for (entity in entities) {
if (random.nextBoolean()) {
results.add(entity)
}
if (results.size == 3) {
break
}
}
for (result in results) {
sender.sendMessage(
"Entity ${result.id.localId} ->",
*result.propertyNames.map { " ${it}: ${result.getProperty(it)}" }.toTypedArray()
"Entity ${entity.id.localId} ->",
*entity.propertyNames.map { " ${it}: ${entity.getProperty(it)}" }.toTypedArray()
)
}
}
}
"delete-all-entities" -> {
if (args.size != 3) {
sender.sendMessage("Invalid Subcommand Usage.")
return true
}
val storeName = args[1]
val entityTypeName = args[2]
val store = plugin.getPersistentStore(storeName)
store.transact {
store.deleteAllEntities(entityTypeName)
}
sender.sendMessage("Deleted all entities for $storeName $entityTypeName")
}
else -> {
sender.sendMessage("Unknown Subcommand.")
}
}
return true
}
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>
): MutableList<String> {
println(args.toList())
return when {
args.isEmpty() -> {
allSubCommands
}
args.size == 1 -> {
allSubCommands.filter { it.startsWith(args[0]) }.toMutableList()
}
else -> {
mutableListOf()
}
}
}
}

View File

@ -28,6 +28,9 @@ class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) :
fun <T, R> find(entityTypeName: String, propertyName: String, value: Comparable<T>, block: (EntityIterable) -> R): R =
transact { block(find(entityTypeName, propertyName, value)) }
fun deleteAllEntities(entityTypeName: String) =
transact { entityStore.deleteEntityType(entityTypeName) }
override fun close() {
entityStore.close()
}