Applying Constant Effects
FORMAT & MIN ENGINE VERSION 1.21.40
This tutorial assumes a basic understanding of blocks, including block states. Check out the blocks guide before starting.
This tutorial aims to show how to apply status effects to entities as long as these entities stand on the block.
Detecting Treaders
Block JSON
We will need to add a couple things to our code, first let's start with a state that will hold true
when stood on, and false
otherwise:
"states": {
"wiki:stood_on": [false, true]
}
Now we need to register our custom components to hook onto the stepOn
and stepOff
events:
"minecraft:custom_components": [
"wiki:detect_treaders"
]
Custom Component Script
import { BlockPermutation, GameMode, Player, world } from "@minecraft/server";
/** @type {import("@minecraft/server").BlockCustomComponent} */
const DetectTreadersBlockComponent = {
onStepOn({ entity, block }) {
if (entity instanceof Player && entity.getGameMode() === GameMode.creative) return;
block.setPermutation(
BlockPermutation.resolve(block.typeId, {
"wiki:stood_on": true,
})
);
},
onStepOff({ entity, block }) {
if (entity instanceof Player && entity.getGameMode() === GameMode.creative) return;
block.setPermutation(
BlockPermutation.resolve(block.typeId, {
"wiki:stood_on": false,
})
);
},
};
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
blockComponentRegistry.registerCustomComponent(
"wiki:detect_treaders",
DetectTreadersBlockComponent
);
});
Applying Effects to Treaders
Block JSON
We also need the block to tick in order to apply the desired effect every tick. For this, we'll use the permutations array so a custom component is only applied if the block is being stepped on:
"permutations": [
{
"condition": "q.block_state('wiki:stood_on')",
"components": {
"minecraft:custom_components": ["wiki:detect_treaders", "wiki:wither_treaders"],
"minecraft:tick": {
"interval_range": [1, 1],
"looping": true
}
}
}
]
Custom Component Script
Now, let's add our event that will give the entity the wither effect:
import { Entity, GameMode, Player, world } from "@minecraft/server";
/** @type {import("@minecraft/server").BlockCustomComponent} */
const WitherTreadersBlockComponent = {
onTick(event) {
const entities = event.dimension.getEntitiesAtBlockLocation(event.block.above().location);
entities.forEach((entity) => {
entity.addEffect("minecraft:wither", 2, { amplifier: 2 });
});
},
};
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
blockComponentRegistry.registerCustomComponent(
"wiki:wither_treaders",
WitherTreadersBlockComponent
);
});
And done! The code above will trigger the desired status effect as long as the entity is standing on a block.
Example JSON
Example Wither Block
{
"format_version": "1.21.40",
"minecraft:block": {
"description": {
"identifier": "wiki:wither_block",
"states": {
"wiki:stood_on": [false, true]
}
},
"components": {
"minecraft:loot": "loot_tables/empty.json",
"minecraft:map_color": "#181818",
"minecraft:geometry": "geometry.wither_block",
"minecraft:material_instances": {
"*": {
"texture": "wither_block"
}
},
"minecraft:custom_components": ["wiki:detect_treaders"]
},
"permutations": [
{
"condition": "q.block_state('wiki:stood_on')",
"components": {
"minecraft:custom_components": ["wiki:detect_treaders", "wiki:wither_treaders"],
"minecraft:tick": {
"interval_range": [1, 1],
"looping": true
}
}
}
]
}
}