Block Actions¶
The fabricpy.actions module provides ready-made Java code snippets for
common gameplay effects that you can return from a block’s event handlers
(on_left_click, on_right_click, on_break).
Every function returns a plain Python string containing one or more Java statements. Combine multiple actions by returning a list from your hook method — the framework joins them automatically.
Quick Example¶
import fabricpy
from fabricpy.actions import apply_effect, play_sound, give_xp
class MagicBlock(fabricpy.Block):
def __init__(self):
super().__init__(
id="mymod:magic_block",
name="Magic Block",
item_group=fabricpy.item_group.BUILDING_BLOCKS,
loot_table=fabricpy.LootTable.drops_self("mymod:magic_block"),
)
def on_right_click(self):
return [
apply_effect("REGENERATION", 400, 1),
play_sound("PLAYER_LEVELUP"),
give_xp(50),
]
Replace Block¶
Swap the clicked block for a different one.
from fabricpy.actions import replace_block
def on_right_click(self):
return replace_block("DIAMOND_BLOCK")
- replace_block(block, *, pos_var='pos', world_var='world')[source]¶
Replace the block at the event position with another block.
- Parameters:
- Returns:
Java code that replaces the block.
- Return type:
Example:
replace_block("DIAMOND_BLOCK") # → world.setBlockAndUpdate(pos, Blocks.DIAMOND_BLOCK.defaultBlockState());
Teleport Player¶
Move the player to absolute or relative coordinates.
from fabricpy.actions import teleport_player
# Relative: teleport 10 blocks up
def on_right_click(self):
return teleport_player(0, 10, 0, relative=True)
# Absolute: teleport to world spawn
def on_left_click(self):
return teleport_player(0, 64, 0)
- teleport_player(x, y, z, *, relative=False, player_var='player', pos_var='pos')[source]¶
Teleport the player to a position.
- Parameters:
x (float) – X coordinate (absolute, or offset when relative is
True).y (float) – Y coordinate (absolute, or offset when relative is
True).z (float) – Z coordinate (absolute, or offset when relative is
True).relative (bool) – When
Truethe coordinates are offsets from the block position referenced by pos_var.player_var (str) – Name of the player variable in scope.
pos_var (str) – Name of the
BlockPosvariable in scope (used when relative isTrue).
- Returns:
Java code that teleports the player.
- Return type:
Example:
teleport_player(100, 200, 100) # absolute teleport_player(0, 10, 0, relative=True) # 10 blocks above the block
Launch Player¶
Apply a velocity impulse to the player (knock-back / launch upward).
from fabricpy.actions import launch_player
# Launch upward
def on_left_click(self):
return launch_player(dy=2.5)
# Launch in a direction
def on_right_click(self):
return launch_player(dx=1.0, dy=0.5, dz=1.0)
- launch_player(dx=0.0, dy=1.0, dz=0.0, *, player_var='player')[source]¶
Launch the player upward or apply knockback.
Adds to the player’s velocity and marks it for immediate client sync.
- Parameters:
- Returns:
Java code that launches the player.
- Return type:
Example:
launch_player(dy=2.0) # launch straight up launch_player(dx=3.0, dy=0.5, dz=0.0) # knockback sideways
Apply Potion / Status Effect¶
Grant a potion effect (by Vanilla MobEffects field name).
from fabricpy.actions import apply_effect
# Speed II for 30 seconds (600 ticks)
def on_right_click(self):
return apply_effect("SPEED", 600, 1)
# Night Vision for 1 minute (default amplifier 0)
def on_left_click(self):
return apply_effect("NIGHT_VISION", 1200)
- apply_effect(effect, duration=200, amplifier=0, *, player_var='player')[source]¶
Apply a potion / status effect to the player.
- Parameters:
effect (str) –
MobEffectsconstant name (Mojang mappings), e.g."SPEED","STRENGTH","REGENERATION","NIGHT_VISION","INVISIBILITY","JUMP_BOOST","SLOW_FALLING","POISON","FIRE_RESISTANCE","LUCK".duration (int) – Duration in ticks (20 ticks = 1 second). Defaults to 200 (10 seconds).
amplifier (int) – Effect amplifier (0 = level I, 1 = level II, …).
player_var (str) – Name of the player variable in scope.
- Returns:
Java code that applies the effect.
- Return type:
Example:
apply_effect("SPEED", duration=600, amplifier=1)
Play Sound¶
Play a Vanilla sound event at the block position.
from fabricpy.actions import play_sound
def on_right_click(self):
return play_sound("ENDER_DRAGON_GROWL")
- play_sound(sound, volume=1.0, pitch=1.0, *, source='BLOCKS', pos_var='pos', world_var='world')[source]¶
Play a sound at the block position.
- Parameters:
sound (str) –
SoundEventsconstant name (Mojang mappings — without theENTITY_/BLOCK_/ITEM_prefix), e.g."LIGHTNING_BOLT_THUNDER","ANVIL_LAND","EXPERIENCE_ORB_PICKUP","ENDER_DRAGON_GROWL".volume (float) – Sound volume (1.0 = normal).
pitch (float) – Sound pitch (1.0 = normal, higher = faster).
source (str) –
SoundSourceconstant:"BLOCKS","PLAYERS","HOSTILE","NEUTRAL","MASTER", etc.pos_var (str) – Name of the
BlockPosvariable in scope.world_var (str) – Name of the
Levelvariable in scope.
- Returns:
Java code that plays the sound.
- Return type:
Note
Use Mojang-mapped field names (without
ENTITY_/BLOCK_/ITEM_prefixes). A handful ofSoundEventsfields areHolder<SoundEvent>rather than plainSoundEvent(e.g.NOTE_BLOCK_PLING,UI_BUTTON_CLICK). These are not compatible with this helper — use a plainSoundEventfield instead (the vast majority).Example:
play_sound("ANVIL_LAND", volume=2.0, pitch=1.5)
Summon Lightning¶
Strike lightning at the block position (server-side only).
from fabricpy.actions import summon_lightning
def on_break(self):
return summon_lightning()
Drop Items¶
Spawn an item stack on the ground at the block position.
from fabricpy.actions import drop_item
def on_left_click(self):
return [
drop_item("DIAMOND", count=3),
drop_item("EMERALD"),
]
Place Fire / Extinguish Area¶
Set fire at the block position or remove fire blocks in a cube around it.
from fabricpy.actions import place_fire, extinguish_area
def on_right_click(self):
return place_fire()
def on_break(self):
return extinguish_area(radius=5)
- place_fire(*, above=True, pos_var='pos', world_var='world')[source]¶
Place fire at or above the block position.
Only places fire if the target position is currently air.
- Parameters:
- Returns:
Java code that places fire.
- Return type:
Example:
place_fire() # fire above the block place_fire(above=False) # fire at the block position
Give / Remove XP¶
Add or subtract experience points from the player.
from fabricpy.actions import give_xp, remove_xp
def on_right_click(self):
return give_xp(100)
def on_left_click(self):
return remove_xp(50)
- give_xp(amount, *, player_var='player')[source]¶
Give experience points to the player.
- Parameters:
- Returns:
Java code that gives XP.
- Return type:
Example:
give_xp(100)
Damage / Heal Nearby Entities¶
Affect all LivingEntity instances within a radius of the block.
from fabricpy.actions import damage_nearby, heal_nearby
def on_right_click(self):
return damage_nearby(8.0, radius=10.0)
def on_break(self):
return heal_nearby(4.0, radius=5.0)
- damage_nearby(amount, radius=5.0, *, exclude_player=True, pos_var='pos', world_var='world', player_var='player')[source]¶
Damage nearby living entities with magic damage.
- Parameters:
amount (float) – Damage amount (half-hearts).
radius (float) – Search radius in blocks (default 5).
exclude_player (bool) – If
True(default), the triggering player is excluded from the damage.pos_var (str) – Name of the
BlockPosvariable in scope.world_var (str) – Name of the
Levelvariable in scope.player_var (str) – Name of the player variable in scope.
- Returns:
Java code that damages nearby entities.
- Return type:
Example:
damage_nearby(6.0, radius=8.0)
Delayed Action (Timer)¶
Schedule a block of Java code to run after a delay (in ticks, 20 = 1 second).
from fabricpy.actions import delayed_action, give_xp
# Give XP 3 seconds after right-click
def on_right_click(self):
return delayed_action(give_xp(100), ticks=60)
- delayed_action(action_code, ticks=20, *, world_var='world')[source]¶
Schedule an action to run after a delay.
Wraps the given Java code in a
TickTaskscheduled on the server, guarded by aServerLevelinstanceof check.- Parameters:
- Returns:
Java code that schedules the delayed action.
- Return type:
Example:
delayed_action(give_xp(100), ticks=40) # give XP after 2 seconds delayed_action(summon_lightning(), ticks=60)
Sculk Sensor Game Event¶
Emit a GameEvent that sculk sensors can detect.
from fabricpy.actions import sculk_event
def on_break(self):
return sculk_event("LIGHTNING_STRIKE")
- sculk_event(event, *, pos_var='pos', world_var='world', player_var='player')[source]¶
Emit a game event that sculk sensors can detect.
- Parameters:
event (str) –
GameEventconstant name, e.g."BLOCK_CHANGE","BLOCK_DESTROY","EXPLODE","STEP","ENTITY_SHAKE","NOTE_BLOCK_PLAY","INSTRUMENT_PLAY","LIGHTNING_STRIKE".pos_var (str) – Name of the
BlockPosvariable in scope.world_var (str) – Name of the
Levelvariable in scope.player_var (str) – Name of the player variable in scope (emitter).
- Returns:
Java code that emits the game event.
- Return type:
Example:
sculk_event("BLOCK_CHANGE") sculk_event("EXPLODE")
Composing Actions¶
All action functions return plain Java strings. Return them as a list to combine multiple actions in a single handler:
from fabricpy.actions import (
apply_effect, give_xp, play_sound, summon_lightning,
)
from fabricpy.message import send_message
def on_right_click(self):
return [
send_message("Let the ritual begin!"),
apply_effect("FIRE_RESISTANCE", 600),
summon_lightning(),
give_xp(50),
play_sound("WITHER_SPAWN"),
]
All Java imports are detected automatically — you never need to manage them yourself.
Full Example¶
See examples/block_actions.py for a complete runnable example
that demonstrates every action in a single mod.