Example Project
This is a small example project guide, showing only how to use the API in your classes.
NOTE: There are also example project repositories available. Please read the Overview page.
Simple Variant Example
For a normal variant, you only need a block registration class and an item registration class.
ModBlocks.java:
package com.example.myaddon;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BlockSetType;
import net.smilenotalive.advanceddoorbells.api.DoorBellApi;
public final class ModBlocks {
public static final Block OAK_DOOR_BELL_SWITCH =
DoorBellApi.registerSwitchVariant(
MyAddon.id("oak_door_bell_switch"),
BlockSetType.OAK,
BlockBehaviour.Properties.ofFullCopy(Blocks.OAK_BUTTON)
);
public static final Block SPRUCE_DOOR_BELL_SPEAKER =
DoorBellApi.registerSpeakerVariant(
MyAddon.id("spruce_door_bell_speaker"),
BlockBehaviour.Properties.ofFullCopy(Blocks.SPRUCE_PLANKS)
);
private ModBlocks() {
}
public static void initialize() {
}
}
ModItems.java:
package com.example.myaddon;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.smilenotalive.advanceddoorbells.api.DoorBellApi;
public final class ModItems {
public static final Item OAK_DOOR_BELL_SWITCH =
DoorBellApi.registerBlockItem(
MyAddon.id("oak_door_bell_switch"),
ModBlocks.OAK_DOOR_BELL_SWITCH
);
public static final Item SPRUCE_DOOR_BELL_SPEAKER =
DoorBellApi.registerBlockItem(
MyAddon.id("spruce_door_bell_speaker"),
ModBlocks.SPRUCE_DOOR_BELL_SPEAKER
);
private ModItems() {
}
public static void initialize() {
CreativeModeTabEvents.modifyOutputEvent(
CreativeModeTabs.FUNCTIONAL_BLOCKS
).register(tab -> {
tab.accept(OAK_DOOR_BELL_SWITCH);
tab.accept(SPRUCE_DOOR_BELL_SPEAKER);
});
}
}
Custom Block Example
When you need custom behavior, create your own block class.
For example, a custom speaker with its own hitbox.
CustomSpeakerBlock.java:
package com.example.myaddon;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.HorizontalDirectionalBlock;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.smilenotalive.advanceddoorbells.server.DoorBellSpeakerBlock;
public final class CustomSpeakerBlock extends DoorBellSpeakerBlock {
public CustomSpeakerBlock(Properties properties) {
super(properties);
}
@Override
protected VoxelShape getShape(
BlockState state,
BlockGetter level,
BlockPos pos,
CollisionContext context
) {
return switch (state.getValue(HorizontalDirectionalBlock.FACING)) {
case NORTH -> Block.box(
4, 4, 14,
12, 12, 16
);
case SOUTH -> Block.box(
4, 4, 0,
12, 12, 2
);
case EAST -> Block.box(
14, 4, 4,
16, 12, 12
);
case WEST -> Block.box(
0, 4, 4,
2, 12, 12
);
case UP, DOWN -> throw new IllegalStateException();
};
}
}
Then register it with the custom overload:
public static final Block CUSTOM_SPEAKER =
DoorBellApi.registerSpeakerVariant(
MyAddon.id("custom_speaker"),
CustomSpeakerBlock::new,
BlockBehaviour.Properties.of()
.strength(2.0F)
.noCollision()
.noOcclusion()
);
The same idea works for switches:
public final class CustomSwitchBlock extends DoorBellSwitchBlock {
public CustomSwitchBlock(Properties properties) {
super(BlockSetType.OAK, properties);
}
// Add custom behavior here
}
and:
DoorBellApi.registerSwitchVariant(
MyAddon.id("custom_switch"),
BlockSetType.OAK,
CustomSwitchBlock::new,
BlockBehaviour.Properties.ofFullCopy(
Blocks.OAK_BUTTON
)
);