Stacked behavior: Copper trapdoor
Start with a copper trapdoor that toggles on interaction. Then insert a rule that requires sneaking, without rewriting the toggle rule. This runs inside an existing Minestom server; lifecycle ownership is optional.
First configure the public Maven repository.
The examples use version 0.1.0; use the same release version for all Twaddle modules.
dependencies { implementation("org.everbuild.twaddle:twaddle-stacked-behaviour-block:0.1.0")}The block module includes the general twaddle-stacked-behaviour API. For other domains, depend on
org.everbuild.twaddle:twaddle-stacked-behaviour:0.1.0 directly.
Create a ruleset
Section titled “Create a ruleset”A ruleset maps subjects to behaviours. Each behaviour binds activities to ordered rule chains. Here, the subject is a block ID and the activity is interaction.
import net.minestom.server.instance.block.Blockimport org.everbuild.twaddle.stacked_behaviour.Ruleimport org.everbuild.twaddle.stacked_behaviour.RuleResultimport org.everbuild.twaddle.stacked_behaviour_block.ruleset.BlockRulesetimport org.everbuild.twaddle.stacked_behaviour_block.type.InteractionActivityContextimport org.everbuild.twaddle.stacked_behaviour_block.type.interact
val ToggleTrapdoor = Rule<InteractionActivityContext>("example:toggle_trapdoor") { context -> val open = context.block.getProperty("open") == "true" context.replaceBlock(context.block.withProperty("open", (!open).toString())) RuleResult.Consume}val ruleset = BlockRuleset.build { blocks(Block.COPPER_TRAPDOOR) { interact.append { install(ToggleTrapdoor) } }}import kotlin.Unit;import net.minestom.server.instance.block.Block;import org.everbuild.twaddle.stacked_behaviour.Rule;import org.everbuild.twaddle.stacked_behaviour.RuleResult;import org.everbuild.twaddle.stacked_behaviour_block.ruleset.BlockRuleset;import org.everbuild.twaddle.stacked_behaviour_block.type.InteractionActivityContext;import static org.everbuild.twaddle.stacked_behaviour_block.type.InteractionActivityKt.getInteractionActivity;
var toggleTrapdoor = Rule.<InteractionActivityContext>rule("example:toggle_trapdoor", context -> { boolean open = "true".equals(context.getBlock().getProperty("open")); context.replaceBlock(context.getBlock().withProperty("open", Boolean.toString(!open))); return RuleResult.consume();});var ruleset = BlockRuleset.build(editor -> editor.blocks(behaviour -> { behaviour.activity(getInteractionActivity()).append(chain -> { chain.install(toggleTrapdoor); return Unit.INSTANCE; });}, Block.COPPER_TRAPDOOR));Changing a property on the existing block preserves its handler and other data. Consume ends the chain and tells
Minestom that interaction was handled, preventing normal item use afterward.
Java ruleset callbacks accept Consumer; chain-edit callbacks currently return Kotlin’s Unit.INSTANCE.
Modify existing rules
Section titled “Modify existing rules”Insert a guard before the toggle. modify returns a new ruleset; the original stays unchanged.
val RequireSneaking = Rule<InteractionActivityContext>("example:require_sneaking") { if (it.player.isSneaking) RuleResult.Next else RuleResult.Consume}val modified = ruleset.modify { blocks(Block.COPPER_TRAPDOOR) { interact.before(ToggleTrapdoor) { install(RequireSneaking) } }}var requireSneaking = Rule.<InteractionActivityContext>rule("example:require_sneaking", context -> context.getPlayer().isSneaking() ? RuleResult.next() : RuleResult.consume());var modified = ruleset.modify(editor -> editor.blocks(behaviour -> { behaviour.activity(getInteractionActivity()).before(toggleTrapdoor, chain -> { chain.install(requireSneaking); return Unit.INSTANCE; });}, Block.COPPER_TRAPDOOR));A sneaking player reaches the toggle via Next. Other players stop at the guard via Consume.
Register handlers
Section titled “Register handlers”After Minestom initialization, compile the modified ruleset. Register each handler by its ID before loading saved
blocks that reference it, and attach the handler to new blocks. Here, instance is an existing instance.
import net.kyori.adventure.key.Keyimport net.minestom.server.MinecraftServerimport org.everbuild.twaddle.stacked_behaviour.engine.loop.LoopingRuleChainApplicatorFactoryimport org.everbuild.twaddle.stacked_behaviour_block.RuleBlockHandlerFactoryimport org.everbuild.twaddle.stacked_behaviour_block.createHandlers
val handlers = modified.createHandlers( LoopingRuleChainApplicatorFactory(), RuleBlockHandlerFactory,) { blockKey -> Key.key("example", blockKey.value()) }handlers.values.forEach { handler -> MinecraftServer.getBlockManager().registerHandler(handler.key) { handler }}
instance.loadChunk(0, 0).join()val trapdoor = Block.COPPER_TRAPDOOR.withHandler(handlers.getValue(Block.COPPER_TRAPDOOR.key()))instance.setBlock(0, 42, 0, trapdoor)import net.kyori.adventure.key.Key;import net.minestom.server.MinecraftServer;import org.everbuild.twaddle.stacked_behaviour.engine.loop.LoopingRuleChainApplicatorFactory;import org.everbuild.twaddle.stacked_behaviour_block.RuleBlockHandlerFactory;import static org.everbuild.twaddle.stacked_behaviour_block.RuleBlockHandlerKt.createHandlers;
var handlers = createHandlers(modified, new LoopingRuleChainApplicatorFactory(), RuleBlockHandlerFactory.INSTANCE, blockKey -> Key.key("example", blockKey.value()));handlers.values().forEach(handler -> MinecraftServer.getBlockManager().registerHandler(handler.getKey(), () -> handler));
instance.loadChunk(0, 0).join();var trapdoor = Block.COPPER_TRAPDOOR.withHandler(handlers.get(Block.COPPER_TRAPDOOR.key()));instance.setBlock(0, 42, 0, trapdoor);Registration does not rewrite existing blocks. After another ruleset modification, compile and attach new handlers.
Change what later rules see
Section titled “Change what later rules see”See rule results for the general execution contract.
Cascade changes what later rules see. For example, this rule presents the trapdoor as closed to the next rule,
insert it before the toggle to always request opening it:
val TreatAsClosed = Rule<InteractionActivityContext>("example:treat_as_closed") { context -> RuleResult.cascade(context.copy(block = context.block.withProperty("open", "false")))}val alwaysOpen = modified.modify { blocks(Block.COPPER_TRAPDOOR) { interact.before(ToggleTrapdoor) { install(TreatAsClosed) } }}var treatAsClosed = Rule.<InteractionActivityContext>rule("example:treat_as_closed", context -> RuleResult.cascade(context.copy( context.getBlock().withProperty("open", "false"), context.getInstance(), context.getBlockFace(), context.getBlockPosition(), context.getCursorPosition(), context.getPlayer(), context.getHand(), context.getTagHandler())));var alwaysOpen = modified.modify(editor -> editor.blocks(behaviour -> { behaviour.activity(getInteractionActivity()).before(toggleTrapdoor, chain -> { chain.install(treatAsClosed); return Unit.INSTANCE; });}, Block.COPPER_TRAPDOOR));Cascading alone does not change the world or dispatch another activity. A later rule must request the world change.
The applicator returns ruleResult and finalContext. Missing activities fall through with Next.
Continue with Stacked behaviour for domains, activities, chain editing, and configuration.