Skip to content

Commit

Permalink
feat(anchors): change anchor storage to allow multiple anchors of the…
Browse files Browse the repository at this point in the history
… same id
  • Loading branch information
andantet committed Jan 12, 2025
1 parent 7a3057a commit 005374c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_build=8
loader_version=0.16.10

# Mod Properties
mod_version=1.14
mod_version=1.14.1
maven_group=net.mcbrawls
mod_id=blueprint

Expand All @@ -19,7 +19,7 @@ fabric_version=0.114.2+1.21.4
kotlin_version=2.1.0
fabric_kotlin_version=1.13.0

codex_version=1.1
codex_version=1.2
slate_version=1.0
fantasy_version=0.6.5+1.21.2
polymer_version=0.11.3+1.21.4
22 changes: 13 additions & 9 deletions src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mcbrawls.blueprint.structure

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import dev.andante.codex.ExtraCodecs
import net.mcbrawls.blueprint.anchor.Anchor
import net.mcbrawls.blueprint.block.BlueprintBlocks
import net.mcbrawls.blueprint.block.entity.RegionIdBlockEntity
Expand Down Expand Up @@ -59,7 +60,7 @@ data class Blueprint(
/**
* The anchors stored within this blueprint.
*/
val anchors: Map<String, Anchor>,
val anchors: List<Pair<String, Anchor>>,
) {
/**
* The size of the blueprint.
Expand Down Expand Up @@ -136,7 +137,7 @@ data class Blueprint(
}

fun placeAnchors(world: ServerWorld, pos: BlockPos) {
anchors.forEach { id, anchor ->
anchors.forEach { (id, anchor) ->
BlueprintEntityTypes.ANCHOR.create(world, SpawnReason.COMMAND)?.also { anchorEntity ->
anchorEntity.id = id
anchorEntity.data = anchor.data
Expand Down Expand Up @@ -190,23 +191,26 @@ data class Blueprint(
BlueprintBlockEntity.CODEC.listOf()
.fieldOf("block_entities")
.xmap({ entry -> entry.associateBy(BlueprintBlockEntity::blockPos) }, { map -> map.values.toList() })
.orElseGet(::emptyMap)
.orElse(emptyMap())
.forGetter(Blueprint::blockEntities),
Codec.unboundedMap(Codec.STRING, SerializableRegion.CODEC)
.fieldOf("regions")
.orElseGet(::emptyMap)
.orElse(emptyMap())
.forGetter(Blueprint::regions),
Codec.unboundedMap(Codec.STRING, Anchor.CODEC)
Codec.withAlternative(
ExtraCodecs.nativePair(Codec.STRING.fieldOf("id").codec(), Anchor.CODEC).listOf(),
Codec.unboundedMap(Codec.STRING, Anchor.CODEC).xmap({ it.toList() }, { it.toMap() })
)
.fieldOf("anchors")
.orElseGet(::emptyMap)
.orElse(emptyList())
.forGetter(Blueprint::anchors),
).apply(instance, ::Blueprint)
}

/**
* An entirely empty blueprint.
*/
val EMPTY = Blueprint(emptyList(), emptyList(), emptyMap(), emptyMap(), emptyMap())
val EMPTY = Blueprint(emptyList(), emptyList(), emptyMap(), emptyMap(), emptyList())

/**
* Flattens a set of progressive futures into one progressive future.
Expand Down Expand Up @@ -267,11 +271,11 @@ data class Blueprint(
}

// create anchors
val anchors = mutableMapOf<String, Anchor>()
val anchors = mutableListOf<Pair<String, Anchor>>()
world.iterateEntities().filterIsInstance<AnchorEntity>().forEach { anchorEntity ->
val id = anchorEntity.getOrCreateId()
val anchor = anchorEntity.createAnchor(min)
anchors[id] = anchor
anchors.add(id to anchor)
}

// create blueprint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object BlueprintTest : ModInitializer {
"point" to PointRegion(Vec3d.ZERO),
"cuboid" to CuboidRegion(Vec3d.ZERO, Vec3d.ZERO)
),
mapOf(
listOf(
"test" to Anchor(Vec3d(10.0, 2.0, 1.0), Vec2f(90.0f, 0.0f), "Custom data, anything here!"),
)
)
Expand Down
Binary file not shown.

0 comments on commit 005374c

Please sign in to comment.