From e33e5aac15dbc743c2bcd44586750c8ad336b61b Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Mon, 17 Mar 2025 19:47:48 +0100 Subject: [PATCH] Rewrite some commands to match vanilla and add /list --- build.gradle.kts | 2 +- .../io/github/dockyardmc/DockyardServer.kt | 4 +-- .../dockyardmc/commands/ArgumentTypes.kt | 4 --- .../dockyardmc/commands/CommandHandler.kt | 11 ++---- .../io/github/dockyardmc/config/Config.kt | 2 -- .../DefaultImplementationModule.kt | 6 ++++ .../block/DefaultBlockHandlers.kt | 5 +-- .../implementations/commands/ClearCommand.kt | 7 ++-- .../implementations/commands/DebugCommand.kt | 8 ++--- ...DockyardCommands.kt => DefaultCommands.kt} | 8 +++-- .../commands/GamemodeCommand.kt | 10 +++--- .../implementations/commands/ListCommand.kt | 36 +++++++++++++++++++ ...TickRateCommand.kt => SchedulerCommand.kt} | 6 ++-- .../implementations/commands/SoundCommand.kt | 11 +++--- .../implementations/commands/TimeCommand.kt | 33 +++++++++++++---- .../commands/VersionAndHelpCommand.kt | 10 +++--- .../implementations/commands/WorldCommand.kt | 28 +++++---------- .../io/github/dockyardmc/motd/ServerStatus.kt | 4 +-- .../{Branding.kt => DockyardBranding.kt} | 2 +- 19 files changed, 121 insertions(+), 76 deletions(-) create mode 100644 src/main/kotlin/io/github/dockyardmc/implementations/DefaultImplementationModule.kt rename src/main/kotlin/io/github/dockyardmc/implementations/commands/{DockyardCommands.kt => DefaultCommands.kt} (65%) create mode 100644 src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt rename src/main/kotlin/io/github/dockyardmc/implementations/commands/{TickRateCommand.kt => SchedulerCommand.kt} (94%) rename src/main/kotlin/io/github/dockyardmc/utils/{Branding.kt => DockyardBranding.kt} (85%) diff --git a/build.gradle.kts b/build.gradle.kts index ddfa781e..e797bb7d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ dependencies { // Minecraft api("io.github.jglrxavpok.hephaistos:common:2.2.0") api("io.github.jglrxavpok.hephaistos:gson:2.2.0") - api("io.github.dockyardmc:scroll:2.7") + api("io.github.dockyardmc:scroll:2.8") implementation("io.github.dockyardmc:wikivg-datagen:1.3") // Pathfinding diff --git a/src/main/kotlin/io/github/dockyardmc/DockyardServer.kt b/src/main/kotlin/io/github/dockyardmc/DockyardServer.kt index 457a263e..5419e3da 100644 --- a/src/main/kotlin/io/github/dockyardmc/DockyardServer.kt +++ b/src/main/kotlin/io/github/dockyardmc/DockyardServer.kt @@ -8,7 +8,7 @@ import io.github.dockyardmc.events.Events import io.github.dockyardmc.events.ServerFinishLoadEvent import io.github.dockyardmc.events.WorldFinishLoadingEvent import io.github.dockyardmc.implementations.block.DefaultBlockHandlers -import io.github.dockyardmc.implementations.commands.DockyardCommands +import io.github.dockyardmc.implementations.commands.DefaultCommands import io.github.dockyardmc.npc.NpcCommand import io.github.dockyardmc.protocol.NetworkCompression import io.github.dockyardmc.protocol.packets.registry.ClientPacketRegistry @@ -66,7 +66,7 @@ class DockyardServer(configBuilder: Config.() -> Unit) { RegistryManager.register(ItemTagRegistry) RegistryManager.register(BiomeTagRegistry) - if(ConfigManager.config.implementationConfig.defaultCommands) DockyardCommands() + if(ConfigManager.config.implementationConfig.defaultCommands) DefaultCommands().register() if(ConfigManager.config.implementationConfig.npcCommand) NpcCommand() if(ConfigManager.config.implementationConfig.spark) SparkDockyardIntegration().initialize() if(ConfigManager.config.implementationConfig.applyBlockPlacementRules) DefaultBlockHandlers().register() diff --git a/src/main/kotlin/io/github/dockyardmc/commands/ArgumentTypes.kt b/src/main/kotlin/io/github/dockyardmc/commands/ArgumentTypes.kt index 2361545e..2fe9ad46 100644 --- a/src/main/kotlin/io/github/dockyardmc/commands/ArgumentTypes.kt +++ b/src/main/kotlin/io/github/dockyardmc/commands/ArgumentTypes.kt @@ -217,10 +217,6 @@ class EnumArgument( } } -class ListArgument() { - -} - class CommandArgumentData( val argument: CommandArgument, diff --git a/src/main/kotlin/io/github/dockyardmc/commands/CommandHandler.kt b/src/main/kotlin/io/github/dockyardmc/commands/CommandHandler.kt index c445f178..9668e227 100644 --- a/src/main/kotlin/io/github/dockyardmc/commands/CommandHandler.kt +++ b/src/main/kotlin/io/github/dockyardmc/commands/CommandHandler.kt @@ -21,8 +21,6 @@ import java.util.* object CommandHandler { - val prefix get() = ConfigManager.config.implementationConfig.commandErrorPrefix - fun handleCommandInput(inputCommand: String, executor: CommandExecutor, testEnv: Boolean = false) { DockyardServer.scheduler.run { val tokens = inputCommand.removePrefix("/").split(" ").toMutableList() @@ -47,19 +45,16 @@ object CommandHandler { } catch (ex: Exception) { if (testEnv) throw IllegalArgumentException(ex) if (ex is CommandException) { - val message = "$prefix${ex.message}" + val message = "${ex.message}" executor.sendMessage(message) } else { log(ex) - if (ConfigManager.config.implementationConfig.notifyUserOfExceptionDuringCommand) { - executor.sendMessage("${prefix}A ${ex::class.qualifiedName} was thrown during execution of this command!") - } + executor.sendMessage("Error | A ${ex.message}'>${ex::class.qualifiedName} was thrown during execution of this command!") } } } } - fun handleCommand( command: Command, executor: CommandExecutor, @@ -117,7 +112,7 @@ object CommandHandler { Float::class -> value.toFloatOrNull() ?: throw CommandException("\"$value\" is not of type Float") Long::class -> value.toLongOrNull() ?: throw CommandException("\"$value\" is not of type Long") UUID::class -> UUID.fromString(value) - Item::class -> ItemRegistry.get(value) ?: throw CommandException("\"$value\" is not of type Item") + Item::class -> ItemRegistry.getOrNull(value) ?: ItemRegistry.getOrNull("minecraft:$value") ?: throw CommandException("\"$value\" is not of type Item") RegistryBlock::class -> { if (value.contains("[")) { //block state diff --git a/src/main/kotlin/io/github/dockyardmc/config/Config.kt b/src/main/kotlin/io/github/dockyardmc/config/Config.kt index 96995d6d..b82ab80c 100644 --- a/src/main/kotlin/io/github/dockyardmc/config/Config.kt +++ b/src/main/kotlin/io/github/dockyardmc/config/Config.kt @@ -47,8 +47,6 @@ class Config { class ImplementationConfig { var applyBlockPlacementRules: Boolean = true - var notifyUserOfExceptionDuringCommand: Boolean = true - var commandErrorPrefix: String = "Error | " var commandNoPermissionsMessage: String = "You do not have permissions to execute this command!" var defaultEntityViewDistanceBlocks: Int = 64 var defaultCommands: Boolean = true diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/DefaultImplementationModule.kt b/src/main/kotlin/io/github/dockyardmc/implementations/DefaultImplementationModule.kt new file mode 100644 index 00000000..6c44ff23 --- /dev/null +++ b/src/main/kotlin/io/github/dockyardmc/implementations/DefaultImplementationModule.kt @@ -0,0 +1,6 @@ +package io.github.dockyardmc.implementations + +interface DefaultImplementationModule { + fun register() +} + diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/block/DefaultBlockHandlers.kt b/src/main/kotlin/io/github/dockyardmc/implementations/block/DefaultBlockHandlers.kt index 0fd36018..e29ed7ff 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/block/DefaultBlockHandlers.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/block/DefaultBlockHandlers.kt @@ -1,8 +1,9 @@ package io.github.dockyardmc.implementations.block +import io.github.dockyardmc.implementations.DefaultImplementationModule import io.github.dockyardmc.world.block.handlers.* -class DefaultBlockHandlers { +class DefaultBlockHandlers: DefaultImplementationModule { private val facingBlocks: List = listOf( "minecraft:furnace", @@ -19,7 +20,7 @@ class DefaultBlockHandlers { "minecraft:bookshelf", ) - fun register() { + override fun register() { BlockHandlerManager.register(BlockHandlerManager.Type.TAG, "minecraft:slabs", SlabBlockHandler()) BlockHandlerManager.register(BlockHandlerManager.Type.BLOCK, "minecraft:barrel", BarrelBlockHandler()) BlockHandlerManager.register(BlockHandlerManager.Type.TAG, "minecraft:buttons", ButtonBlockHandler()) diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/ClearCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/ClearCommand.kt index 0adfbede..cc034946 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/ClearCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/ClearCommand.kt @@ -1,6 +1,8 @@ package io.github.dockyardmc.implementations.commands import io.github.dockyardmc.commands.Commands +import io.github.dockyardmc.commands.PlayerArgument +import io.github.dockyardmc.player.Player class ClearCommand { @@ -8,8 +10,9 @@ class ClearCommand { Commands.add("/clear") { withPermission("dockyard.commands.clear") withDescription("Clears your inventory") - execute { - val player = it.getPlayerOrThrow() + addOptionalArgument("player", PlayerArgument()) + execute { ctx -> + val player = getArgumentOrNull("player") ?: ctx.getPlayerOrThrow() player.inventory.clear() } } diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/DebugCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/DebugCommand.kt index ca248a1a..a264fc85 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/DebugCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/DebugCommand.kt @@ -14,7 +14,7 @@ class DebugCommand { addSubcommand("world") { addArgument("world", WorldArgument()) - execute { + execute { ctx -> val world = getArgument("world") val message = buildString { append("\n") @@ -25,12 +25,12 @@ class DebugCommand { appendLine(" Scheduler running: ${(!world.scheduler.paused.value).toScrollText()}") appendLine(" Scheduler tick rate: ${world.scheduler.tickRate.value.inWholeMilliseconds}ms") } - it.sendMessage(message) + ctx.sendMessage(message) } } addSubcommand("events") { - execute { - it.sendMessage(Events.debugTree()) + execute { ctx -> + ctx.sendMessage(Events.debugTree()) } } } diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/DockyardCommands.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/DefaultCommands.kt similarity index 65% rename from src/main/kotlin/io/github/dockyardmc/implementations/commands/DockyardCommands.kt rename to src/main/kotlin/io/github/dockyardmc/implementations/commands/DefaultCommands.kt index 268ff140..25d46309 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/DockyardCommands.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/DefaultCommands.kt @@ -1,10 +1,11 @@ package io.github.dockyardmc.implementations.commands import io.github.dockyardmc.config.ConfigManager +import io.github.dockyardmc.implementations.DefaultImplementationModule -class DockyardCommands { +class DefaultCommands: DefaultImplementationModule { - init { + override fun register() { GamemodeCommand() VersionAndHelpCommand() WorldCommand() @@ -12,8 +13,9 @@ class DockyardCommands { GiveCommand() TeleportCommand() TimeCommand() - TickRateCommand() + SchedulerCommand() ClearCommand() + ListCommand() if(ConfigManager.config.debug) { DebugCommand() } diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/GamemodeCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/GamemodeCommand.kt index 599266be..f520ec55 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/GamemodeCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/GamemodeCommand.kt @@ -38,7 +38,7 @@ class GamemodeCommand { addOptionalArgument("player", PlayerArgument()) - execute { + execute { ctx -> val map = mapOf( "gmc" to GameMode.CREATIVE, "gms" to GameMode.SURVIVAL, @@ -46,16 +46,16 @@ class GamemodeCommand { "gma" to GameMode.ADVENTURE ) - val command = it.command.removePrefix("/") + val command = ctx.command.removePrefix("/") val gamemode: GameMode = map[command]!! - val player: Player = getArgumentOrNull("player") ?: it.getPlayerOrThrow() + val player: Player = getArgumentOrNull("player") ?: ctx.getPlayerOrThrow() val name = gamemode.name.properStrictCase() - if(player == it.player) { + if(player == ctx.player) { player.sendMessage("Set your own gamemode to $name") } else { - it.sendMessage("Set gamemode of $player to $name") + ctx.sendMessage("Set gamemode of $player to $name") player.sendMessage("Your gamemode has been updated to $name") } diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt new file mode 100644 index 00000000..486f37ab --- /dev/null +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt @@ -0,0 +1,36 @@ +package io.github.dockyardmc.implementations.commands + +import io.github.dockyardmc.commands.Commands +import io.github.dockyardmc.player.PlayerManager + +class ListCommand { + + init { + Commands.add("/list") { + execute { ctx -> + val size = PlayerManager.players.size + if(size == 0) { + ctx.sendMessage(" ") + ctx.sendMessage("There are no players online") + ctx.sendMessage(" ") + return@execute + } + + val message = buildString { + val playerWord = if(size == 1) "player" else "players" + val isAreWord = if(size == 1) "is" else "are" + appendLine(" ") + appendLine("There $isAreWord ${PlayerManager.players.size} $playerWord online:") + PlayerManager.players.forEachIndexed { index, player -> + if(index % 5 == 0 && index != 0) append("\n") + append("${player.username}") + if(index != (size - 1)) append(", ") + } + appendLine(" ") + } + + ctx.sendMessage(message) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/TickRateCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/SchedulerCommand.kt similarity index 94% rename from src/main/kotlin/io/github/dockyardmc/implementations/commands/TickRateCommand.kt rename to src/main/kotlin/io/github/dockyardmc/implementations/commands/SchedulerCommand.kt index de755888..6f8f7e81 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/TickRateCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/SchedulerCommand.kt @@ -7,11 +7,11 @@ import io.github.dockyardmc.extentions.toScrollText import io.github.dockyardmc.world.World import kotlin.time.Duration.Companion.milliseconds -class TickRateCommand { +class SchedulerCommand { init { - Commands.add("/tickrate") { - withPermission("dockyard.commands.tickrate") + Commands.add("/scheduler") { + withPermission("dockyard.commands.scheduler") withDescription("Lets you change tickrate/pause/resume scheduler of a world") addSubcommand("set") { diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/SoundCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/SoundCommand.kt index 7ef2b9c7..9a9b342f 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/SoundCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/SoundCommand.kt @@ -11,21 +11,20 @@ class SoundCommand { init { Commands.add("/playsound") { withPermission("dockyard.commands.playsound") - withDescription("Plays sounds") + withDescription("Plays a sound to player") - addArgument("player", PlayerArgument()) addArgument("sound", SoundArgument()) - addOptionalArgument("volume", FloatArgument()) + addArgument("category", EnumArgument(SoundCategory::class)) + addArgument("player", PlayerArgument()) addOptionalArgument("volume", FloatArgument()) addOptionalArgument("pitch", FloatArgument()) - addOptionalArgument("category", EnumArgument(SoundCategory::class)) execute { val player = getArgument("player") val sound = getArgument("sound") - val volume = getArgumentOrNull("volume") ?: 0.5f + val volume = getArgumentOrNull("volume") ?: 1f val pitch = getArgumentOrNull("pitch") ?: 1f - val category = getEnumArgumentOrNull("category") ?: SoundCategory.MASTER + val category = getEnumArgument("category") sound.category = category sound.pitch = pitch diff --git a/src/main/kotlin/io/github/dockyardmc/implementations/commands/TimeCommand.kt b/src/main/kotlin/io/github/dockyardmc/implementations/commands/TimeCommand.kt index c8428228..070ba1a2 100644 --- a/src/main/kotlin/io/github/dockyardmc/implementations/commands/TimeCommand.kt +++ b/src/main/kotlin/io/github/dockyardmc/implementations/commands/TimeCommand.kt @@ -6,19 +6,40 @@ import io.github.dockyardmc.player.Player class TimeCommand { + private companion object { + val TIMES_OF_DAY: Map = mapOf( + "day" to 1000, + "midnight" to 18000, + "night" to 13000, + "noon" to 6000 + ) + + fun suggestTime(player: Player): List { + return TIMES_OF_DAY.keys.toList() + } + } + init { Commands.add("/time") { withDescription("Lets you change and freeze time of the world") withPermission("dockyard.commands.time") addSubcommand("set") { - addArgument("time", LongArgument(), simpleSuggestion("