From 1741f3b2e57999f1be200ac36a718b93e11ab965 Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 29 Jun 2024 16:07:35 +0200 Subject: [PATCH] feat: generate block sound files --- build.gradle.kts | 1 + .../blocky/BlockyBrigadierCommands.kt | 2 +- .../com/mineinabyss/blocky/BlockyPlugin.kt | 2 +- .../ResourcepackGeneration.kt | 73 ++++++++++++++++++- .../blocky/listeners/BlockySoundListener.kt | 12 +-- 5 files changed, 78 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 866218b..84db417 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,5 @@ import net.minecrell.pluginyml.paper.PaperPluginDescription +import kotlin.io.path.Path plugins { alias(idofrontLibs.plugins.mia.kotlin.jvm) diff --git a/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt b/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt index 87bd1da..fa1eccb 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt @@ -35,7 +35,7 @@ object BlockyBrigadierCommands { blockyPrefabs.filter { it !in inheritedPrefabs }.sortedBy { it.prefabs.size } .forEach { prefabs.loader.reload(it) } } - ResourcepackGeneration().generateDefaultAssets() + ResourcepackGeneration.generateDefaultAssets() sender.success("Blocky has been reloaded!") } } diff --git a/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt b/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt index 5dafc3a..290618c 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt @@ -90,7 +90,7 @@ class BlockyPlugin : JavaPlugin() { if (!disableCustomSounds) listeners(BlockySoundListener()) } - ResourcepackGeneration().generateDefaultAssets() + ResourcepackGeneration.generateDefaultAssets() } diff --git a/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt b/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt index 251b6e5..7d799bb 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt @@ -16,20 +16,90 @@ import org.bukkit.block.data.BlockData import org.bukkit.block.data.type.NoteBlock import org.bukkit.block.data.type.Tripwire import team.unnamed.creative.ResourcePack +import team.unnamed.creative.base.Writable import team.unnamed.creative.blockstate.BlockState import team.unnamed.creative.blockstate.MultiVariant import team.unnamed.creative.blockstate.Variant import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter +import team.unnamed.creative.sound.Sound +import team.unnamed.creative.sound.SoundEntry +import team.unnamed.creative.sound.SoundEvent +import team.unnamed.creative.sound.SoundRegistry -class ResourcepackGeneration { +object ResourcepackGeneration { private val resourcePack = ResourcePack.resourcePack() fun generateDefaultAssets() { resourcePack.blockState(blockState(SetBlock.BlockType.NOTEBLOCK)) resourcePack.blockState(blockState(SetBlock.BlockType.WIRE)) + + registerRequiredSounds() + MinecraftResourcePackWriter.minecraft().writeToDirectory(blocky.plugin.dataFolder.resolve("pack"), resourcePack) } + private fun registerRequiredSounds() { + if (blocky.config.disableCustomSounds) return + + val soundRegistry = resourcePack.soundRegistry("minecraft") ?: SoundRegistry.soundRegistry("minecraft", emptyList()) + + SoundRegistry.soundRegistry(soundRegistry.namespace(), soundRegistry.sounds().plus(listOf( + SoundEvent.soundEvent(Key.key("minecraft:block.stone.place"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.stone.break"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.stone.hit"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.stone.fall"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.stone.step"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.wood.place"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.wood.break"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.wood.hit"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.wood.fall"), true, null, listOf()), + SoundEvent.soundEvent(Key.key("minecraft:block.wood.step"), true, null, listOf()) + ))).let(resourcePack::soundRegistry) + + val blockyRegistry = resourcePack.soundRegistry("blocky") ?: SoundRegistry.soundRegistry("blocky", emptyList()) + SoundRegistry.soundRegistry(blockyRegistry.namespace(), blockyRegistry.sounds().plus(listOf( + SoundEvent.soundEvent(Key.key("blocky:block.stone.place"), false, "subtitles.block.generic.place", stoneDig), + SoundEvent.soundEvent(Key.key("blocky:block.stone.break"), false, "subtitles.block.generic.break", stoneDig), + SoundEvent.soundEvent(Key.key("blocky:block.stone.hit"), false, "subtitles.block.generic.hit", stoneStep), + SoundEvent.soundEvent(Key.key("blocky:block.stone.fall"), false, "subtitles.block.generic.fall", stoneStep), + SoundEvent.soundEvent(Key.key("blocky:block.stone.step"), false, "subtitles.block.generic.step", stoneStep), + SoundEvent.soundEvent(Key.key("blocky:block.wood.place"), false, "subtitles.block.generic.place", woodDig), + SoundEvent.soundEvent(Key.key("blocky:block.wood.break"), false, "subtitles.block.generic.break", woodDig), + SoundEvent.soundEvent(Key.key("blocky:block.wood.hit"), false, "subtitles.block.generic.hit", woodStep), + SoundEvent.soundEvent(Key.key("blocky:block.wood.fall"), false, "subtitles.block.generic.fall", woodStep), + SoundEvent.soundEvent(Key.key("blocky:block.wood.step"), false, "subtitles.block.generic.step", woodStep) + ))).let(resourcePack::soundRegistry) + } + + private val stoneDig = listOf( + SoundEntry.soundEntry().key(Key.key("dig/stone1")).build(), + SoundEntry.soundEntry().key(Key.key("dig/stone2")).build(), + SoundEntry.soundEntry().key(Key.key("dig/stone3")).build(), + SoundEntry.soundEntry().key(Key.key("dig/stone4")).build() + ) + private val stoneStep = listOf( + SoundEntry.soundEntry().key(Key.key("step/stone1")).build(), + SoundEntry.soundEntry().key(Key.key("step/stone2")).build(), + SoundEntry.soundEntry().key(Key.key("step/stone3")).build(), + SoundEntry.soundEntry().key(Key.key("step/stone4")).build(), + SoundEntry.soundEntry().key(Key.key("step/stone5")).build(), + SoundEntry.soundEntry().key(Key.key("step/stone6")).build(), + ) + private val woodDig = listOf( + SoundEntry.soundEntry().key(Key.key("dig/wood1")).build(), + SoundEntry.soundEntry().key(Key.key("dig/wood2")).build(), + SoundEntry.soundEntry().key(Key.key("dig/wood3")).build(), + SoundEntry.soundEntry().key(Key.key("dig/wood4")).build() + ) + private val woodStep = listOf( + SoundEntry.soundEntry().key(Key.key("step/wood1")).build(), + SoundEntry.soundEntry().key(Key.key("step/wood2")).build(), + SoundEntry.soundEntry().key(Key.key("step/wood3")).build(), + SoundEntry.soundEntry().key(Key.key("step/wood4")).build(), + SoundEntry.soundEntry().key(Key.key("step/wood5")).build(), + SoundEntry.soundEntry().key(Key.key("step/wood6")).build(), + ) + private fun blockState(blockType: SetBlock.BlockType): BlockState { val multiVariant = gearyBlocks.block2Prefab.blockMap[blockType]?.mapIndexed { index, blockData -> val query = blockPrefabs.firstOrNull { it.block.blockId == index } ?: return@mapIndexed null @@ -128,5 +198,4 @@ class ResourcepackGeneration { return "north=${hasFace(BlockFace.NORTH)},south=${hasFace(BlockFace.SOUTH)},west=${hasFace(BlockFace.WEST)},east=${hasFace(BlockFace.EAST)},attached=$isAttached,disarmed=$isDisarmed,powered=$isPowered" } - } diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockySoundListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockySoundListener.kt index 49b7916..dc611e4 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockySoundListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockySoundListener.kt @@ -6,7 +6,6 @@ import com.mineinabyss.blocky.components.features.BlockySound import com.mineinabyss.blocky.helpers.* import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.entities.toGeary -import com.mineinabyss.idofront.messaging.broadcast import io.papermc.paper.event.block.BlockBreakProgressUpdateEvent import org.bukkit.* import org.bukkit.entity.LivingEntity @@ -23,11 +22,9 @@ class BlockySoundListener : Listener { @EventHandler fun BlockPlaceEvent.onPlace() { - if (player.gameMode == GameMode.CREATIVE) return val soundGroup = block.blockSoundGroup.placeSound if (soundGroup != Sound.BLOCK_WOOD_PLACE && soundGroup != Sound.BLOCK_STONE_PLACE) return - val sound = block.toGearyOrNull()?.get()?.placeSound ?: ("blocky:" + soundGroup.key.asMinimalString()) - broadcast(sound) + val sound = block.toGearyOrNull()?.get()?.placeSound ?: ("blocky:${soundGroup.key.key}") block.world.playSound(block.location, sound, SoundCategory.BLOCKS, DEFAULT_HIT_VOLUME, DEFAULT_HIT_PITCH) } @@ -36,16 +33,15 @@ class BlockySoundListener : Listener { if ((entity as? Player)?.gameMode == GameMode.CREATIVE) return val soundGroup = block.blockSoundGroup.hitSound if (soundGroup != Sound.BLOCK_WOOD_HIT && soundGroup != Sound.BLOCK_STONE_HIT) return - val sound = block.toGearyOrNull()?.get()?.hitSound ?: ("blocky:" + soundGroup.key.asMinimalString()) + val sound = block.toGearyOrNull()?.get()?.hitSound ?: ("blocky:${soundGroup.key.key}") block.world.playSound(block.location, sound, SoundCategory.BLOCKS, DEFAULT_HIT_VOLUME, DEFAULT_HIT_PITCH) } @EventHandler fun BlockBreakEvent.onBreak() { - if (player.gameMode == GameMode.CREATIVE) return val soundGroup = block.blockSoundGroup.breakSound if (soundGroup != Sound.BLOCK_WOOD_BREAK && soundGroup != Sound.BLOCK_STONE_BREAK) return - val sound = block.toGearyOrNull()?.get()?.breakSound ?: ("blocky:" + soundGroup.key.asMinimalString()) + val sound = block.toGearyOrNull()?.get()?.breakSound ?: ("blocky:${soundGroup.key.key}") block.world.playSound(block.location, sound, SoundCategory.BLOCKS, DEFAULT_HIT_VOLUME, DEFAULT_HIT_PITCH) } @@ -68,7 +64,7 @@ class BlockySoundListener : Listener { GameEvent.STEP -> blockySound?.stepSound GameEvent.HIT_GROUND -> blockySound?.fallSound else -> return - } ?: ("blocky:" + soundGroup.key.asMinimalString()) + } ?: ("blocky:${soundGroup.key.key}") val (volume, pitch) = when (event) { GameEvent.STEP -> DEFAULT_STEP_VOLUME to DEFAULT_STEP_PITCH