refactor: SoundUtils 유틸리티로 사운드 재생 로직 중앙화

- 새 파일: SoundUtils.kt - 모든 사운드 재생 함수 통합
- 새 파일: LocationUtils.kt - 플레이어 위치 변환 유틸리티
- 10개 파일에서 중복 사운드 재생 코드 제거 (약 150줄)
- GUI: EssentialsMenuGui, CoordinateGui, TeleportGui, AntimobGui
- Command: SpawnCommand, PlayerCommand, CoordinateCommand, TeleportCommand
- Event/Util: PlayerEvents, MessageUtils
This commit is contained in:
Caadiq 2025-12-30 19:40:36 +09:00
parent fd4540c9e7
commit 2bf161bf15
12 changed files with 89 additions and 145 deletions

View file

@ -8,6 +8,7 @@ import com.beemer.essentials.gui.Menu
import com.beemer.essentials.gui.createPageContainer import com.beemer.essentials.gui.createPageContainer
import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.DimensionUtils
import com.beemer.essentials.util.MessageUtils import com.beemer.essentials.util.MessageUtils
import com.beemer.essentials.util.SoundUtils
import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.arguments.StringArgumentType
import net.minecraft.commands.Commands import net.minecraft.commands.Commands
import net.minecraft.network.chat.Component 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) player.teleportTo(level, coord.x, coord.y, coord.z, player.yRot, player.xRot)
// 텔레포트 사운드 재생 // 텔레포트 사운드 재생
val teleportSound = SoundUtils.playTeleport(player)
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
)
MessageUtils.sendSuccess(player, "{$name}(으)로 이동했습니다.") MessageUtils.sendSuccess(player, "{$name}(으)로 이동했습니다.")
return 1 return 1
} }

View file

@ -4,6 +4,7 @@ import com.beemer.essentials.config.PlayerConfig
import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.CommandUtils
import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.DimensionUtils
import com.beemer.essentials.util.MessageUtils import com.beemer.essentials.util.MessageUtils
import com.beemer.essentials.util.SoundUtils
import net.minecraft.commands.Commands import net.minecraft.commands.Commands
import net.neoforged.bus.api.SubscribeEvent import net.neoforged.bus.api.SubscribeEvent
import net.neoforged.neoforge.event.RegisterCommandsEvent import net.neoforged.neoforge.event.RegisterCommandsEvent
@ -51,20 +52,7 @@ object PlayerCommand {
player.xRot player.xRot
) )
// 텔레포트 사운드 재생 // 텔레포트 사운드 재생
val teleportSound = SoundUtils.playTeleport(player)
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
)
MessageUtils.sendSuccess(player, "이전 위치로 이동했습니다.") MessageUtils.sendSuccess(player, "이전 위치로 이동했습니다.")
1 1
} }

View file

@ -6,6 +6,7 @@ import com.beemer.essentials.data.Location
import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.CommandUtils
import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.DimensionUtils
import com.beemer.essentials.util.MessageUtils import com.beemer.essentials.util.MessageUtils
import com.beemer.essentials.util.SoundUtils
import net.minecraft.commands.Commands import net.minecraft.commands.Commands
import net.neoforged.bus.api.SubscribeEvent import net.neoforged.bus.api.SubscribeEvent
import net.neoforged.neoforge.event.RegisterCommandsEvent import net.neoforged.neoforge.event.RegisterCommandsEvent
@ -106,23 +107,7 @@ object SpawnCommand {
player.xRot player.xRot
) )
// 텔레포트 사운드 재생 // 텔레포트 사운드 재생
val teleportSound = SoundUtils.playTeleport(player)
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
)
MessageUtils.sendSuccess( MessageUtils.sendSuccess(
player, player,
"스폰으로 이동했습니다." "스폰으로 이동했습니다."

View file

@ -5,6 +5,7 @@ import com.beemer.essentials.gui.TeleportGui.Companion.CONTAINER_SIZE
import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.nickname.NicknameDataStore
import com.beemer.essentials.util.CommandUtils import com.beemer.essentials.util.CommandUtils
import com.beemer.essentials.util.MessageUtils import com.beemer.essentials.util.MessageUtils
import com.beemer.essentials.util.SoundUtils
import com.beemer.essentials.util.TranslationUtils import com.beemer.essentials.util.TranslationUtils
import com.mojang.authlib.GameProfile import com.mojang.authlib.GameProfile
import net.minecraft.ChatFormatting import net.minecraft.ChatFormatting
@ -57,20 +58,7 @@ object TeleportCommand {
} }
// 플레이어가 있을 때만 클릭 사운드 재생 // 플레이어가 있을 때만 클릭 사운드 재생
val clickSound = SoundUtils.playClick(player)
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
)
player.openMenu( player.openMenu(
SimpleMenuProvider( SimpleMenuProvider(

View file

@ -5,6 +5,7 @@ import com.beemer.essentials.config.SpawnConfig
import com.beemer.essentials.data.Location import com.beemer.essentials.data.Location
import com.beemer.essentials.util.ChatUtils import com.beemer.essentials.util.ChatUtils
import com.beemer.essentials.util.DimensionUtils import com.beemer.essentials.util.DimensionUtils
import com.beemer.essentials.util.SoundUtils
import net.minecraft.ChatFormatting import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
@ -42,27 +43,11 @@ object PlayerEvents {
player.server.execute { player.server.execute {
sendWelcomeGuide(player) 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) { private fun sendWelcomeGuide(player: ServerPlayer) {
val separator = val separator =

View file

@ -1,11 +1,11 @@
package com.beemer.essentials.gui package com.beemer.essentials.gui
import com.beemer.essentials.config.AntimobConfig import com.beemer.essentials.config.AntimobConfig
import com.beemer.essentials.util.SoundUtils
import net.minecraft.ChatFormatting import net.minecraft.ChatFormatting
import net.minecraft.core.component.DataComponents import net.minecraft.core.component.DataComponents
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundSource
import net.minecraft.world.Container import net.minecraft.world.Container
import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.inventory.AbstractContainerMenu
@ -97,12 +97,7 @@ class AntimobGui(
): ItemStack = ItemStack.EMPTY ): ItemStack = ItemStack.EMPTY
private fun playClickSound(player: ServerPlayer) { private fun playClickSound(player: ServerPlayer) {
// 커스텀 클릭 사운드 (리소스팩: custom.click) SoundUtils.playClick(player)
val customClick =
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(
net.minecraft.resources.ResourceLocation.parse("minecraft:custom.click")
)
player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f)
} }
private fun makeMobHead(mob: String): ItemStack { private fun makeMobHead(mob: String): ItemStack {

View file

@ -5,6 +5,7 @@ import com.beemer.essentials.config.PlayerConfig
import com.beemer.essentials.data.Location import com.beemer.essentials.data.Location
import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.nickname.NicknameDataStore
import com.beemer.essentials.util.MessageUtils 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.translateBiome
import com.beemer.essentials.util.TranslationUtils.translateDimension import com.beemer.essentials.util.TranslationUtils.translateDimension
import java.util.UUID import java.util.UUID
@ -16,7 +17,6 @@ import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundSource
import net.minecraft.world.Container import net.minecraft.world.Container
import net.minecraft.world.SimpleContainer import net.minecraft.world.SimpleContainer
import net.minecraft.world.SimpleMenuProvider import net.minecraft.world.SimpleMenuProvider
@ -140,13 +140,7 @@ class Menu(
player.teleportTo(level, x, y, z, player.yRot, player.xRot) player.teleportTo(level, x, y, z, player.yRot, player.xRot)
// 텔레포트 사운드 재생 // 텔레포트 사운드 재생
val teleportSound = SoundUtils.playTeleport(player)
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(
net.minecraft.resources.ResourceLocation.parse(
"minecraft:custom.teleport"
)
)
player.playNotifySound(teleportSound, SoundSource.MASTER, 0.2f, 1.0f)
MessageUtils.sendSuccess(player, "{$coordName}(으)로 이동했습니다.") MessageUtils.sendSuccess(player, "{$coordName}(으)로 이동했습니다.")
} }
player.closeContainer() player.closeContainer()
@ -154,12 +148,7 @@ class Menu(
} }
private fun playClickSound(player: ServerPlayer) { private fun playClickSound(player: ServerPlayer) {
// 커스텀 클릭 사운드 (리소스팩: custom.click) SoundUtils.playClick(player)
val customClick =
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(
net.minecraft.resources.ResourceLocation.parse("minecraft:custom.click")
)
player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f)
} }
private fun getTotalPages(): Int { private fun getTotalPages(): Int {

View file

@ -1,11 +1,11 @@
package com.beemer.essentials.gui package com.beemer.essentials.gui
import com.beemer.essentials.util.SoundUtils
import net.minecraft.ChatFormatting import net.minecraft.ChatFormatting
import net.minecraft.core.component.DataComponents import net.minecraft.core.component.DataComponents
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundSource
import net.minecraft.world.Container import net.minecraft.world.Container
import net.minecraft.world.SimpleContainer import net.minecraft.world.SimpleContainer
import net.minecraft.world.SimpleMenuProvider import net.minecraft.world.SimpleMenuProvider
@ -148,14 +148,7 @@ class EssentialsMenuGui(
} }
private fun playClickSound(player: ServerPlayer) { private fun playClickSound(player: ServerPlayer) {
// 커스텀 클릭 사운드 (리소스팩: custom.click) SoundUtils.playClick(player)
val customClick =
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(
net.minecraft.resources.ResourceLocation.parse(
"minecraft:custom.click"
)
)
player.playNotifySound(customClick, SoundSource.MASTER, 0.2f, 1.0f)
} }
private fun openWorkbench(player: ServerPlayer) { private fun openWorkbench(player: ServerPlayer) {
@ -259,11 +252,7 @@ fun openEssentialsMenu(player: ServerPlayer) {
val container = createEssentialsMenuContainer() val container = createEssentialsMenuContainer()
// 메뉴 오픈 사운드 재생 // 메뉴 오픈 사운드 재생
val menuSound = SoundUtils.playMenu(player)
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)
// 커스텀 GUI 이미지를 위한 유니코드 문자 // 커스텀 GUI 이미지를 위한 유니코드 문자
// \uF808 = -8 (왼쪽으로 8px 이동) // \uF808 = -8 (왼쪽으로 8px 이동)

View file

@ -4,8 +4,8 @@ import com.beemer.essentials.config.PlayerConfig
import com.beemer.essentials.data.Location import com.beemer.essentials.data.Location
import com.beemer.essentials.nickname.NicknameDataStore import com.beemer.essentials.nickname.NicknameDataStore
import com.beemer.essentials.util.MessageUtils import com.beemer.essentials.util.MessageUtils
import com.beemer.essentials.util.SoundUtils
import net.minecraft.server.level.ServerPlayer import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundSource
import net.minecraft.world.Container import net.minecraft.world.Container
import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.inventory.AbstractContainerMenu import net.minecraft.world.inventory.AbstractContainerMenu
@ -142,14 +142,7 @@ class TeleportGui(
} }
private fun playClickSound(player: ServerPlayer) { private fun playClickSound(player: ServerPlayer) {
// 커스텀 텔레포트 사운드 (리소스팩: custom.teleport) SoundUtils.playTeleport(player)
val teleportSound =
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(
net.minecraft.resources.ResourceLocation.parse(
"minecraft:custom.teleport"
)
)
player.playNotifySound(teleportSound, SoundSource.MASTER, 0.2f, 1.0f)
} }
override fun stillValid(player: net.minecraft.world.entity.player.Player): Boolean = true override fun stillValid(player: net.minecraft.world.entity.player.Player): Boolean = true

View file

@ -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)
}
}

View file

@ -55,7 +55,7 @@ object MessageUtils {
fun sendError(player: ServerPlayer, text: String) { fun sendError(player: ServerPlayer, text: String) {
player.sendSystemMessage(error(text)) player.sendSystemMessage(error(text))
playErrorSound(player) SoundUtils.playError(player)
} }
fun sendWarning(player: ServerPlayer, text: String) { fun sendWarning(player: ServerPlayer, text: String) {
@ -65,32 +65,7 @@ object MessageUtils {
/** 알림 메시지 (사운드 포함) - 서버 접속 시 등 */ /** 알림 메시지 (사운드 포함) - 서버 접속 시 등 */
fun sendNotification(player: ServerPlayer, text: String) { fun sendNotification(player: ServerPlayer, text: String) {
player.sendSystemMessage(info(text)) player.sendSystemMessage(info(text))
playNotificationSound(player) SoundUtils.playNotification(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
)
} }
/** 스타일 텍스트 파싱 {중괄호} 안의 텍스트는 강조 색상으로 표시 */ /** 스타일 텍스트 파싱 {중괄호} 안의 텍스트는 강조 색상으로 표시 */

View file

@ -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)
}
}