Skip to content

Commit

Permalink
feat: sounds provider
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Dec 10, 2024
1 parent f6854a7 commit 35de577
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.mcbrawls.packin.resource.provider
package net.mcbrawls.packin.resource.pack

import net.mcbrawls.packin.resource.PackResource
import net.minecraft.util.Identifier

/**
* Collects resources for a pack.
*/
fun interface ResourceCollector {
/**
* Collect an existing resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.mcbrawls.packin.PackinMod.addProperty
import net.mcbrawls.packin.listener.PackinResourceLoader
import net.mcbrawls.packin.resource.PackResource
import net.mcbrawls.packin.resource.pack.PackinResourcePack
import net.mcbrawls.packin.resource.pack.ResourceCollector
import net.minecraft.util.Identifier

class FontProvider(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.mcbrawls.packin.resource.provider

import net.mcbrawls.packin.resource.pack.PackinResourcePack
import net.mcbrawls.packin.resource.pack.ResourceCollector

interface ResourceProvider {
/**
* Collects all resources to be added to the pack.
*/
fun collectResources(pack: PackinResourcePack, collector: ResourceCollector)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package net.mcbrawls.packin.resource.provider

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import net.mcbrawls.packin.listener.PackinResourceLoader
import net.mcbrawls.packin.resource.pack.PackinResourcePack
import net.mcbrawls.packin.resource.pack.ResourceCollector
import net.minecraft.util.Identifier

class SoundProvider(
/**
* The sound ids to register.
*/
vararg val sounds: Identifier,

/**
* A sound to replace sounds that are not found in the source files.
*/
val unimplementedSound: Identifier? = null,
) : ResourceProvider {
override fun collectResources(pack: PackinResourcePack, collector: ResourceCollector) {
var hasUnimplemented = false

sounds.groupBy(Identifier::getNamespace)
.forEach { (namespace, ids) ->
// register all sound ids
val invalidSoundPaths = ids.filterNot { id ->
val soundPath = createSoundPath(id)
val soundResource = PackinResourceLoader[soundPath]
if (soundResource != null) {
collector.collect(soundResource)
true
} else {
hasUnimplemented = true
false
}
}

// register sounds json for namespace
val soundsJson = JsonObject().apply {
ids.forEach { id ->
val usedId = if (invalidSoundPaths.contains(id)) {
unimplementedSound
} else {
id
}

if (usedId != null) {
add(
id.path,
JsonObject().apply {
add(
"sounds",
JsonArray().apply {
add(usedId.toString())
}
)
}
)
}
}
}

collector.collect(Identifier.of(namespace, "sounds.json"), soundsJson.toString().encodeToByteArray())
}

// collect unimplemented sound if applicable
if (hasUnimplemented) {
unimplementedSound?.let { id ->
val path = createSoundPath(id)
val resource = PackinResourceLoader[path]
resource?.also(collector::collect)
}
}
}

/**
* Creates the file path of the given sound id.
*/
fun createSoundPath(id: Identifier): Identifier {
return id.withPath { "sounds/$it.ogg" }
}
}
25 changes: 18 additions & 7 deletions src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import net.mcbrawls.packin.font.FontMetrics.Companion.minecraftWidth
import net.mcbrawls.packin.resource.pack.PackMetadata
import net.mcbrawls.packin.resource.pack.PackinResourcePack
import net.mcbrawls.packin.resource.provider.FontProvider
import net.mcbrawls.packin.resource.provider.SoundProvider
import net.minecraft.command.argument.IdentifierArgumentType
import net.minecraft.server.command.CommandManager
import net.minecraft.text.Text
Expand All @@ -36,13 +37,23 @@ object PackinTest : ModInitializer {
dispatcher.register(
CommandManager.literal("out")
.executes { context ->
val packBytes = PackinResourcePack.create(PackMetadata("Test", Text.literal("Test"))) {
addProvider(FontProvider(Identifier.of("brawls", "pinch"), 7.0f, 4.0f))
addProvider(FontProvider(Identifier.of("brawls", "love_bug"), 9.0f, 8.0f))
addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f))
}.createZip()
val path = Path("out.zip")
path.writeBytes(packBytes)
runCatching {
val packBytes = PackinResourcePack.create(PackMetadata("Test", Text.literal("Test"))) {
addProvider(FontProvider(Identifier.of("brawls", "pinch"), 7.0f, 4.0f))
addProvider(FontProvider(Identifier.of("brawls", "love_bug"), 9.0f, 8.0f))
addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f))

addProvider(
SoundProvider(
Identifier.of("brawls", "global/action_success"),
Identifier.of("brawls", "global/doesnt_exist"),
unimplementedSound = Identifier.of("brawls", "unimplemented")
)
)
}.createZip()
val path = Path("out.zip")
path.writeBytes(packBytes)
}.exceptionOrNull()?.printStackTrace()
1
}
)
Expand Down

0 comments on commit 35de577

Please sign in to comment.