-
Notifications
You must be signed in to change notification settings - Fork 0
Bootstrap Helpers
Butterfly API provides two small helpers for initialization code that should only run once:
BootstrapRunOnce
Bootstrap groups one or more initialization steps and prevents them from being executed more than once.
Create one with:
private static final Bootstrap BOOTSTRAP =
Bootstrap.create();Then run your initialization:
public static void init() {
BOOTSTRAP.run(
ModItems::init,
ModBlocks::init,
ModSounds::init
);
}The first call runs every supplied step.
Later calls do nothing.
BOOTSTRAP.hasRun();Returns true after the bootstrap successfully runs.
run(...) also returns a boolean:
boolean ran = BOOTSTRAP.run(
ModItems::init,
ModBlocks::init
);It returns:
true
when the initialization runs for the first time.
It returns:
false
when the bootstrap has already run.
RunOnce is the smaller version of the same idea.
Create one with:
private static final RunOnce INIT =
RunOnce.create();Then:
public static void init() {
INIT.run(() -> {
// initialization
});
}INIT.hasRun();Like Bootstrap, run(...) returns true when the action runs and false when it has already run.
Use Bootstrap when initialization naturally consists of multiple steps:
BOOTSTRAP.run(
ModItems::init,
ModBlocks::init,
ModEntities::init
);Use RunOnce when you simply need to protect one action:
INIT.run(ModItems::register);Both helpers are designed to prevent accidental duplicate initialization.