Skip to content

Commit

Permalink
Restore sync command
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed Oct 19, 2024
1 parent 58be341 commit c2ed5b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.projectcitybuild.pcbridge.features.register.commands.CodeCommand
import com.projectcitybuild.pcbridge.features.register.commands.RegisterCommand
import com.projectcitybuild.pcbridge.features.staffchat.commands.StaffChatCommand
import com.projectcitybuild.pcbridge.features.groups.actions.SyncPlayerGroups
import com.projectcitybuild.pcbridge.features.groups.commands.SyncCommand
import com.projectcitybuild.pcbridge.features.groups.listener.SyncRankListener
import com.projectcitybuild.pcbridge.features.groups.repositories.SyncRepository
import com.projectcitybuild.pcbridge.features.playerstate.listeners.PlayerSyncRequestListener
Expand Down Expand Up @@ -522,4 +523,11 @@ private fun Module.groups() {
syncPlayerGroups = get(),
)
}

factory {
SyncCommand(
server = get(),
eventBroadcaster = get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.projectcitybuild.pcbridge.features.announcements.listeners.Announceme
import com.projectcitybuild.pcbridge.features.bans.listeners.AuthorizeConnectionListener
import com.projectcitybuild.pcbridge.features.chat.listeners.EmojiChatListener
import com.projectcitybuild.pcbridge.features.chat.listeners.FormatNameChatListener
import com.projectcitybuild.pcbridge.features.groups.commands.SyncCommand
import com.projectcitybuild.pcbridge.features.invisframes.commands.InvisFrameCommand
import com.projectcitybuild.pcbridge.features.invisframes.listeners.FrameItemInsertListener
import com.projectcitybuild.pcbridge.features.invisframes.listeners.FrameItemRemoveListener
Expand Down Expand Up @@ -114,6 +115,10 @@ private class Lifecycle : KoinComponent {
handler = get<CodeCommand>(),
argsParser = CodeCommand.Args.Parser(),
)
register(
handler = get<SyncCommand>(),
argsParser = SyncCommand.Args.Parser(),
)
}
listenerRegistry.register(
get<AnnounceJoinListener>(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.projectcitybuild.pcbridge.features.groups.commands

import com.projectcitybuild.pcbridge.features.groups.events.PlayerSyncRequestedEvent
import com.projectcitybuild.pcbridge.support.messages.CommandHelpBuilder
import com.projectcitybuild.pcbridge.support.spigot.BadCommandUsageException
import com.projectcitybuild.pcbridge.support.spigot.CommandArgsParser
import com.projectcitybuild.pcbridge.support.spigot.SpigotCommand
import com.projectcitybuild.pcbridge.support.spigot.SpigotEventBroadcaster
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Server
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class SyncCommand(
private val server: Server,
private val eventBroadcaster: SpigotEventBroadcaster,
) : SpigotCommand<SyncCommand.Args> {
override val label = "sync"

Expand All @@ -20,21 +26,52 @@ class SyncCommand(
check(sender is Player) {
"Only players can use this command"
}
// TODO
if (args.playerName.isEmpty()) {
sync(sender, message = "Fetching player data...")
} else {
syncOther(sender, playerName = args.playerName)
}
}

private suspend fun syncOther(sender: Player, playerName: String) {
check (sender.hasPermission("pcbridge.sync.other")) {
"You do not have permission"
}
val player = server.onlinePlayers.firstOrNull { it.name.lowercase() == playerName.lowercase() }
if (player == null) {
sender.sendMessage(
MiniMessage.miniMessage().deserialize("<color:red>Player not found: ${playerName}</color>")
)
return
}
sync(player, message = "Fetching player data for ${player.name}...")

sender.sendMessage(
MiniMessage.miniMessage().deserialize("<color:green>Player data synced</color>")
)
}

private suspend fun sync(player: Player, message: String) {
player.sendMessage(
MiniMessage.miniMessage().deserialize("<color:gray>$message</color>")
)
eventBroadcaster.broadcast(
PlayerSyncRequestedEvent(playerUUID = player.uniqueId),
)
}

data class Args(
val finishSyncing: Boolean,
val playerName: String,
) {
class Parser : CommandArgsParser<Args> {
override fun parse(args: List<String>): Args {
if (args.isEmpty()) {
return Args(finishSyncing = false)
return Args(playerName = "")
}
if (args[0] != "finish") {
if (args.size > 1) {
throw BadCommandUsageException()
}
return Args(finishSyncing = true)
return Args(playerName = args[0])
}
}
}
Expand Down

This file was deleted.

0 comments on commit c2ed5b9

Please sign in to comment.