Skip to content

Commit

Permalink
feat: generate block sound files
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jun 29, 2024
1 parent 51eda3e commit 1741f3b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net.minecrell.pluginyml.paper.PaperPluginDescription
import kotlin.io.path.Path

plugins {
alias(idofrontLibs.plugins.mia.kotlin.jvm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BlockyPlugin : JavaPlugin() {
if (!disableCustomSounds) listeners(BlockySoundListener())
}

ResourcepackGeneration().generateDefaultAssets()
ResourcepackGeneration.generateDefaultAssets()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<BlockySound>()?.placeSound ?: ("blocky:" + soundGroup.key.asMinimalString())
broadcast(sound)
val sound = block.toGearyOrNull()?.get<BlockySound>()?.placeSound ?: ("blocky:${soundGroup.key.key}")
block.world.playSound(block.location, sound, SoundCategory.BLOCKS, DEFAULT_HIT_VOLUME, DEFAULT_HIT_PITCH)
}

Expand All @@ -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<BlockySound>()?.hitSound ?: ("blocky:" + soundGroup.key.asMinimalString())
val sound = block.toGearyOrNull()?.get<BlockySound>()?.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<BlockySound>()?.breakSound ?: ("blocky:" + soundGroup.key.asMinimalString())
val sound = block.toGearyOrNull()?.get<BlockySound>()?.breakSound ?: ("blocky:${soundGroup.key.key}")
block.world.playSound(block.location, sound, SoundCategory.BLOCKS, DEFAULT_HIT_VOLUME, DEFAULT_HIT_PITCH)
}

Expand All @@ -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
Expand Down

0 comments on commit 1741f3b

Please sign in to comment.