-
Notifications
You must be signed in to change notification settings - Fork 56
Dispenser Behaviors
Add dispenser behaviors that don't overwrite everyone else's.
Vanilla's dispenser registry is one behavior per item, last registration wins. DispenserHelper
wraps whatever is already registered instead, so several mods can add behavior to the same item and
each falls through to the next when it doesn't apply. It also runs on datapack reload, so you can
use tags.
public static void init() {
RegHelper.addDynamicDispenserBehaviorRegistration(MyMod::registerDispenserBehaviors);
}
// runs on datapack reload, so tags are available here
public static void registerDispenserBehaviors(DispenserHelper.Event event) {
event.registerPlaceBlock(Items.EMERALD_BLOCK);
event.register(new SpawnDragonBehavior(Items.DRAGON_EGG));
}public static class SpawnDragonBehavior extends DispenserHelper.AdditionalDispenserBehavior {
protected SpawnDragonBehavior(Item item) {
super(item);
}
@Override
protected InteractionResultHolder<ItemStack> customBehavior(BlockSource source, ItemStack stack) {
BlockPos front = source.pos().relative(source.state().getValue(DispenserBlock.FACING));
if (source.level().getBlockState(front).isAir()) {
// ... do the thing
return InteractionResultHolder.success(stack);
}
// pass falls back to the previously registered behavior
return InteractionResultHolder.pass(stack);
}
}Returning pass is the important part: that's what keeps the chain intact.
Example: DispenserHelperExample
Basics Platform Helpers Registration Networking Events Configs Config Screen
Resources Runtime Resource Packs Texture Manipulation Resource Helpers Block Set API
Client Custom Models Item Rendering Rendered Textures Post Shaders GUI Toolkit Colors
World Block and Item Interfaces Additional Item Placements Improved Entities Fake Levels World Data Dispenser Behaviors
Utilities Codec Utilities Misc Helpers Commands
Datapacks Villagers Soft Fluids Map Markers Spawn Boxes Global Datapack Folder