@@ -9,7 +9,6 @@ import io.github.pylonmc.rebar.block.RebarBlockSchema
99import io.github.pylonmc.rebar.datatypes.RebarSerializers
1010import io.github.pylonmc.rebar.entity.EntityStorage
1111import io.github.pylonmc.rebar.entity.RebarEntity
12- import io.github.pylonmc.rebar.entity.base.RebarInteractEntity
1312import io.github.pylonmc.rebar.entity.display.BlockDisplayBuilder
1413import io.github.pylonmc.rebar.entity.display.ItemDisplayBuilder
1514import io.github.pylonmc.rebar.entity.display.transform.TransformBuilder
@@ -19,10 +18,12 @@ import io.github.pylonmc.rebar.event.RebarBlockSerializeEvent
1918import io.github.pylonmc.rebar.event.RebarBlockUnloadEvent
2019import io.github.pylonmc.rebar.item.builder.ItemStackBuilder
2120import io.github.pylonmc.rebar.registry.RebarRegistry
21+ import io.github.pylonmc.rebar.util.getRelative
2222import io.github.pylonmc.rebar.util.position.ChunkPosition
2323import io.github.pylonmc.rebar.util.position.position
2424import io.github.pylonmc.rebar.util.rebarKey
2525import io.github.pylonmc.rebar.util.rotateVectorToFace
26+ import io.github.pylonmc.rebar.waila.Waila
2627import io.github.pylonmc.rebar.waila.WailaDisplay
2728import kotlinx.coroutines.delay
2829import net.kyori.adventure.text.Component
@@ -38,7 +39,6 @@ import org.bukkit.entity.ItemDisplay
3839import org.bukkit.entity.Player
3940import org.bukkit.event.EventHandler
4041import org.bukkit.event.Listener
41- import org.bukkit.event.player.PlayerInteractEntityEvent
4242import org.bukkit.persistence.PersistentDataContainer
4343import org.bukkit.util.Vector
4444import org.jetbrains.annotations.ApiStatus
@@ -89,6 +89,14 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
8989 interface MultiblockComponent {
9090 fun matches (block : Block ): Boolean
9191
92+ /* *
93+ * Sets [block] to a 'default' value which matches this MultiblockComponent.
94+ *
95+ * For example, if a MultiblockComponent can be grass or dirt, this should set
96+ * the block to either grass or dirt
97+ */
98+ fun placeDefaultBlock (block : Block )
99+
92100 companion object {
93101
94102 @JvmStatic
@@ -147,6 +155,12 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
147155
148156 override fun matches (block : Block ): Boolean = ! BlockStorage .isRebarBlock(block) && block.type in materials
149157
158+ override fun placeDefaultBlock (block : Block ) {
159+ if (block.type.isAir && ! BlockStorage .isRebarBlock(block)) {
160+ block.type = materials.first()
161+ }
162+ }
163+
150164 override fun spawnGhostBlock (block : Block ): UUID {
151165 val blockDataList = blockDataList()
152166 val display = BlockDisplayBuilder ()
@@ -215,6 +229,12 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
215229 return false
216230 }
217231
232+ override fun placeDefaultBlock (block : Block ) {
233+ if (block.type.isAir && ! BlockStorage .isRebarBlock(block)) {
234+ block.blockData = blockDatas.first()
235+ }
236+ }
237+
218238 override fun spawnGhostBlock (block : Block ): UUID {
219239 val stringDatas: List <String > = blockDatas.map { it.getAsString(true ) }
220240 val display = BlockDisplayBuilder ()
@@ -265,6 +285,10 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
265285 return false
266286 }
267287
288+ override fun placeDefaultBlock (block : Block ) {
289+ multiblockComponents.first().placeDefaultBlock(block)
290+ }
291+
268292 override fun spawnGhostBlocks (block : Block ): List <UUID > {
269293 var blockDisplay: BlockDisplay ? = null
270294 var itemDisplay: ItemDisplay ? = null
@@ -353,6 +377,12 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
353377
354378 override fun matches (block : Block ): Boolean = BlockStorage .get(block)?.schema?.key == key
355379
380+ override fun placeDefaultBlock (block : Block ) {
381+ if (block.type.isAir && ! BlockStorage .isRebarBlock(block)) {
382+ BlockStorage .placeBlock(block, key)
383+ }
384+ }
385+
356386 override fun spawnGhostBlock (block : Block ): UUID {
357387 val schema = schema()
358388 val display = ItemDisplayBuilder ()
@@ -377,6 +407,11 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
377407 */
378408 val components: Map <Vector3i , MultiblockComponent >
379409
410+ /* *
411+ * Automatically implemented by RebarBlock
412+ */
413+ fun getWaila (player : Player ): WailaDisplay ?
414+
380415 /* *
381416 * Sets the 'direction' we expect the multiblock to be built in. North is considered the default facing direction -
382417 * ie setFacing(BlockFace.NORTH) will preserve the original multiblock structure without rotating it.
@@ -471,6 +506,27 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
471506 val maxCorner: Vector3i
472507 get() = Vector3i (horizontalRadius, components.keys.maxOf { it.y }, horizontalRadius)
473508
509+ fun getMultiblockBlock (position : Vector3i ): Block {
510+ val direction = getMultiblockDirection()
511+ return if (direction != null ) {
512+ block.getRelative(rotateVectorToFace(position, direction))
513+ } else {
514+ block.getRelative(position)
515+ }
516+ }
517+
518+ fun getMultiblockComponent (position : Vector3i ) =
519+ BlockStorage .get(getMultiblockBlock(position))
520+
521+ fun <T > getMultiblockComponent (clazz : Class <T >, position : Vector3i ) =
522+ BlockStorage .getAs(clazz, getMultiblockBlock(position))
523+
524+ fun getMultiblockComponentOrThrow (position : Vector3i ) =
525+ getMultiblockComponent(position) ? : throw IllegalStateException (" There is no Rebar block at $position " )
526+
527+ fun <T > getMultiblockComponentOrThrow (clazz : Class <T >, position : Vector3i ) =
528+ getMultiblockComponent(clazz, position) ? : throw IllegalStateException (" There is no Rebar block at $position or it is not of type $clazz " )
529+
474530 override val chunksOccupied: Set <ChunkPosition >
475531 get() {
476532 val chunks = mutableSetOf<ChunkPosition >()
@@ -505,13 +561,19 @@ interface RebarSimpleMultiblock : RebarMultiblock, RebarEntityHolderBlock, Rebar
505561 EntityStorage .get(heldEntities[key]!! )!! .entity.remove()
506562 heldEntities.remove(key)
507563 }
564+ for (position in components.keys) {
565+ Waila .addWailaOverride(getMultiblockBlock(position), this ::getWaila)
566+ }
508567 }
509568
510569 @MustBeInvokedByOverriders
511570 override fun onMultiblockUnformed (partUnloaded : Boolean ) {
512571 if (! partUnloaded) {
513572 spawnGhostBlocks()
514573 }
574+ for (position in components.keys) {
575+ Waila .removeWailaOverride(getMultiblockBlock(position))
576+ }
515577 }
516578
517579 override fun isPartOfMultiblock (otherBlock : Block ): Boolean = validStructures().any {
0 commit comments