Skip to content

Commit

Permalink
✨ Finished schematics & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KodingDev committed Jul 23, 2020
1 parent 53c4601 commit e36880d
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.zerite.craftlib.commons.world

/**
* Stores information about a single block in the world
*
* @author Koding
* @since 0.1.0-SNAPSHOT
*/
data class Block(
val id: Int,
val metadata: Int,
val blockLight: Int = 0,
val skyLight: Int = 0
) {

companion object {
/**
* Constant value for an air block.
*/
@JvmField
val AIR = Block(0, 0)
}

/**
* The location of this block.
*/
lateinit var location: BlockLocation
}

/**
* Vector for storing a 3D block location.
*
* @author Koding
* @since 0.1.0-SNAPSHOT
*/
data class BlockLocation(
val x: Int,
val y: Int,
val z: Int
)
34 changes: 34 additions & 0 deletions craftlib-nbt/src/main/kotlin/dev/zerite/craftlib/nbt/DSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package dev.zerite.craftlib.nbt

import dev.zerite.craftlib.nbt.impl.*
import java.io.InputStream
import java.io.OutputStream

/**
* Builds a compound tag.
Expand Down Expand Up @@ -151,3 +153,35 @@ val Any?.asTag: NBTTag
is LongArray -> LongArrayTag(this)
else -> error("Don't know how to convert $this into a NBT tag")
}

/**
* Writes a NBT tag to the provided output stream.
*
* @param stream The stream to write to.
* @param compressed Whether the tag we are writing should be compressed.
*
* @author Koding
* @since 0.1.3
*/
suspend fun NBTTag.write(stream: OutputStream, compressed: Boolean = false) =
if (compressed) NBTIO.writeCompressed(this, stream)
else NBTIO.write(this, stream)

/**
* Reads a NBT tag from this input stream.
*
* @param compressed Whether this tag we are reading is compressed.
* @author Koding
* @since 0.1.3
*/
suspend fun InputStream.readTag(compressed: Boolean = false) =
if (compressed) NBTIO.readCompressed(this)
else NBTIO.read(this)

/**
* Converts a NBT tag into a named NBT tag.
*
* @author Koding
* @since 0.1.3
*/
fun <T : NBTTag> T.named(name: String) = named(name, this)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.zerite.craftlib.protocol.data.world

import dev.zerite.craftlib.commons.world.Block
import dev.zerite.craftlib.commons.world.BlockLocation

/**
* Stores a chunk's full data, including the block information and
* lighting data.
Expand Down Expand Up @@ -69,36 +72,6 @@ data class Chunk(internal var blocks: Array<Block?> = arrayOfNulls(DESIRED_BLOCK
override fun hashCode() = blocks.contentHashCode()
}

/**
* Stores information about a single block in the world
*
* @author Koding
* @since 0.1.0-SNAPSHOT
*/
data class Block(
val id: Int,
val metadata: Int,
val blockLight: Int,
val skyLight: Int
) {
/**
* The location of this block.
*/
lateinit var location: BlockLocation
}

/**
* Vector for storing a 3D block location.
*
* @author Koding
* @since 0.1.0-SNAPSHOT
*/
data class BlockLocation(
val x: Int,
val y: Int,
val z: Int
)

/**
* Stores information about a chunk.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.zerite.craftlib.protocol.data.world

import dev.zerite.craftlib.commons.io.ByteNibbleArray
import dev.zerite.craftlib.commons.world.Block
import dev.zerite.craftlib.commons.world.BlockLocation
import dev.zerite.craftlib.protocol.util.ext.toByteArray
import dev.zerite.craftlib.protocol.util.ext.toShortArray
import dev.zerite.craftlib.protocol.util.ext.trim
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.zerite.craftlib.protocol.data.world

import dev.zerite.craftlib.commons.world.Block

/**
* Simple utility class which allows you to set any coordinate in a world with
* a block and automatically update the chunks map.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.zerite.craftlib.protocol.packet.play.server.world

import dev.zerite.craftlib.protocol.data.world.Block
import dev.zerite.craftlib.commons.world.Block
import dev.zerite.craftlib.protocol.data.world.Chunk
import dev.zerite.craftlib.protocol.data.world.ChunkColumn
import dev.zerite.craftlib.protocol.packet.PacketTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.zerite.craftlib.protocol.packet.play.server.world

import dev.zerite.craftlib.protocol.data.world.Block
import dev.zerite.craftlib.commons.world.Block
import dev.zerite.craftlib.protocol.data.world.Chunk
import dev.zerite.craftlib.protocol.data.world.ChunkColumn
import dev.zerite.craftlib.protocol.packet.PacketTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package dev.zerite.craftlib.schematic.data
package dev.zerite.craftlib.schematic

import dev.zerite.craftlib.commons.world.Block
import dev.zerite.craftlib.nbt.impl.CompoundTag

/**
* Houses all the valid schematic values which have either been parsed from a
Expand All @@ -7,12 +10,20 @@ package dev.zerite.craftlib.schematic.data
* @author Koding
* @since 0.1.3
*/
data class Schematic(
data class Schematic @JvmOverloads constructor(
var width: Short,
var height: Short,
var length: Short,
var materials: SchematicMaterials,
var blocks: Array<SchematicBlock>
var blocks: Array<Block> = Array(width * height * length) { Block.AIR },
var entities: List<CompoundTag> = arrayListOf(),
var tileEntities: List<CompoundTag> = arrayListOf(),
var originX: Int = 0,
var originY: Int = 0,
var originZ: Int = 0,
var offsetX: Int = 0,
var offsetY: Int = 0,
var offsetZ: Int = 0
) {

companion object {
Expand Down Expand Up @@ -44,7 +55,8 @@ data class Schematic(
* @since 0.1.3
*/
@Suppress("UNUSED")
fun index(x: Int, y: Int, z: Int) = index(x, y, z, length, width)
fun index(x: Int, y: Int, z: Int) =
index(x, y, z, length, width)

/**
* Retrieves a block at the given coordinate, otherwise returning
Expand All @@ -58,7 +70,7 @@ data class Schematic(
* @author Koding
* @since 0.1.3
*/
operator fun get(x: Int, y: Int, z: Int) = blocks.getOrElse(index(x, y, z)) { SchematicBlock.AIR }
operator fun get(x: Int, y: Int, z: Int) = blocks.getOrElse(index(x, y, z)) { Block.AIR }

/**
* Sets a block at the given coordinate. All positions are relative
Expand All @@ -72,7 +84,7 @@ data class Schematic(
* @author Koding
* @since 0.1.3
*/
operator fun set(x: Int, y: Int, z: Int, value: SchematicBlock) {
operator fun set(x: Int, y: Int, z: Int, value: Block) {
blocks[index(x, y, z)] = value
}

Expand Down Expand Up @@ -101,22 +113,6 @@ data class Schematic(
}
}

/**
* Stores a pair of a block ID and metadata value.
*
* @author Koding
* @since 0.1.3
*/
data class SchematicBlock(
var id: Int,
var metadata: Byte
) {
companion object {
@JvmField
val AIR = SchematicBlock(0, 0)
}
}

/**
* Stores the possible value for the schematic materials string.
*
Expand Down
Loading

0 comments on commit e36880d

Please sign in to comment.