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:52] – 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); | ||
Line 114: | 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 object, and the '' | + | As all possible states have been built, an equal state for a block is a same object, and the '' |
tutorial/blockstate.1671155557.txt.gz · Last modified: 2022/12/16 01:52 by solidblock