Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Walls
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Mar 17, 2021
1 parent d0fce1f commit 0bacb73
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.studiocode.miniatureblocks.build

import de.studiocode.miniatureblocks.build.concurrent.SyncTaskExecutor
import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.toThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.build.concurrent.toAsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.Direction
import de.studiocode.miniatureblocks.resourcepack.model.Direction.*
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
Expand Down Expand Up @@ -35,7 +35,7 @@ class BuildDataCreator(min: Location, max: Location) {
private val syncExecutor = SyncTaskExecutor()

private val batches = ConcurrentLinkedQueue<Set<Point3D>>()
private val processedWorldData = ConcurrentHashMap<Point3D, Pair<ThreadSafeBlockData, Part>>()
private val processedWorldData = ConcurrentHashMap<Point3D, Pair<AsyncBlockData, Part>>()
private val data = ConcurrentHashMap<Point3D, Part>()

fun createData(): BuildData {
Expand Down Expand Up @@ -69,22 +69,22 @@ class BuildDataCreator(min: Location, max: Location) {
awaitTermination(time, timeUnit)
}

private fun loadWorldData(points: List<Point3D>, after: (HashMap<Point3D, ThreadSafeBlockData>) -> Unit) {
private fun loadWorldData(points: List<Point3D>, after: (HashMap<Point3D, AsyncBlockData>) -> Unit) {
syncExecutor.submit { // run sync as it accesses Bukkit
val worldData = HashMap<Point3D, ThreadSafeBlockData>()
val worldData = HashMap<Point3D, AsyncBlockData>()
points.forEach {
val block = it.getBlock(world)
val material = block.type
if (BlockTexture.has(material)) {
val data = block.blockData.toThreadSafeBlockData(material)
val data = block.blockData.toAsyncBlockData(material)
worldData[it] = data // save data (block and material) of this point
}
}
after(worldData) // continue processing async
}
}

private fun processWorldData(worldInfo: HashMap<Point3D, ThreadSafeBlockData>) {
private fun processWorldData(worldInfo: HashMap<Point3D, AsyncBlockData>) {
partsExecutorService.submit {
try {
worldInfo.forEach { (point, data) -> processedWorldData[point] = data to Part.createPart(data) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package de.studiocode.miniatureblocks.build.concurrent

import de.studiocode.miniatureblocks.resourcepack.model.Direction
import de.studiocode.miniatureblocks.util.VersionUtils
import de.studiocode.miniatureblocks.util.isWall
import org.bukkit.Material
import org.bukkit.block.BlockFace
import org.bukkit.block.data.*
import org.bukkit.block.data.Bisected.Half
import org.bukkit.block.data.type.*
import org.bukkit.block.data.type.Slab.Type
import org.bukkit.block.data.type.Wall.Height

open class ThreadSafeBlockData(val material: Material)
open class AsyncBlockData(val material: Material)

open class DirectionalBlockData(material: Material, blockData: Directional) : ThreadSafeBlockData(material) {
open class DirectionalBlockData(material: Material, blockData: Directional) : AsyncBlockData(material) {
val facing = blockData.facing
}

class OrientableBlockData(material: Material, blockData: Orientable) : ThreadSafeBlockData(material) {
class OrientableBlockData(material: Material, blockData: Orientable) : AsyncBlockData(material) {
val axis = blockData.axis
}

open class MultipleFacingBlockData(material: Material, blockData: MultipleFacing) : ThreadSafeBlockData(material) {
open class MultipleFacingBlockData(material: Material, blockData: MultipleFacing) : AsyncBlockData(material) {
val faces = HashSet(blockData.faces)
}

class SlabBlockData(material: Material, blockData: Slab) : ThreadSafeBlockData(material) {
class SlabBlockData(material: Material, blockData: Slab) : AsyncBlockData(material) {
val top = blockData.type == Type.TOP
}

Expand All @@ -45,25 +50,28 @@ class GateBlockData(material: Material, blockData: Gate) : DirectionalBlockData(
val open = blockData.isOpen
}

class DaylightDetectorBlockData(material: Material, blockData: DaylightDetector) : ThreadSafeBlockData(material) {
class DaylightDetectorBlockData(material: Material, blockData: DaylightDetector) : AsyncBlockData(material) {
val inverted = blockData.isInverted
}

class SnowBlockData(material: Material, blockData: Snow) : ThreadSafeBlockData(material) {
class SnowBlockData(material: Material, blockData: Snow) : AsyncBlockData(material) {
val layers = blockData.layers
val maximumLayers = blockData.maximumLayers
}

class SnowableBlockData(material: Material, blockData: Snowable) : ThreadSafeBlockData(material) {
class SnowableBlockData(material: Material, blockData: Snowable) : AsyncBlockData(material) {
val snowy = blockData.isSnowy
}

class SwitchBlockData(material: Material, blockData: Switch) : DirectionalBlockData(material, blockData) {
val attachedFace = blockData.attachedFace
}

fun BlockData.toThreadSafeBlockData(material: Material) =
class WallBlockData(material: Material, val up: Boolean, val faces: HashMap<BlockFace, Boolean>) : AsyncBlockData(material)

fun BlockData.toAsyncBlockData(material: Material) =
when {
material.isWall() -> createWallBlockData(material, this)
isSlab() -> SlabBlockData(material, this as Slab)
isSnow() -> SnowBlockData(material, this as Snow)
this is Stairs -> StairBlockData(material, this)
Expand All @@ -76,9 +84,30 @@ fun BlockData.toThreadSafeBlockData(material: Material) =
this is Directional -> DirectionalBlockData(material, this)
this is Orientable -> OrientableBlockData(material, this)
this is MultipleFacing -> MultipleFacingBlockData(material, this)
else -> ThreadSafeBlockData(material)
else -> AsyncBlockData(material)
}

fun BlockData.isSlab() = this is Slab && this.type != Type.DOUBLE

fun BlockData.isSnow() = this is Snow && this.layers < this.maximumLayers
private fun BlockData.isSlab() = this is Slab && this.type != Type.DOUBLE

private fun BlockData.isSnow() = this is Snow && this.layers < this.maximumLayers

private fun createWallBlockData(material: Material, blockData: BlockData): WallBlockData {
val up: Boolean
val faces = HashMap<BlockFace, Boolean>()
if (VersionUtils.isVersionOrHigher("1.16.0")) {
val wall = blockData as Wall
up = wall.isUp
Direction.cardinalPoints
.map { it.blockFace }
.forEach {
val height = wall.getHeight(it)
if (height != Height.NONE) faces[it] = height == Height.TALL
}
} else {
val fence = blockData as Fence
up = true
fence.faces.forEach { faces[it] = false }
}

return WallBlockData(material, up, faces)
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ enum class Direction(

companion object {

val cardinalPoints = values().filter { it.stepY == 0 }

fun of(blockFace: BlockFace): Direction = values().first { it.blockFace == blockFace }
fun of(axis: Axis): Direction = values().first { it.axis == axis }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ abstract class Part {

companion object {

fun createPart(data: ThreadSafeBlockData): Part =
fun createPart(data: AsyncBlockData): Part =
when {
data is StairBlockData -> StairPart(data)
data is SlabBlockData -> SlabPart(data)
Expand All @@ -82,6 +82,7 @@ abstract class Part {
data is SwitchBlockData -> SwitchPart(data)
data is DaylightDetectorBlockData -> DaylightDetectorPart(data)
data is SnowBlockData -> SnowPart(data)
data is WallBlockData -> WallPart(data)
data.material.isFence() -> FencePart(data as MultipleFacingBlockData)
data.material.isGlassPane() -> GlassPanePart(data as MultipleFacingBlockData)
data.material.isCrossMaterial() -> CrossPart(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.impl.TexturedElement
import de.studiocode.miniatureblocks.resourcepack.model.part.Part
Expand All @@ -9,7 +9,7 @@ import de.studiocode.miniatureblocks.util.point.Point3D

private const val CARPET_HEIGHT = 1.0 / 16.0

class CarpetPart(data: ThreadSafeBlockData) : Part() {
class CarpetPart(data: AsyncBlockData) : Part() {

private val blockTexture = BlockTexture.of(data.material)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.Texture
import de.studiocode.miniatureblocks.resourcepack.model.part.Part
Expand All @@ -10,7 +10,7 @@ import org.bukkit.Axis

class CrossPart(private val textures: Array<String>) : Part() {

constructor(data: ThreadSafeBlockData) : this(BlockTexture.of(data.material).textures)
constructor(data: AsyncBlockData) : this(BlockTexture.of(data.material).textures)

override val elements = listOf(CrossElement1(), CrossElement2())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package de.studiocode.miniatureblocks.resourcepack.model.part.impl
import de.studiocode.miniatureblocks.build.concurrent.DirectionalBlockData
import de.studiocode.miniatureblocks.build.concurrent.OrientableBlockData
import de.studiocode.miniatureblocks.build.concurrent.SnowableBlockData
import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.Direction
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.Texture
Expand All @@ -15,7 +15,7 @@ private val UV = Texture.UV(0.0, 0.0, 1.0, 1.0)
private val FROM = Point3D(0.0, 0.0, 0.0)
private val TO = Point3D(1.0, 1.0, 1.0)

class CubePart(data: ThreadSafeBlockData) : Part() {
class CubePart(data: AsyncBlockData) : Part() {

private val blockTexture = BlockTexture.of(data.material)
private val textures = blockTexture.textures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.DirectionalBlockData
import de.studiocode.miniatureblocks.build.concurrent.MultipleFacingBlockData
import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.Direction
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.Texture
Expand All @@ -12,7 +12,7 @@ import de.studiocode.miniatureblocks.util.point.Point3D

private const val SIZE = 1.0 / 16.0

class FlatPart(data: ThreadSafeBlockData) : Part() {
class FlatPart(data: AsyncBlockData) : Part() {

private val blockTexture = BlockTexture.of(data.material)
private val textures = blockTexture.textures
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.impl.TexturedElement
import de.studiocode.miniatureblocks.resourcepack.model.part.Part
Expand Down Expand Up @@ -33,7 +33,7 @@ private fun createCactusElement(texture: BlockTexture): Element {
)
}

class MiscPart(data: ThreadSafeBlockData) : Part() {
class MiscPart(data: AsyncBlockData) : Part() {

override val elements = ArrayList<Element>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.Texture
import de.studiocode.miniatureblocks.resourcepack.model.element.impl.SameTextureElement
Expand All @@ -15,7 +15,7 @@ private const val HALF_POT_SIZE = POT_SIZE / 2
private const val BASE_HEIGHT = 5.0 / 16.0
private const val RAIL_SIZE = 1.0 / 16.0

class PotPart(data: ThreadSafeBlockData) : Part() {
class PotPart(data: AsyncBlockData) : Part() {

private val textures = BlockTexture.of(data.material).textures
private val potTexture = textures[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.ThreadSafeBlockData
import de.studiocode.miniatureblocks.build.concurrent.AsyncBlockData
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.impl.TexturedElement
import de.studiocode.miniatureblocks.resourcepack.model.part.Part
Expand All @@ -9,7 +9,7 @@ import de.studiocode.miniatureblocks.util.point.Point3D

private const val PRESSURE_PLATE_HEIGHT = 1.0 / 16.0

class PressurePlatePart(data: ThreadSafeBlockData) : Part() {
class PressurePlatePart(data: AsyncBlockData) : Part() {

private val blockTexture = BlockTexture.of(data.material)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.studiocode.miniatureblocks.resourcepack.model.part.impl

import de.studiocode.miniatureblocks.build.concurrent.WallBlockData
import de.studiocode.miniatureblocks.resourcepack.model.Direction
import de.studiocode.miniatureblocks.resourcepack.model.element.Element
import de.studiocode.miniatureblocks.resourcepack.model.element.impl.TexturedElement
import de.studiocode.miniatureblocks.resourcepack.model.part.Part
import de.studiocode.miniatureblocks.resourcepack.texture.BlockTexture
import de.studiocode.miniatureblocks.util.point.Point3D

const val TALL_HEIGHT = 1.0
const val LOW_HEIGHT = 14.0 / 16.0
const val HALF_SIDE_WIDTH = 6.0 / 16.0 / 2.0

class WallPart(val data: WallBlockData) : Part() {

private val blockTexture = BlockTexture.of(data.material)
private val halfCenterSize = (if (data.up) 8.0 / 16.0 else 6.0 / 16.0) / 2
private val centerHeight = if (data.up || data.faces.all { (_, tall) -> tall }) TALL_HEIGHT else LOW_HEIGHT
private val sideLength = if (data.up) 4.0 / 16.0 else 5.0 / 16.0
override val elements = ArrayList<Element>()

init {
elements += createCenterElement()
data.faces.forEach { (blockFace, _) -> elements += createSideElement(Direction.of(blockFace)) }
}

private fun createCenterElement(): Element {
return TexturedElement(
Point3D(0.5 - halfCenterSize, 0.0, 0.5 - halfCenterSize),
Point3D(0.5 + halfCenterSize, centerHeight, 0.5 + halfCenterSize),
blockTexture
)
}

private fun createSideElement(direction: Direction): Element {
val element = TexturedElement(
Point3D(0.5 - HALF_SIDE_WIDTH, 0.0, 0.0),
Point3D(0.5 + HALF_SIDE_WIDTH, getHeight(direction), sideLength),
blockTexture
)

element.rotatePosAroundYAxis(direction.yRot)
return element
}

private fun getHeight(direction: Direction) = if (data.faces[direction.blockFace]!!) TALL_HEIGHT else LOW_HEIGHT

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class BlockTexture(materialName: String, val textures: Array<String>, val defaul
+ obj.get("custom").asJsonArray)
.map { gson.fromJson(it)!! })

textures.removeIf { it.material == null }
return textures
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.studiocode.miniatureblocks.util

import com.google.common.base.Preconditions
import de.studiocode.miniatureblocks.MiniatureBlocks
import net.lingala.zip4j.ZipFile
import java.io.File
import java.io.FileOutputStream
Expand Down Expand Up @@ -40,7 +41,7 @@ object FileUtils {
}

private fun listExtractableFiles(path: String): List<String> {
val zip = ZipFile(File("D:\\Users\\nicht\\Documents\\Development\\1.16.5 Testserver\\plugins\\MiniatureBlocks-0.10-SNAPSHOT-shaded.jar"))
val zip = ZipFile(MiniatureBlocks.INSTANCE.pluginFile)

return zip.fileHeaders
.filter { !it.isDirectory }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ fun Material.isGlassPane() = name.endsWith("GLASS_PANE")

fun Material.isFence() = name.endsWith("FENCE")

fun Material.isWall() = name.endsWith("WALL")

fun Material.isTraversable() = isAir || name == "WATER" || name == "LAVA"

object MaterialUtils {
Expand Down
Loading

0 comments on commit 0bacb73

Please sign in to comment.