tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockstate [2023/11/18 08:13] – [Adding models for your blockstates] update Minecraft Wiki link solidblock | tutorial:blockstate [2024/10/27 14:57] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Giving a block state ====== | ====== Giving a block state ====== | ||
Every type of block in Minecraft is represented by a singular '' | Every type of block in Minecraft is represented by a singular '' | ||
Line 7: | Line 9: | ||
In fact, you can also use existing properties defined in vanilla, which can be found in '' | In fact, you can also use existing properties defined in vanilla, which can be found in '' | ||
+ | |||
+ | Create the class first: | ||
<code java> | <code java> | ||
public class ChargeableBlock extends Block { | public class ChargeableBlock extends Block { | ||
public static final BooleanProperty CHARGED = BooleanProperty.of(" | public static final BooleanProperty CHARGED = BooleanProperty.of(" | ||
| | ||
- | | + | public ChargeableBlock(Settings settings) { |
- | | + | super(settings); |
+ | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | We register the blocks in the form described in [[blocks]]. | ||
<code java> | <code java> | ||
- | public class ExampleMod implements ModInitializer | + | public |
- | | + | |
- | public | + | public |
- | Registry.register(Registries.BLOCK, | + | // For versions since 1.21.2: |
- | | + | public static final Chargeable CHARGEABLE_BLOCK = register(" |
- | | + | |
+ | // [...] | ||
} | } | ||
</ | </ | ||
Line 35: | Line 43: | ||
} | } | ||
</ | </ | ||
- | Then we need to set the default state of our property | + | |
+ | Then we need to set the default state of our property. Go to the constructor | ||
<code java> | <code java> | ||
public class ChargeableBlock extends Block { | public class ChargeableBlock extends Block { | ||
- | [...] | ||
public ChargeableBlock(Settings settings) { | public ChargeableBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
Line 51: | Line 59: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | + | public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { |
player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, | player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, | ||
world.setBlockState(pos, | world.setBlockState(pos, | ||
Line 67: | Line 75: | ||
if (world.getBlockState(pos).get(CHARGED)){ | if (world.getBlockState(pos).get(CHARGED)){ | ||
// Summoning the Lighting Bolt at the block | // Summoning the Lighting Bolt at the block | ||
- | LightningEntity lightningEntity = (LightningEntity) | + | LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(world); |
lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos)); | lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos)); | ||
world.spawnEntity(lightningEntity); | world.spawnEntity(lightningEntity); |
tutorial/blockstate.1700295207.txt.gz · Last modified: 2023/11/18 08:13 by solidblock