-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
...in/resource/provider/ResourceCollector.kt → ...packin/resource/pack/ResourceCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/net/mcbrawls/packin/resource/provider/ResourceProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/kotlin/net/mcbrawls/packin/resource/provider/SoundProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters