Skip to content

Creating Events

Setup

The events API lets you easily create your own events by inheriting from the BaseEvent class.

MyEvent.kt
package com.example.mymod.events
import dev.wendigodrip.thebrokenscript.api.event.BaseEvent
import net.minecraft.world.entity.Entity
import net.minecraft.world.level.Level
import net.minecraft.world.phys.Vec3
// BaseEvent(weight: Int)
class MyEvent : BaseEvent(1) {
// level: The level the event was executed in.
// entity: The entity the event was executed on.
// pos: The position of the entity, where the event was executed.
override fun execute(level: Level, entity: Entity, pos: Vec3) {
// Your code here...
}
}

There are a LOT of utilities in the base class, so I’d recommend taking a good look at the docs!

Weights

The weight property, which you need to specify when calling the super constructor, determines how often events are called.

All events have their own weights which determine how often an event is triggered. A higher number will mean the event is more frequent. The weight must be an integer, meaning you can’t use 0.25 or something like that.

All current events in The Broken Script itself use the weight of 1. This may change in the future.

Registration

Don’t forget to register the event:

MyMod.kt
package com.example.mymod
import net.neoforged.fml.common.Mod
import thedarkcolour.kotlinforforge.neoforge.forge.MOD_BUS
import dev.wendigodrip.thebrokenscript.api.registry.RegistryWrapper
import com.example.mymod.events.MyEvent
@Mod(MyMod.MOD_ID)
object MyMod {
const val MOD_ID: String = "my_mod"
val REGISTRAR = RegistryWrapper(MOD_ID)
init {
// Your code here...
// [ ID ] [Constructor]
REGISTRAR.event("my_event", ::MyEvent)
REGISTRAR.setup(MOD_BUS) // <- Make sure you register everything before calling setup()!
}
}

After registering, you can test your event using the /tbs dev event force [id] command, but make sure to enable cheats in the config first!