Skip to content

Commit

Permalink
feat: impl LitematicaIO
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Sep 7, 2023
1 parent f9fd648 commit 56a8eab
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 104 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/github/zly2006/reden/rvc/DefaultPlacement.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.zly2006.reden.rvc

import net.minecraft.util.math.BlockPos
import net.minecraft.world.World

class DefaultPlacement(
override val structure: IStructure,
override val world: World,
override val origin: BlockPos
): IPlacement {
override var name: String = structure.name
override var enabled: Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SchematicImpl(
}

override fun createPlacement(world: World, origin: BlockPos): IPlacement {
TODO("Not yet implemented")
return DefaultPlacement(this, world, origin)
}
}

Expand Down
61 changes: 46 additions & 15 deletions src/main/java/com/github/zly2006/reden/rvc/tracking/LitematicaIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fi.dy.masa.litematica.selection.AreaSelection
import fi.dy.masa.litematica.selection.Box
import net.minecraft.util.math.BlockBox
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
Expand Down Expand Up @@ -47,6 +48,10 @@ object LitematicaIO: StructureIO {
)
boxes.mapIndexed { index, box ->
val subRegionContainer = litematica.getSubRegionContainer(index.toString())!!
val blockEntityMap = litematica.getBlockEntityMapForRegion(index.toString())!!
val blockTicks = litematica.getScheduledBlockTicksForRegion(index.toString())!!
val fluidTicks = litematica.getScheduledFluidTicksForRegion(index.toString())!!
val entityInfos = litematica.getEntityListForRegion(index.toString())!!
structure.cachedPositions.keys.filter { it in box }.forEach { pos ->
subRegionContainer.set(
pos.x - box.minX,
Expand All @@ -55,37 +60,63 @@ object LitematicaIO: StructureIO {
structure.world.getBlockState(pos)
)
}
structure.blockEntities.keys.filter { it in box }.forEach { pos ->
blockEntityMap[pos] = structure.blockEntities[pos]
}
structure.entities.forEach {
entityInfos.add(LitematicaSchematic.EntityInfo(
Vec3d.ZERO,
it.value
))
}
subRegionContainer
}
// todo
litematica.writeToFile(
path.toFile(),
"RVC-Export-Test",
true,
)
return
// todo: finish non-tracked structures and move saving code to the end of the function
}
else {
val boxName = "RVC Structure"
litematica = LitematicaSchematic.createEmptySchematic(
AreaSelection().apply {
addSubRegionBox(
Box(
BlockPos.ORIGIN,
BlockPos(
structure.xSize,
structure.ySize,
structure.zSize
),
"RVC Structure"
BlockPos(structure.xSize, structure.ySize, structure.zSize),
boxName
),
false
)
},
"RVC"
)
TODO("Not yet implemented")
val subRegionContainer = litematica.getSubRegionContainer(boxName)!!
val blockEntityMap = litematica.getBlockEntityMapForRegion(boxName)!!
val entityInfos = litematica.getEntityListForRegion(boxName)!!
for (x in 0 until structure.xSize) {
for (y in 0 until structure.ySize) {
for (z in 0 until structure.zSize) {
val pos = BlockPos(x, y, z)
subRegionContainer.set(x, y, z, structure.getBlockState(pos))
val be = structure.getBlockEntityData(pos)
if (be != null) {
blockEntityMap[pos] = be
}
}
}
}
structure.entities.forEach {
entityInfos.add(LitematicaSchematic.EntityInfo(
Vec3d.ZERO,
it.value
))
}
}
litematica.metadata.description = "Reden Exported to Litematica"
litematica.metadata.name = structure.name
litematica.metadata.timeCreated = System.currentTimeMillis()
litematica.writeToFile(
path.toFile(),
"Reden-Exported-${litematica.metadata.name}",
true,
)
}

override fun load(path: Path, structure: IWritableStructure) = throw UnsupportedOperationException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class TrackedStructure (
return BlockBox(minX, minY, minZ, maxX, maxY, maxZ)
}

fun splitCuboids(): List<BlockBox> {
fun splitCuboids(
untrackedIsIgnored: Boolean = false,
): List<BlockBox> {
class IncludeEntry(
var cuboid: BlockBox?,
val points: SortedSet<BlockPos> = sortedSetOf()
Expand All @@ -82,108 +84,110 @@ class TrackedStructure (
cuboid = BlockBox(minX, minY, minZ, maxX, maxY, maxZ)
}
}
val result: MutableList<IncludeEntry>

val result = mutableListOf(IncludeEntry(cachedPositions.keys))
cachedIgnoredPositions.forEach { ignored ->
val iter = result.listIterator()
while (iter.hasNext()) {
val entry = iter.next()
if (entry.cuboid?.contains(ignored.key) == true) {
// Note: in this block element[i] is always removing
// select if we can split by an axis without add more cuboids
if (entry.points.none { it.x == ignored.key.x }) {
iter.remove()
// split by x
iter.add(
IncludeEntry(
entry.points.filter { it.x < ignored.key.x }
if (untrackedIsIgnored)
result = (cachedPositions.keys).map { IncludeEntry(listOf(it)) }.toMutableList()
else {
result = mutableListOf(IncludeEntry(cachedPositions.keys))
cachedIgnoredPositions.forEach { ignored ->
val iter = result.listIterator()
while (iter.hasNext()) {
val entry = iter.next()
if (entry.cuboid?.contains(ignored.key) == true) {
// Note: in this block element[i] is always removing
// select if we can split by an axis without add more cuboids
if (entry.points.none { it.x == ignored.key.x }) {
iter.remove()
// split by x
iter.add(
IncludeEntry(
entry.points.filter { it.x < ignored.key.x }
)
)
)
iter.add(
IncludeEntry(
entry.points.filter { it.x > ignored.key.x }
iter.add(
IncludeEntry(
entry.points.filter { it.x > ignored.key.x }
)
)
)
}
else if (entry.points.none { it.y == ignored.key.y }) {
iter.remove()
// split by y
iter.add(
IncludeEntry(
entry.points.filter { it.y < ignored.key.y }
} else if (entry.points.none { it.y == ignored.key.y }) {
iter.remove()
// split by y
iter.add(
IncludeEntry(
entry.points.filter { it.y < ignored.key.y }
)
)
)
iter.add(
IncludeEntry(
entry.points.filter { it.y > ignored.key.y }
iter.add(
IncludeEntry(
entry.points.filter { it.y > ignored.key.y }
)
)
)
}
else if (entry.points.none { it.z == ignored.key.z }) {
iter.remove()
// split by z
iter.add(
IncludeEntry(
entry.points.filter { it.z < ignored.key.z }
} else if (entry.points.none { it.z == ignored.key.z }) {
iter.remove()
// split by z
iter.add(
IncludeEntry(
entry.points.filter { it.z < ignored.key.z }
)
)
)
iter.add(
IncludeEntry(
entry.points.filter { it.z > ignored.key.z }
iter.add(
IncludeEntry(
entry.points.filter { it.z > ignored.key.z }
)
)
)
}
else {
var entryToSplit = entry
iter.remove()
// first, split by x
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.x < ignored.key.x }
} else {
var entryToSplit = entry
iter.remove()
// first, split by x
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.x < ignored.key.x }
)
)
)
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.x > ignored.key.x }
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.x > ignored.key.x }
)
)
)
// then add same x points to the new cuboids
entryToSplit = IncludeEntry(entryToSplit.points.filter { it.x == ignored.key.x })
if (entryToSplit.cuboid?.contains(ignored.key) != true) {
iter.add(entryToSplit)
}
// second, split by y
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.y < ignored.key.y }
// then add same x points to the new cuboids
entryToSplit = IncludeEntry(entryToSplit.points.filter { it.x == ignored.key.x })
if (entryToSplit.cuboid?.contains(ignored.key) != true) {
iter.add(entryToSplit)
}
// second, split by y
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.y < ignored.key.y }
)
)
)
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.y > ignored.key.y }
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.y > ignored.key.y }
)
)
)
// then add same y points to the new cuboids
entryToSplit = IncludeEntry(entryToSplit.points.filter { it.y == ignored.key.y })
if (entryToSplit.cuboid?.contains(ignored.key) != true) {
iter.add(entryToSplit)
}
// third, split by z
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.z < ignored.key.z }
// then add same y points to the new cuboids
entryToSplit = IncludeEntry(entryToSplit.points.filter { it.y == ignored.key.y })
if (entryToSplit.cuboid?.contains(ignored.key) != true) {
iter.add(entryToSplit)
}
// third, split by z
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.z < ignored.key.z }
)
)
)
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.z > ignored.key.z }
iter.add(
IncludeEntry(
entryToSplit.points.filter { it.z > ignored.key.z }
)
)
)
// then add same z point is ignored
// then add same z point is ignored
}
}
}
result.removeIf { it.points.isEmpty() || it.cuboid == null }
}
result.removeIf { it.points.isEmpty() || it.cuboid == null }
}

return result.map { it.cuboid!! }
Expand All @@ -193,7 +197,6 @@ class TrackedStructure (
val pos: BlockPos,
val predicate: TrackPredicate
) {

fun spreadAround(world: World, successConsumer: (BlockPos) -> Unit, failConsumer: ((BlockPos) -> Unit)? = null) {
val x = pos.x
val y = pos.y
Expand Down

0 comments on commit 56a8eab

Please sign in to comment.