From 2bf161bf15a375e25c30c68fe8aab844999856c7 Mon Sep 17 00:00:00 2001 From: Caadiq Date: Tue, 30 Dec 2025 19:40:36 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20SoundUtils=20=EC=9C=A0=ED=8B=B8?= =?UTF-8?q?=EB=A6=AC=ED=8B=B0=EB=A1=9C=20=EC=82=AC=EC=9A=B4=EB=93=9C=20?= =?UTF-8?q?=EC=9E=AC=EC=83=9D=20=EB=A1=9C=EC=A7=81=20=EC=A4=91=EC=95=99?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 새 파일: SoundUtils.kt - 모든 사운드 재생 함수 통합 - 새 파일: LocationUtils.kt - 플레이어 위치 변환 유틸리티 - 10개 파일에서 중복 사운드 재생 코드 제거 (약 150줄) - GUI: EssentialsMenuGui, CoordinateGui, TeleportGui, AntimobGui - Command: SpawnCommand, PlayerCommand, CoordinateCommand, TeleportCommand - Event/Util: PlayerEvents, MessageUtils --- .../essentials/command/CoordinateCommand.kt | 14 +----- .../essentials/command/PlayerCommand.kt | 16 +------ .../beemer/essentials/command/SpawnCommand.kt | 19 +------- .../essentials/command/TeleportCommand.kt | 16 +------ .../beemer/essentials/event/PlayerEvents.kt | 19 +------- .../com/beemer/essentials/gui/AntimobGui.kt | 9 +--- .../beemer/essentials/gui/CoordinateGui.kt | 17 ++----- .../essentials/gui/EssentialsMenuGui.kt | 17 ++----- .../com/beemer/essentials/gui/TeleportGui.kt | 11 +---- .../beemer/essentials/util/LocationUtils.kt | 23 ++++++++++ .../beemer/essentials/util/MessageUtils.kt | 29 +----------- .../com/beemer/essentials/util/SoundUtils.kt | 44 +++++++++++++++++++ 12 files changed, 89 insertions(+), 145 deletions(-) create mode 100644 Essentials/src/main/kotlin/com/beemer/essentials/util/LocationUtils.kt create mode 100644 Essentials/src/main/kotlin/com/beemer/essentials/util/SoundUtils.kt diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/command/CoordinateCommand.kt b/Essentials/src/main/kotlin/com/beemer/essentials/command/CoordinateCommand.kt index b8d1168..99b3e26 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/command/CoordinateCommand.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/command/CoordinateCommand.kt @@ -8,6 +8,7 @@ import com.beemer.essentials.gui.Menu import com.beemer.essentials.gui.createPageContainer import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import com.mojang.brigadier.arguments.StringArgumentType import net.minecraft.commands.Commands import net.minecraft.network.chat.Component @@ -219,18 +220,7 @@ object CoordinateCommand { player.teleportTo(level, coord.x, coord.y, coord.z, player.yRot, player.xRot) // 텔레포트 사운드 재생 - val teleportSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.teleport" - ) - ) - player.playNotifySound( - teleportSound, - net.minecraft.sounds.SoundSource.MASTER, - 0.2f, - 1.0f - ) + SoundUtils.playTeleport(player) MessageUtils.sendSuccess(player, "{$name}(으)로 이동했습니다.") return 1 } diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/command/PlayerCommand.kt b/Essentials/src/main/kotlin/com/beemer/essentials/command/PlayerCommand.kt index 960cdc3..8355e02 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/command/PlayerCommand.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/command/PlayerCommand.kt @@ -4,6 +4,7 @@ import com.beemer.essentials.config.PlayerConfig import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import net.minecraft.commands.Commands import net.neoforged.bus.api.SubscribeEvent import net.neoforged.neoforge.event.RegisterCommandsEvent @@ -51,20 +52,7 @@ object PlayerCommand { player.xRot ) // 텔레포트 사운드 재생 - val teleportSound = - net.minecraft.sounds.SoundEvent - .createVariableRangeEvent( - net.minecraft.resources - .ResourceLocation.parse( - "minecraft:custom.teleport" - ) - ) - player.playNotifySound( - teleportSound, - net.minecraft.sounds.SoundSource.MASTER, - 0.2f, - 1.0f - ) + SoundUtils.playTeleport(player) MessageUtils.sendSuccess(player, "이전 위치로 이동했습니다.") 1 } diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/command/SpawnCommand.kt b/Essentials/src/main/kotlin/com/beemer/essentials/command/SpawnCommand.kt index 81d3a2e..2ce022c 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/command/SpawnCommand.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/command/SpawnCommand.kt @@ -6,6 +6,7 @@ import com.beemer.essentials.data.Location import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import net.minecraft.commands.Commands import net.neoforged.bus.api.SubscribeEvent import net.neoforged.neoforge.event.RegisterCommandsEvent @@ -106,23 +107,7 @@ object SpawnCommand { player.xRot ) // 텔레포트 사운드 재생 - val teleportSound = - net.minecraft.sounds.SoundEvent - .createVariableRangeEvent( - net.minecraft - .resources - .ResourceLocation - .parse( - "minecraft:custom.teleport" - ) - ) - player.playNotifySound( - teleportSound, - net.minecraft.sounds.SoundSource - .MASTER, - 0.2f, - 1.0f - ) + SoundUtils.playTeleport(player) MessageUtils.sendSuccess( player, "스폰으로 이동했습니다." diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/command/TeleportCommand.kt b/Essentials/src/main/kotlin/com/beemer/essentials/command/TeleportCommand.kt index f417b3d..c120ea4 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/command/TeleportCommand.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/command/TeleportCommand.kt @@ -5,6 +5,7 @@ import com.beemer.essentials.gui.TeleportGui.Companion.CONTAINER_SIZE import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import com.beemer.essentials.util.TranslationUtils import com.mojang.authlib.GameProfile import net.minecraft.ChatFormatting @@ -57,20 +58,7 @@ object TeleportCommand { } // 플레이어가 있을 때만 클릭 사운드 재생 - val clickSound = - net.minecraft.sounds.SoundEvent - .createVariableRangeEvent( - net.minecraft.resources - .ResourceLocation.parse( - "minecraft:custom.click" - ) - ) - player.playNotifySound( - clickSound, - net.minecraft.sounds.SoundSource.MASTER, - 0.2f, - 1.0f - ) + SoundUtils.playClick(player) player.openMenu( SimpleMenuProvider( diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/event/PlayerEvents.kt b/Essentials/src/main/kotlin/com/beemer/essentials/event/PlayerEvents.kt index 5b55421..a38afd9 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/event/PlayerEvents.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/event/PlayerEvents.kt @@ -5,6 +5,7 @@ import com.beemer.essentials.config.SpawnConfig import com.beemer.essentials.data.Location import com.beemer.essentials.util.ChatUtils import com.beemer.essentials.util.DimensionUtils +import com.beemer.essentials.util.SoundUtils import net.minecraft.ChatFormatting import net.minecraft.network.chat.Component import net.minecraft.server.level.ServerPlayer @@ -42,27 +43,11 @@ object PlayerEvents { player.server.execute { sendWelcomeGuide(player) // 알림 사운드 재생 - playNotificationSound(player) + SoundUtils.playNotification(player) } } } - /** 알림 사운드 재생 */ - private fun playNotificationSound(player: ServerPlayer) { - val notificationSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.notification" - ) - ) - player.playNotifySound( - notificationSound, - net.minecraft.sounds.SoundSource.MASTER, - 0.5f, - 1.0f - ) - } - /** 신규 플레이어 도움말 */ private fun sendWelcomeGuide(player: ServerPlayer) { val separator = diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/gui/AntimobGui.kt b/Essentials/src/main/kotlin/com/beemer/essentials/gui/AntimobGui.kt index 1219a29..21f431a 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/gui/AntimobGui.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/gui/AntimobGui.kt @@ -1,11 +1,11 @@ package com.beemer.essentials.gui import com.beemer.essentials.config.AntimobConfig +import com.beemer.essentials.util.SoundUtils import net.minecraft.ChatFormatting import net.minecraft.core.component.DataComponents import net.minecraft.network.chat.Component import net.minecraft.server.level.ServerPlayer -import net.minecraft.sounds.SoundSource import net.minecraft.world.Container import net.minecraft.world.entity.player.Inventory import net.minecraft.world.inventory.AbstractContainerMenu @@ -97,12 +97,7 @@ class AntimobGui( ): ItemStack = ItemStack.EMPTY private fun playClickSound(player: ServerPlayer) { - // 커스텀 클릭 사운드 (리소스팩: custom.click) - val customClick = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse("minecraft:custom.click") - ) - player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playClick(player) } private fun makeMobHead(mob: String): ItemStack { diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/gui/CoordinateGui.kt b/Essentials/src/main/kotlin/com/beemer/essentials/gui/CoordinateGui.kt index 129d856..ac9a28d 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/gui/CoordinateGui.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/gui/CoordinateGui.kt @@ -5,6 +5,7 @@ import com.beemer.essentials.config.PlayerConfig import com.beemer.essentials.data.Location import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import com.beemer.essentials.util.TranslationUtils.translateBiome import com.beemer.essentials.util.TranslationUtils.translateDimension import java.util.UUID @@ -16,7 +17,6 @@ import net.minecraft.network.chat.Component import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceLocation import net.minecraft.server.level.ServerPlayer -import net.minecraft.sounds.SoundSource import net.minecraft.world.Container import net.minecraft.world.SimpleContainer import net.minecraft.world.SimpleMenuProvider @@ -140,13 +140,7 @@ class Menu( player.teleportTo(level, x, y, z, player.yRot, player.xRot) // 텔레포트 사운드 재생 - val teleportSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.teleport" - ) - ) - player.playNotifySound(teleportSound, SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playTeleport(player) MessageUtils.sendSuccess(player, "{$coordName}(으)로 이동했습니다.") } player.closeContainer() @@ -154,12 +148,7 @@ class Menu( } private fun playClickSound(player: ServerPlayer) { - // 커스텀 클릭 사운드 (리소스팩: custom.click) - val customClick = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse("minecraft:custom.click") - ) - player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playClick(player) } private fun getTotalPages(): Int { diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/gui/EssentialsMenuGui.kt b/Essentials/src/main/kotlin/com/beemer/essentials/gui/EssentialsMenuGui.kt index 544bf66..b75e871 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/gui/EssentialsMenuGui.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/gui/EssentialsMenuGui.kt @@ -1,11 +1,11 @@ package com.beemer.essentials.gui +import com.beemer.essentials.util.SoundUtils import net.minecraft.ChatFormatting import net.minecraft.core.component.DataComponents import net.minecraft.nbt.CompoundTag import net.minecraft.network.chat.Component import net.minecraft.server.level.ServerPlayer -import net.minecraft.sounds.SoundSource import net.minecraft.world.Container import net.minecraft.world.SimpleContainer import net.minecraft.world.SimpleMenuProvider @@ -148,14 +148,7 @@ class EssentialsMenuGui( } private fun playClickSound(player: ServerPlayer) { - // 커스텀 클릭 사운드 (리소스팩: custom.click) - val customClick = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.click" - ) - ) - player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playClick(player) } private fun openWorkbench(player: ServerPlayer) { @@ -259,11 +252,7 @@ fun openEssentialsMenu(player: ServerPlayer) { val container = createEssentialsMenuContainer() // 메뉴 오픈 사운드 재생 - val menuSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse("minecraft:custom.menu") - ) - player.playNotifySound(menuSound, net.minecraft.sounds.SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playMenu(player) // 커스텀 GUI 이미지를 위한 유니코드 문자 // \uF808 = -8 (왼쪽으로 8px 이동) diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/gui/TeleportGui.kt b/Essentials/src/main/kotlin/com/beemer/essentials/gui/TeleportGui.kt index 00517f0..fe2a142 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/gui/TeleportGui.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/gui/TeleportGui.kt @@ -4,8 +4,8 @@ import com.beemer.essentials.config.PlayerConfig import com.beemer.essentials.data.Location import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.util.MessageUtils +import com.beemer.essentials.util.SoundUtils import net.minecraft.server.level.ServerPlayer -import net.minecraft.sounds.SoundSource import net.minecraft.world.Container import net.minecraft.world.entity.player.Inventory import net.minecraft.world.inventory.AbstractContainerMenu @@ -142,14 +142,7 @@ class TeleportGui( } private fun playClickSound(player: ServerPlayer) { - // 커스텀 텔레포트 사운드 (리소스팩: custom.teleport) - val teleportSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.teleport" - ) - ) - player.playNotifySound(teleportSound, SoundSource.MASTER, 0.2f, 1.0f) + SoundUtils.playTeleport(player) } override fun stillValid(player: net.minecraft.world.entity.player.Player): Boolean = true diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/util/LocationUtils.kt b/Essentials/src/main/kotlin/com/beemer/essentials/util/LocationUtils.kt new file mode 100644 index 0000000..93072d9 --- /dev/null +++ b/Essentials/src/main/kotlin/com/beemer/essentials/util/LocationUtils.kt @@ -0,0 +1,23 @@ +package com.beemer.essentials.util + +import com.beemer.essentials.data.Location +import net.minecraft.server.level.ServerPlayer + +/** 위치 관련 유틸리티 플레이어 위치를 Location 객체로 변환하는 공통 로직 */ +object LocationUtils { + /** 플레이어의 현재 위치를 Location 객체로 변환 */ + fun fromPlayer(player: ServerPlayer): Location { + val pos = player.position() + val blockPos = player.blockPosition() + val level = player.level() + + val dimension = level.dimension().location().toString() + val biome = + level.getBiome(blockPos) + .unwrapKey() + .map { it.location().toString() } + .orElse("minecraft:plains") + + return Location(dimension = dimension, biome = biome, x = pos.x, y = pos.y, z = pos.z) + } +} diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/util/MessageUtils.kt b/Essentials/src/main/kotlin/com/beemer/essentials/util/MessageUtils.kt index e169a6f..df1c5be 100644 --- a/Essentials/src/main/kotlin/com/beemer/essentials/util/MessageUtils.kt +++ b/Essentials/src/main/kotlin/com/beemer/essentials/util/MessageUtils.kt @@ -55,7 +55,7 @@ object MessageUtils { fun sendError(player: ServerPlayer, text: String) { player.sendSystemMessage(error(text)) - playErrorSound(player) + SoundUtils.playError(player) } fun sendWarning(player: ServerPlayer, text: String) { @@ -65,32 +65,7 @@ object MessageUtils { /** 알림 메시지 (사운드 포함) - 서버 접속 시 등 */ fun sendNotification(player: ServerPlayer, text: String) { player.sendSystemMessage(info(text)) - playNotificationSound(player) - } - - // === 사운드 재생 === - - private fun playErrorSound(player: ServerPlayer) { - val errorSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse("minecraft:custom.error") - ) - player.playNotifySound(errorSound, net.minecraft.sounds.SoundSource.MASTER, 0.2f, 1.0f) - } - - private fun playNotificationSound(player: ServerPlayer) { - val notificationSound = - net.minecraft.sounds.SoundEvent.createVariableRangeEvent( - net.minecraft.resources.ResourceLocation.parse( - "minecraft:custom.notification" - ) - ) - player.playNotifySound( - notificationSound, - net.minecraft.sounds.SoundSource.MASTER, - 0.5f, - 1.0f - ) + SoundUtils.playNotification(player) } /** 스타일 텍스트 파싱 {중괄호} 안의 텍스트는 강조 색상으로 표시 */ diff --git a/Essentials/src/main/kotlin/com/beemer/essentials/util/SoundUtils.kt b/Essentials/src/main/kotlin/com/beemer/essentials/util/SoundUtils.kt new file mode 100644 index 0000000..832d111 --- /dev/null +++ b/Essentials/src/main/kotlin/com/beemer/essentials/util/SoundUtils.kt @@ -0,0 +1,44 @@ +package com.beemer.essentials.util + +import net.minecraft.resources.ResourceLocation +import net.minecraft.server.level.ServerPlayer +import net.minecraft.sounds.SoundEvent +import net.minecraft.sounds.SoundSource + +/** 사운드 재생 유틸리티 모든 커스텀 사운드 재생 로직을 중앙화 */ +object SoundUtils { + // 기본 볼륨 (20%) + private const val DEFAULT_VOLUME = 0.2f + private const val DEFAULT_PITCH = 1.0f + + /** 클릭 사운드 - GUI 버튼 클릭 시 */ + fun playClick(player: ServerPlayer) { + playSound(player, "minecraft:custom.click") + } + + /** 텔레포트 사운드 - 스폰, 좌표이동, TPA, back 시 */ + fun playTeleport(player: ServerPlayer) { + playSound(player, "minecraft:custom.teleport") + } + + /** 에러 사운드 - 오류 메시지 시 */ + fun playError(player: ServerPlayer) { + playSound(player, "minecraft:custom.error") + } + + /** 알림 사운드 - 서버 접속 시 */ + fun playNotification(player: ServerPlayer) { + playSound(player, "minecraft:custom.notification") + } + + /** 메뉴 오픈 사운드 - 메뉴 GUI 열기 시 */ + fun playMenu(player: ServerPlayer) { + playSound(player, "minecraft:custom.menu") + } + + /** 공통 사운드 재생 */ + private fun playSound(player: ServerPlayer, soundId: String) { + val sound = SoundEvent.createVariableRangeEvent(ResourceLocation.parse(soundId)) + player.playNotifySound(sound, SoundSource.MASTER, DEFAULT_VOLUME, DEFAULT_PITCH) + } +}