@@ -7,7 +7,12 @@ import io.github.pylonmc.rebar.config.RebarConfig
77import io.github.pylonmc.rebar.datatypes.RebarSerializers
88import io.github.pylonmc.rebar.entity.EntityStorage
99import 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
1013import io.github.pylonmc.rebar.event.RebarBlockWailaEvent
14+ import io.github.pylonmc.rebar.event.RebarEntityDeathEvent
15+ import io.github.pylonmc.rebar.event.RebarEntityUnloadEvent
1116import io.github.pylonmc.rebar.event.RebarEntityWailaEvent
1217import io.github.pylonmc.rebar.i18n.RebarArgument
1318import 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}
0 commit comments