Skip to content

Commit

Permalink
feat: region blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Oct 21, 2024
1 parent b53cf8c commit a4d5ef4
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 34 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ loom {

runs {
authenticatedClient {
client()
inherit(client)
name("Minecraft Client (Authenticated)")
programArg("--msa")
}

testClient {
client()
inherit(client)
name("Minecraft Client (Test)")
source(sourceSets.testClient)
runDir("runtest")
Expand All @@ -101,7 +101,7 @@ loom {
}

testServer {
server()
inherit(server)
name("Minecraft Server (Test)")
source(sourceSets.test)
runDir("runtest")
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.command.BlueprintEditorCommand
import net.mcbrawls.blueprint.editor.block.BlueprintEditorBlocks
import net.mcbrawls.blueprint.network.BlueprintConfigC2SPacket
import net.mcbrawls.blueprint.player.BlueprintPlayerData.Companion.blueprintData
import net.mcbrawls.blueprint.resource.BlueprintManager
Expand All @@ -25,6 +26,8 @@ object BlueprintMod : ModInitializer {
override fun onInitialize() {
logger.info("Initializing $MOD_NAME")

BlueprintEditorBlocks

// register config packet receiver
PayloadTypeRegistry.playC2S().register(BlueprintConfigC2SPacket.PACKET_ID, BlueprintConfigC2SPacket.PACKET_CODEC)

Expand Down
11 changes: 0 additions & 11 deletions src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.mcbrawls.blueprint.command
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType
import dev.andante.codex.encodeQuick
import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.ModContainer
import net.fabricmc.loader.api.Version
Expand All @@ -13,23 +12,13 @@ import net.mcbrawls.blueprint.BlueprintMod.MOD_NAME
import net.mcbrawls.blueprint.asExtremeties
import net.mcbrawls.blueprint.resource.BlueprintManager
import net.mcbrawls.blueprint.structure.Blueprint
import net.mcbrawls.blueprint.structure.BlueprintBlockEntity
import net.mcbrawls.blueprint.structure.PalettedState
import net.minecraft.block.BlockState
import net.minecraft.command.argument.BlockPosArgumentType
import net.minecraft.command.argument.IdentifierArgumentType
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtIo
import net.minecraft.nbt.NbtOps
import net.minecraft.server.command.CommandManager.argument
import net.minecraft.server.command.CommandManager.literal
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.math.BlockBox
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3i
import java.nio.file.Path

object BlueprintCommand {
const val BLUEPRINT_KEY = "blueprint"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package net.mcbrawls.blueprint.command

import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
import net.mcbrawls.blueprint.editor.BlueprintEditorHandler
import net.mcbrawls.blueprint.editor.BlueprintEditorWorld
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mcbrawls.blueprint.editor

import net.minecraft.command.argument.EntityArgumentType.player
import net.minecraft.registry.RegistryKey
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerPlayerEntity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mcbrawls.blueprint.editor

import net.fabricmc.fabric.api.networking.v1.PlayerLookup.world
import net.mcbrawls.blueprint.resource.BlueprintManager
import net.mcbrawls.blueprint.structure.Blueprint
import net.minecraft.block.BlockState
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.mcbrawls.blueprint.editor.block

import net.mcbrawls.blueprint.BlueprintMod
import net.minecraft.block.AbstractBlock
import net.minecraft.block.Block
import net.minecraft.registry.Registries
import net.minecraft.registry.Registry
import net.minecraft.util.Identifier

object BlueprintEditorBlocks {
val POINT_REGION = register("point_region", PointRegionBlock(AbstractBlock.Settings.create().dropsNothing()))

private fun register(id: String, block: Block): Block {
return Registry.register(Registries.BLOCK, Identifier.of(BlueprintMod.MOD_ID, id), block)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.mcbrawls.blueprint.editor.block

import eu.pb4.polymer.core.api.block.PolymerBlock
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks

class PointRegionBlock(settings: Settings) : Block(settings), PolymerBlock, RegionBlock {
override fun getPolymerBlockState(state: BlockState): BlockState {
return Blocks.YELLOW_WOOL.defaultState
}
}
26 changes: 26 additions & 0 deletions src/main/kotlin/net/mcbrawls/blueprint/editor/block/RegionBlock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.mcbrawls.blueprint.editor.block

import net.mcbrawls.blueprint.region.PointRegion
import net.mcbrawls.blueprint.region.serialization.SerializableRegion
import net.minecraft.block.BlockState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d

interface RegionBlock {
companion object {
fun trySaveRegion(
world: ServerWorld,
pos: BlockPos,
state: BlockState,
regions: MutableMap<String, SerializableRegion>
): Boolean {
if (state.block is PointRegionBlock) {
regions["${regions.size}"] = PointRegion(Vec3d.ofCenter(pos))
return true
}

return false
}
}
}
36 changes: 19 additions & 17 deletions src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package net.mcbrawls.blueprint.structure
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import dev.andante.codex.encodeQuick
import net.fabricmc.fabric.api.networking.v1.PlayerLookup.world
import net.mcbrawls.blueprint.asExtremeties
import net.mcbrawls.blueprint.editor.block.RegionBlock
import net.mcbrawls.blueprint.region.serialization.SerializableRegion
import net.minecraft.block.Block
import net.minecraft.block.BlockState
Expand Down Expand Up @@ -181,28 +180,31 @@ data class Blueprint(
val palette = mutableListOf<BlockState>()
val blockEntities = mutableListOf<BlueprintBlockEntity>()
val palettedBlockStates = mutableListOf<PalettedState>()
val regions = mutableMapOf<String, SerializableRegion>()

positions.forEach { pos ->
val relativePos = pos.subtract(min)

// state
val state = world.getBlockState(pos)
if (!state.isAir) {
// build palette
if (state !in palette) {
palette.add(state)
if (!RegionBlock.trySaveRegion(world, pos, state, regions)) {
if (!state.isAir) {
// build palette
if (state !in palette) {
palette.add(state)
}

// create paletted state
val paletteId = palette.indexOf(state)
palettedBlockStates.add(PalettedState(relativePos, paletteId))
}

// create paletted state
val paletteId = palette.indexOf(state)
palettedBlockStates.add(PalettedState(relativePos, paletteId))
}

// block entity
val blockEntity = world.getBlockEntity(pos)
if (blockEntity != null) {
val nbt = blockEntity.createNbt(world.registryManager)
blockEntities.add(BlueprintBlockEntity(relativePos, nbt))
// block entity
val blockEntity = world.getBlockEntity(pos)
if (blockEntity != null) {
val nbt = blockEntity.createNbt(world.registryManager)
blockEntities.add(BlueprintBlockEntity(relativePos, nbt))
}
}
}

Expand All @@ -211,7 +213,7 @@ data class Blueprint(
val size = Vec3i(blockBox.blockCountX, blockBox.blockCountZ, blockBox.blockCountZ)

// create blueprint
val blueprint = Blueprint(palette, palettedBlockStates, blockEntities.associateBy(BlueprintBlockEntity::blockPos), size, mapOf())
val blueprint = Blueprint(palette, palettedBlockStates, blockEntities.associateBy(BlueprintBlockEntity::blockPos), size, regions)
val nbt = CODEC.encodeQuick(NbtOps.INSTANCE, blueprint)

// save blueprint
Expand Down

0 comments on commit a4d5ef4

Please sign in to comment.