添加多状态的方块

image-20231029162325525

image-20231029162337992

添加方块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

package net.tutorialmod.block.custom;

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.BlockHitResult;

public class ZirconLampBlock extends Block {

public static final BooleanProperty LIT = BooleanProperty.create("lit");
public ZirconLampBlock(Properties properties) {
super(properties);
}

@Override
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) {
if(!level.isClientSide() && interactionHand == InteractionHand.MAIN_HAND) {
level.setBlock(blockPos, blockState.cycle(LIT),3);
}
return InteractionResult.SUCCESS;
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(LIT);
}

}

注册方块

1
2
3
4
5
6
7

//lamp
public static final RegistrySupplier<Block> ZIRCON_LAMP = registerBlock("zircon_lamp",
() -> new ZirconLampBlock(BlockBehaviour.Properties.copy(Blocks.STONE)
.strength(6f).requiresCorrectToolForDrops()
.lightLevel(state -> state.getValue(ZirconLampBlock.LIT) ? 15 : 0)));

添加blockstate

1
2
3
4
5
6
7
8
9
10
11

{
"variants": {
"lit=false": {
"model": "tutorialmod:block/zircon_lamp_off"
},
"lit=true": {
"model": "tutorialmod:block/zircon_lamp_on"
}
}
}

添加方块model

1
2
3
4
5
6
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "tutorialmod:block/zircon_lamp_off"
}
}
1
2
3
4
5
6
7

{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "tutorialmod:block/zircon_lamp_on"
}
}

添加物品model

1
2
3
{
"parent": "tutorialmod:block/zircon_lamp_off"
}

添加贴图

添加语言文件