Skip to content

Commit

Permalink
Merge pull request #125 from Auxilor/playerless-triggers
Browse files Browse the repository at this point in the history
Changed libreforge to no longer be based around players
  • Loading branch information
WillFP authored Nov 21, 2023
2 parents 10e7318 + a7228fe commit f689851
Show file tree
Hide file tree
Showing 287 changed files with 2,162 additions and 844 deletions.
112 changes: 112 additions & 0 deletions core/src/main/kotlin/com/willfp/libreforge/Dispatcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.willfp.libreforge

import org.bukkit.Location
import org.bukkit.block.Block
import org.bukkit.entity.Entity
import java.util.UUID

/**
* A dispatcher represents an object that can execute effects, hold holders,
* and be used in conditions, for example a player, entity, block, or server.
*/
interface Dispatcher<T> {
/**
* The UUID of the dispatcher.
*/
val uuid: UUID

/**
* The dispatcher itself.
*/
val dispatcher: T

/**
* The location of the dispatcher.
*/
val location: Location?
}

/**
* Get the dispatcher as a specific type.
*/
inline fun <reified T> Dispatcher<*>.get(): T? {
return this.dispatcher as? T
}

/**
* Check if the dispatcher is a specific type.
*/
inline fun <reified T> Dispatcher<*>.isType(): Boolean {
return this.dispatcher is T
}

/**
* Run a block if the dispatcher is a specific type.
*/
inline fun <reified T> Dispatcher<*>.ifType(block: (T) -> Unit) {
val dispatcher = this.dispatcher as? T ?: return
return block(dispatcher)
}

/**
* A dispatcher for an entity.
*/
@JvmInline
value class EntityDispatcher(
override val dispatcher: Entity
) : Dispatcher<Entity> {
override val uuid
get() = dispatcher.uniqueId

override val location
get() = dispatcher.location
}

/**
* Convert an entity to a dispatcher.
*/
fun Entity.toDispatcher(): Dispatcher<Entity> = EntityDispatcher(this)

@JvmInline
value class BlockDispatcher(
override val dispatcher: Block
) : Dispatcher<Block> {
override val uuid
get() = UUID(0, 0)

override val location
get() = dispatcher.location
}

/**
* Convert a block to a dispatcher.
*/
fun Block.toDispatcher(): Dispatcher<Block> = BlockDispatcher(this)

/**
* A dispatcher for a location.
*/
@JvmInline
value class LocationDispatcher(
override val dispatcher: Location
) : Dispatcher<Location> {
override val uuid
get() = UUID(0, 0)

override val location
get() = dispatcher
}

/**
* Convert a location to a dispatcher.
*/
fun Location.toDispatcher(): Dispatcher<Location> = LocationDispatcher(this)

/**
* A dispatcher for the global scope.
*/
object GlobalDispatcher : Dispatcher<Unit> {
override val uuid = UUID(0, 0)
override val dispatcher = Unit
override val location = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@ object EffectCollisionFixer : Listener {
@EventHandler(priority = EventPriority.LOWEST)
fun clearOnQuit(event: PlayerQuitEvent) {
val player = event.player
for ((holder, effects) in player.providedActiveEffects) {
val dispatcher = player.toDispatcher()

for ((holder, effects) in dispatcher.providedActiveEffects) {
for (effect in effects) {
effect.disable(player, holder)
effect.disable(dispatcher, holder)
}
}

// Extra fix for pre-4.2.3
player.fixAttributes()

player.updateHolders()
player.purgePreviousHolders()
dispatcher.updateHolders()
dispatcher.purgePreviousHolders()
}

@EventHandler(priority = EventPriority.LOWEST)
fun scanOnJoin(event: PlayerJoinEvent) {
val player = event.player
val dispatcher = player.toDispatcher()

// Extra fix for pre-4.2.3
player.fixAttributes()

player.updateHolders()
dispatcher.updateHolders()

plugin.scheduler.run {
player.updateEffects()
dispatcher.updateEffects()
}
}

Expand Down
29 changes: 21 additions & 8 deletions core/src/main/kotlin/com/willfp/libreforge/Holder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,38 @@ interface ProvidedHolder {
operator fun component2() = provider

/**
* Get not met lines for a [player].
* Get not met lines for a [dispatcher].
*/
fun getNotMetLines(player: Player): List<String> {
fun getNotMetLines(dispatcher: Dispatcher<*>): List<String> {
val lines = mutableListOf<String>()

lines += holder.conditions.getNotMetLines(player, this)
lines += holder.effects.map { it.conditions }.flatMap { it.getNotMetLines(player, this) }
lines += holder.conditions.getNotMetLines(dispatcher, this)
lines += holder.effects.map { it.conditions }.flatMap { it.getNotMetLines(dispatcher, this) }

return lines
}

/**
* Get if the holder is showing any not met lines for a [player], or if any
* Get not met lines for a [player].
*/
fun getNotMetLines(player: Player): List<String> =
getNotMetLines(player.toDispatcher())

/**
* Get if the holder is showing any not met lines for a [dispatcher], or if any
* conditions are not met and have showNotMet set to true (e.g. for Enchantment Strikethrough).
*/
fun isShowingAnyNotMet(player: Player): Boolean {
return holder.conditions.isShowingAnyNotMet(player, this)
|| holder.effects.any { it.conditions.isShowingAnyNotMet(player, this) }
fun isShowingAnyNotMet(dispatcher: Dispatcher<*>): Boolean {
return holder.conditions.isShowingAnyNotMet(dispatcher, this)
|| holder.effects.any { it.conditions.isShowingAnyNotMet(dispatcher, this) }
}

/**
* Get if the holder is showing any not met lines for a [player], or if any
* conditions are not met and have showNotMet set to true (e.g. for Enchantment Strikethrough).
*/
fun isShowingAnyNotMet(player: Player): Boolean =
isShowingAnyNotMet(player.toDispatcher())
}

/**
Expand Down
Loading

0 comments on commit f689851

Please sign in to comment.