你可以帮助我们来翻译此条目,但请勿使用机器翻译。
概述
这篇文章和“资源包教程”有类似之处. 当然我们也建议您最好先了解一下如何创建一个资源包, 这篇教程可能会忽略一些与资源包的共同点. 下面会讲解如下内容:
- 简述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也不能和行为包重合。如果真的出现相同的情况,则会导致只能导入其中一个包。
制作一个图标
为行为包制作图标的过程其实是和为资源包制作图标一样的, 可以在创建资源包中查看细节。当然,资源包和行为包图标一样也是可以的。
测试自己的行为包
现在可以尝试运行了!创建一个新的世界,在创建世界页面的左边可以看到行为包选项,点进去之后找到你导入的行为包并选中。如果有弹窗就点确定,行为包会导致这个世界不再能够获得成就:
请记住,每当您更改行为包时,您都需要创建一个新世界,因为游戏会在创建世界时制作行为包的副本。同样,如果您修改世界文件夹中包含的加载项,则可以避免这种情况。请记住,如果你这样做,你需要手动将它复制回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.js你将会注意到“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 中有一个随机化函数,可以随机地生成大猪或小猪
Making pigs explode
Adding components
Now that we've looked at how an entity is setup; it is time to actually modify the pig's code!
Find the creeper.json file in the Vanilla Behavior Pack's entity folder (Vanilla_Behavior_Pack/entities/) and open it in a new text editor window. You should now have the pig.json from our custom add-on and creeper.json from Vanilla open in two separate windows.
The first thing we want to do is to have the pig find a target. We can do this by adding the "minecraft:nearest_attackable_target" and "minecraft:target_nearby_sensor" components into the components object for pig.json.
Find "minecraft:behavior.random_look_around" in the components object, it should be the last component before the "events" section.
Add a comma after the closing bracket of random_look_around. This is where we are going to add the targeting component. Either type or copy and paste the target_nearby_sensor and nearest_attackable_target components from creeper.json into pig.json. The end of Pig.json's "components" section should now look something like this (new text is highlighted in green):
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"
}
}
...
Notice that the ending brace for target_nearby_sensor does not have a comma after it because it is the last value in the value list for "components".
minecraft:nearest_attackable_target
This component finds the nearest attack target given certain restrictions.
- Priority – This sets how important this component is. Components with a higher priority will be more likely to be run. Note that a lower number means a higher priority and that priorities can be negative!
- Entity_types – This is where we filter for what types of entities this entity can set as its target.
- Max_dist – if the entity we want to be able to targets is within this many blocks from this entity than we can target it
- Must_see – whether the entity that we want to target must be seen to be able to be set as our target
Notice that in our code, we are filtering for type player and that we will be able to set it as our target once they get within 16 blocks this entity.
minecraft:target_nearby_sensor
This component will trigger events based on how close the entity's target is.
- Inside_range – the distance in blocks that a target is considered to be inside this entity's range
- Outside_range – the distance in blocks that a target is considered to be outside this entity's range
- On_inside_range – this will trigger the event specified when a target is close than inside_range
- On_outside_range – this will trigger the event specified when a target is farther than outside_range
Notice that on_inside_range and on_outside_range are going to be triggering the events "minecraft:start_exploding" and "minecraft:stop_exploding" respectively. This won't currently do anything because we have not added these events yet!
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.
| 版本 |
| ||||||
|---|---|---|---|---|---|---|---|
| 开发 |
| ||||||
| 技术性 | |||||||
| 多人游戏 | |||||||
| 特色功能 | |||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
