Skip to content

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.

build.gradle.kts
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.

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.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 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) }
}
}

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.

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) }
}
}

A sneaking player reaches the toggle via Next. Other players stop at the guard via Consume.

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.Key
import net.minestom.server.MinecraftServer
import org.everbuild.twaddle.stacked_behaviour.engine.loop.LoopingRuleChainApplicatorFactory
import org.everbuild.twaddle.stacked_behaviour_block.RuleBlockHandlerFactory
import 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)

Registration does not rewrite existing blocks. After another ruleset modification, compile and attach new handlers.

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) }
}
}

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.