From a4d5ef459707abdf44fa4f3ba720d4730d3c8d42 Mon Sep 17 00:00:00 2001 From: Daniel Orr Date: Mon, 21 Oct 2024 08:20:32 +0100 Subject: [PATCH] feat: region blocks --- build.gradle | 6 ++-- .../net/mcbrawls/blueprint/BlueprintMod.kt | 3 ++ .../blueprint/command/BlueprintCommand.kt | 11 ------ .../command/BlueprintEditorCommand.kt | 1 - .../editor/BlueprintEditorHandler.kt | 1 - .../blueprint/editor/BlueprintEditorWorld.kt | 1 - .../editor/block/BlueprintEditorBlocks.kt | 16 +++++++++ .../editor/block/PointRegionBlock.kt | 12 +++++++ .../blueprint/editor/block/RegionBlock.kt | 26 ++++++++++++++ .../mcbrawls/blueprint/structure/Blueprint.kt | 36 ++++++++++--------- 10 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/main/kotlin/net/mcbrawls/blueprint/editor/block/BlueprintEditorBlocks.kt create mode 100644 src/main/kotlin/net/mcbrawls/blueprint/editor/block/PointRegionBlock.kt create mode 100644 src/main/kotlin/net/mcbrawls/blueprint/editor/block/RegionBlock.kt diff --git a/build.gradle b/build.gradle index c618227..8917ec0 100644 --- a/build.gradle +++ b/build.gradle @@ -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") @@ -101,7 +101,7 @@ loom { } testServer { - server() + inherit(server) name("Minecraft Server (Test)") source(sourceSets.test) runDir("runtest") diff --git a/src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt b/src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt index 6b5db4b..74e6811 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/BlueprintMod.kt @@ -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 @@ -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) diff --git a/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt b/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt index 2e78198..00555ea 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt @@ -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 @@ -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" diff --git a/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintEditorCommand.kt b/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintEditorCommand.kt index 28301e4..356a84e 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintEditorCommand.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintEditorCommand.kt @@ -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 diff --git a/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorHandler.kt b/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorHandler.kt index d050923..5111f5d 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorHandler.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorHandler.kt @@ -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 diff --git a/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorWorld.kt b/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorWorld.kt index b671ad9..9be742e 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorWorld.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/editor/BlueprintEditorWorld.kt @@ -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 diff --git a/src/main/kotlin/net/mcbrawls/blueprint/editor/block/BlueprintEditorBlocks.kt b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/BlueprintEditorBlocks.kt new file mode 100644 index 0000000..4fd5bfe --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/BlueprintEditorBlocks.kt @@ -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) + } +} diff --git a/src/main/kotlin/net/mcbrawls/blueprint/editor/block/PointRegionBlock.kt b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/PointRegionBlock.kt new file mode 100644 index 0000000..d17088a --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/PointRegionBlock.kt @@ -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 + } +} diff --git a/src/main/kotlin/net/mcbrawls/blueprint/editor/block/RegionBlock.kt b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/RegionBlock.kt new file mode 100644 index 0000000..eba0b47 --- /dev/null +++ b/src/main/kotlin/net/mcbrawls/blueprint/editor/block/RegionBlock.kt @@ -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 + ): Boolean { + if (state.block is PointRegionBlock) { + regions["${regions.size}"] = PointRegion(Vec3d.ofCenter(pos)) + return true + } + + return false + } + } +} diff --git a/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt b/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt index 87ab307..6ae2617 100644 --- a/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt +++ b/src/main/kotlin/net/mcbrawls/blueprint/structure/Blueprint.kt @@ -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 @@ -181,28 +180,31 @@ data class Blueprint( val palette = mutableListOf() val blockEntities = mutableListOf() val palettedBlockStates = mutableListOf() + val regions = mutableMapOf() 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)) + } } } @@ -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