diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt index 65871d3..c314360 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/FoundationCorePlugin.kt @@ -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 + } } } diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt index 5a9e148..1b86dde 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/PersistentStoreCommand.kt @@ -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): 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() + 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 + ): MutableList { + println(args.toList()) + return when { + args.isEmpty() -> { + allSubCommands + } + args.size == 1 -> { + allSubCommands.filter { it.startsWith(args[0]) }.toMutableList() + } + else -> { + mutableListOf() + } + } + } } diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt index ca0b5f0..ab42bd8 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/persist/PersistentStore.kt @@ -28,6 +28,9 @@ class PersistentStore(corePlugin: FoundationCorePlugin, fileStoreName: String) : fun find(entityTypeName: String, propertyName: String, value: Comparable, block: (EntityIterable) -> R): R = transact { block(find(entityTypeName, propertyName, value)) } + fun deleteAllEntities(entityTypeName: String) = + transact { entityStore.deleteEntityType(entityTypeName) } + override fun close() { entityStore.close() }