Skip to content

Commit

Permalink
The 'identity' of event context that input to 'arising' and 'presagin…
Browse files Browse the repository at this point in the history
…g' have actual type now.
  • Loading branch information
cao-awa committed Nov 18, 2024
1 parent 35d815b commit 11c543e
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 162 deletions.
31 changes: 16 additions & 15 deletions document/script/kotlin/event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

## Event types

| Key | Notes | Environment | Cancelable | Cascade events |
|------------------:|:-----------------------------------:|------------:|-----------:|--------------------------------------:|
| SERVER_TICK | Trigger in every server tick | SERVER | false | * |
| ITEM_USE_ON_BLOCK | Trigger when an item use on a block | ALL | true | * |
| BREAKING_BLOCK | Trigger when breaking block | ALL | true | ```BREAK_BLOCK``` ```BROKEN_BLOCK``` |
| BREAK_BLOCK | Trigger when broking block | SERVER | true | ```BROKEN_BLOCK``` |
| BROKEN_BLOCK | Trigger when broken block | ALL | false | * |
| PLACE_BLOCK | Trigger when placing block | ALL | true | ```PLACED_BLOCK``` |
| PLACED_BLOCK | Trigger when block placed | ALL | false | * |
| USE_BLOCK | Trigger when using block | ALL | true | ```USED_BLOCK``` |
| USED_BLOCK | Trigger when block used | ALL | false | * |
| ENTITY_DAMAGE | Trigger when entity damaging | ALL | true | ```ENTITY_DAMAGED``` |
| ENTITY_DAMAGED | Trigger when entity damaged | ALL | false | * |
| ENTITY_DIE | Trigger when entity dying | ALL | true | ```ENTITY_DEAD``` |
| ENTITY_DIED | Trigger when entity died | ALL | false | * |
| Key | Notes | Environment | Cancelable | Cascade events | Input instance |
|------------------:|:-----------------------------------:|------------:|-----------:|--------------------------------------:|:---------------:|
| SERVER_TICK | Trigger in every server tick | SERVER | false | * | MinecraftServer |
| ITEM_USE_ON_BLOCK | Trigger when an item use on a block | ALL | true | * | Item |
| BREAKING_BLOCK | Trigger when breaking block | ALL | true | ```BREAK_BLOCK``` ```BROKEN_BLOCK``` | Block |
| BREAK_BLOCK | Trigger when broking block | SERVER | true | ```BROKEN_BLOCK``` | Block |
| BROKEN_BLOCK | Trigger when broken block | ALL | false | * | Block |
| PLACE_BLOCK | Trigger when placing block | ALL | true | ```PLACED_BLOCK``` | Block |
| PLACED_BLOCK | Trigger when block placed | ALL | false | * | Block |
| USE_BLOCK | Trigger when using block | ALL | true | ```USED_BLOCK``` | Block |
| USED_BLOCK | Trigger when block used | ALL | false | * | Block |
| ENTITY_DAMAGE | Trigger when entity damaging | ALL | true | ```ENTITY_DAMAGED``` | EntityType<*> |
| ENTITY_DAMAGED | Trigger when entity damaged | ALL | false | * | EntityType<*> |
| ENTITY_DIE | Trigger when entity dying | ALL | true | ```ENTITY_DEAD``` | EntityType<*> |
| ENTITY_DIED | Trigger when entity died | ALL | false | * | EntityType<*> |

### Cascade events

Cascade events are one or more events that only can happen when the preceding event is successful.

For example, the ```BREAKING_BLOCK``` event occurs when a player starting mining a block, normally, it takes time for the player to destroy the block, and only then can ```BREAK_BLOCK``` event occur, followed by the ```BROKEN_BLOCK``` event.\
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import java.util.*

abstract class ConiumEvent<P : ParameterSelective> : ListTriggerable<P>() {
companion object {
private val events: MutableMap<ConiumEventType, ConiumEvent<*>> = CollectionFactor.hashMap()
private val foreverContext: MutableMap<ConiumEventType, MutableList<ConiumEventContext<*>>> = CollectionFactor.hashMap()
private val events: MutableMap<ConiumEventType<*>, ConiumEvent<*>> = CollectionFactor.hashMap()
private val foreverContext: MutableMap<ConiumEventType<*>, MutableList<ConiumEventContext<*>>> = CollectionFactor.hashMap()

@JvmField
val itemUseOnBlockEvent = ConiumItemUseOnBlockEvent()
Expand Down Expand Up @@ -70,20 +70,20 @@ abstract class ConiumEvent<P : ParameterSelective> : ListTriggerable<P>() {
* @param type the type of event
*/
@JvmStatic
fun request(type: ConiumEventType): ConiumEventContext<out ParameterSelective> {
fun request(type: ConiumEventType<*>): ConiumEventContext<out ParameterSelective> {
return this.events[type]!!.requirement()
}

@JvmStatic
fun <X : ConiumEvent<X>> findEvent(type: ConiumEventType): X {
fun <X : ConiumEvent<X>> findEvent(type: ConiumEventType<*>): X {
return this.events[type] as X
}

fun forever(eventType: ConiumEventType, context: ConiumEventContext<*>) {
fun forever(eventType: ConiumEventType<*>, context: ConiumEventContext<*>) {
this.foreverContext.computeIfAbsent(eventType) { CollectionFactor.arrayList() }.add(context)
}

fun forever(eventType: ConiumEventType): MutableList<ConiumEventContext<*>> = this.foreverContext[eventType] ?: Collections.emptyList()
fun forever(eventType: ConiumEventType<*>): MutableList<ConiumEventContext<*>> = this.foreverContext[eventType] ?: Collections.emptyList()

fun resetForever() {
this.foreverContext.clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.cao.awa.conium.event.context

import com.github.cao.awa.conium.event.ConiumEvent
import com.github.cao.awa.conium.event.type.ConiumEventArgTypes
import com.github.cao.awa.conium.event.type.ConiumEventType
import com.github.cao.awa.conium.parameter.*

Expand Down Expand Up @@ -89,27 +90,29 @@ object ConiumEventContextBuilder {

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun request(
eventType: ConiumEventType,
arising: ParameterSelective1<Boolean, Any> = ParameterSelective1 { _ -> true }
fun <I : Any> request(
eventType: ConiumEventType<I>,
arising: ParameterSelective1<Boolean, I> = ParameterSelective1 { _ -> true }
): ConiumEventContext<ParameterSelective1<Boolean, Any>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(true).lifecycle(DynamicArgsLifecycle.FOREVER)
)

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise {
(it as? I)?.let(arising::arise) ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1> request(
eventType: ConiumEventType,
fun <I, P1> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arising: ParameterSelective2<Boolean, Any, P1> = ParameterSelective2 { _, _ -> true }
arising: ParameterSelective2<Boolean, I, P1> = ParameterSelective2 { _, _ -> true }
): ConiumEventContext<ParameterSelective2<Boolean, Any, P1>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -120,18 +123,20 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1 ->
(i as? I)?.let { arising.arise(it, p1) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2> request(
eventType: ConiumEventType,
fun <I, P1, P2> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arising: ParameterSelective3<Boolean, Any, P1, P2> = ParameterSelective3 { _, _, _ -> true }
arising: ParameterSelective3<Boolean, I, P1, P2> = ParameterSelective3 { _, _, _ -> true }
): ConiumEventContext<ParameterSelective3<Boolean, Any, P1, P2>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -143,19 +148,21 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2 ->
(i as? I)?.let { arising.arise(it, p1, p2) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2, P3> request(
eventType: ConiumEventType,
fun <I, P1, P2, P3> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>,
arising: ParameterSelective4<Boolean, Any, P1, P2, P3> = ParameterSelective4 { _, _, _, _ -> true }
arising: ParameterSelective4<Boolean, I, P1, P2, P3> = ParameterSelective4 { _, _, _, _ -> true }
): ConiumEventContext<ParameterSelective4<Boolean, Any, P1, P2, P3>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -168,20 +175,22 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2, p3 ->
(i as? I)?.let { arising(it, p1, p2, p3) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2, P3, P4> request(
eventType: ConiumEventType,
fun <I, P1, P2, P3, P4> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>,
arg4: DynamicArgType<P4>,
arising: ParameterSelective5<Boolean, Any, P1, P2, P3, P4> = ParameterSelective5 { _, _, _, _, _ -> true }
arising: ParameterSelective5<Boolean, I, P1, P2, P3, P4> = ParameterSelective5 { _, _, _, _, _ -> true }
): ConiumEventContext<ParameterSelective5<Boolean, Any, P1, P2, P3, P4>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -195,21 +204,23 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2, p3, p4 ->
(i as? I)?.let { arising.arise(it, p1, p2, p3, p4) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2, P3, P4, P5> request(
eventType: ConiumEventType,
fun <I, P1, P2, P3, P4, P5> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>,
arg4: DynamicArgType<P4>,
arg5: DynamicArgType<P5>,
arising: ParameterSelective6<Boolean, Any, P1, P2, P3, P4, P5> = ParameterSelective6 { _, _, _, _, _, _ -> true }
arising: ParameterSelective6<Boolean, I, P1, P2, P3, P4, P5> = ParameterSelective6 { _, _, _, _, _, _ -> true }
): ConiumEventContext<ParameterSelective6<Boolean, Any, P1, P2, P3, P4, P5>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -224,22 +235,24 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2, p3, p4, p5 ->
(i as? I)?.let { arising(it, p1, p2, p3, p4, p5) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2, P3, P4, P5, P6> request(
eventType: ConiumEventType,
fun <I, P1, P2, P3, P4, P5, P6> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>,
arg4: DynamicArgType<P4>,
arg5: DynamicArgType<P5>,
arg6: DynamicArgType<P6>,
arising: ParameterSelective7<Boolean, Any, P1, P2, P3, P4, P5, P6> = ParameterSelective7 { _, _, _, _, _, _, _ -> true }
arising: ParameterSelective7<Boolean, I, P1, P2, P3, P4, P5, P6> = ParameterSelective7 { _, _, _, _, _, _, _ -> true }
): ConiumEventContext<ParameterSelective7<Boolean, Any, P1, P2, P3, P4, P5, P6>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -255,23 +268,25 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2, p3, p4, p5, p6 ->
(i as? I)?.let { arising(it, p1, p2, p3, p4, p5, p6) } ?: true
}

return context
}

@JvmStatic
@SuppressWarnings("UNCHECKED_CAST")
fun <P1, P2, P3, P4, P5, P6, P7> request(
eventType: ConiumEventType,
fun <I, P1, P2, P3, P4, P5, P6, P7> request(
eventType: ConiumEventType<I>,
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>,
arg4: DynamicArgType<P4>,
arg5: DynamicArgType<P5>,
arg6: DynamicArgType<P6>,
arg7: DynamicArgType<P7>,
arising: ParameterSelective8<Boolean, Any, P1, P2, P3, P4, P5, P6, P7> = ParameterSelective8 { _, _, _, _, _, _, _, _ -> true }
arising: ParameterSelective8<Boolean, I, P1, P2, P3, P4, P5, P6, P7> = ParameterSelective8 { _, _, _, _, _, _, _, _ -> true }
): ConiumEventContext<ParameterSelective8<Boolean, Any, P1, P2, P3, P4, P5, P6, P7>> {
val context = ConiumEventContext(
DynamicArgsBuilder.requires(
Expand All @@ -288,7 +303,9 @@ object ConiumEventContextBuilder {

ConiumEvent.forever(eventType, context)

context.arise(arising)
context.arise { i, p1, p2, p3, p4, p5, p6, p7 ->
(i as? I)?.let { arising(it, p1, p2, p3, p4, p5, p6, p7) } ?: true
}

return context
}
Expand Down
Loading

0 comments on commit 11c543e

Please sign in to comment.