minecraft-mod/Essentials/src/main/kotlin/com/beemer/essentials/nickname/NicknameCommand.kt

161 lines
8.8 KiB
Kotlin
Raw Normal View History

package com.beemer.essentials.nickname
import com.mojang.brigadier.arguments.StringArgumentType
import net.minecraft.ChatFormatting
import net.minecraft.commands.Commands
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.neoforged.bus.api.SubscribeEvent
import net.neoforged.neoforge.event.RegisterCommandsEvent
/** 닉네임 명령어 /닉네임 변경 <닉네임> /닉네임 초기화 */
object NicknameCommand {
@SubscribeEvent
fun onRegisterCommands(event: RegisterCommandsEvent) {
// 한글 명령어
event.dispatcher.register(
Commands.literal("닉네임")
.then(
Commands.literal("변경")
.then(
Commands.argument(
"닉네임",
StringArgumentType
.greedyString()
)
.executes { context ->
val player =
context.source
.entity as?
ServerPlayer
?: return@executes 0
val nickname =
StringArgumentType
.getString(
context,
"닉네임"
)
.trim()
executeSet(player, nickname)
}
)
)
.then(
Commands.literal("초기화").executes { context ->
val player =
context.source.entity as? ServerPlayer
?: return@executes 0
executeReset(player)
}
)
)
// 영어 명령어
event.dispatcher.register(
Commands.literal("nickname")
.then(
Commands.literal("set")
.then(
Commands.argument(
"name",
StringArgumentType
.greedyString()
)
.executes { context ->
val player =
context.source
.entity as?
ServerPlayer
?: return@executes 0
val nickname =
StringArgumentType
.getString(
context,
"name"
)
.trim()
executeSet(player, nickname)
}
)
)
.then(
Commands.literal("reset").executes { context ->
val player =
context.source.entity as? ServerPlayer
?: return@executes 0
executeReset(player)
}
)
)
}
private fun executeSet(player: ServerPlayer, nickname: String): Int {
// 유효성 검사: 길이
if (nickname.length < 2 || nickname.length > 16) {
player.sendSystemMessage(
Component.literal("닉네임은 2~16자 사이여야 합니다.").withStyle {
it.withColor(ChatFormatting.RED)
}
)
return 0
}
// 유효성 검사: 중복
if (NicknameDataStore.isNicknameTaken(nickname, player.uuid)) {
player.sendSystemMessage(
Component.literal("이미 사용 중인 닉네임입니다.").withStyle {
it.withColor(ChatFormatting.RED)
}
)
return 0
}
// 닉네임 저장 및 적용 (gameProfile.name = 실제 마인크래프트 이름)
NicknameDataStore.setNickname(player.uuid, player.gameProfile.name, nickname)
NicknameManager.applyNickname(player, nickname)
player.sendSystemMessage(
Component.literal("닉네임이 ")
.withStyle { it.withColor(ChatFormatting.GOLD) }
.append(
Component.literal(nickname).withStyle {
it.withColor(ChatFormatting.AQUA)
}
)
.append(
Component.literal("(으)로 변경되었습니다.").withStyle {
it.withColor(ChatFormatting.GOLD)
}
)
)
return 1
}
private fun executeReset(player: ServerPlayer): Int {
if (!NicknameDataStore.hasNickname(player.uuid)) {
player.sendSystemMessage(
Component.literal("설정된 닉네임이 없습니다.").withStyle {
it.withColor(ChatFormatting.RED)
}
)
return 0
}
NicknameDataStore.removeNickname(player.uuid)
NicknameManager.removeNickname(player)
player.sendSystemMessage(
Component.literal("닉네임이 초기화되었습니다.").withStyle {
it.withColor(ChatFormatting.GOLD)
}
)
return 1
}
}