Skip to content

Commit

Permalink
Add events supported: 'FLUID_SCHEDULE_TICK', 'FLUID_SCHEDULE_TICKED',…
Browse files Browse the repository at this point in the history
… 'BLOCK_SCHEDULE_TICK', 'BLOCK_SCHEDULE_TICKED'.

Fix script unresolved reference: 'ENTITY_TICK', 'ENTITY_TICKED'
Add event context args: 'RANDOM', 'SCHEDULE_TICK_VIEW', 'FLUID_STATE'.
Add method 'preRequest' in 'ConiumEventContextBuilder', used to make 'presage' trigger.
Methods 'request' in 'ConiumEventContextBuilder' can input two lambdas now, first is 'arise', seconds is 'presage'.
  • Loading branch information
cao-awa committed Nov 20, 2024
1 parent df18182 commit 50851b7
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 25 deletions.
38 changes: 21 additions & 17 deletions document/script/kotlin/event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

## Event types

| Key | Notes | Environment | Cancelable | Cascade events | Input instance |
|-------------------:|:--------------------------------------:|------------:|-----------:|--------------------------------------:|:---------------:|
| SERVER_TICK | Trigger in every server tick | SERVER | false | Too many events | MinecraftServer |
| SERVER_TICK_TAIL | Trigger in every server tick completed | SERVER | false | * | MinecraftServer |
| ITEM_USE_ON_BLOCK | Trigger when an item use on a block | ALL | true | * | Item |
| ITEM_USED_ON_BLOCK | Trigger when an item used 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<*> |
| Key | Notes | Environment | Cancelable | Cascade events | Input instance |
|----------------------:|:--------------------------------------:|------------:|-----------:|--------------------------------------:|:---------------:|
| SERVER_TICK | Trigger in every server tick | SERVER | false | Too many events | MinecraftServer |
| SERVER_TICK_TAIL | Trigger in every server tick completed | SERVER | false | * | MinecraftServer |
| ITEM_USE_ON_BLOCK | Trigger when an item use on a block | ALL | true | * | Item |
| ITEM_USED_ON_BLOCK | Trigger when an item used 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<*> |
| BLOCK_SCHEDULE_TICK | Trigger when schedule ticking block | SERVER | true | ```BLOCK_SCHEDULE_TICKED``` | Block |
| BLOCK_SCHEDULE_TICKED | Trigger when block ticked | SERVER | false | * | Block |
| FLUID_SCHEDULE_TICK | Trigger when schedule ticking fluid | SERVER | true | ```FLUID_SCHEDULE_TICKED``` | Fluid |
| FLUID_SCHEDULE_TICKED | Trigger when fluid ticked | SERVER | false | * | Fluid |

### Cascade events

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.cao.awa.conium.block.event.tick

import com.github.cao.awa.conium.event.ConiumEvent
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires
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.ParameterSelective
import com.github.cao.awa.conium.parameter.ParameterSelective5
import net.minecraft.block.AbstractBlock.AbstractBlockState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.random.Random
import net.minecraft.world.tick.ScheduledTickView

class ConiumBlockScheduleTickEvent : ConiumEvent<ParameterSelective5<Boolean, ServerWorld, BlockPos, AbstractBlockState, ScheduledTickView, Random>>() {
override fun requirement(): ConiumEventContext<out ParameterSelective> {
return requires(
ConiumEventArgTypes.SERVER_WORLD,
ConiumEventArgTypes.BLOCK_POS,
ConiumEventArgTypes.BLOCK_STATE,
ConiumEventArgTypes.SCHEDULE_TICK_VIEW,
ConiumEventArgTypes.RANDOM
).attach(
forever(ConiumEventType.BLOCK_SCHEDULE_TICK)
).arise { identity, world, pos, blockState, scheduler, random ->
noFailure(identity) {
it.arise(world, pos, blockState, scheduler, random)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.cao.awa.conium.block.event.tick

import com.github.cao.awa.conium.event.ConiumEvent
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires
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.ParameterSelective
import com.github.cao.awa.conium.parameter.ParameterSelective5
import net.minecraft.block.AbstractBlock.AbstractBlockState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.random.Random
import net.minecraft.world.tick.ScheduledTickView

class ConiumBlockScheduleTickedEvent : ConiumEvent<ParameterSelective5<Boolean, ServerWorld, BlockPos, AbstractBlockState, ScheduledTickView, Random>>() {
override fun requirement(): ConiumEventContext<out ParameterSelective> {
return requires(
ConiumEventArgTypes.SERVER_WORLD,
ConiumEventArgTypes.BLOCK_POS,
ConiumEventArgTypes.BLOCK_STATE,
ConiumEventArgTypes.SCHEDULE_TICK_VIEW,
ConiumEventArgTypes.RANDOM
).attach(
forever(ConiumEventType.BLOCK_SCHEDULE_TICKED)
).arise { identity, world, pos, blockState, scheduler, random ->
noFailure(identity) {
it.arise(world, pos, blockState, scheduler, random)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.cao.awa.conium.block.event.tick.fluid

import com.github.cao.awa.conium.event.ConiumEvent
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires
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.ParameterSelective
import com.github.cao.awa.conium.parameter.ParameterSelective5
import net.minecraft.block.AbstractBlock.AbstractBlockState
import net.minecraft.fluid.FluidState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.world.tick.ScheduledTickView

class ConiumFluidScheduleTickEvent : ConiumEvent<ParameterSelective5<Boolean, ServerWorld, BlockPos, AbstractBlockState, FluidState, ScheduledTickView>>() {
override fun requirement(): ConiumEventContext<out ParameterSelective> {
return requires(
ConiumEventArgTypes.SERVER_WORLD,
ConiumEventArgTypes.BLOCK_POS,
ConiumEventArgTypes.BLOCK_STATE,
ConiumEventArgTypes.FLUID_STATE,
ConiumEventArgTypes.SCHEDULE_TICK_VIEW
).attach(
forever(ConiumEventType.FLUID_SCHEDULE_TICK)
).arise { identity, world, pos, blockState, fluidState, scheduler ->
noFailure(identity) {
it.arise(world, pos, blockState, fluidState, scheduler)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.cao.awa.conium.block.event.tick.fluid

import com.github.cao.awa.conium.event.ConiumEvent
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires
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.ParameterSelective
import com.github.cao.awa.conium.parameter.ParameterSelective5
import net.minecraft.block.AbstractBlock.AbstractBlockState
import net.minecraft.fluid.FluidState
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.world.tick.ScheduledTickView

class ConiumFluidScheduleTickedEvent : ConiumEvent<ParameterSelective5<Boolean, ServerWorld, BlockPos, AbstractBlockState, FluidState, ScheduledTickView>>() {
override fun requirement(): ConiumEventContext<out ParameterSelective> {
return requires(
ConiumEventArgTypes.SERVER_WORLD,
ConiumEventArgTypes.BLOCK_POS,
ConiumEventArgTypes.BLOCK_STATE,
ConiumEventArgTypes.FLUID_STATE,
ConiumEventArgTypes.SCHEDULE_TICK_VIEW
).attach(
forever(ConiumEventType.FLUID_SCHEDULE_TICKED)
).arise { identity, world, pos, blockState, fluidState, scheduler ->
noFailure(identity) {
it.arise(world, pos, blockState, fluidState, scheduler)
}
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import com.github.cao.awa.conium.block.event.breaking.ConiumBreakingBlockEvent
import com.github.cao.awa.conium.block.event.breaking.ConiumBrokenBlockEvent
import com.github.cao.awa.conium.block.event.place.ConiumPlaceBlockEvent
import com.github.cao.awa.conium.block.event.place.ConiumPlacedBlockEvent
import com.github.cao.awa.conium.block.event.tick.ConiumBlockScheduleTickEvent
import com.github.cao.awa.conium.block.event.tick.ConiumBlockScheduleTickedEvent
import com.github.cao.awa.conium.block.event.tick.fluid.ConiumFluidScheduleTickEvent
import com.github.cao.awa.conium.block.event.tick.fluid.ConiumFluidScheduleTickedEvent
import com.github.cao.awa.conium.block.event.use.ConiumUseBlockEvent
import com.github.cao.awa.conium.block.event.use.ConiumUsedBlockEvent
import com.github.cao.awa.conium.entity.event.damage.ConiumEntityDamageEvent
Expand All @@ -14,9 +18,11 @@ import com.github.cao.awa.conium.entity.event.die.ConiumEntityDieEvent
import com.github.cao.awa.conium.entity.event.tick.ConiumEntityTickEvent
import com.github.cao.awa.conium.entity.event.tick.ConiumEntityTickedEvent
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder
import com.github.cao.awa.conium.event.server.tick.ConiumServerTickEvent
import com.github.cao.awa.conium.event.server.tick.ConiumServerTickTailEvent
import com.github.cao.awa.conium.event.trigger.ListTriggerable
import com.github.cao.awa.conium.event.type.ConiumEventArgTypes
import com.github.cao.awa.conium.event.type.ConiumEventType
import com.github.cao.awa.conium.item.event.use.ConiumItemUseOnBlockEvent
import com.github.cao.awa.conium.item.event.use.ConiumItemUsedOnBlockEvent
Expand Down Expand Up @@ -80,6 +86,18 @@ abstract class ConiumEvent<P : ParameterSelective> : ListTriggerable<P>() {
@JvmField
val entityDead = ConiumEntityDeadEvent()

@JvmField
val fluidScheduleTick = ConiumFluidScheduleTickEvent()

@JvmField
val fluidScheduleTicked = ConiumFluidScheduleTickedEvent()

@JvmField
val blockScheduleTick = ConiumBlockScheduleTickEvent()

@JvmField
val blockScheduleTicked = ConiumBlockScheduleTickedEvent()

/**
* Before event fires, create event context by requirements.
*
Expand Down Expand Up @@ -127,21 +145,43 @@ abstract class ConiumEvent<P : ParameterSelective> : ListTriggerable<P>() {
this.events[ConiumEventType.ENTITY_DIE] = this.entityDie
this.events[ConiumEventType.ENTITY_DEAD] = this.entityDead

this.events[ConiumEventType.FLUID_SCHEDULE_TICK] = this.fluidScheduleTick
this.events[ConiumEventType.FLUID_SCHEDULE_TICKED] = this.fluidScheduleTicked
this.events[ConiumEventType.BLOCK_SCHEDULE_TICK] = this.blockScheduleTick
this.events[ConiumEventType.BLOCK_SCHEDULE_TICKED] = this.blockScheduleTicked
}

fun clearEntitySubscribes() {
this.entityTick.clearSubscribes()
this.entityTicked.clearSubscribes()
this.entityDamage.clearSubscribes()
this.entityDamaged.clearSubscribes()
this.entityDie.clearSubscribes()
this.entityDead.clearSubscribes()
}

fun clearItemSubscribes() {
this.itemUseOnBlockEvent.clearSubscribes()
this.itemUsedOnBlockEvent.clearSubscribes()
}

fun clearServerTickSubscribes() {
this.serverTick.clearSubscribes()
}

fun clearBlockSubscribes() {
this.breakingBlock.clearSubscribes()
this.breakBlock.clearSubscribes()
this.brokenBlock.clearSubscribes()
this.placeBlock.clearSubscribes()
this.placedBlock.clearSubscribes()
this.useBlock.clearSubscribes()
this.usedBlock.clearSubscribes()

this.fluidScheduleTick.clearSubscribes()
this.fluidScheduleTicked.clearSubscribes()
this.blockScheduleTick.clearSubscribes()
this.blockScheduleTicked.clearSubscribes()
}
}

Expand Down
Loading

0 comments on commit 50851b7

Please sign in to comment.