Skip to content

Commit

Permalink
If region is left blank for events, then it triggers for all regions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Jan 2, 2025
1 parent 9faff96 commit da9fe24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ fun <T : Any> Var<T>.get(player: Player?, interactionContext: InteractionContext

class ConstVar<T : Any>(val value: T) : Var<T> {
override fun get(player: Player, interactionContext: InteractionContext?): T = value

override fun toString(): String {
return "ConstVar($value)"
}
}

class BackedVar<T : Any>(
Expand All @@ -65,6 +69,10 @@ class BackedVar<T : Any>(
val entry = ref.get() ?: throw IllegalStateException("Could not find variable entry, $ref")
return entry.get(VarContext(player, data, klass, interactionContext))
}

override fun toString(): String {
return "BackedVar(ref=$ref, data=$data, klass=$klass)"
}
}

class MappedVar<T : Any>(
Expand All @@ -74,6 +82,9 @@ class MappedVar<T : Any>(
override fun get(player: Player, interactionContext: InteractionContext?): T {
return mapper(player, variable.get(player, interactionContext))
}
override fun toString(): String {
return "MappedVar(variable=$variable, mapper=$mapper)"
}
}

fun <T : Any> Var<T>.map(mapper: (Player, T) -> T): Var<T> = MappedVar(this, mapper)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.typewritermc.core.entries.Query
import com.typewritermc.core.entries.Ref
import com.typewritermc.core.extension.annotations.Entry
import com.typewritermc.core.extension.annotations.EntryListener
import com.typewritermc.core.extension.annotations.Help
import com.typewritermc.core.interaction.context
import com.typewritermc.engine.paper.entry.TriggerableEntry
import com.typewritermc.engine.paper.entry.entries.EventEntry
Expand All @@ -25,11 +26,12 @@ class EnterRegionEventEntry(
override val id: String = "",
override val name: String = "",
override val triggers: List<Ref<TriggerableEntry>> = emptyList(),
@Help("If left blank, it will trigger for all regions")
val region: String = "",
) : EventEntry

@EntryListener(EnterRegionEventEntry::class)
fun onEnterRegions(event: RegionsEnterEvent, query: Query<EnterRegionEventEntry>) {
query.findWhere { it.region in event }.triggerAllFor(event.player, context())
query.findWhere { it.region.isBlank() || it.region in event }.triggerAllFor(event.player, context())
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.typewritermc.core.entries.Query
import com.typewritermc.core.entries.Ref
import com.typewritermc.core.extension.annotations.Entry
import com.typewritermc.core.extension.annotations.EntryListener
import com.typewritermc.core.extension.annotations.Help
import com.typewritermc.core.interaction.context
import com.typewritermc.engine.paper.entry.TriggerableEntry
import com.typewritermc.engine.paper.entry.entries.EventEntry
Expand All @@ -25,11 +26,12 @@ class ExitRegionEventEntry(
override val id: String = "",
override val name: String = "",
override val triggers: List<Ref<TriggerableEntry>> = emptyList(),
@Help("If left blank, it will trigger for all regions")
val region: String = "",
) : EventEntry

@EntryListener(ExitRegionEventEntry::class)
fun onExitRegions(event: RegionsExitEvent, query: Query<ExitRegionEventEntry>) {
query.findWhere { it.region in event }.triggerAllFor(event.player, context())
query.findWhere { it.region.isBlank() || it.region in event }.triggerAllFor(event.player, context())
}

0 comments on commit da9fe24

Please sign in to comment.