Creating Events
Setup
The events API lets you easily create your own events by inheriting from the BaseEvent class.
package com.example.mymod.events
import dev.wendigodrip.thebrokenscript.api.event.BaseEventimport net.minecraft.world.entity.Entityimport net.minecraft.world.level.Levelimport 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... }}
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;
public class MyEvent extends BaseEvent { public MyEvent() { super(/* weight = */ 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 public void execute(Level level, Entity entity, Vec3 pos) { // 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:
package com.example.mymod
import net.neoforged.fml.common.Modimport thedarkcolour.kotlinforforge.neoforge.forge.MOD_BUSimport dev.wendigodrip.thebrokenscript.api.registry.RegistryWrapperimport 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()! }}
package com.example.mymod;
import net.neoforged.fml.ModContainer;import net.neoforged.fml.common.Mod;import net.neoforged.bus.api.IEventBus;import dev.wendigodrip.thebrokenscript.api.registry.RegistryWrapper;import com.example.mymod.events.MyEvent;
@Mod(MyMod.MOD_ID)public class MyMod { public static final String MOD_ID = "my_mod"; public static final RegistryWrapper REGISTRAR = new RegistryWrapper(MOD_ID);
public MyMod(IEventBus bus, ModContainer mod) { // Your code here...
// [ ID ] [Constructor] REGISTRAR.event("my_event", MyEvent::new); REGISTRAR.setup(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!