Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement
Information icon
此特性为携带版独有。
Ic translate
此条目的(部分)内容需要翻译。

你可以帮助我们来翻译此条目,但请勿使用机器翻译

此条目的内容需要改进。

这些内容因翻译质量较低而违反或未达到条目标准
你可以帮助我们来改进此条目。

概述

这篇文章和“资源包教程”有类似之处. 当然我们也建议您最好先了解一下如何创建一个资源包, 这篇教程可能会忽略一些与资源包的共同点. 下面会讲解如下内容:

  • 简述JSON数据格式
  • 如何创建行为包
  • 如何改变怪物行为

如果你看过站内关于pig.json的修改教程,那么可能会辅助阅读本篇文章。

所需时间

完成本教程大约需要1至1个半小时。

所需工具

跟随下面给出的步骤继续教程:

文本编辑器

可以使用任意一种文本编辑器,但我建议你使用一些编程用的IDE。在PC上,Notepad++是一个非常不错的选择,不仅免费,而且能够对编程语法进行高亮显示,你可以从https://notepad-plus-plus.org/下载到Notepad++。其他软件比如Visual Studio Code也是不错的选择。而在iOS上,用Textcode Viewer则是不错的选择。

文件管理器

可以使用任何一种文件管理器。我建议:Windows可以用系统自带的文件管理器(但是请你安装好压缩和解压用的软件,如WinRAR,7z等)。而iOS设备上则可以用文件全能王(英文名FileMaster);安卓可以使用ES文件管理器。

JSON数据格式

在开始制作行为包之前,我们需要先快速了解一下JSON是怎么运作的。我们不会去太过于深入JSON技术的一些细节,但是如果你希望了解更多,你可以参阅http://www.json.org/

JSON里由两种不同的数据结构所组成:

  • 字符串
    • 例如: "name": "value"
  • 数组/对象数组
    • 例如: ["value1","value2"]

你可以创建一个数组,里面放一些有顺序的内容。可以是标量(字符串、数值、布尔值和空值)也可以是对象(对象和数组)。

对象范例

"minecraft:scale": { "value": "0.5" }

上面的对象由一个字符串组成,其中"minecraft:scale"是键(该对象的名称),值是另一个键值对("value": "0.5")

带集合的对象范例

{
  "minecraft:health": {
    "value": "10",
    "max": "10"
  }
}

带数组的对象范例

{
  "minecraft: type_family": {
    "family": ["monster","undead"]
  }
}

注意

键与值之间总是使用冒号分开,值之间则使用逗号分开。另外,每一个大括号{必须封闭},即不允许只出现一个左大括号但是没有又大括号的情况;中括号[]、双引号""同理。这一点非常重要!请务必记住。如果上述符号没有封闭,将可能造成错误的结果,让你的JSON代码无法执行。

幸运的是,我们可以使用一些在线的工具网站来帮助我们检查JSON,你可以使用http://pro.jsonlint.com/或如果访问速度较慢,也可以使用国内类似的网站如http://www.bejson.com/来检查你JSON的合法性;你只需要复制你的JSON代码到该页面,然后点击右边的检查按钮(√)即可。如果检查按钮显示为绿色,则说明你的JSON正常,反之,如果检查按钮为红色,他将尝试告诉你错误的原因。注意!这个网站只能检查这个JSON是否有错误比如漏掉大括号或者中括号等,即使通过也并不能代表你的JSON能够在游戏里正确的运行!我们也会检查你在json文件中写的注释,注释在json中通常是无效的,但是当你在文档中写了"//注释"的时候工具也会告诉你"JSON文件无效",记得在使用工具检查代码前先移除所有注释!

创造一个行为包

行为包作为Add-Ons的一部分,允许你更改一个实体的行为来达到全新的表现效果。不过目前仅支持添加或修改一个实体已经存在的行为(当然这个实体也应该是存在的)。这个简单的教程将向您展示如何将爬行者的爆炸行为添加到猪身上,不过在修改猪之前,我们要先让别人知道自己是这个Add-On的作者。

当然,下面的步骤你也可以用在一个材质包上:

  • 将原版行为包复制下来并放到一个专门的文件夹中,命名为“教程行为包”
  • 在此寻找行为包文件夹(Windows 10版):C:\Users\帐户名称\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang\behavior_packs
  • 创建一个名为pack_manifest.json的JSON文件

manifest.json

你可以在这个manifest找到你需要的内容。

manifest.json
{
  "header": {
    "pack_id": "e3c5a9be-1cfc-4a74-9f70-9213bab32090",
    "name": "教程数据包",
    "packs_version": "0.0.1",
    "description": "教程资源包5",
    "modules": [
      {
        "description": "未使用",
        "version": "0.0.1",
        "uuid": "14d5fb6b-ef0a-47e5-8a98-d615709c1a03",
        "type": "data"
      }
    ],
    "dependencies": [
      {
        "description": "依赖于教程材质包",
        "version": "0.0.1",
        "uuid": "891f5751-bb0e-47c6-91f0-fdc4e76949ef",
        "type": "resources"
      }
    ]
  }
}

当然,你完全可以把 “manifest.json”复制进你的行为包文件夹并修改它。如果你需要让你行为包配套材质绑定到行为包上,就要让行为包中“dependencies”里面的“uuid” 和材质包中modules的“uuid”相同。

注意

单从文件名称和大致结构看来材质包和行为包的“manifest.json”似乎是完全一样的。但是modules中的“type”决定了它是那种类型的包,行为包对应“data”,材质包对应“resources”。不过与此同时,你需要让header和modules的UUID不同,同样的材质包的所有UUID也不能和行为包重合。如果真的出现相同的情况,则会导致只能导入其中一个包。

制作一个图标

为行为包制作图标的过程其实是和为资源包制作图标一样的, 可以在创建资源包中查看细节。当然,资源包和行为包图标一样也是可以的。

测试自己的行为包

现在可以尝试运行了!创建一个新的世界,在创建世界页面的左边可以看到行为包选项,点进去之后找到你导入的行为包并选中。如果有弹窗就点确定,行为包会导致这个世界不再能够获得成就:

FileExtensions

请记住,每当您更改行为包时,您都需要创建一个新世界,因为游戏会在创建世界时制作行为包的副本。同样,如果您修改世界文件夹中包含的加载项,则可以避免这种情况。请记住,如果你这样做,你需要手动将它复制回com.mojang / behavior_packs文件夹,如果你想将这个包应用到其他世界。

设置JSON文件结构

现在我们的这个包所需的东西已经准备好了,让我们把这些json文件放进一个文件夹里。

  • 在这个名为"tutorial_behavior_pack"的文件夹里,新建一个名为“entities”的文件夹。

我们将要把所有改变实体行为的json文件放在entities文件夹内。我们开始吧。首先,把pig.json从\Vanilla_Behavior_Pack\entities\pig.json复制出来,然后,把它粘贴到entities文件夹里面。现在pig.json已经复制完了。把它用你选择的文本编辑器打开。

实体 JSON 概述

在我们修改JSON代码前,让我们首先简要地看看我们的实体.json怎么建立。

Minecraft实体JSON有4个部分:

minecraft:entity

这是每个生物的主要对象,接下来所有行为behavior和事件event都是这个对象中包含的值。

entity.json伪代码
{
  "minecraft:entity": {pig} scale 11.111
    "format_version": 0.1,
    "component_groups": { component groups go here },
    "components": { components go here },
    "events": { events go here }
  }
}
注意

在Minecraft中,这不是有效的json!这只是一个如何编写一个可用实体json的示例。 如果你查看一下现有的pig.json你将会注意到“component groups go here”这一部分事实上已经写入了真正的部件组。

注意2

format_version 是用来告诉Minecraft该读取哪个版本的JSON. 当我们升级JSON文件并添加新组件时, 应当告诉游戏它需要寻找对应版本.而此事通过修改format_version后的版本号来完成

所以,请你尽可能不改变这个数字.因为,当Minecraft的默认JSON更新时,你也需要更新你的JSON来适应新的变化.一旦你改变了你的JSON,你就需要改变这个版本号.你应该把官方已更新的JSON复制过来,做好你自己的修改后再覆盖回去。

(啰嗦一堆就是告诉你别用自己的版本号,去用官方的。)

复合标签组

用于定义一组行为,用于简单地去除或增加多个行为,对于改变生物状态十分有用(例如幼年猪与成年猪行为的转换)。

在pig.json中含有pig_baby、pig_adult两个复合标签组。可以看到,只有当某个部件组当前被加入这个实体中时,这个部件组才会运行。因此,当这只猪是幼年猪时,pig_baby这个复合标签组将会被加入并运行,而pig_adult则不会。

复合标签

用于定义该种生物的共有行为。

可以看到“minecraft:identifier”和“minecraft:type_family”存在于这一部分,因为我们希望每一种猪都有这些行为。

事件

事件用于加入或移除复合标签组。事件被我们加入的部件或者Minecraft本身的代码所触发。

事件可以使用randomize函数在不同结果之间进行选择(请参阅pig.json中的“minecraft:entity_spawned”)。 请注意,randomize的权重可以增加到100以上。这是因为它们不是百分比! 如果你把成年猪的体重设定为23,然后将生猪的体重设定为100,那么你将有23/123的机会获得成年猪和100/123的机会获得一头小猪。

代码组件:

minecraft:identifier

这个语句对每一个实体来说都是必要的. 它告诉MC你的JSON对哪些实体有效.

请注意:标识符(identifyier)必须被正确命名!如果乱输你可能会遇到一些不可描述的事情或是实体大罢工。不知道/不确定如何命名?请参考原版行为包中关于那个实体的JSON文件。(在 /Vanilla_Behavior_Pack /entities/)

minecraft:type_family

它为实体设置族类型。族类型利用多种多样的要素,以便选择特定的实体。具体例子请参考 “minecraft:rideable” 和 “minecraft:breedable”。

minecraft:entity_spawned

当你规定的的实体生成时,这个语句会被触发。 在 pig.json 中有一个随机化函数,可以随机地生成大猪或小猪。你可以阅读它来加深理解。

让猪爆炸吧!

添加组件

现在我们已经明白了一个实体是如何被设置的;是时候魔改一下关于猪的代码了!


找到 creeper.json和 pig.json 文件(在 Vanilla_Behavior_Pack/entities/)然后分别打开它们。你眼前应该有两个分离的窗口(这样便于来回切换)。

首先,让猪找到一个受害者。你可以往pig.json的组件对象中写入"minecraft:nearest_attackable_target" 和"minecraft:target_nearby_sensor"来达到这一目的。

然后,猪要找目标。在组件对象中找到 "minecraft:behavior.random_look_around"。它应该刚好在 "events" 小节之前。

最后,爆炸吧!从 creeper.json 中复制”target_nearby_sensor” 和 “nearest_attackable_target”组件,粘贴到pig.json的”random_look_around”组件之后。(下边有详细代码,自己看)


pig.json excerpt:

...

"minecraft:behavior.random_look_around": {
  "priority": 9
},
"minecraft:behavior.nearest_attackable_target": {
  "priority": 1,
  "entity_types": [
    {
      "filters": { "other_with_families": "player" },
      "max_dist": 16
    }
  ], 
  "must_see": true
},
"minecraft:target_nearby_sensor": {
  "inside_range": 3.0,
  "outside_range": 7.0,
  "on_inside_range": {
    "event": "minecraft:start_exploding",
    "target": "self"
  },
  "on_outside_range": {
    "event": "minecraft:stop_exploding",
    "target": "self"
  }
}

...

注意下最后的括弧后没加逗号,这是因为 "minecraft:target_nearby_sensor"是最后一个组件(话说完了,不用逗号)。

minecraft:nearest_attackable_target

这个组件让实体在你定义的限制下攻击最近的目标。 下面是一些设置的说明。

  • Priority – 优先级,设置这个组件的重要程度。优先级数越高,组件就越可能优先运行(比如两个组件冲突时)。
  • Entity_types – 选择你定义的实体,把它/它们设置为目标。(上边提到过)
  • Max_dist – 最大距离。目标在这个范围内才能被追踪和攻击。
  • Must_see – 必须被看见。目标必须在玩家的视野范围内,才会被你设置的实体攻击。

在上面的代码中,我们选定了玩家作为目标。当玩家走进以猪为中心16块为半径的区域时,就会遭到追击。

minecraft:target_nearby_sensor

目标靠近传感器。在你设定的范围内如果有目标,这个组件就会被触发。

  • Inside_range – 设置“内部范围”的距离
  • Outside_range – 设置“外部范围”的距离
  • On_inside_range – 当目标进入“内部范围”,触发这个函数内的语句。
  • On_outside_range – 当目标脱离“外部范围”,触发该函数内的语句。

上面的代码中,我们想要利用 on_inside_range 和 on_outside_range 触发 "minecraft:start_exploding" (开始爆炸)和 "minecraft:stop_exploding"(停止爆炸)。当然现在啥也不会发生,因为我们还没添加这两个事件呢!(下面就要讲怎么添加事件)

Adding events

Scroll down to the bottom of the "events" object in pig.json and add a comma after the closing curly brace of "minecraft:on_unleash". This is where we are going to add our new start and stop exploding events.

Either type or copy and paste the "minecraft:start_exploding" and "minecraft:stop_exploding" events from creeper.json into pig.json. The end of Pig.jsons events section should now look something like this (new text is highlighted in green):

pig.json excerpt:

...

"minecraft:on_unleash": {
  "remove": {
    "component_groups": [
      "minecraft:pig_leashed"
    ]
  }
},
"minecraft:start_exploding": {
  "remove": {
    "component_groups":[
      "minecraft:hunting"
    ]
  },
  "add": {
    "component_groups": [
      "minecraft:exploding"
    ]
  }
},
"minecraft:stop_exploding": {
  "remove": {
    "component_groups": [
      "minecraft:exploding"
    ]
  },
  "add": {
    "component_groups": [
      "minecraft:hunting"
    ]
  }
}

...

Now that we have brought over the creeper events, we need to do some cleanup. Our pig doesn't need a hunting component group so we can remove the "remove" section from start_exploding and the "add" section from stop_exploding. Our code should now look like:

pig.json excerpt:

...

"minecraft:on_unleash": {
  "remove": {
    "component_groups": [
      "minecraft:pig_leashed"
    ]
  }
},
"minecraft:start_exploding": {
  "add": {
    "component_groups": [
      "minecraft:exploding"
    ]
  }
},
"minecraft:stop_exploding": {
  "remove": {
    "component_groups": [
      "minecraft:exploding"
    ]
  }
}

...

Notice that both events are adding and removing a component group called "minecraft:exploding". Our pig does not have this component group yet!

Adding component groups

Scroll back up to the top of pig.json to the "component_groups" section. Find the last component group, which should be pig_leashed, and add a comma to its closing curly brace. Now either type the following, or copy the "minecraft:exploding" component group from creeper.json. Pig.json should now look like the following (new code in green):

pig.json excerpt

...

  "minecraft:pig_leashed": {
    "minecraft:behavior.move_towards_restriction": {
      "priority": 2,
      "speed_multiplier": 1.0
    }
  },
  "minecraft:exploding": {
    "minecraft:explode": {
      "fuseLength": 1.5,
      "fuseLit": true,
      "power": 3,
      "causesFire": false
    }
  }
},
"components": {
  "minecraft:damage_sensor": {

...

minecraft:explode

fuseLength – how long it takes, in seconds, until this entity explodes from when the fuse is lit

fuseLit – whether the fuse is automatically lit when this component is added

power – this determines the damage done, and how big the explosion radius is

causesFire – this determines whether the explosion will cause nearby blocks to catch on fire or not

Let's add a scale component to this group as well, so that our pig grows in size before it explodes! Add a comma to the closing curly brace of the "explode" component and put in the following (new text in green):

pig.json excerpt

...

  "minecraft:exploding": {
    "minecraft:explode": {
      "fuseLength": 1.5,
      "fuseLit": true,
      "power": 3,
      "causesFire": false
    },
    "minecraft:scale": {
      "value": 2      
    }
  }
},
"components": {
  "minecraft:damage_sensor": {

...

minecraft:scale

Value – a multiplier to the normal scale of the entity. A value of 2 will double the scale, a value of .5 will halve the scale, etc.

Testing our add-on

  • Launch the game
  • Create a new world
  • Set world to creative
  • Turn on cheats
  • Click on add-ons
  • Find "Tutorial Behavior Pack" in the list on the right and click it
  • Click play
  • Spawn a pig using the pig spawn egg from the creative inventory
  • Run 20 blocks or so away
  • Switch mode to survival by using the slash command: /gamemode s
  • Run towards the pig

Congratulations!

You’ve only just scratched the surface of what is possible with Add-ons! If you want to be able to do more than just make things explode, check out the Entity Component Reference Guide to see other behaviors and components you can add to entities! You can also always look at how the Vanilla Behavior pack is set up for examples.

注意保留的不可修改行为

至今,不是某个实体所有的行为都是可编写的数据!你不能改变某些特殊的移动行为,例如蝙蝠、恶魂、烈焰人的飞行行为,或者鱿鱼、守卫者、远古守卫者等的游泳行为。这也会干涉到你可能会向它们加入的行为。例如给予鱿鱼近战攻击(melee attack)行为时,它将不会运行,因为它在水中时它的游泳行为仍然被强行编码在程序中。顺带一提,在任何时候生成另一个实体或者物品是不可能的。比如说鸡生蛋的能力(对于行为包来说)是不可控的数据,你不能修改它。

当然,对于我们行为包作者来说,我们希望这些情况在将来会有所改善——行为包的制作会更加自由,而功能会更强大!

挑战

既然你已经有了一些基础的经验,你可以挑战一下下列任务:

  • 使僵尸可以被玩家骑乘;
    • 再尝试让玩家能够用某种东西控制僵尸的方向。

(注:可以参照猪的item_controllable以及controlled_by_player部件)

  • 使村民在持有弓时转变为骷髅。(注:可以参照狼的 tameable 部件和村民的 transformation 部件。)
  • 改变太阳和月亮的材质(和材质包类似)
  • 使牛可以发射恶魂的烈焰弹。

Troubleshooting JSON

If your JSON changes don’t appear to be working in game, check the following things:

  • Make sure that you’ve typed everything correctly. Typos and missing punctuation will cause unwanted behavior!
  • Make sure the .json file is in the correct folder and named correctly!
    • You should have a folder called entities that contains all your .json files
  • Make sure your entity has the correct identifier
    • Check the .json file in the Vanilla Minecraft pack if you aren’t sure what it is supposed to be
    • Vanilla’s .json files can be found in /Vanilla_Behavior_Pack/entities

Exporting and Sharing

Now that you’ve created a behavior pack, you probably want to share it with friends! To do this, perform the following:

  • Navigate into your behavior pack folder
    • (C:\Users\帐户名称\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang\behavior_packs\tutorial_behavior_pack)
  • Select all of the files and folders
  • Right click on the highlighted files and select send to -> compressed zip folder
  • Name the file tutorial_behavior_pack.mcpack
    • Make sure to change the file extension to .mcpack
    • when the popup asks you if you are sure you want to change the file extension. Hit Yes

You can now give your friends that .mcpack file! When they open it, Minecraft will launch if it isn’t running yet, and then it will import your resource pack for them to use!

Note

You can test to see if this work by deleting your copy in your resource_packs folder (make a backup first!), then opening your mcpack to import it. If it doesn’t work, make sure you selected the contents of your resource_pack and not the folder itself when making the .mcpack zip.

Note 2

If your behavior pack references a resource pack, the mcpack won’t automatically include that resource pack as well, even if you have given the behavior pack_manifest a dependency on that pack. If you have a resource pack dependency either: make a .mcpack for the resource pack as well, or make a world and add the behavior and resource packs you want to export and then export that world. When that world is imported, the save folder for that world will have a folder for the behavior_packs and resource_packs of that world. You can copy those packs to your local folders to be able to see them in the behavior and resource pack menus and add them to other worlds.

Advertisement