tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockstate [2022/12/16 01:26] – [A note about performance] 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 4: | Line 6: | ||
This is what '' | This is what '' | ||
| | ||
- | First we define the boolean property of the block - whether or not it is charged (careful not to import the wrong BooleanProperty!): | + | First we define the boolean property of the block - whether or not it is charged (careful not to import the wrong '' |
+ | |||
+ | 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(" | ||
| | ||
- | // The block instance. You can place it anywhere. | + | |
- | public static final ChargeableBlock | + | super(settings); |
- | | + | } |
- | new Identifier(" | + | } |
- | new ChargeableBlock( | + | </ |
+ | |||
+ | We register the blocks in the form described in [[blocks]]. | ||
+ | <code java> | ||
+ | public final class TutorialBlocks { | ||
+ | | ||
+ | public static final Chargeable | ||
+ | // For versions since 1.21.2: | ||
+ | | ||
+ | |||
+ | // [...] | ||
} | } | ||
</ | </ | ||
Line 27: | 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 43: | 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 59: | 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); | ||
Line 106: | Line 122: | ||
Variants are based on possible permutations of the properties added to your block. A property can be totally ignored in the blockstate JSON if you want, like in the first blockstate JSON where we ignored the '' | Variants are based on possible permutations of the properties added to your block. A property can be totally ignored in the blockstate JSON if you want, like in the first blockstate JSON where we ignored the '' | ||
- | This is only a simple introduction to blockstate JSONs. All of the tricks you can do with blockstate and model JSONs are documented on the [[https:// | + | This is only a simple introduction to blockstate JSONs. All of the tricks you can do with blockstate and model JSONs are documented on the [[https:// |
==== A note about performance ==== | ==== A note about performance ==== | ||
- | Every possible | + | Every possible |
- | As all possible states have been built, an equal state for a block is a same state, and the '' | + | As all possible states have been built, an equal state for a block is a same object, and the '' |
tutorial/blockstate.1671153993.txt.gz · Last modified: 2022/12/16 01:26 by solidblock