tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockstate [2020/03/19 10:19] – don't hardcode a single Block instance in onUse juuz | tutorial:blockstate [2022/12/16 01:26] – [A note about performance] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== 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 to have a hardness of '' | + | |
- | it would become harder and gain a hardness of '' | + | |
| | ||
- | First we define the boolean property of the block - whether or not it is hard (careful not to import the wrong BooleanProperty!): | + | First we define the boolean property of the block - whether or not it is charged |
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
- | public static final BooleanProperty | + | public static final BooleanProperty |
+ | |||
+ | // The block instance. You can place it anywhere. | ||
+ | public static final ChargeableBlock CHARGEABLE_BLOCK = Registry.register( | ||
+ | Registries.BLOCK, | ||
+ | new Identifier(" | ||
+ | new ChargeableBlock( /* write anything appropriate here */ )); | ||
} | } | ||
</ | </ | ||
- | Then we need to register the property | + | 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 in the block constructor: | + | Then we need to set the default state of our property in the block constructor |
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
- | public | + | public |
super(settings); | super(settings); | ||
- | setDefaultState(getStateManager().getDefaultState().with(HARDENED, false)); | + | setDefaultState(getDefaultState().with(CHARGED, false)); |
} | } | ||
| | ||
} | } | ||
</ | </ | ||
- | (To set multiple properties, chain '' | + | Now, we need to be able to charge the block, through the '' |
- | + | ||
- | Now, to set the property we need to call | + | |
- | + | ||
- | (Replace | + | |
<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, Hand hand, BlockHitResult hit) { | ||
- | world.setBlockState(pos, | + | |
+ | | ||
return ActionResult.SUCCESS; | return ActionResult.SUCCESS; | ||
} | } | ||
} | } | ||
</ | </ | ||
- | And to use the property we call '' | + | |
+ | And finally, | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
- | if(hardened) return 2.0f; | + | // Summoning the Lighting Bolt at the block |
- | | + | LightningEntity lightningEntity = (LightningEntity) EntityType.LIGHTNING_BOLT.create(world); |
+ | | ||
+ | world.spawnEntity(lightningEntity); | ||
+ | | ||
+ | |||
+ | world.setBlockState(pos, | ||
+ | super.onSteppedOn(world, | ||
} | } | ||
} | } | ||
Line 66: | Line 72: | ||
==== 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 80: | Line 86: | ||
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 // | + | If you want to have different models for each blockstate, you should |
- | <code JavaScript resources/ | + | <code JavaScript resources/ |
{ | { | ||
" | " | ||
- | "hardened=false": | + | "charged=false": |
- | "hardened=true": | + | "charged=true": |
} | } | ||
} | } | ||
</ | </ | ||
- | 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 state, and the '' |
tutorial/blockstate.txt · Last modified: 2024/07/04 16:30 by mineblock11