Skip to content

PersistedCatenation

youyihj edited this page Jul 31, 2023 · 4 revisions

PersistedCatenation

@since 1.13.0

Persisted Catenation can be serialized and restarts when server starts next time.

Creates a persisted catenation

Call mods.zenutils.CatenationPersistence.registerPersistedCatenation(String key) to create a persisted catenation type. It returns a CatenationPersistenceEntryBuilder, which defines catenation tasks and necessary parameters to start the catenation.

Persisted Catenation can hold some serializable objects. These objects should be given when start the catenation. The catenation will be ran when all objects are ready. For example, if a catenation holds a player, it will be paused when the player logs out and restart when the player logs in next time. These objects can't be changed while running the catenation.

This behavior should be ran in script loading, rather than events. It is reloadable.

mods.zenutils.CatenationPersistence.registerPersistedCatenation("sendMessage")
    // defines catenation tasks
    // The function is a mods.zenutils.ICatenationFactory
    // Takes an IWorld, and returns a Catenation
    .setCatenationFactory(function(world) {
        return world.catenation()
            .sleep(200)
            .then(function(world, context) {
                // accesses objects the catenation holds
                context.getPlayer().sendMessage(context.getPersistedData().asString());
            })
            .start();
    })
    // defines what objects the catenation should handle
    .addPlayerHolder()
    .addDataHolder()
    // registers the persistent catenation type
    .register();

Runs a persisted catenation

Call mods.zenutils.CatenationPersistence.startPersistedCatenation(String key, IWorld world) or world.persistedCatenation(String key) start a persisted catenation. Since it takes an IWorld, the function only can be invoked in events. The method returns a PersistedCatenationStarter. Give serializable objects and start the catenation by it.

import crafttweaker.event.PlayerLoggedInEvent;

events.onPlayerLoggedIn(function(event as PlayerLoggedInEvent) {
    val world = event.player.world;
    if (!world.remote) {
        mods.zenutils.CatenationPersistence.startPersistedCatenation("sendMessage", world)
            // gives serializable objects the catenation should hold defined in CatenationPersistenceEntryBuilder
            .withPlayer(event.player)
            .withData("test")
            // starts the catenation, returns a Catenation
            .start();
    }
});

Available Serializable Objects

Type Define in builder Access in context Give in starter When is it ready Default Serialize Key
IPlayer addPlayerHolder(key) getPlayer(key) withPlayer(player, key) The player is in the world "player"
IEntity addEntityHolder(key) getEntity(key) withEntity(entity, key) The entity is alive "entity"
IBlockPos addPositionHolder(key) getPosition(key) withPosition(pos, key) The chunk at the position is loaded "pos"
IData addDataHolder(key) getPersistedData(key) withData(data, key) Always "data"
Clone this wiki locally