Skip to content

Commit 34af68e

Browse files
authored
Merge pull request #807 from pylonmc/human/waila-suppliers
Waila Suppliers
2 parents af416af + fec90b6 commit 34af68e

7 files changed

Lines changed: 98 additions & 16 deletions

File tree

rebar/src/main/kotlin/io/github/pylonmc/rebar/block/BlockStorage.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ object BlockStorage : Listener {
647647
blocksBySchema.computeIfAbsent(phantomBlock.schema) { mutableListOf() }.add(phantomBlock)
648648
blocksByChunk[blockPos.chunk]!!.remove(block)
649649
blocksByChunk[blockPos.chunk]!!.add(phantomBlock)
650+
651+
RebarBlockPhantomEvent(block.block, block, phantomBlock).callEvent()
650652
}
651653

652654
@JvmSynthetic

rebar/src/main/kotlin/io/github/pylonmc/rebar/block/RebarBlock.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.github.pylonmc.rebar.util.position.BlockPosition
2525
import io.github.pylonmc.rebar.util.position.position
2626
import io.github.pylonmc.rebar.util.rebarKey
2727
import io.github.pylonmc.rebar.waila.WailaDisplay
28+
import io.github.pylonmc.rebar.waila.WailaSupplier
2829
import io.papermc.paper.datacomponent.DataComponentTypes
2930
import net.kyori.adventure.key.Key
3031
import org.bukkit.*
@@ -49,7 +50,7 @@ import org.bukkit.persistence.PersistentDataContainer
4950
*
5051
* @see BlockStorage
5152
*/
52-
open class RebarBlock private constructor(val block: Block) : Keyed {
53+
open class RebarBlock private constructor(val block: Block) : WailaSupplier, Keyed {
5354

5455
/**
5556
* All the data needed to create or load the block.
@@ -217,7 +218,7 @@ open class RebarBlock private constructor(val block: Block) : Keyed {
217218
*
218219
* @return the WAILA configuration, or null if WAILA should not be shown for this block.
219220
*/
220-
open fun getWaila(player: Player): WailaDisplay? {
221+
override fun getWaila(player: Player): WailaDisplay? {
221222
return WailaDisplay.of(this, player)
222223
}
223224

rebar/src/main/kotlin/io/github/pylonmc/rebar/block/interfaces/SimpleRebarMultiblock.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.pylonmc.rebar.block.interfaces
22

33
import io.github.pylonmc.rebar.block.BlockStorage
4+
import io.github.pylonmc.rebar.block.RebarBlock
45
import io.github.pylonmc.rebar.block.context.BlockCreateContext
56
import io.github.pylonmc.rebar.datatypes.RebarSerializers
67
import io.github.pylonmc.rebar.event.RebarBlockDeserializeEvent
@@ -287,7 +288,7 @@ interface SimpleRebarMultiblock : RebarMultiblock, GhostBlockHolderRebarBlock, E
287288
removeGhostBlock(getRotatedPosition(position))
288289
}
289290
for (position in components.keys) {
290-
Waila.addWailaOverride(getMultiblockBlock(position), this::getWaila)
291+
Waila.addWailaOverride(getMultiblockBlock(position), this as RebarBlock)
291292
}
292293
}
293294

rebar/src/main/kotlin/io/github/pylonmc/rebar/entity/RebarEntity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import io.github.pylonmc.rebar.event.RebarEntitySerializeEvent
1010
import io.github.pylonmc.rebar.registry.RebarRegistry
1111
import io.github.pylonmc.rebar.util.rebarKey
1212
import io.github.pylonmc.rebar.waila.WailaDisplay
13+
import io.github.pylonmc.rebar.waila.WailaSupplier
14+
import org.bukkit.Keyed
1315
import org.bukkit.NamespacedKey
1416
import org.bukkit.entity.Entity
1517
import org.bukkit.entity.Player
@@ -28,13 +30,15 @@ import org.bukkit.persistence.PersistentDataContainer
2830
* Rebar blocks. This is because it doesn't make sense for Rebar to manage spawning entities. However, your
2931
* entity must still have a load constructor that takes a single parameter of type [E].
3032
*/
31-
abstract class RebarEntity<out E: Entity>(val entity: E) {
33+
abstract class RebarEntity<out E: Entity>(val entity: E) : WailaSupplier, Keyed {
3234

33-
val key = entity.persistentDataContainer.get(rebarEntityKeyKey, RebarSerializers.NAMESPACED_KEY)
35+
@JvmField val key = entity.persistentDataContainer.get(rebarEntityKeyKey, RebarSerializers.NAMESPACED_KEY)
3436
?: throw IllegalStateException("Entity did not have a Rebar key; did you mean to call RebarEntity(NamespacedKey, Entity) instead of RebarEntity(Entity)?")
3537
val schema = RebarRegistry.ENTITIES.getOrThrow(key)
3638
val uuid = entity.uniqueId
3739

40+
override fun getKey() = key
41+
3842
constructor(key: NamespacedKey, entity: E): this(initialiseRebarEntity<E>(key, entity))
3943

4044
/**
@@ -45,7 +49,7 @@ abstract class RebarEntity<out E: Entity>(val entity: E) {
4549
*
4650
* @return the WAILA configuration, or null if WAILA should not be shown for this block.
4751
*/
48-
open fun getWaila(player: Player): WailaDisplay? = null
52+
override fun getWaila(player: Player): WailaDisplay? = null
4953

5054
/**
5155
* Returns the item that should be given when the entity is middle clicked.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.pylonmc.rebar.event
2+
3+
import io.github.pylonmc.rebar.block.PhantomBlock
4+
import io.github.pylonmc.rebar.block.RebarBlock
5+
import org.bukkit.block.Block
6+
import org.bukkit.event.Event
7+
import org.bukkit.event.HandlerList
8+
9+
/**
10+
* Called when a [RebarBlock] turns into a [PhantomBlock] due to errors or the command.
11+
*/
12+
class RebarBlockPhantomEvent(
13+
val block: Block,
14+
val rebarBlock: RebarBlock,
15+
val phantomBlock: PhantomBlock
16+
) : Event() {
17+
18+
override fun getHandlers(): HandlerList
19+
= handlerList
20+
21+
companion object {
22+
@JvmStatic
23+
val handlerList: HandlerList = HandlerList()
24+
}
25+
}

rebar/src/main/kotlin/io/github/pylonmc/rebar/waila/Waila.kt

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import io.github.pylonmc.rebar.config.RebarConfig
77
import io.github.pylonmc.rebar.datatypes.RebarSerializers
88
import io.github.pylonmc.rebar.entity.EntityStorage
99
import io.github.pylonmc.rebar.entity.RebarEntity
10+
import io.github.pylonmc.rebar.event.RebarBlockBreakEvent
11+
import io.github.pylonmc.rebar.event.RebarBlockPhantomEvent
12+
import io.github.pylonmc.rebar.event.RebarBlockUnloadEvent
1013
import io.github.pylonmc.rebar.event.RebarBlockWailaEvent
14+
import io.github.pylonmc.rebar.event.RebarEntityDeathEvent
15+
import io.github.pylonmc.rebar.event.RebarEntityUnloadEvent
1116
import io.github.pylonmc.rebar.event.RebarEntityWailaEvent
1217
import io.github.pylonmc.rebar.i18n.RebarArgument
1318
import io.github.pylonmc.rebar.util.breakProgress
@@ -184,7 +189,7 @@ class Waila private constructor(
184189
return
185190
}
186191

187-
var display = entityOverrides[targetEntity]?.invoke(player)
192+
var display = entityOverrides[targetEntity]?.getWaila(player)
188193
?: entity.let(EntityStorage::get)?.getWaila(player)
189194

190195
if (display == null && player.wailaConfig.vanillaWailaEnabled) {
@@ -221,7 +226,7 @@ class Waila private constructor(
221226
return
222227
}
223228

224-
var display = blockOverrides[targetBlock]?.invoke(player)
229+
var display = blockOverrides[targetBlock]?.getWaila(player)
225230
?: block.let(BlockStorage::get)?.getWaila(player)
226231

227232
if (!BlockStorage.isRebarBlock(block) && display == null && player.wailaConfig.vanillaWailaEnabled) {
@@ -265,8 +270,8 @@ class Waila private constructor(
265270
private val wailaKey = rebarKey("waila")
266271
private val wailas = mutableMapOf<UUID, Waila>()
267272

268-
private val blockOverrides = mutableMapOf<BlockPosition, (Player) -> WailaDisplay?>()
269-
private val entityOverrides = mutableMapOf<UUID, (Player) -> WailaDisplay?>()
273+
private val blockOverrides = mutableMapOf<BlockPosition, WailaSupplier>()
274+
private val entityOverrides = mutableMapOf<UUID, WailaSupplier>()
270275

271276
@JvmStatic
272277
fun getWaila(player: Player): Waila? {
@@ -353,8 +358,8 @@ class Waila private constructor(
353358
* old override will be replaced.
354359
*/
355360
@JvmStatic
356-
fun addWailaOverride(position: BlockPosition, provider: (Player) -> WailaDisplay?) {
357-
blockOverrides[position] = provider
361+
fun addWailaOverride(position: BlockPosition, supplier: WailaSupplier) {
362+
blockOverrides[position] = supplier
358363
}
359364

360365
/**
@@ -367,8 +372,8 @@ class Waila private constructor(
367372
* old override will be replaced.
368373
*/
369374
@JvmStatic
370-
fun addWailaOverride(block: Block, provider: (Player) -> WailaDisplay?)
371-
= addWailaOverride(block.position, provider)
375+
fun addWailaOverride(block: Block, supplier: WailaSupplier)
376+
= addWailaOverride(block.position, supplier)
372377

373378
/**
374379
* Adds a WAILA override for the given entity. This will always show the
@@ -379,8 +384,8 @@ class Waila private constructor(
379384
* old override will be replaced.
380385
*/
381386
@JvmStatic
382-
fun addWailaOverride(entity: Entity, provider: (Player) -> WailaDisplay?) {
383-
entityOverrides[entity.uniqueId] = provider
387+
fun addWailaOverride(entity: Entity, supplier: WailaSupplier) {
388+
entityOverrides[entity.uniqueId] = supplier
384389
}
385390

386391
/**
@@ -418,5 +423,41 @@ class Waila private constructor(
418423
private fun onPlayerQuit(event: PlayerQuitEvent) {
419424
removePlayer(event.player)
420425
}
426+
427+
private fun removeOverrides(block: RebarBlock) {
428+
blockOverrides.values.removeIf { it === block }
429+
entityOverrides.values.removeIf { it === block }
430+
}
431+
432+
private fun removeOverrides(entity: RebarEntity<*>) {
433+
blockOverrides.values.removeIf { it === entity }
434+
entityOverrides.values.removeIf { it === entity }
435+
}
436+
437+
@EventHandler(priority = EventPriority.MONITOR)
438+
private fun onBlockBreak(event: RebarBlockBreakEvent) {
439+
removeOverrides(event.rebarBlock)
440+
}
441+
442+
@EventHandler(priority = EventPriority.MONITOR)
443+
private fun onBlockUnload(event: RebarBlockUnloadEvent) {
444+
removeOverrides(event.rebarBlock)
445+
}
446+
447+
@EventHandler(priority = EventPriority.MONITOR)
448+
private fun onBlockPhantom(event: RebarBlockPhantomEvent) {
449+
removeOverrides(event.rebarBlock)
450+
}
451+
452+
@EventHandler(priority = EventPriority.MONITOR)
453+
private fun onEntityRemove(event: RebarEntityDeathEvent) {
454+
// TODO: this will need changed to RebarEntityRemoveEvent when my other PR is opened & merged
455+
removeOverrides(event.rebarEntity)
456+
}
457+
458+
@EventHandler(priority = EventPriority.MONITOR)
459+
private fun onEntityUnload(event: RebarEntityUnloadEvent) {
460+
removeOverrides(event.rebarEntity)
461+
}
421462
}
422463
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.pylonmc.rebar.waila
2+
3+
import org.bukkit.entity.Player
4+
5+
@FunctionalInterface
6+
fun interface WailaSupplier {
7+
fun getWaila(player: Player): WailaDisplay?
8+
}

0 commit comments

Comments
 (0)