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

Rewrite some commands to match vanilla and add /list #116

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/dockyardmc/DockyardServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ class EnumArgument(
}
}

class ListArgument() {

}


class CommandArgumentData(
val argument: CommandArgument,
Expand Down
11 changes: 3 additions & 8 deletions src/main/kotlin/io/github/dockyardmc/commands/CommandHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = "<red>${ex.message}"
executor.sendMessage(message)
} else {
log(ex)
if (ConfigManager.config.implementationConfig.notifyUserOfExceptionDuringCommand) {
executor.sendMessage("${prefix}A <orange>${ex::class.qualifiedName} <red>was thrown during execution of this command!")
}
executor.sendMessage("<dark_red>Error <dark_gray>| <red>A <orange><hover:show_text:'<dark_red>${ex.message}'>${ex::class.qualifiedName}</hover> <red>was thrown during execution of this command!")
}
}
}
}


fun handleCommand(
command: Command,
executor: CommandExecutor,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/io/github/dockyardmc/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class Config {

class ImplementationConfig {
var applyBlockPlacementRules: Boolean = true
var notifyUserOfExceptionDuringCommand: Boolean = true
var commandErrorPrefix: String = "<dark_red>Error <dark_gray>| <red>"
var commandNoPermissionsMessage: String = "You do not have permissions to execute this command!"
var defaultEntityViewDistanceBlocks: Int = 64
var defaultCommands: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.dockyardmc.implementations

interface DefaultImplementationModule {
fun register()
}

Original file line number Diff line number Diff line change
@@ -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<String> = listOf(
"minecraft:furnace",
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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 {

init {
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>("player") ?: ctx.getPlayerOrThrow()
player.inventory.clear()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DebugCommand {

addSubcommand("world") {
addArgument("world", WorldArgument())
execute {
execute { ctx ->
val world = getArgument<World>("world")
val message = buildString {
append("\n")
Expand All @@ -25,12 +25,12 @@ class DebugCommand {
appendLine(" <gray>Scheduler running: ${(!world.scheduler.paused.value).toScrollText()}")
appendLine(" <gray>Scheduler tick rate: <lime>${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())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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()
SoundCommand()
GiveCommand()
TeleportCommand()
TimeCommand()
TickRateCommand()
SchedulerCommand()
ClearCommand()
ListCommand()
if(ConfigManager.config.debug) {
DebugCommand()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ class GamemodeCommand {

addOptionalArgument("player", PlayerArgument())

execute {
execute { ctx ->
val map = mapOf(
"gmc" to GameMode.CREATIVE,
"gms" to GameMode.SURVIVAL,
"gmsp" to GameMode.SPECTATOR,
"gma" to GameMode.ADVENTURE
)

val command = it.command.removePrefix("/")
val command = ctx.command.removePrefix("/")
val gamemode: GameMode = map[command]!!
val player: Player = getArgumentOrNull<Player>("player") ?: it.getPlayerOrThrow()
val player: Player = getArgumentOrNull<Player>("player") ?: ctx.getPlayerOrThrow()

val name = gamemode.name.properStrictCase()

if(player == it.player) {
if(player == ctx.player) {
player.sendMessage("<gray>Set your own gamemode to <white>$name")
} else {
it.sendMessage("<gray>Set gamemode of <white>$player <gray>to <white>$name")
ctx.sendMessage("<gray>Set gamemode of <white>$player <gray>to <white>$name")
player.sendMessage("<gray>Your gamemode has been updated to <white>$name")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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("<gray>There are <red>no players<gray> 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("<gray>There $isAreWord <lime>${PlayerManager.players.size}<gray> $playerWord online:")
PlayerManager.players.forEachIndexed { index, player ->
if(index % 5 == 0 && index != 0) append("\n")
append("<yellow>${player.username}<gray>")
if(index != (size - 1)) append(", ")

Check warning on line 27 in src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt

View check run for this annotation

codefactor.io / CodeFactor

src/main/kotlin/io/github/dockyardmc/implementations/commands/ListCommand.kt#L27

Parentheses in (size - 1) are unnecessary and can be replaced with: size - 1. (detekt.UnnecessaryParentheses)
}
appendLine(" ")
}

ctx.sendMessage(message)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("player")
val sound = getArgument<Sound>("sound")
val volume = getArgumentOrNull<Float>("volume") ?: 0.5f
val volume = getArgumentOrNull<Float>("volume") ?: 1f
val pitch = getArgumentOrNull<Float>("pitch") ?: 1f
val category = getEnumArgumentOrNull<SoundCategory>("category") ?: SoundCategory.MASTER
val category = getEnumArgument<SoundCategory>("category")

sound.category = category
sound.pitch = pitch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,40 @@ import io.github.dockyardmc.player.Player

class TimeCommand {

private companion object {
val TIMES_OF_DAY: Map<String, Long> = mapOf(
"day" to 1000,
"midnight" to 18000,
"night" to 13000,
"noon" to 6000
)

fun suggestTime(player: Player): List<String> {
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("<time>"))
execute {
val time = getArgument<Long>("time")
val player = it.getPlayerOrThrow()
addArgument("time", StringArgument(), ::suggestTime)
execute { ctx ->
val time = getArgument<String>("time")
val player = ctx.getPlayerOrThrow()
var newTime: Long? = null

if (TIMES_OF_DAY.contains(time)) newTime = TIMES_OF_DAY[time]!!

if (newTime == null) {
val tryParse = time.toLongOrNull() ?: throw CommandException("$time is not of type Long, nor does it represent 'day', 'midnight', 'night', or 'noon'")
newTime = tryParse
}

player.world.time.value = time
player.sendMessage("<gray>Set time in world <white>${player.world.name}<gray> to <white>$time")
player.world.time.value = newTime
player.sendMessage("<gray>Set time in world <white>${player.world.name}<gray> to <white>$newTime")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.dockyardmc.implementations.commands

import io.github.dockyardmc.DockyardServer
import io.github.dockyardmc.commands.Commands
import io.github.dockyardmc.utils.Branding
import io.github.dockyardmc.utils.DockyardBranding

class VersionAndHelpCommand {

Expand All @@ -12,16 +12,16 @@ class VersionAndHelpCommand {
withAliases("ver", "info", "server", "dockyard")
execute {
it.sendMessage("")
it.sendMessage("<gray>This server is running <${Branding.COLOR}>DockyardMC ${DockyardServer.versionInfo.dockyardVersion}<gray>. A custom Minecraft server implementation in Kotlin. <yellow><hover:show_text:'<yellow>https://github.com/DockyardMC/Dockyard'><click:open_url:https://github.com/DockyardMC/Dockyard>[Github]<reset>")
it.sendMessage("<gray>This server is running <${DockyardBranding.COLOR}>DockyardMC ${DockyardServer.versionInfo.dockyardVersion}<gray>. A custom Minecraft server implementation in Kotlin. <yellow><hover:show_text:'<yellow>https://github.com/DockyardMC/Dockyard'><click:open_url:https://github.com/DockyardMC/Dockyard>[Github]<reset>")
it.sendMessage("")
}
}

Commands.add("/help") {
withDescription("Shows list of commands")
withAliases("commands")
execute {
val accessibleCommands = Commands.commands.filter { command -> !command.value.isAlias && it.hasPermission(command.value.permission) }
execute { ctx ->
val accessibleCommands = Commands.commands.filter { command -> !command.value.isAlias && ctx.hasPermission(command.value.permission) }

val message = buildString {
val commandSize = when (accessibleCommands.size) {
Expand All @@ -38,7 +38,7 @@ class VersionAndHelpCommand {
appendLine("<gray> - <yellow>/${command.key} <gray> - <gray>$description")
}
}
it.sendMessage(message)
ctx.sendMessage(message)
}
}
}
Expand Down
Loading