tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockstate [2021/07/18 03:06] – The function which the old article was using, getHardness(), was incorrect. I changed what the blockstates did and how they did them. Instead of changing the hardness, I changed the "charge" value, which summons lightning. tsunaminal | 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 '' |
- | This makes it impossible to change a specific block' | + | |
- | as every other block of that type will be affected! | + | This is what '' |
- | But, what if you //do// want to give a singular block state, so it can change based on some condition? | + | |
- | This is what '' | + | |
- | Say we wanted a block that can summon lightning, but only when charged up. | + | |
| | ||
- | 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 MyBlock | + | public class ChargeableBlock |
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> | ||
+ | public final class TutorialBlocks { | ||
+ | // For versions below 1.21.2: | ||
+ | public static final Chargeable CHARGEABLE_BLOCK = register(" | ||
+ | // For versions since 1.21.2: | ||
+ | public static final Chargeable CHARGEABLE_BLOCK = register(" | ||
+ | | ||
+ | // [...] | ||
} | } | ||
</ | </ | ||
Then we need to register the properties of the block by overriding '' | Then we need to register the properties of the block by overriding '' | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@Override | @Override | ||
- | protected void appendProperties(StateManager.Builder< | + | protected void appendProperties(StateManager.Builder< |
- | | + | |
} | } | ||
| | ||
} | } | ||
</ | </ | ||
- | 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 MyBlock | + | public class ChargeableBlock |
- | [...] | + | public |
- | public | + | |
super(settings); | super(settings); | ||
- | setDefaultState(getStateManager().getDefaultState().with(CHARGED, | + | setDefaultState(getDefaultState().with(CHARGED, |
} | } | ||
| | ||
} | } | ||
</ | </ | ||
- | Now, we need to be able to charge the block, through the '' | + | Now, we need to be able to charge the block, through the '' |
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@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, |
return ActionResult.SUCCESS; | return ActionResult.SUCCESS; | ||
} | } | ||
Line 48: | Line 67: | ||
</ | </ | ||
- | + | And finally, to use the '' | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | And Finally, to use the '' | + | |
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@Override | @Override | ||
public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) { | public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) { | ||
- | if (world.getBlockState(pos).get(ACTIVATED)){ | + | 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); | ||
} | } | ||
- | world.setBlockState(pos, | + | world.setBlockState(pos, |
super.onSteppedOn(world, | super.onSteppedOn(world, | ||
} | } | ||
Line 81: | Line 88: | ||
==== Adding models for your blockstates ==== | ==== Adding models for your blockstates ==== | ||
- | You can also make the texture and model of your block change based on the state. This is done through a JSON file called a Blockstate | + | You can also make the texture and model of your block change based on the state. This is done through a JSON file called a " |
- | Let's say you register an instance of '' | + | Let's say you register an instance of '' |
- | <code JavaScript resources/ | + | <code JavaScript resources/ |
{ | { | ||
" | " | ||
- | "": | + | "": |
} | } | ||
} | } | ||
Line 95: | Line 102: | ||
Let's break this simple example down. There are a couple important parts to this JSON: | Let's break this simple example down. There are a couple important parts to this JSON: | ||
- | - The ''" | + | * The '' |
- | - A variant named ''""'' | + | |
- | - The object assigned to the ''""'' | + | |
- | - The ''" | + | |
- | If you want to have different models for each blockstate, you should add multiple variants. For the same '' | + | If you want to have different models for each blockstate, you should add multiple variants. For the same '' |
- | <code JavaScript resources/ | + | <code JavaScript resources/ |
{ | { | ||
" | " | ||
- | " | + | " |
- | " | + | " |
} | } | ||
} | } | ||
Line 113: | Line 120: | ||
In this JSON, there are two variants, one for each possibility of the '' | In this JSON, there are two variants, one for each possibility of 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 '' | + | 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 '' |
tutorial/blockstate.1626577560.txt.gz · Last modified: 2021/07/18 03:06 by tsunaminal