Minecraft Fabric模组开发 (五) 第一个方块

第一个方块

  • 创建方块(以创建一个铁熔炉为例,最好的方法当然是继承原版熔炉的FurnaceBlock类或者其父类AbstractFurnaceBlock ,这里为了学习,重新写一个铁熔炉类)

    public static final Block IRON_FURNACE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build());
    
  • 注册方块和方块物品(方块对应的物品)

    Registry.register(Registry.BLOCK, new Identifier("demo", "iron_furnace_block"), IRON_FURNACE_BLOCK);
    Registry.register(Registry.ITEM, new Identifier("demo", "iron_furnace_block"), new BlockItem(IRON_FURNACE_BLOCK, new Item.Settings().group(DEMO_GROUP)));
    
  • 添加材质(参考原版熔炉实现)

    Blockstate: src/main/resources/assets/demo/blockstates/iron_furnace_block.json
    Block Model: src/main/resources/assets/demo/models/block/iron_furnace_block.json
    Item Model: src/main/resources/assets/demo/models/item/iron_furnace_block.json
    Block Texture: src/main/resources/assets/demo/textures/block/iron_furnace_block_top.png
    src/main/resources/assets/demo/textures/block/iron_furnace_block_front.png
    src/main/resources/assets/demo/textures/block/iron_furnace_block_side.png
    
    //Blockstate
    {
        "variants": {
            "facing=north,lit=false": { "model": "demo:block/iron_furnace_block" },
            "facing=south,lit=false": { "model": "demo:block/iron_furnace_block", "y": 180 },
            "facing=west,lit=false":  { "model": "demo:block/iron_furnace_block", "y": 270 },
            "facing=east,lit=false":  { "model": "demo:block/iron_furnace_block", "y": 90 },
            "facing=north,lit=true": { "model": "demo:block/iron_furnace_block_on" },
            "facing=south,lit=true": { "model": "demo:block/iron_furnace_block_on", "y": 180 },
            "facing=west,lit=true":  { "model": "demo:block/iron_furnace_block_on", "y": 270 },
            "facing=east,lit=true":  { "model": "demo:block/iron_furnace_block_on", "y": 90 }
        }
    }
    
    //Block Model  iron_furnace_block_on
    {
      "parent": "block/orientable",
      "textures": {
          "top": "demo:block/iron_furnace_block_top",
          "front": "demo:block/iron_furnace_block_front_on",
          "side": "demo:block/iron_furnace_block_side"
      }
    }
    
    //Block Model iron_furnace_block
    {
        "parent": "block/orientable",
        "textures": {
            "top": "demo:block/iron_furnace_block_top",
            "front": "demo:block/iron_furnace_block_front",
            "side": "demo:block/iron_furnace_block_side"
        }
    }
    
    //Item Model
    {
      "parent": "demo:block/iron_furnace_block"
    }
    
  • 添加合成表

    {
      "type": "minecraft:crafting_shaped",
      "pattern": [
        "###",
        "# #",
        "###"
      ],
      "key": {
        "#": {
          "item": "minecraft:iron_ingot"
        }
      },
      "result": {
        "item": "demo:iron_furnace_block"
      }
    }
    
  • 添加战利品表

    • 生存模式破坏方块时的掉落物

    • src/main/resources/data/damo/loot_tables/blocks/iron_furnace_block.json

      "type": "minecraft:block",
        "pools": [
          {
            "rolls": 1,
            "entries": [
              {
                "type": "minecraft:item",
                "name": "demo:iron_furnace_block"
              }
            ],
            "conditions": [
              {
                "condition": "minecraft:survives_explosion"
              }
            ]
          }
        ]
      }
      
  • 运行游戏,发现物品栏显示正常,但放置后材质丢失,因为没有设置方块放置时的状态

  • 添加方块状态

    • 新建类,继承自Block类

      public class IronFurnaceBlock extends Block {
          public IronFurnaceBlock(Settings settings) {
              super(settings);
          }
      }
      
    • 类中添加方块状态

      public static final DirectionProperty FACING;
      public static final BooleanProperty LIT;
      
      @Override
      protected void appendProperties(StateFactory.Builder<Block, BlockState> builder) {
          builder.add(FACING, LIT);
      }
      
      static {
          FACING = HorizontalFacingBlock.FACING;
          LIT = RedstoneTorchBlock.LIT;
      }
      
    • 重写获取物品放置时的状态方法

      @Override
      public BlockState getPlacementState(ItemPlacementContext ctx) {
      return this.getDefaultState().with(FACING,ctx.getPlayerFacing().getOpposite());
      }
      
    • 主模组类中,方块创建修改为自定义方块类

      public static final Block IRON_FURNACE_BLOCK = new IronFurnaceBlock(FabricBlockSettings.of(Material.METAL).build());
      
  • 运行游戏,查看效果

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FvMnsuHf-1584884018569)(./Resource/image/18.png)]

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值