Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add geary load command #84

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,55 @@ package com.mineinabyss.geary.papermc.plugin

import com.mineinabyss.geary.papermc.GearyMCContext
import com.mineinabyss.geary.papermc.GearyMCContextKoin
import com.mineinabyss.geary.papermc.GearyPlugin
import com.mineinabyss.geary.papermc.StartupEventListener
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.PrefabManager
import com.mineinabyss.idofront.commands.arguments.stringArg
import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor
import com.mineinabyss.idofront.messaging.*
import com.rylinaux.plugman.util.PluginUtil
import kotlinx.coroutines.launch
import okio.Path.Companion.toPath
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import java.io.File
import java.nio.file.Path
import kotlin.io.path.listDirectoryEntries

internal class GearyCommands : IdofrontCommandExecutor(), TabCompleter, GearyMCContext by GearyMCContextKoin() {
override val commands = commands(geary) {
"geary" {
"load" {
val prefab by stringArg()
action {
val prefabKey = PrefabKey.of(prefab)
val file: File? = gearyEntityFiles.firstOrNull { it.name == "${prefabKey.key}.yml" }

if (file == null) {
sender.error("Could not load <i>$prefab</i>, as no file could be tied to this entity")
return@action
}

if (prefabKey.toEntityOrNull() != null) {
sender.error("<i>$prefab</i> has already been loaded")
sender.warn("Use /geary reread if you wish to reload it")
return@action
}

prefabManager.loadFromFile(prefabKey.namespace, file)
// If entity is null it means it failed to load, and we should skip it
if (prefabKey.toEntityOrNull() == null) sender.error("Failed to load <i>$prefab</i> into a GearyEntity")
else sender.success("Loaded <i>$prefab</i> into a GearyEntity")
}
}
"reread" {
val prefab by stringArg()
action {
engine.launch {
prefabManager.reread(
PrefabKey.of(prefab).toEntity()
)
prefabManager.reread(PrefabKey.of(prefab).toEntity())
}
sender.success("Reloaded $prefab GearyEntity")
}
}
"fullreload" {
Expand All @@ -30,6 +59,7 @@ internal class GearyCommands : IdofrontCommandExecutor(), TabCompleter, GearyMCC
depends.forEach { PluginUtil.unload(it) }
PluginUtil.reload(geary)
depends.forEach { PluginUtil.load(it.name) }
sender.success("Reloaded all GearyEntities")
}
}
// "countArchetypes" {
Expand All @@ -49,6 +79,7 @@ internal class GearyCommands : IdofrontCommandExecutor(), TabCompleter, GearyMCC
}
}

private val gearyEntityFiles = geary.dataFolder.walkTopDown().filter { it.name != "config.yml" }.toMutableList()
override fun onTabComplete(
sender: CommandSender,
command: org.bukkit.command.Command,
Expand All @@ -57,13 +88,20 @@ internal class GearyCommands : IdofrontCommandExecutor(), TabCompleter, GearyMCC
): List<String> {
return when (args.size) {
1 -> listOf(
"load",
"reread",
"fullreload",
"countArchetypes"
)

2 -> {
when (args[0]) {
"load" -> gearyEntityFiles.filter {
val arg = args[1].lowercase()
val prefab = "${it.getGearyNamespace()}:${it.nameWithoutExtension}"
(it.nameWithoutExtension.startsWith(arg) || prefab.startsWith(arg)) &&
PrefabKey.of(prefab).toEntityOrNull() == null
}.map { "${it.getGearyNamespace()}:${it.nameWithoutExtension}" }.toList()
"reread" -> prefabManager.keys.filter {
val arg = args[1].lowercase()
it.key.startsWith(arg) || it.full.startsWith(arg)
Expand All @@ -76,4 +114,6 @@ internal class GearyCommands : IdofrontCommandExecutor(), TabCompleter, GearyMCC
else -> listOf()
}
}

private fun File.getGearyNamespace() = this.path.replaceFirst(geary.dataFolder.path + "\\", "").substringBefore("\\")
}