Skip to content

Commit

Permalink
TPS Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Jan 16, 2024
1 parent 1a76f09 commit 7e1d62b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ val allCommands = arrayOf(
ChatSyncCommand,
LinkRoleCommand,
UptimeCommand,
StopCommand
StopCommand,
TPSCommand
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ object StopCommand : HandledSlashCommand {
minecraftHandler?.stopServer()
}
}
}

object TPSCommand : HandledSlashCommand {
override val command = Commands.slash("tps", "Shows information about the tick speed").setDefaultPermissions(
DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR)
)

override fun handle(event: SlashCommandInteractionEvent) {
event.interaction.reply(minecraftHandler?.tickReport() ?: "No Information Available").queue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import net.minecraft.server.MinecraftServer
import net.minecraft.server.level.ServerPlayer
import java.util.*
import kotlin.jvm.optionals.getOrNull
import kotlin.math.min
import kotlin.math.round

/**
* Wrapper class around the [MinecraftServer] to provide convenience
Expand Down Expand Up @@ -49,6 +51,11 @@ class MinecraftHandler(private val server: MinecraftServer) : ListenerAdapter()
server.halt(false)
}

fun tickReport(): String {
// Idea from the TPSCommand in Forge
return "Average Tick Time: ${round(server.averageTickTime * 100) / 100} (TPS: ${min(20.0, 1000.0/server.averageTickTime)})"
}

/**
* Fetches the [GameProfile] of a given Minecraft user ID
* @param id a User ID possibly associated with a Minecraft Account
Expand Down

0 comments on commit 7e1d62b

Please sign in to comment.