-
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.
Implement blueprint loading from data
- Loading branch information
Showing
6 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
src/main/kotlin/net/mcbrawls/blueprint/resource/BlueprintManager.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,84 @@ | ||
package net.mcbrawls.blueprint.resource | ||
|
||
import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener | ||
import net.mcbrawls.blueprint.BlueprintMod | ||
import net.mcbrawls.blueprint.structure.Blueprint | ||
import net.minecraft.nbt.NbtIo | ||
import net.minecraft.nbt.NbtOps | ||
import net.minecraft.resource.Resource | ||
import net.minecraft.resource.ResourceFinder | ||
import net.minecraft.resource.ResourceManager | ||
import net.minecraft.util.Identifier | ||
import net.minecraft.util.profiler.Profiler | ||
import org.slf4j.LoggerFactory | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Executor | ||
|
||
object BlueprintManager : SimpleResourceReloadListener<Map<Identifier, Blueprint>> { | ||
/** | ||
* The identifier of the blueprint resource listener. | ||
*/ | ||
val RESOURCE_ID = Identifier(BlueprintMod.MOD_ID, "blueprints") | ||
|
||
/** | ||
* The resource finder for blueprint nbt files. | ||
*/ | ||
val FINDER = ResourceFinder("blueprints", ".nbt") | ||
|
||
val LOGGER = LoggerFactory.getLogger("Blueprint Manager") | ||
|
||
/** | ||
* The blueprints currently loaded in the game instance. | ||
*/ | ||
private val blueprints: MutableMap<Identifier, Blueprint> = mutableMapOf() | ||
|
||
override fun load( | ||
manager: ResourceManager, | ||
profiler: Profiler, | ||
executor: Executor | ||
): CompletableFuture<Map<Identifier, Blueprint>> { | ||
return CompletableFuture.supplyAsync { | ||
// find all resources | ||
val resources: Map<Identifier, Resource> = FINDER.findResources(manager) | ||
|
||
// load blueprint data and build map | ||
buildMap { | ||
resources.forEach { (location, resource) -> | ||
// read raw blueprint nbt data | ||
val blueprintNbt = try { | ||
resource.inputStream.use(NbtIo::readCompressed) | ||
} catch (exception: Exception) { | ||
LOGGER.error("Could not load blueprint: $location", exception) | ||
return@forEach | ||
} | ||
|
||
// attempt decode blueprint | ||
val blueprintDataResult = Blueprint.CODEC.decode(NbtOps.INSTANCE, blueprintNbt) | ||
|
||
// log error if present | ||
val optionalResult = blueprintDataResult.resultOrPartial(LOGGER::error) | ||
|
||
// put blueprint if present | ||
optionalResult.ifPresent { result -> this[location] = result.first } | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun apply( | ||
data: Map<Identifier, Blueprint>, | ||
manager: ResourceManager, | ||
profiler: Profiler, | ||
executor: Executor | ||
): CompletableFuture<Void> { | ||
return CompletableFuture.runAsync { | ||
// consume resultant data | ||
blueprints.clear() | ||
blueprints.putAll(data) | ||
} | ||
} | ||
|
||
override fun getFabricId(): Identifier { | ||
return RESOURCE_ID | ||
} | ||
} |
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
Empty file.