方块性状

来自Minecraft基岩版开发Wiki

方块性状(Block traits)旨在为创作者提供一种快捷方式,以使用原版方块状态,而无需在自定义方块上定义和管理一系列事件或触发器。虽然可以使用自定义状态和排列来设置同一方块的多种变体(无论是开/关、是否易燃等),但通过性状暴露的状态使您能够访问某些原版方块所包含的固有数据。

例子:

  • 通过使用 minecraft:placement_direction性状来设置 minecraft:cardinal_direction 状态,你可以创造出一个自定义熔炉,这会使其正面朝向玩家。
  • 通过使用minecraft:placement_position性状来设置minecraft:block_face(方块依附在其他方块的面,如:火把,头颅)和minecraft:vertical_half(如果方块放置在顶部或底部)的状态,你可以轻松创建类似于台阶或活板门的方块。


与方块性状相关的状态值仍可以通过事件和命令设置。如果事件和性状使用相同的触发器,则它们可能会相互冲突,例如 on_place

方块性状列表[编辑]

以下是可以使用的方块性状:

placement_direction[编辑]

placement_direction 可以添加包含玩家放置方块时旋转信息的状态。例如,如果在玩家面朝南时放置一个使用 placement_direction 的方块,状态值将为“south”。这将使数据驱动的方块能够复制熔炉、南瓜或陶瓦方块的旋转行为。请注意,尽管方块包含信息,但仍需配置排列来决定方块的放置方式、外观和行为。

这个性状可以包含以下两个状态:

  • minecraft:cardinal_direction 是一个包含4个值的状态,表示玩家放置方块时的朝向。该状态的值为“north”、“south”、“east”和“west”。
  • minecraft:facing_direction 是一个包含6个值的状态,表示玩家放置方块时的整体方向。该状态的值为“down”、“up”、“north”、“south”、“east”和“west”。

这个性状还可以配置一个 y_rotation_offset,可以通过度数(例如 90.0)为当前状态增加偏移量,换句话说,当旋转偏移为 90.0 时,如果玩家面朝南放置方块,该方块的状态将为“east”。此旋转偏移仅适用于水平状态值(north、south、east、west)。

placement_direction 示例[编辑]

"traits": {
 "minecraft:placement_direction": {
  "enabled_states": ["minecraft:cardinal_direction",
   "minecraft:facing_direction"],
  "y_rotation_offset": 90.0
 }
}

placement_position[编辑]

placement_position包含有关玩家放置方块位置的信息。这使得方块能够复刻台阶和楼梯的倒置放置,以及火把和藤蔓的附着行为。

这个性状可以包含以下两个状态:

  • minecraft:block_face 是一个包含6个值的状态,表示方块放置时所接触的面。该状态的值为“up”、“down”、“north”、“south”、“east”和“west”。例如,如果使用 `minecraft:block_face` 的方块放置在邻近方块的南面,则状态值将为“south”。
  • minecraft:vertical_half 是一个包含2个值的状态,用于指定方块是放置在上半部分还是下半部分。该状态的值为“bottom”和“top”。对于使用 minecraft:vertical_half 的方块,如果方块放置在另一个方块的“down”面或在水平面中线的上方,则状态值将为“top”。

placement_position 示例[编辑]

"traits": {
    "minecraft:placement_position": {
        "enabled_states": ["minecraft:block_face", "minecraft:vertical_half"]
    }
}

自定义方块代码示例[编辑]

现在您已经了解了方块性状的概念以及可用的性状,让我们看看它们是如何协同工作的:

{
 "format_version": "1.20.0",
 "minecraft:block": {
  "description": {
   "identifier": "test:rotatable_block",
   "menu_category": {
    "category": "construction"
   },
    "traits": {
     "minecraft:placement_direction": {
      "enabled_states": ["minecraft:cardinal_direction"]
     }
    }
   },
   "components": {
    ...
   },
   "permutations": {
    "condition": "query.block_state('minecraft:cardinal_direction')== 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 180, 0]
       }
      }
     }
    }
   }

方块性状示例方块[编辑]

现在您已经成为方块性状的专家(您现在确实是专家,对吗?),我们将为您提供四个示例方块,您可以利用方块性状将它们添加到您的世界中:一个笑脸方块、一个台阶方块、一个火把方块和一个箭头方块!

笑脸方块示例[编辑]

笑脸方块使用 minecraft:cardinal_direction,以类似于原版雕刻南瓜方块的方式运作。方块的“面”将根据玩家放置方块时的朝向进行旋转。笑脸方块随后将在 y 轴上旋转,像雕刻南瓜一样,朝向四个主要方向。

{
 "format_version": "1.20.20",
 "minecraft:block": {
  "description": {
   "identifier": "testpack:smiley_block",
   "menu_category": {
    "category": "construction"
   },
   "traits": {
    "minecraft:placement_direction": {
     "enabled_states": ["minecraft:cardinal_direction"]
    }
   }
  }, 
  "components": {
   "minecraft:collision_box": {
    "origin": [-6, 0, -6],
    "size": [12, 12, 12]
   },
   "minecraft:selection_box": {
    "origin": [-6, 0, -6],
    "size": [12, 12, 12]
   },
   "minecraft:geometry": "geometry.small_cube",
   "minecraft:material_instances": {
    "*": {
     "texture": "smiley",
     "render_method": "opaque"
    }
   }
  },
  "permutations": [
   { // north
    "condition": "query.block_state ('minecraft:cardinal_direction') == 'north'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 0, 0]
     }
    }
   },
   { // south
    "condition": "query.block_state ('minecraft:cardinal_direction') == 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 180, 0]
     }
    }
   },
   { // west
    "condition": "query.block_state ('minecraft:cardinal_direction') == 'west'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 90, 0]
     }
    }
   },
   { // east
    "condition": "query.block_state ('minecraft:cardinal_direction') == 'east'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, -90, 0]
     }
    }
   }
  ]
 }
}

台阶方块示例[编辑]

示例台阶方块的行为类似于原版台阶,得益于 minecraft:vertical_half 状态。如果玩家点击方块的下表面,或在上半部分的方块侧面(点击位置 y 值 > 0.5),则 minecraft:vertical_half 状态将设置为“top”;否则,将设置为“bottom”。

> [!提示] 示例台阶方块不会合并成双层台阶;该方块仅复制原版台阶的上下放置行为。

{
 "format_version": "1.20.20",
 "minecraft:block": {
  "description": {
   "identifier": "testpack:slab_block",
   "menu_category": {
    "category": "construction"
   },
   "traits" : {
    "minecraft:placement_position": {
     "enabled_states": ["minecraft:vertical_half"]
    }
   }
  },
  "components": {
   "minecraft:geometry": "geometry.slab",
   "minecraft:material_instances": {
    "*": {
     "texture": "slab",
     "render_method": "opaque"
    }
   },
   "minecraft:selection_box": {
    "origin": [-8, 0, -8],
    "size": [16, 8, 16]
   },
   "minecraft:collision_box": {
    "origin": [-8, 0, -8],
    "size": [16, 8, 16]
   }
  },
  "permutations": [
   { // upper_slab
    "condition": "query.block_state('minecraft:vertical_half') == 'top'",
    "components": {
     "minecraft:transformation": {
      "translation": [0, 0.5, 0]
     }
    }
   }
  ]
 }
}

火把方块示例[编辑]

火把方块很好地展示了 minecraft:block_face 状态;火把可以放置在方块的侧面和上表面。此外,放置在方块侧面的火把与放置在方块顶部的火把在几何形状上有所不同。


{
 "format_version": "1.20.20",
 "minecraft:block": {
  "description": {
   "identifier": "testpack:torch_block",
   "menu_category": {
    "category": "construction"
   },
   "traits" : {
    "minecraft:placement_position": {
     "enabled_states": ["minecraft:block_face"]
    }
   }
  },
  "components": {
   "minecraft:collision_box": {
    "origin": [-0.5, 0, -0.5],
    "size": [1, 10, 1]
   },
   "minecraft:selection_box": {
    "origin": [-1, 0, -1],
    "size": [2, 10, 2]
   },
   "minecraft:geometry": "geometry.torch",
   "minecraft:material_instances": {
    "*": {
     "texture": "torch",
     "render_method": "opaque"
    }
   },
   "minecraft:placement_filter":{
    "conditions": [
     {
      "allowed_faces": ["up", "side"]
     }
    ]
   },
   "minecraft:light_emission": 14
  },
  "permutations": [
   { // north
    "condition": "query.block_state('minecraft:block_face') == 'north'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 0, 0]
     }
    }
   },
   { // south
    "condition": "query.block_state('minecraft:block_face') == 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 180, 0]
     }
    }
   },
   { // west
    "condition": "query.block_state('minecraft:block_face') == 'west'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 90, 0]
     }
    }
   },
   { // east
    "condition": "query.block_state('minecraft:block_face') == 'east'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, -90, 0]
     }
    }
   },
   { // all side faces
    "condition": "query.block_state('minecraft:block_face') != 'up' && query.block_state('minecraft:block_face') != 'down'",
    "components": {
     "minecraft:collision_box": {
      "origin": [-2, 3, 4],
      "size": [4, 10, 4]
     },
     "minecraft:selection_box": {
      "origin": [-2, 3, 4],
      "size": [4, 10, 4]
     },
     "minecraft:geometry": "geometry.wall_torch"
    }
   }
  ]
 }
}

箭头方块示例[编辑]

最后但并非最不重要的是示例箭头方块。该方块同时使用 minecraft:cardinal_directionminecraft:facing_direction 状态。其结果是一个可以整体旋转的方块,同时也会根据玩家的朝向进行旋转,类似于原版观察者方块的行为。

{
 "format_version": "1.20.20",
 "minecraft:block": {
  "description": {
   "identifier": "testpack:arrow_block",
   "menu_category": {
    "category": "construction"
   },
   "traits" : {
    "minecraft:placement_direction": {
     "enabled_states": ["minecraft:cardinal_direction", "minecraft:facing_direction"],
     "y_rotation_offset": 180.0
    }
   }
  },
  "components": {
   "minecraft:collision_box": {
    "origin": [-8, 0, -8],
    "size": [16, 16, 16]
   },
   "minecraft:geometry": "geometry.cube",
   "minecraft:material_instances": {
    "*": {
     "texture": "arrow",
     "render_method": "opaque"
    }
   }
  },
  "permutations": [
   { // north
    "condition": "query.block_state('minecraft:facing_direction') == 'north'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 180, 0]
     }
    }
   },
   { // south
    "condition": "query.block_state('minecraft:facing_direction') == 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 0, 0]
     }
    }
   },
   { // west
    "condition": "query.block_state('minecraft:facing_direction') == 'west'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, -90, 0]
     }
    }
   },
   { // east
    "condition": "query.block_state('minecraft:facing_direction') == 'east'",
    "components": {
     "minecraft:transformation": {
      "rotation": [0, 90, 0]
     }
    }
   },
   { // up and north
    "condition": "query.block_state('minecraft:facing_direction') == 'up' && query.block_state('minecraft:cardinal_direction') == 'north'",
    "components": {
     "minecraft:transformation": {
      "rotation": [90, 180, 0]
     }
    }
   },
   { // up and south
    "condition": "query.block_state('minecraft:facing_direction') == 'up' && query.block_state('minecraft:cardinal_direction') == 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [90, 0, 0]
     }
    }
   },
   { // up and west
    "condition": "query.block_state('minecraft:facing_direction') == 'up' && query.block_state('minecraft:cardinal_direction') == 'west'",
    "components": {
     "minecraft:transformation": {
      "rotation": [90, -90, 0]
     }
    }
   },
   { // up and east
    "condition": "query.block_state('minecraft:facing_direction') == 'up' && query.block_state('minecraft:cardinal_direction') == 'east'",
    "components": {
     "minecraft:transformation": {
      "rotation": [90, 90, 0]
     }
    }
   },
   { // down and north
    "condition": "query.block_state('minecraft:facing_direction') == 'down' && query.block_state('minecraft:cardinal_direction') == 'north'",
    "components": {
     "minecraft:transformation": {
      "rotation": [-90, 180, 0]
     }
    }
   },
   { // down and south
    "condition": "query.block_state('minecraft:facing_direction') == 'down' && query.block_state('minecraft:cardinal_direction') == 'south'",
    "components": {
     "minecraft:transformation": {
      "rotation": [-90, 0, 0]
     }
    }
   },
   { // down and west
    "condition": "query.block_state('minecraft:facing_direction') == 'down' && query.block_state('minecraft:cardinal_direction') == 'west'",
    "components": {
     "minecraft:transformation": {
      "rotation": [-90, -90, 0]
     }
    }
   },
   { // down and east
    "condition": "query.block_state('minecraft:facing_direction') == 'down' && query.block_state('minecraft:cardinal_direction') == 'east'",
    "components": {
     "minecraft:transformation": {
      "rotation": [-90, 90, 0]
     }
    }
   }
  ]
 }
}

历史[编辑]

基岩版
1.20.01.20.0.20description中添加了traits字段,用于为方块添加原版状态。

参考[编辑]