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 @@ -11,6 +11,7 @@ import io.github.pylonmc.rebar.entity.packet.BlockTextureEntity.Companion.SCALE_
import io.github.pylonmc.rebar.entity.packet.BlockTextureEntity.Companion.SCALE_DISTANCE_THRESHOLD
import io.github.pylonmc.rebar.util.IMMEDIATE_FACES
import io.github.pylonmc.rebar.util.delayTicks
import io.github.pylonmc.rebar.util.isChunkLoaded
import io.github.pylonmc.rebar.util.position.BlockPosition
import kotlinx.coroutines.launch
import net.minecraft.network.protocol.Packet
Expand Down Expand Up @@ -376,7 +377,7 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
var brightestLight = 0.toByte()
if (brightest !== ZERO_VECTOR) {
val delegateBlock = block.block.getRelative(brightest.x.toInt(), brightest.y.toInt(), brightest.z.toInt()) as? CraftBlock
if (delegateBlock?.blockState?.hasLightingData ?: false) {
if (delegateBlock != null && delegateBlock.isChunkLoaded && delegateBlock.blockState.hasLightingData) {
brightestLight = delegateBlock.lightLevel
if (brightestLight >= 15) return brightest
brightestFace = IMMEDIATE_FACES.firstOrNull { it.modX == brightest.x.toInt() && it.modY == brightest.y.toInt() && it.modZ == brightest.z.toInt() }
Expand All @@ -386,7 +387,7 @@ class BlockTextureEntityImpl : BlockTextureEntity, SyncedDataHolder {
for (face in IMMEDIATE_FACES) {
if (brightestFace == face) continue
val delegateBlock = block.block.getRelative(face) as? CraftBlock ?: continue
if (delegateBlock.blockState.hasLightingData) {
if (delegateBlock.isChunkLoaded && delegateBlock.blockState.hasLightingData) {
val light = delegateBlock.lightLevel
if (light > brightestLight) {
brightestLight = light
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ object BlockStorage : Listener {
} else if (block.schema.key.isFromAddon(addon)) {
RebarBlockSchema.schemaCache[block.block.position] = PhantomBlock.schema
RebarBlock.serialize(block, block.block.chunk.persistentDataContainer.adapterContext)?.let { pdc ->
PhantomBlock(pdc, block.schema.key, block.block)
PhantomBlock(pdc, block.schema.key, block.block, true)
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import java.util.UUID
class PhantomBlock(
val pdc: PersistentDataContainer,
val erroredBlockKey: NamespacedKey,
block: Block
block: Block,
val preventErrorEntity: Boolean = false
) : RebarBlock(block, pdc), BlockBreakRebarBlockHandler {

override var disableBlockTextureEntity: Boolean = true
Expand All @@ -62,20 +63,23 @@ class PhantomBlock(
}

init {
errorOutlineEntityId = block.world.spawn(block.location.toCenterLocation(), ItemDisplay::class.java) { display ->
display.setItemStack(ItemStackBuilder.of(Material.BARRIER).set(DataComponentTypes.ITEM_MODEL, PhantomBlock.key).build())
display.glowColorOverride = Color.RED
display.isGlowing = true
display.isPersistent = false
display.brightness = Display.Brightness(15, 15)
display.setTransformationMatrix(TransformBuilder().scale(1.001f).buildForItemDisplay())
}.uniqueId
if (!preventErrorEntity) {
errorOutlineEntityId = block.world.spawn(block.location.toCenterLocation(), ItemDisplay::class.java) { display ->
display.setItemStack(ItemStackBuilder.of(Material.BARRIER).set(DataComponentTypes.ITEM_MODEL, PhantomBlock.key).build())
display.glowColorOverride = Color.RED
display.isGlowing = true
display.isPersistent = false
display.brightness = Display.Brightness(15, 15)
display.setTransformationMatrix(TransformBuilder().scale(1.001f).buildForItemDisplay())
}.uniqueId
}
}

override fun onPostBlockBreak(context: BlockBreakContext) {
errorOutlineEntityId?.let { uuid ->
block.world.getEntity(uuid)?.remove()
}
errorOutlineEntityId = null
}

override fun getWaila(player: Player): WailaDisplay? {
Expand Down
28 changes: 19 additions & 9 deletions rebar/src/main/kotlin/io/github/pylonmc/rebar/block/RebarBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ open class RebarBlock private constructor(val block: Block) : Keyed {
/**
* Returns the item that should be used to display the block's texture.
*
* By default, returns the item with the same key as the block, marked with the
* [rebarBlockTextureEntityKey]. The item will also have custom model data with
* By default, returns the rebar item with the same key as the block, marked with the
* [rebarBlockTextureEntityKey], or if there is no matching rebar item, an item with
* the block material & key. The item will also have custom model data with
* the vanilla block state properties of the block, merged with any custom
* properties provided by the block. (see [getBlockTextureProperties])
* This allows resource packs to provide different models/textures for different
Expand All @@ -201,14 +202,23 @@ open class RebarBlock private constructor(val block: Block) : Keyed {
*
* @return the item that should be used to display the block's texture
*/
open fun getBlockTextureItem() = defaultItem?.getItemStack()?.let { ItemStackBuilder(it) }?.apply {
editPdc { it.set(rebarBlockTextureEntityKey, RebarSerializers.BOOLEAN, true) }
val properties = NmsAccessor.instance.getStateProperties(block, getBlockTextureProperties())
for ((property, value) in properties) {
addCustomModelDataString("$property=$value")
open fun getBlockTextureItem(): ItemStack? {
val builder = if (defaultItem != null) {
ItemStackBuilder.of(defaultItem.getItemStack())
} else {
ItemStackBuilder.of(schema.material)
.addCustomModelDataString(key.toString())
}
set(DataComponentTypes.ITEM_MODEL, Key.key("air"))
}?.build()

return builder.apply {
editPdc { it.set(rebarBlockTextureEntityKey, RebarSerializers.BOOLEAN, true) }
val properties = NmsAccessor.instance.getStateProperties(block, getBlockTextureProperties())
for ((property, value) in properties) {
addCustomModelDataString("$property=$value")
}
set(DataComponentTypes.ITEM_MODEL, Key.key("air"))
}.build()
}

/**
* WAILA is the text that shows up when looking at a block to tell you what the block is.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,17 @@ interface TickingRebarBlock {
private val dispatchers = mutableMapOf<RebarBlockSchema, CoroutineDispatcher>()

private fun startTicker(tickingBlock: TickingRebarBlock) {
if (tickingBlock.tickingData.job != null) {
tickingBlock.tickingData.job!!.cancel()
}

val rebarBlock = tickingBlock as RebarBlock
val dispatcher = dispatchers.getOrPut(rebarBlock.schema) {
if (tickingBlock.isAsync) Dispatchers.Default
else Rebar.mainThreadDispatcher
}
val scope = ChunkScope(Rebar.scope.coroutineContext.createChildContext(), rebarBlock.block.chunk.position)
tickingBlocks[tickingBlock]?.job = scope.launch(dispatcher) {
tickingBlock.tickingData.job = scope.launch(dispatcher) {
while (true) {
delayTicks(tickingBlock.tickInterval.toLong())
try {
Expand Down
Loading