Skip to content

Commit

Permalink
feat: Fly Command (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Apr 24, 2024
1 parent 0cc6c2e commit 9cf19d2
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.time.Duration.Companion.seconds
object TableUsers : Table("users") {
val userUUID = varchar("uuid", 45)
val userName = varchar("username", 24)
val userRank = enumerationByName("rank", 24, Ranks::class).default(Ranks.Default)
val userRank = enumerationByName("rank", 24, Ranks::class).default(Ranks.Guest)
val userLanguage = enumerationByName("language", 2, Languages::class).default(Languages.EN)

val userFirstJoined = timestamp("firstJoined").defaultExpression(CurrentTimestamp())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlin.time.Duration
data class DatabaseUser(
val uuid: UUID,
val username: String,
val rank: Ranks = Ranks.Default,
val rank: Ranks = Ranks.Guest,
val language: Languages = Languages.EN,

// Other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun String.toOfflinePlayerIfCached(): OfflinePlayer? {


val Player.canBuild: Boolean
get() = gameMode == GameMode.SPECTATOR || (this.toDatabaseUser().rank.isHigherOrEqual(Ranks.Staff) && hasBuildTag)
get() = gameMode == GameMode.SPECTATOR || (this.toDatabaseUser().rank.isHigherOrEqual(Ranks.Trial) && hasBuildTag)

var Player.hasBuildTag: Boolean
get() = this.scoreboardTags.contains("builder")
Expand All @@ -82,4 +82,12 @@ fun UUID.toDatabaseUser(): DatabaseUser {

fun UUID.toDatabaseUserDB(): DatabaseUser {
return getDatabaseUserOrNull(this) ?: createDatabaseUser(DatabaseUser(this, Bukkit.getPlayer(this)?.name ?: Bukkit.getOfflinePlayer(this).name ?: "Unknown"))
}

fun CommandSender.isRankOrHigher(rank: Ranks): Boolean {
return if (this is Player) {
this.toDatabaseUser().rank.isHigherOrEqual(rank)
} else {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package net.blockventuremc.modules.general.commands.club

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.extensions.isRankOrHigher
import net.blockventuremc.extensions.sendMessagePrefixed
import net.blockventuremc.extensions.toDatabaseUser
import net.blockventuremc.extensions.translate
import net.blockventuremc.modules.general.model.Ranks
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.permissions.PermissionDefault

/**
* Class representing a command for toggling fly mode.
*
* @property name The name of the command.
* @property description The description of the command.
* @property permission The permission required to use the command.
* @property permissionDefault The default permission level for the command.
* @property usage The usage of the command.
*/
@BlockCommand(
name = "fly",
description = "Toggle fly mode",
permission = "blockventure.fly",
permissionDefault = PermissionDefault.TRUE,
usage = "/fly",
)
class FlyCommand : CommandExecutor {

/**
* Executes the command when it is invoked.
*
* @param sender The command sender.
* @param command The command being executed.
* @param label The command label.
* @param args The command arguments.
* @return True if the command was executed successfully, false otherwise.
*/
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
if (args.isNotEmpty() && sender.isRankOrHigher(Ranks.Trial)) {
val target = sender.server.getPlayerExact(args[0])
if (target == null) {
sender.sendMessagePrefixed("Player not found.")
return true
}
changeFlyModeOfUser(sender, target)
return true
}

if (sender !is Player) {
sender.sendMessagePrefixed("This command is only available to players. Use /fly <player> to toggle fly mode for another player.")
return true
}

if (!sender.isRankOrHigher(Ranks.ClubMember)) {
sender.sendMessagePrefixed(sender.translate("no_permission.club_member")?.message ?: "This command is only available to Club Members.")
sender.allowFlight = false
return true
}

changeFlyModeOfUser(sender, sender)

return true
}

/**
* Toggles the fly mode of a given player.
*
* @param commandExecutor The command executor.
* @param targetPlayer The player whose fly mode will be changed.
*/
private fun changeFlyModeOfUser(commandExecutor: CommandSender, targetPlayer: Player) {
val wasFlightAllowed = targetPlayer.allowFlight

targetPlayer.allowFlight = !wasFlightAllowed
val flightModeStatusMessage = if (targetPlayer.allowFlight) "enabled" else "disabled"

sendFlightModeToggleMessage(targetPlayer, flightModeStatusMessage)

if (targetPlayer != commandExecutor) {
sendNotifyExecutorMessage(commandExecutor, targetPlayer, flightModeStatusMessage)
}
}

/**
* Sends a flight mode toggle message to the player.
*
* @param player The player who will receive the message.
* @param flightModeStatusMessage The status message indicating whether the flight mode is enabled or disabled.
*/
private fun sendFlightModeToggleMessage(player: Player, flightModeStatusMessage: String) {
val message = player.toDatabaseUser().translate(
"fly_mode_toggled",
mapOf("enabled" to flightModeStatusMessage)
)?.message ?: "Fly mode is now $flightModeStatusMessage"
player.sendMessagePrefixed(message)
}

/**
* Sends a notification message to the command executor.
*
* @param commandExecutor The command sender who executed the command.
* @param targetPlayer The player for which the flight mode was toggled.
* @param flightModeStatusMessage The status message indicating whether the flight mode is enabled or disabled.
*/
private fun sendNotifyExecutorMessage(
commandExecutor: CommandSender,
targetPlayer: Player,
flightModeStatusMessage: String
) {
val message = if (commandExecutor is Player) {
val translatedMessage = commandExecutor.toDatabaseUser().translate(
"fly_mode_toggled_by",
mapOf(
"enabled" to flightModeStatusMessage,
"player" to targetPlayer.name
)
)?.message
translatedMessage ?: "Fly mode of ${targetPlayer.name} is now $flightModeStatusMessage"
} else {
"Fly mode of ${targetPlayer.name} is now $flightModeStatusMessage"
}
commandExecutor.sendMessagePrefixed(message)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package net.blockventuremc.modules.general.commands
package net.blockventuremc.modules.general.commands.crew

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.consts.*
import net.blockventuremc.extensions.*
import net.blockventuremc.extensions.hasBuildTag
import net.blockventuremc.extensions.sendMessagePrefixed
import net.blockventuremc.extensions.toDatabaseUser
import net.blockventuremc.extensions.translate
import org.bukkit.command.CommandExecutor
import org.bukkit.permissions.PermissionDefault
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import kotlin.to


@BlockCommand(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.blockventuremc.modules.general.commands
package net.blockventuremc.modules.general.commands.crew

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.cache.PlayerCache
Expand All @@ -10,6 +10,12 @@ import org.bukkit.command.TabCompleter
import org.bukkit.permissions.PermissionDefault
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import kotlin.collections.filter
import kotlin.collections.find
import kotlin.collections.map
import kotlin.collections.sortedByDescending
import kotlin.text.equals
import kotlin.text.startsWith

@BlockCommand(
name = "rank",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.blockventuremc.modules.general.commands
package net.blockventuremc.modules.general.commands.crew

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.database.model.DatabaseUser
Expand All @@ -21,7 +21,7 @@ class TestCommand: CommandExecutor {

val u = DatabaseUser(sender.uniqueId, sender.name)

if (!u.rank.isHigherOrEqual(Ranks.Staff)) {
if (!u.rank.isHigherOrEqual(Ranks.Crew)) {
sender.sendMessagePrefixed("You do not have permission to use this command.")
return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.blockventuremc.modules.general.commands
package net.blockventuremc.modules.general.commands.guests

import net.blockventuremc.annotations.BlockCommand
import net.blockventuremc.extensions.getLogger
Expand All @@ -10,7 +10,9 @@ import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.permissions.PermissionDefault
import java.lang.management.ManagementFactory
import kotlin.time.Duration.Companion.milliseconds
import kotlin.to


@BlockCommand(
Expand All @@ -31,7 +33,7 @@ class OnlinetimeCommand : CommandExecutor {
args: Array<out String>
): Boolean {
if (sender !is Player) {
val jreRunningSince = System.currentTimeMillis() - java.lang.management.ManagementFactory.getRuntimeMXBean().startTime
val jreRunningSince = System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().startTime
val runTimeDuration = jreRunningSince.milliseconds
getLogger().info("The server is running for $runTimeDuration")
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package net.blockventuremc.modules.general.model

enum class Ranks(val color: String) {
Crew("#54a0ff"),
Staff("#576574"),
Plus("#f368e0"),
Default("#c8d6e5");
Trial("#576574"),
ClubMember("#ea8685"),
Guest("#c8d6e5");

fun isHigherOrEqual(rank: Ranks): Boolean {
return this.ordinal <= rank.ordinal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object RegisterManager {
try {
commandInstance.onCommand(sender, command, label, args)
} catch (e: Exception) {
sender.sendMessagePrefixed("Ein Fehler ist aufgetreten!")
sender.sendMessagePrefixed("An error occurred while executing the command.")
throw e
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/translations/en-US.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"build_mode_toggled": "Build mode is now %enabled%.",
"onlinetime": "Your online time is %onlinetime%."
"fly_mode_toggled": "Fly mode is now %enabled%.",
"fly_mode_toggled_by": "Fly mode of %player% is now %enabled%.",
"onlinetime": "Your online time is %onlinetime%.",
"no_permission.club_member": "This command is only available to <color:#ea8685>Club Members</color>."
}

0 comments on commit 9cf19d2

Please sign in to comment.