zh_cn:tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
zh_cn:tutorial:blockstate [2019/12/19 02:58] – lightcolour | zh_cn:tutorial:blockstate [2022/12/16 01:52] – [赋予方块状态] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== 给一个方块状态 ====== | + | ====== 赋予方块状态 |
- | Minecraft中的每种类型的区块都由一个单独的“区块”实例表示。 | + | Minecraft 中的每种类型的方块都由一个单独的 |
- | 这样就无法仅通过更改“块”实例的状态来更改特定块的状态, | + | |
- | 因为该类型的其他所有块都会受到影响! | + | 这就是 '' |
- | 但是,如果您想给出一个单一的块状态,以便它可以根据某些条件改变,该怎么办? | + | |
- | 这就是'' | + | |
- | 假设我们希望块的硬度通常为'' | + | |
- | 它会变得更硬并获得“ 2”的硬度。 | + | |
| | ||
- | 首先,我们定义块的boolean属性-是否很难(小心不要导入错误的BooleanProperty!): | + | 首先,我们定义方块的布尔值属性——是否充能(小心不要导入错误的 |
+ | |||
+ | 事实上你也可以使用原版已有的属性,可以在 '' | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
- | public static final BooleanProperty | + | public static final BooleanProperty |
+ | |||
+ | // 方块实例。可以放在任何地方。确保这个类已经初始化。 | ||
+ | public static final ChargeableBlock CHARGEABLE_BLOCK = new ChargeableBlock(FabricBlockSettings.copyOf(Blocks.STONE)); | ||
} | } | ||
</ | </ | ||
- | 然后,我们需要通过Override'' | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class ExampleMod implements ModInitializer { |
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | Registry.register(Registries.BLOCK, | ||
+ | Registry.register(Registries.ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 然后,我们需要通过覆盖 '' | ||
+ | |||
+ | <code java> | ||
+ | public class ChargeableBlock | ||
[...] | [...] | ||
@Override | @Override | ||
- | protected void appendProperties(StateManager.Builder< | + | protected void appendProperties(StateManager.Builder< |
- | | + | |
} | } | ||
- | | + | |
} | } | ||
</ | </ | ||
- | 然后,我们需要在块构造函数中设置属性的默认状态: | + | 然后,我们需要在方块构造器中设置属性的默认状态(要设置多个属性,请通过调用 '' |
<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)); |
} | } | ||
- | | + | |
} | } | ||
</ | </ | ||
- | (要设置多个属性,请链接“ with()”调用) | ||
- | 现在,要设置属性,我们需要调用'' | + | 现在,我们需要能够通过 '' |
- | + | ||
- | (用块的实例替换'' | + | |
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | world.setBlockState(pos, | + | player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, |
- | return | + | world.setBlockState(pos, |
+ | return | ||
} | } | ||
} | } | ||
</ | </ | ||
- | 并使用该属性,我们称为'' | + | |
+ | 最后,要使用 | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class ChargeableBlock |
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
- | if(hardened) return 2.0f; | + | // 在方块的位置召唤闪电 |
- | | + | LightningEntity lightningEntity = (LightningEntity) EntityType.LIGHTNING_BOLT.create(world); |
+ | | ||
+ | world.spawnEntity(lightningEntity); | ||
+ | | ||
+ | |||
+ | world.setBlockState(pos, | ||
+ | super.onSteppedOn(world, | ||
} | } | ||
} | } | ||
Line 66: | Line 84: | ||
==== 为方块状态添加模型 ==== | ==== 为方块状态添加模型 ==== | ||
- | 您还可以根据状态更改块的纹理和模型。 这是通过称为Blockstate | + | 你可能还需要使得纹理和模型能够根据状态来改变,这是通过一个叫做“方块状态 |
- | 假设您将'' | + | 比如说,你为 |
- | <code JavaScript resources/ | + | <code JavaScript resources/ |
{ | { | ||
- | " | + | |
- | "": | + | |
+ | "": | ||
+ | | ||
} | } | ||
+ | |||
} | } | ||
</ | </ | ||
+ | 让我们分解一下这个简单的例子。JSON 有几个重要的部分: | ||
- | Let's break this simple example down. There are a couple important parts to this JSON: | + | - ''" |
+ | - 名为 ''""'' | ||
+ | - 分配给 ''""'' | ||
+ | - ''" | ||
- | - The ''" | + | 如果//确实//想要为每个方块状态使用不同的模型,则需要添加多个变种。对于我们上面使用的同一个 |
- | - A variant named ''""'' | + | |
- | - The object assigned to the ''""'' | + | |
- | - The ''" | + | |
- | If you //do// want to have different models for each blockstate, you'd want to add multiple variants. For the same '' | + | <code JavaScript resources/ |
- | + | { | |
- | <code JavaScript resources/ | + | " |
- | { | + | "charged=false": |
- | " | + | "charged=true": |
- | "hardened=false": | + | |
- | "hardened=true": | + | |
} | } | ||
- | } | ||
</ | </ | ||
- | In this JSON, there are two variants, one for each possibility of the '' | + | 在此 |
+ | |||
+ | 变种基于添加到方块中的属性的可能排列。如果需要,可以在方块状态 JSON 中完全忽略某个属性,例如在第一个方块状态 JSON 中我们忽略了 | ||
+ | |||
+ | 这只是对方块状态 JSON 的简单介绍。[[https:// | ||
+ | |||
+ | ==== 关于性能的注意事项 ==== | ||
- | 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 '' | + | 游戏开始时会注册方块的所有可能状态。这意味着,如果具有14个布尔属性,则该方块具有2 ^ 14 = 16384个不同的状态,并且会注册这2 ^ 14个状态。因此,方块不应包含太多的方块状态属性。相反,方块状态应主要保留用于视觉效果,需要使用更高级的状态应使用[[zh_cn:tutorial:blockentity|方块实体]]。 |
- | 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 ==== | + | |
- | Every possible state of a block is registered at the start of the game. This means that if you have 14 boolean properties, the block has 2^14 = 16384 different states and 2^14 states are registered. For this reason blocks should not contain too many blockstate properties. Rather, blockstates should be mostly reserved for visuals, and [[tutorial: | + |
zh_cn/tutorial/blockstate.txt · Last modified: 2023/11/18 08:13 by solidblock