Skip to content

Commit

Permalink
feat: anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jan 12, 2025
1 parent 8ab748c commit 3e23610
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 98 deletions.
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//file:noinspection All

plugins {
id "fabric-loom" version "1.8-SNAPSHOT"
id "fabric-loom" version "1.9-SNAPSHOT"
id "org.jetbrains.kotlin.jvm"
id "maven-publish"
}
Expand All @@ -17,8 +17,8 @@ base {

repositories {
maven {
name = "Andante"
url = "https://maven.andante.dev/releases/"
name = "MC Brawls"
url = "https://maven.mcbrawls.net/releases/"
}

maven {
Expand Down Expand Up @@ -53,13 +53,13 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:$loader_version"

modImplementation "net.fabricmc.fabric-api:fabric-api:$fabric_version"
modImplementation "net.fabricmc:fabric-language-kotlin:$fabric_kotlin_version"
modImplementation "net.fabricmc:fabric-language-kotlin:$fabric_kotlin_version+kotlin.$kotlin_version"

include modImplementation ("dev.andante:codex:$codex_version")
include modImplementation ("net.mcbrawls:codex:$codex_version")
include modImplementation ("net.mcbrawls:slate:$slate_version")

include modImplementation ("xyz.nucleoid:fantasy:$fantasy_version")
include modImplementation ("eu.pb4:polymer-core:$polymer_version")
include modImplementation ("eu.pb4:sgui:$sgui_version")

// test
testImplementation sourceSets.main.output
Expand Down Expand Up @@ -124,12 +124,12 @@ tasks.withType(AbstractArchiveTask).configureEach {
}

tasks.withType(JavaCompile).configureEach {
it.options.release = 17
it.options.release = 21
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = 17
jvmTarget = 21
}
}

Expand Down
18 changes: 9 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.21.3
yarn_build=2
loader_version=0.16.7
minecraft_version=1.21.4
yarn_build=8
loader_version=0.16.10

# Mod Properties
mod_version=1.13
maven_group=net.mcbrawls
mod_id=blueprint

# Dependencies
fabric_version=0.107.0+1.21.3
fabric_version=0.114.2+1.21.4

kotlin_version=2.0.21
fabric_kotlin_version=1.12.3+kotlin.2.0.21
kotlin_version=2.1.0
fabric_kotlin_version=1.13.0

codex_version=1.5.0
sgui_version=1.7.2+1.21.2
codex_version=1.1
slate_version=1.0
fantasy_version=0.6.5+1.21.2
polymer_version=0.10.1+1.21.3
polymer_version=0.11.3+1.21.4
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pluginManagement {
mod_id

repositories {
maven {
name = "Fabric"
Expand All @@ -13,4 +15,4 @@ pluginManagement {
}
}

rootProject.name = "blueprint"
rootProject.name = mod_id
2 changes: 2 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.mcbrawls.blueprint.block.BlueprintBlocks
import net.mcbrawls.blueprint.block.entity.BlueprintBlockEntityTypes
import net.mcbrawls.blueprint.command.BlueprintCommand
import net.mcbrawls.blueprint.command.BlueprintEditorCommand
import net.mcbrawls.blueprint.entity.BlueprintEntityTypes
import net.mcbrawls.blueprint.item.BlueprintItems
import net.mcbrawls.blueprint.network.BlueprintConfigC2SPacket
import net.mcbrawls.blueprint.player.BlueprintPlayerData.Companion.blueprintData
Expand All @@ -31,6 +32,7 @@ object BlueprintMod : ModInitializer {
BlueprintBlocks
BlueprintItems
BlueprintBlockEntityTypes
BlueprintEntityTypes

// register config packet receiver
PayloadTypeRegistry.playC2S().register(BlueprintConfigC2SPacket.PACKET_ID, BlueprintConfigC2SPacket.PACKET_CODEC)
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/anchor/Anchor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.mcbrawls.blueprint.anchor

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import dev.andante.codex.ExtraCodecs
import dev.andante.codex.nullableFieldOf
import net.minecraft.util.math.Vec2f
import net.minecraft.util.math.Vec3d

/**
* An entity-like point within a blueprint that can hold custom data.
*/
data class Anchor(
val position: Vec3d,
val rotation: Vec2f,
val data: String? = null,
) {
companion object {
val CODEC: Codec<Anchor> = RecordCodecBuilder.create { instance ->
instance.group(
Vec3d.CODEC.fieldOf("position").forGetter(Anchor::position),
ExtraCodecs.VEC_2F.fieldOf("rotation").forGetter(Anchor::rotation),
Codec.STRING.nullableFieldOf("data").forGetter(Anchor::data),
).apply(instance, ::Anchor)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.mcbrawls.blueprint.block

import net.mcbrawls.blueprint.BlueprintMod
import net.mcbrawls.blueprint.editor.block.PointRegionBlock
import net.mcbrawls.blueprint.block.region.PointRegionBlock
import net.minecraft.block.AbstractBlock
import net.minecraft.block.Block
import net.minecraft.registry.Registries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.mcbrawls.blueprint.block.entity

import net.mcbrawls.blueprint.structure.Blueprint
import net.minecraft.block.BlockState
import net.minecraft.block.entity.BlockEntity
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtElement
import net.minecraft.registry.RegistryWrapper
import net.minecraft.util.math.BlockPos
import java.nio.charset.StandardCharsets
import java.util.UUID
import net.minecraft.util.math.Vec3d

class RegionIdBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(BlueprintBlockEntityTypes.REGION_ID, pos, state) {
var id: String? = null
Expand All @@ -16,19 +16,10 @@ class RegionIdBlockEntity(pos: BlockPos, state: BlockState) : BlockEntity(Bluepr
* Gets the stored identifier or creates one from the block entity's world key and position.
* @return a region id
*/
fun getOrCreateRegionId(): String {
// return stored
fun getOrCreateId(): String {
id?.also { return it }

// create custom
val world = world ?: throw IllegalStateException("World not set")
val key = world.registryKey
val worldId = key.value

val data = worldId.toString() + pos.toShortString()
val uuid = UUID.nameUUIDFromBytes(data.toByteArray(StandardCharsets.UTF_8))

return uuid.toString()
return Blueprint.createUniqueId(world ?: throw IllegalStateException("World not set"), Vec3d.of(pos))
}

override fun writeNbt(nbt: NbtCompound, lookup: RegistryWrapper.WrapperLookup) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.mcbrawls.blueprint.editor.block
package net.mcbrawls.blueprint.block.region

import com.mojang.serialization.MapCodec
import eu.pb4.polymer.core.api.block.PolymerBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.mcbrawls.blueprint.editor.block
package net.mcbrawls.blueprint.block.region

import net.mcbrawls.blueprint.block.entity.RegionIdBlockEntity
import net.mcbrawls.blueprint.editor.gui.RegionIdInputGui
import net.mcbrawls.blueprint.region.serialization.SerializableRegion
import net.mcbrawls.blueprint.structure.Blueprint.Companion.openInputGui
import net.minecraft.block.BlockState
import net.minecraft.block.BlockWithEntity
import net.minecraft.block.entity.BlockEntity
Expand Down Expand Up @@ -81,13 +81,13 @@ abstract class RegionBlock(settings: Settings) : BlockWithEntity(settings) {
pos: BlockPos,
relativePos: BlockPos,
state: BlockState,
regions: MutableMap<String, SerializableRegion>
regions: MutableMap<String, SerializableRegion>,
): Boolean {
val block = state.block
if (block is RegionBlock) {
val blockEntity = world.getBlockEntity(pos)
if (blockEntity is RegionIdBlockEntity) {
val regionId = blockEntity.getOrCreateRegionId()
val regionId = blockEntity.getOrCreateId()
val region = block.saveRegion(world, pos, relativePos, blockEntity)
regions[regionId] = region
}
Expand All @@ -102,26 +102,19 @@ abstract class RegionBlock(settings: Settings) : BlockWithEntity(settings) {
* Opens the region id editor gui for a given region id block entity.
*/
fun openRegionIdEditorGui(player: ServerPlayerEntity, blockEntity: RegionIdBlockEntity) {
val regionId = blockEntity.getOrCreateRegionId()

val gui = RegionIdInputGui(player) { gui, input ->
val regionId = blockEntity.getOrCreateId()
openInputGui(player, Text.literal("Region ID"), regionId) { input ->
if (input != regionId) {
if (input.isBlank()) {
val id = blockEntity.id
gui.player.sendMessage(Text.literal("No region ID set. Still: \"$id\"").formatted(Formatting.RED))
player.sendMessage(Text.literal("No region ID set. Still: \"$id\"").formatted(Formatting.RED))
} else {
val processedInput = input.trim()
blockEntity.id = processedInput
gui.player.sendMessage(Text.literal("Set region ID: \"$processedInput\"").formatted(Formatting.GREEN))
player.sendMessage(Text.literal("Set region ID: \"$processedInput\"").formatted(Formatting.GREEN))
}
}

true
}

gui.setDefaultInputValue(regionId)

gui.open()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ object BlueprintCommand {
}

private fun executeSave(context: CommandContext<ServerCommandSource>): Int {
val world = context.source.world

// gather arguments
val blueprintId = IdentifierArgumentType.getIdentifier(context, BLUEPRINT_KEY)

Expand All @@ -74,7 +72,9 @@ object BlueprintCommand {
val (min, max) = inputStartPosition.asExtremeties(inputEndPosition)

// save
val pathString = Blueprint.save(world, min, max, blueprintId)
val source = context.source
val blueprint = Blueprint.save(source.world, min, max)
val pathString = BlueprintManager.saveGenerated(source.server, blueprintId, blueprint)

// feedback
context.source.sendFeedback({ Text.literal("Saved blueprint to \"$pathString\"") }, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class BlueprintEditorWorld(
key: RegistryKey<World>,
config: RuntimeWorldConfig
) : RuntimeWorld(server, key, config, Style.TEMPORARY) {
val sourceBlueprint: Blueprint? get() = BlueprintManager[blueprintId]

private var minX = BLUEPRINT_PLACEMENT_POS.x
private var minY = BLUEPRINT_PLACEMENT_POS.y
private var minZ = BLUEPRINT_PLACEMENT_POS.z
Expand All @@ -36,7 +38,7 @@ class BlueprintEditorWorld(
* @return whether this created a new blueprint
*/
internal fun initializeBlueprint(): Boolean {
val blueprint = BlueprintManager[blueprintId]
val blueprint = sourceBlueprint
return if (blueprint != null) {
val size = blueprint.size
maxX += size.x
Expand Down Expand Up @@ -110,7 +112,23 @@ class BlueprintEditorWorld(
*/
fun saveBlueprint(): String {
val (min, max) = getRoughBlueprintBoundingBox()
return Blueprint.save(this, min, max, blueprintId)

val generatedBlueprint = Blueprint.save(this, min, max).let { generatedBlueprint ->
val blueprint = sourceBlueprint
if (blueprint != null) {
Blueprint(
generatedBlueprint.palette,
generatedBlueprint.palettedBlockStates,
generatedBlueprint.blockEntities,
blueprint.regions + generatedBlueprint.regions,
blueprint.anchors + generatedBlueprint.anchors,
)
} else {
generatedBlueprint
}
}

return BlueprintManager.saveGenerated(server, blueprintId, generatedBlueprint)
}

companion object {
Expand Down

This file was deleted.

Loading

0 comments on commit 3e23610

Please sign in to comment.