方塊性狀

出自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欄位,用於為方塊加入原版狀態。

參考[編輯]