Skip to content

Commit

Permalink
Implement blueprint loading from data
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 15, 2023
1 parent 75f978f commit 2b5445e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package net.mcbrawls.blueprint
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.fabricmc.fabric.api.resource.ResourceManagerHelper
import net.mcbrawls.blueprint.command.BlueprintCommand
import net.mcbrawls.blueprint.network.BlueprintConfigC2SPacket
import net.mcbrawls.blueprint.player.BlueprintPlayerData.Companion.blueprintData
import net.mcbrawls.blueprint.resource.BlueprintManager
import net.minecraft.resource.ResourceType
import org.slf4j.LoggerFactory

object BlueprintMod : ModInitializer {
Expand All @@ -28,6 +31,9 @@ object BlueprintMod : ModInitializer {
CommandRegistrationCallback.EVENT.register { dispatcher, _, environment ->
BlueprintCommand.register(dispatcher, environment)
}

// register resource listener
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(BlueprintManager)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ object BlueprintCommand {

//

if (environment.dedicated) {
// blueprint environment
}

dispatcher.register(builder)
}

Expand Down
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
}
}
12 changes: 8 additions & 4 deletions src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ data class Blueprint(
Vec3d.CODEC
.fieldOf("size")
.forGetter(Blueprint::size),
Codec.unboundedMap(
Codec.STRING,
SerializableRegion.CODEC
).fieldOf("regions").forGetter(Blueprint::regions)
Codec.unboundedMap(Codec.STRING, SerializableRegion.CODEC)
.fieldOf("regions")
.forGetter(Blueprint::regions)
).apply(instance, ::Blueprint)
}

/**
* An entirely empty blueprint.
*/
val EMPTY = Blueprint(emptyList(), emptyList(), Vec3d.ZERO, emptyMap())
}
}
10 changes: 6 additions & 4 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"environment": "client"
}
],
"loom:injected_interfaces": {
"net/minecraft/class_3222": [
"net/mcbrawls/blueprint/player/BlueprintPlayerAccessor"
]
"custom": {
"loom:injected_interfaces": {
"net/minecraft/class_3222": [
"net/mcbrawls/blueprint/player/BlueprintPlayerAccessor"
]
}
},
"depends": {
"minecraft": "~1.20.2",
Expand Down
Empty file.

0 comments on commit 2b5445e

Please sign in to comment.