Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ import kotlin.math.abs
import kotlin.math.floor
import me.owdding.mortem.core.event.catacomb.CatacombNodeChangeEvent
import me.owdding.mortem.core.event.catacomb.CatacombRoomChangeEvent
import net.minecraft.nbt.NbtIo
import tech.thatgravyboat.skyblockapi.utils.extentions.filterKeysNotNull
import tech.thatgravyboat.skyblockapi.utils.text.TextStyle.hover
import tech.thatgravyboat.skyblockapi.utils.text.TextStyle.onClick
import java.util.concurrent.CompletableFuture
import kotlin.io.path.createParentDirectories

@Module
object CatacombsManager {
Expand Down Expand Up @@ -326,6 +330,41 @@ object CatacombsManager {
append(format(playerNode.roomToWorld(roomPos)))
}.sendWithPrefix()
}
event.registerWithCallback("mortem dev export_room") {
val catacomb = catacomb ?: return@registerWithCallback

val playerNode = catacomb.grid[worldPosToGridPos(McPlayer.self!!.blockPosition())]

if (playerNode !is RoomNode) return@registerWithCallback

CompletableFuture.runAsync {
val structureData = playerNode.exportToStructure()

if (structureData == null) {
Text.of("Failed to export room") {
color = CatppuccinColors.Mocha.red
}.sendWithPrefix()

return@runAsync
}

val fileName = "structures/${System.currentTimeMillis()}.nbt"

val savePath = defaultPath.resolve(fileName)

savePath.createParentDirectories()

NbtIo.writeCompressed(structureData, savePath)

Text.of("Wrote current room to ") {
append(fileName) {
color = CatppuccinColors.Mocha.pink
hover = Text.of(savePath.toString())
}
color = CatppuccinColors.Mocha.lavender
}.sendWithPrefix()
}
}
}

private val defaultPath: Path = McClient.config.resolve("mortem/data")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import me.owdding.mortem.core.catacombs.CatacombDoorType
import me.owdding.mortem.core.catacombs.CatacombRoomType
import me.owdding.mortem.core.catacombs.CatacombsColorProvider
import me.owdding.mortem.core.catacombs.StoredCatacombRoom
import me.owdding.mortem.generated.MortemCodecs
import me.owdding.mortem.utils.StructureUtils
import me.owdding.mortem.utils.Utils
import me.owdding.mortem.utils.extensions.mutableCopy
import me.owdding.mortem.utils.extensions.sendWithPrefix
import me.owdding.mortem.utils.extensions.toVec2d
import net.minecraft.core.BlockPos
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtOps
import net.minecraft.world.level.block.Rotation
import org.joml.Vector2i
import org.joml.Vector3d
import org.joml.Vector3dc
import org.joml.Vector3i
import org.joml.Vector3ic
import org.joml.component1
import org.joml.component2
import org.joml.*
import tech.thatgravyboat.skyblockapi.helpers.McLevel
import tech.thatgravyboat.skyblockapi.utils.text.Text
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -176,6 +176,59 @@ class RoomNode(
val origin = getCenter().toVec2d().add(0.5, 0.5)
return room.add(origin.x, 0.0, origin.y)
}

fun exportToStructure(): CompoundTag? {

val chunks = buildList {
for (first in positions) {
add(
Vector2i(
(-12 + first.x * 2) * 16 + 7,
(-12 + first.y * 2) * 16 + 7,
),
)
for (second in positions) {
if ((first.x - second.x == 1) != (first.y - second.y == 1)) {
add(
Vector2i(
(-12 + first.x + second.x) * 16 + 7,
(-12 + first.y + second.y) * 16 + 7,
),
)
}
}
}
if (shape == CatacombRoomShape.TWO_BY_TWO) {
val centerX = positions.sumOf { 2 * it.x } / positions.size
val centerY = positions.sumOf { 2 * it.y } / positions.size

add(
Vector2i(
(-12 + centerX) * 16 + 7,
(-12 + centerY) * 16 + 7,
),
)
}
}

val roomStructure = StructureUtils.encodeStructureFromRegions(
McLevel.level,
chunks.map { center ->
BlockPos(center.x - 15, 0, center.y - 15) to BlockPos(center.x + 15, 255, center.y + 15)
},
rotation ?: Rotation.NONE,
) ?: return null

if (backingData != null) {
val storedRoomCodec = MortemCodecs.getCodec<StoredCatacombRoom>()

val roomData = storedRoomCodec.encodeStart(NbtOps.INSTANCE, backingData).getOrThrow()

roomStructure.put("backing_room_data", roomData)
}

return roomStructure
}
}

enum class CatacombRoomShape {
Expand Down
201 changes: 201 additions & 0 deletions src/main/kotlin/me/owdding/mortem/utils/StructureUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package me.owdding.mortem.utils

import net.minecraft.core.BlockPos
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtOps
import net.minecraft.util.ProblemReporter
import net.minecraft.world.level.EmptyBlockGetter
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.Blocks
import net.minecraft.world.level.block.Rotation
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate
import net.minecraft.world.level.storage.TagValueOutput
import tech.thatgravyboat.skyblockapi.utils.extentions.minus
import tech.thatgravyboat.skyblockapi.utils.extentions.plus
import kotlin.jvm.optionals.getOrNull
import kotlin.math.max
import kotlin.math.min

object StructureUtils {
// mostest efficientest code ever written
fun encodeStructureFromRegions(
level: Level,
regions: Iterable<Pair<BlockPos, BlockPos>>,
rotation: Rotation = Rotation.NONE,
ignored: List<Block> = listOf(Blocks.AIR, Blocks.VOID_AIR, Blocks.CAVE_AIR, Blocks.STRUCTURE_VOID),
): CompoundTag? {

var minCorner: BlockPos? = null
var maxCorner: BlockPos? = null

val queuedBlocks = mutableMapOf<BlockPos, BlockState>()

for (region in regions) {
val regionMin = BlockPos(
min(region.first.x, region.second.x),
min(region.first.y, region.second.y),
min(region.first.z, region.second.z),
)
val regionMax = BlockPos(
max(region.first.x, region.second.x),
max(region.first.y, region.second.y),
max(region.first.z, region.second.z),
)

if (!level.isLoaded(regionMin) || !level.isLoaded(regionMax)) return null

var blockMin = regionMax
var blockMax = regionMin

for (blockPos in BlockPos.betweenClosed(regionMin, regionMax)) {
if (blockPos in queuedBlocks) continue

val blockState = level.getBlockState(blockPos)
if (!ignored.stream().anyMatch(blockState::`is`)) {
if (blockMax.x > blockPos.x || blockMax.y > blockPos.y || blockMax.z > blockPos.z) {
blockMin = BlockPos(
min(blockPos.x, blockMin.x),
min(blockPos.y, blockMin.y),
min(blockPos.z, blockMin.z),
)
}
if (blockMax.x < blockPos.x || blockMax.y < blockPos.y || blockMax.z < blockPos.z) {
blockMax = BlockPos(
max(blockPos.x, blockMax.x),
max(blockPos.y, blockMax.y),
max(blockPos.z, blockMax.z),
)
}
queuedBlocks[blockPos.immutable()] = blockState
}
}

if (blockMin == regionMax) blockMin = regionMin
if (blockMax == regionMin) blockMax = regionMax

if (minCorner == null || maxCorner == null) {
minCorner = blockMin
maxCorner = blockMax
} else {
minCorner = BlockPos(
min(blockMin.x, minCorner.x),
min(blockMin.y, minCorner.y),
min(blockMin.z, minCorner.z),
)
maxCorner = BlockPos(
max(blockMax.x, maxCorner.x),
max(blockMax.y, maxCorner.y),
max(blockMax.z, maxCorner.z),
)
}
}

if (minCorner == null || maxCorner == null) return null

val size = maxCorner - minCorner + BlockPos(1, 1, 1)

val rotatedSize = when (rotation) {
Rotation.NONE, Rotation.CLOCKWISE_180 -> size
Rotation.CLOCKWISE_90, Rotation.COUNTERCLOCKWISE_90 -> BlockPos(size.z, size.y, size.x)
}

val template = StructureTemplate()
val fullBlockList = mutableListOf<StructureTemplate.StructureBlockInfo>()
val blockEntitiesList = mutableListOf<StructureTemplate.StructureBlockInfo>()
val otherBlocksList = mutableListOf<StructureTemplate.StructureBlockInfo>()

val structureEntities = mutableListOf<StructureTemplate.StructureEntityInfo>()

for ((blockPos, blockState) in queuedBlocks) {
if (isOutOfBounds(minCorner, maxCorner, blockPos)) continue
if (ignored.stream().anyMatch(blockState::`is`)) continue

val relativeBlockPos = blockPos.subtract(minCorner)

val rotatedBlockPos = when (rotation) {
Rotation.NONE -> relativeBlockPos
Rotation.CLOCKWISE_90 -> BlockPos(
rotatedSize.x - relativeBlockPos.z - 1,
relativeBlockPos.y,
relativeBlockPos.x,
)

Rotation.CLOCKWISE_180 -> BlockPos(
rotatedSize.x - relativeBlockPos.x - 1,
relativeBlockPos.y,
rotatedSize.z - relativeBlockPos.z - 1,
)

Rotation.COUNTERCLOCKWISE_90 -> BlockPos(
relativeBlockPos.z,
relativeBlockPos.y,
rotatedSize.z - relativeBlockPos.x - 1,
)
}

val rotatedBlockState = blockState.rotate(rotation)

val blockEntity = level.getBlockEntity(blockPos)

val info = if (blockEntity != null) {
val tagValueOutput =
TagValueOutput.createWithContext(ProblemReporter.DISCARDING, level.registryAccess())
blockEntity.saveWithId(tagValueOutput)
StructureTemplate.StructureBlockInfo(rotatedBlockPos, rotatedBlockState, tagValueOutput.buildResult())
} else {
StructureTemplate.StructureBlockInfo(rotatedBlockPos, rotatedBlockState, null)
}

when {
info.nbt != null -> blockEntitiesList.add(info)
info.state.block.hasDynamicShape() || !info.state.isCollisionShapeFullBlock(
EmptyBlockGetter.INSTANCE,
BlockPos.ZERO,
) -> otherBlocksList.add(info)

else -> fullBlockList.add(info)
}
}

val blockInfoList = buildList {
val comparator = Comparator.comparingInt<StructureTemplate.StructureBlockInfo> { it.pos.y }
.thenComparingInt { it.pos.x }
.thenComparingInt { it.pos.z }

fullBlockList.sortWith(comparator)
addAll(fullBlockList)
otherBlocksList.sortWith(comparator)
addAll(otherBlocksList)
blockEntitiesList.sortWith(comparator)
addAll(blockEntitiesList)
}

template.palettes.clear()
template.palettes.add(StructureTemplate.Palette(blockInfoList))
template.entityInfoList.addAll(structureEntities)

template.size = rotatedSize

val nbt = CompoundTag()

template.save(nbt)

val blockPosCodec = BlockPos.CODEC

val encodedMinPos = blockPosCodec.encodeStart(NbtOps.INSTANCE, minCorner).getOrThrow()
val encodedMaxPos = blockPosCodec.encodeStart(NbtOps.INSTANCE, maxCorner).getOrThrow()

nbt.putString("source_rotation", rotation.serializedName)
nbt.put("source_min_corner", encodedMinPos)
nbt.put("source_max_corner", encodedMaxPos)
nbt.putLong("export_time", System.currentTimeMillis())

return nbt
}

private fun isOutOfBounds(startPos: BlockPos, endPos: BlockPos, query: BlockPos) = query.x > endPos.x || query.x < startPos.x ||
query.y > endPos.y || query.y < startPos.y ||
query.z > endPos.z || query.z < startPos.z
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"mixins": [
"mortem.mixins.json"
],
"accessWidener": "mortem.accesswidener",
"depends": {
"fabricloader": ">=0.16",
"minecraft": ">=${minecraft}"
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/mortem.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessWidener v2 named
accessible field net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate entityInfoList Ljava/util/List;
accessible field net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate palettes Ljava/util/List;
accessible field net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate size Lnet/minecraft/core/Vec3i;
accessible method net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate$Palette <init> (Ljava/util/List;)V