mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 21:41:32 +00:00
Persistent Store Command Tweaks
- delete-all-entities command. - Basic tab completion.
This commit is contained in:
@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component
|
|||||||
import net.kyori.adventure.text.TextComponent
|
import net.kyori.adventure.text.TextComponent
|
||||||
import org.bukkit.GameMode
|
import org.bukkit.GameMode
|
||||||
import org.bukkit.command.CommandExecutor
|
import org.bukkit.command.CommandExecutor
|
||||||
|
import org.bukkit.command.TabCompleter
|
||||||
import org.bukkit.event.EventHandler
|
import org.bukkit.event.EventHandler
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
import org.bukkit.plugin.java.JavaPlugin
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
@ -81,6 +82,9 @@ class FoundationCorePlugin : JavaPlugin(), Listener {
|
|||||||
for (name in names) {
|
for (name in names) {
|
||||||
val command = getCommand(name) ?: throw Exception("Failed to get $name command")
|
val command = getCommand(name) ?: throw Exception("Failed to get $name command")
|
||||||
command.setExecutor(executor)
|
command.setExecutor(executor)
|
||||||
|
if (executor is TabCompleter) {
|
||||||
|
command.tabCompleter = executor
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package cloud.kubelet.foundation.core.command
|
package cloud.kubelet.foundation.core.command
|
||||||
|
|
||||||
import cloud.kubelet.foundation.core.FoundationCorePlugin
|
import cloud.kubelet.foundation.core.FoundationCorePlugin
|
||||||
import jetbrains.exodus.entitystore.Entity
|
|
||||||
import org.bukkit.command.Command
|
import org.bukkit.command.Command
|
||||||
import org.bukkit.command.CommandExecutor
|
import org.bukkit.command.CommandExecutor
|
||||||
import org.bukkit.command.CommandSender
|
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 {
|
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
|
||||||
if (args.isEmpty()) {
|
if (args.isEmpty()) {
|
||||||
sender.sendMessage("Invalid Command Usage.")
|
sender.sendMessage("Invalid Command Usage.")
|
||||||
@ -36,32 +37,54 @@ class PersistentStoreCommand(private val plugin: FoundationCorePlugin) : Command
|
|||||||
val storeName = args[1]
|
val storeName = args[1]
|
||||||
val entityTypeName = args[2]
|
val entityTypeName = args[2]
|
||||||
val store = plugin.getPersistentStore(storeName)
|
val store = plugin.getPersistentStore(storeName)
|
||||||
val random = Random()
|
|
||||||
store.transact {
|
store.transact {
|
||||||
val entities = getAll(entityTypeName)
|
val entities = getAll(entityTypeName).take(3)
|
||||||
val results = mutableListOf<Entity>()
|
|
||||||
for (entity in entities) {
|
for (entity in entities) {
|
||||||
if (random.nextBoolean()) {
|
|
||||||
results.add(entity)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (results.size == 3) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (result in results) {
|
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
"Entity ${result.id.localId} ->",
|
"Entity ${entity.id.localId} ->",
|
||||||
*result.propertyNames.map { " ${it}: ${result.getProperty(it)}" }.toTypedArray()
|
*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 -> {
|
else -> {
|
||||||
sender.sendMessage("Unknown Subcommand.")
|
sender.sendMessage("Unknown Subcommand.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
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 =
|
fun <T, R> find(entityTypeName: String, propertyName: String, value: Comparable<T>, block: (EntityIterable) -> R): R =
|
||||||
transact { block(find(entityTypeName, propertyName, value)) }
|
transact { block(find(entityTypeName, propertyName, value)) }
|
||||||
|
|
||||||
|
fun deleteAllEntities(entityTypeName: String) =
|
||||||
|
transact { entityStore.deleteEntityType(entityTypeName) }
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
entityStore.close()
|
entityStore.close()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user