mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-02 13:10:55 +00:00
Persistent Store Command Tweaks
- delete-all-entities command. - Basic tab completion.
This commit is contained in:
parent
b91602d719
commit
ecdb6a2898
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user