快轉到主要內容

新增自訂物品和方塊|Minecraft基岩版Add-On模組製作教學

分類   遊戲攻略 Minecraft
標籤   Minecraft Add-On
🗓️ 民國113年 甲辰年
✍ 切換正體/簡體字
目錄

想要自訂武器,或者是自訂方塊嗎?

基岩版Add-on有一個特色: 方塊可以不是方形的,可以直接套用實體的模型檔案!

文章目標:製作一把能夠合成的「超級劍」,使用會召喚閃電,只要拿在手上,玩家會持續獲得回復的狀態效果。並且新增一個能合成的「苦力怕方塊」,挖掉後會生成TNT。

本文的例子,我們將製作方形與不規則形狀的二種方塊。雖外形不同但二者功能一樣。

影片過程參考

1. 自訂物品
#

這個超級劍的ID為newitem:supersword

  1. 註冊物品

行為包新增items資料夾,新增檔案supersword.json,填入:

{
	"format_version": "1.16.100",
	"minecraft:item": {
		"description": {
			//物品ID
			"identifier": "newitem:supersword",
			//分類
			"category": "items"
		},
		"components": {
			//物品名稱,要在資源包的/texts/zh_TW.json編輯
			"minecraft:display_name": {
				"value": "item.newitem:supersword.name"
			},
			//創造背包中物品的分類
			"minecraft:creative_category": {
				"parent": "itemGroup.name.items"
			},
			//物品在手上的位置,圖片像素16x16以上就要重新設定座標。
			"minecraft:render_offsets": "tools",
			//物品材質名稱(參照資源包/textures/item_texture所定義的)
			"minecraft:icon": {
				"texture": "supersword"
			},
			//使用物品觸發事件
			"minecraft:on_use": {
				"on_use": {
					"event": "use"
				}
			},
			//攻擊傷害
			"minecraft:damage": 20,
			//一組數量
			"minecraft:max_stack_size": 1,
			//冷卻時間
			"minecraft:cooldown": {
				"category": "chorus_fruit",
				"duration": 5
			}
		},
		//事件
		"events": {
			"use": {
				//發射物品
				"shoot": {
					"projectile": "minecraft:snowball",
					"launch_power": 5,
					"angle_offset": 20
				},
				//執行指令
				"run_command": {
					"command": [
						"summon lightning_bolt"
					],
					"target": "holder"
				}
			}
		}
	}
}
  1. 這裡添加on_use這個組件,於是在玩家使用物品(點螢幕)時會執行events裡所寫的run_command

Events區塊不只可以執行指令,也能修改為「添加狀態效果」或者「發射投射物」

例如再加一個發射雪球,並且設定成和召喚閃電這個功能同時執行。

如果材質解析度大於16x16,物品會偏移手中,就必須從render_offset這裡調整。可以自由設定物品在手中的位置以及縮放大小,可參考 這篇文章解決。

  1. 大部分的程式是在行為包,資源包只用來註冊材質。

在資源包的textures/的item_textures.json,用於註冊顯示物品的圖示路徑:

{
	"resource_pack_name": "超級劍與苦力怕方塊資源包",
	"texture_name": "atlas.items",
	"texture_data": {
		"supersword": {
			"textures": "textures/items/supersword"
		}
	}
}

將物品的材質放到/textures/items/下面,取名為supersword.png

至此物品的基本功能完成。

2. 自訂方塊
#

目前除非使用實體模型,否則方塊不會在創造背包出現。要用/give指令獲取。

傳統方形方塊
#

這個方塊的ID為:newblock:creeperblock

  1. 到行為包blocks資料夾下面新增creeprblock.json

輸入程式,設定玩家破壞後執行生成tnt的指令:

{
    "format_version": "1.16.100",
    "minecraft:block": {
        "description": {
        //方塊ID
            "identifier": "newblock:creeperblock"
        },
        "components": {
            //當玩家敲碎方塊(生存模式)執行事件
            "minecraft:on_player_destroyed": {
                "event": "spawn_tnt",
                "target": "self"
            }
        },
        "events": {
            //執行指令
            "spawn_tnt": {
                "run_command": {
                    "command": [
                        "summon tnt",
                        "summon tnt",
                        "summon tnt"
                    ]
                }
            }
        }
    }
}
  1. 這裡要做一個傳統方形方塊。

跟實體不一樣,所有新增的方形方塊都在資源包的blocks.json裡面註冊。

資源包的blocks.json寫上各個面的材質名稱。這個檔案裡面也可以註明方塊取用的聲音。

{
    "format_version": [
        1,
        1,
        0
    ],
    //方形苦力怕方塊
    "newblock:creeperblock": {
        "textures": {
            "up": "creeperblock_up",
            "down": "creeperblock_down",
            "north": "creeperblock_north",
            "south": "creeperblock_south",
            "west": "creeperblock_west",
            "east": "creeperblock_east"
        },
        "sound": "tnt"
    }
}
  1. 註明材質路徑。textures/terrain_textures.json這個檔案就是記載方塊的材質路徑。
{
	"resource_pack_name": "超級劍與苦力怕方塊資源包",
	"texture_name": "atlas.terrain",
	"padding": 8,
	"num_mip_levels": 4,
	"texture_data": {
		//方形苦力怕方塊
		"creeperblock_north": {
			"textures": "textures/blocks/creeperblock/north"
		},
		"creeperblock_east": {
			"textures": "textures/blocks/creeperblock/east"
		},
		"creeperblock_west": {
			"textures": "textures/blocks/creeperblock/west"
		},
		"creeperblock_south": {
			"textures": "textures/blocks/creeperblock/south"
		},
		"creeperblock_up": {
			"textures": "textures/blocks/creeperblock/up"
		},
		"creeperblock_down": {
			"textures": "textures/blocks/creeperblock/down"
		}
	}
}

一般來說方塊的材質是放在資源包/textures/blocks/裡面。

不規則形狀的方塊
#

這個不規則的方塊ID為:newblock:creeperblock_u

  1. 在行為包blocks資料夾新增creeperblock_u.json,填入以下程式,只是這裡還要註明使用的模型以及材質,其餘功能則和上面的傳統方塊一樣。
{
    "format_version": "1.16.100",
    "minecraft:block": {
        "description": {
            //方塊ID
            "identifier": "newblock:creeperblock_u"
        },
        "components": {
            //當玩家敲碎方塊(生存模式)執行事件
            "minecraft:on_player_destroyed": {
                "event": "spawn_tnt",
                "target": "self"
            },
        //指定使用實體模型
            "minecraft:geometry": "geometry.creeperblock_u",
            //texture為資源包textures/terrain_texture.json裡面定義
            "minecraft:material_instances": {
                "*": {
                    "texture": "creeperblock_u",
                    "render_method": "blend",
                    "face_dimming": true,
                    "ambient_occlusion": true
                }
            }
        },
        "events": {
            //執行指令
            "spawn_tnt": {
                "run_command": {
                    "command": [
                        "summon tnt",
                        "summon tnt",
                        "summon tnt"
                    ]
                }
            }
        }
    }
}
  1. 若是不規則形狀的方塊就不用在資源包的blocks註冊方塊了,blocks.json的功用只剩下使用音效。
{
    "format_version": [
        1,
        1,
        0
    ],
    //方形苦力怕方塊
    "newblock:creeperblock": {
        "textures": {
            "up": "creeperblock_up",
            "down": "creeperblock_down",
            "north": "creeperblock_north",
            "south": "creeperblock_south",
            "west": "creeperblock_west",
            "east": "creeperblock_east"
        },
        "sound": "tnt"
    },
    //不規則形狀苦力怕方塊
    "newblock:creeperblock_u": {
        "sound": "anvil"
    }
}
  1. textures/terrain_textures.json註明材質路徑。

  2. 跟實體一樣,在資源包/models/entities/裡面放入模型檔案。取名為creeperblock_u.json,geometry的ID是geometry.creeperblock_u,但不需要註冊實體。

  3. 材質就按慣例放/textures/entity/creeperblock_u.png

3. 為物品和方塊添加合成表 & 設定語言檔案
#

  1. 在行為包recipes資料夾下建立三個檔案:
supersword.json
creeperblock.json
creeperblock_u.json
  1. 編輯supersword.json,設定超級劍的配方為鑽石劍x3。
{
  "format_version": "1.12",
  "minecraft:recipe_shaped": {
    "description": {
      "identifier": "newitem:supersword"
    },
    "tags": [
      "crafting_table"
    ],
    "pattern": [
      " # ",
      " # ",
      " # "
    ],
    "key": {
      "#": {
        "item": "minecraft:diamond_sword"
      }
    },
    "result": {
      "item": "newitem:supersword",
      "count": 1
    }
  }
}

Identifier是物品或方塊的ID。

tag代表要在合成台進行。

設定有序合成,“key"對應的是物品/方塊ID。透過key和空格組成九宮格合成表,最下面"result"是合成結果。

  1. creeperblock.json苦力怕方塊的配方是tnt x3。
{
  "format_version": "1.12",
  "minecraft:recipe_shaped": {
    "description": {
      "identifier": "newblock:creeperblock"
    },
    "tags": [
      "crafting_table"
    ],
    "pattern": [
      " ##",
      " # ",
      "   "
    ],
    "key": {
      "#": {
        "item": "minecraft:tnt"
      }
    },
    "result": {
      "item": "newblock:creeperblock",
      "count": 1
    }
  }
}
  1. creeperblock_u.json,不規則苦力怕方塊的配方是tnt x4。
{
  "format_version": "1.12",
  "minecraft:recipe_shaped": {
    "description": {
      "identifier": "newblock:creeperblock_u"
    },
    "tags": [
      "crafting_table"
    ],
    "pattern": [
      " ##",
      " ##",
      "   "
    ],
    "key": {
      "#": {
        "item": "minecraft:tnt"
      }
    },
    "result": {
      "item": "newblock:creeperblock_u",
      "count": 1
    }
  }
}
  1. 在資源包texts資料夾新增zh_TW.lang,開始編輯物品和方塊的顯示名稱。
item.newitem:supersword.name=超級劍
tile.newblock:creeperblock.name=苦力怕方塊
tile.newblock:creeperblock_u.name=不規則形狀苦力怕方塊

4. 使用animation_controller製作連閃器,重複執行指令。
#

雖然使用物品可以執行指令,但是如果要在背景偵測手上物品,並重複執行「給予狀態效果的」指令呢?

  1. 這裡要複製官方範本行為包/entities/player.json過來

  2. player.json插入一個"animation_controller"

{
  "format_version": "1.16.0",
  "minecraft:entity": {
    "description": {
      "identifier": "minecraft:player",
      "is_spawnable": false,
      "is_summonable": false,
      "is_experimental": false,
      //插入動畫
      "animations": {
        "command": "controller.animation.command"
      },
      "scripts": {
        "animate": [
          "command"
        ]
      }
    },
    //(下略)
    }
}
  1. 行為包/animation_controllers/,新增command.json

裡面開始撰寫指令:

{
  "format_version": "1.10.0",
  "animation_controllers": {
    "controller.animation.command": {
      "states": {
        //default,預設的state
        "default": {
          "transitions": [
            //手上拿著超級劍,切換到run_command的state
            {
              "run_command": "(query.get_equipped_item_name=='supersword') "
            }
          ]
        },
        "run_command": {
          //一進入這個state就執行指令
          "on_entry": [
            "/execute @p ~ ~ ~ effect @s regeneration 1 1 true"
          ],
          "transitions": [
            //無條件返回default state
            {
              "default": "(1.0)"
            }
          ]
        }
      }
    }
  }
}

這個動畫裡面有二個state,一旦transition裡面條件符合,就會切換到另一個state。如果填1.0的話就是執行完on_entry之後無條件切換。

每個state都有on_entry區塊,裡面可以填入指令,或者對實體執行事件。

這裡我寫切換到run_command的state的條件是query.get_equipped_item_name,偵測玩家手上的物品ID,命名空間可以省去。

  1. 因此,當玩家拿著「超級劍」,就會一直獲得回復效果。

也可以把指令寫成一個.mcfunction檔案,放在行為包functions下,再用/function指令喚出。

下載範本
#

這裡下載這篇教學的範本檔案,檔案皆含有註解。

也可於 Github檢視原始碼。

  • 這篇文章資訊參考自 Bedrock Dev Wiki。新增物品的方法是1.16.100加入的,format_version就必須是這個或者更高版本。由於新增物品和方塊還是實驗性功能且更新很快速,啟用Add-on前,必須在世界選項裡把測試版功能全部開啟。

相關文章

組件群組(component_groups)的概念|Minecraft基岩版Add-On模組製作教學
分類   遊戲攻略 Minecraft
標籤   Minecraft Add-On Minecraft Behavior Pack
認識與使用bridge. Add-On Editor|Minecraft基岩版Add-On模組製作教學
分類   遊戲攻略 Minecraft
標籤   Minecraft Add-On
新增基本實體與新生物|Minecraft基岩版Add-On模組製作教學
分類   遊戲攻略 Minecraft
標籤   Minecraft Add-On BlockBench

留言板

此處提供二種留言板。點選按鈕,選擇您覺得方便的留言板。要討論程式碼請用Giscus,匿名討論請用Disqus。

這是Giscus留言板,需要Github帳號才能留言。支援markdown語法,若要上傳圖片請貼Imgur或Postimages。您的留言會在Github Discussions向所有人公開。

這是Disqus留言板,您可能會看到Disqus強制投放的廣告。有時留言可能會被系統判定需審核,導致延遲顯示,請見諒。若要上傳圖片請貼Imgur或Postimages。