你可以帮助我们来翻译此条目,但请勿使用机器翻译。
概述
这篇文章和“资源包教程”有类似之处. 当然我们也建议您最好先了解一下如何创建一个资源包, 这篇教程可能会忽略一些与资源包的共同点. 下面会讲解如下内容:
- 简述JSON数据格式
- 如何创建行为包
- 如何改变怪物行为
如果你看过站内关于pig.json的修改教程,那么可能会辅助阅读本篇文章。
所需时间
完成本教程大约需要1至1个半小时。
所需工具
跟随下面给出的步骤继续教程:
文本编辑器
可以使用任意一种文本编辑器,但我建议你使用一些编程用的IDE。Notepad++是一个非常不错的选择,不仅免费,而且能够对编程语法进行高亮显示,你可以从https://notepad-plus-plus.org/下载到Notepad++。
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/.来检查你JSON的合法性;你只需要复制你的JSON代码到该页面,然后点击右边的检查按钮(√)即可。如果检查按钮显示为绿色,则说明你的JSON正常,反之,如果检查按钮为红色,他将尝试告诉你错误的原因。注意!这个网站只能检查这个JSON是否有错误比如漏掉大括号或者中括号等,即使通过也并不能代表你的JSON能够在游戏里正确的运行!我们也会检查你在json文件中写的注释,注释在json中通常是无效的,但是当你在文档中写了"//注释"的时候工具也会告诉你"JSON文件无效",记得在使用工具检查代码前先移除所有注释!
创造一个行为包
行为包作为Add-Ons(外接程序)的一部分,允许你更改一个实体的行为来达到全新的表现效果。不过目前仅支持添加或修改一个实体已经存在的行为(当然这个实体也应该是存在的)。这个简单的教程将向您展示如何将爬行者的爆炸行为添加到猪身上,不过在修改猪之前,我们要先让别人知道自己是这个Add-On的作者。
当然,下面的步骤你也可以用在一个材质包上:
- 将原版行为包复制下来并放到一个专门的文件夹中,命名为“教程行为包”
- 原版行为包除了拆包,可以从官方网站下载
- 找到“manifest.json”
manifest.json
也许你可以参考一下这个json的写法:
manifest.json
{
"format_version": 1,
"header": {
"description": "Example vanilla behavior pack",
"name": "Vanilla Behavior Pack",
"uuid": "ee649bcf-256c-4013-9068-6a802b89d756",
"version": [0, 0, 1]
},
"modules": [
{
"description": "Example vanilla behavior pack",
"type": "data",
"uuid": "fa6e90c8-c925-460f-8155-c8a60b753caa",
"version": [0, 0, 1]
}
],
"dependencies": [
{
"uuid": "743f6949-53be-44b6-b326-398005028819",
"version": [0, 0, 1]
}
]
}
当然,你完全可以把 “manifest.json”复制进你的行为包文件夹并修改它。如果你需要让你行为包配套材质绑定到行为包上,就要让行为包中“dependencies”里面的“uuid” 和材质包中modules的“uuid”相同。
注意:
单从文件名称和大致结构看来材质包和行为包的“manifest.json”似乎是完全一样的。但是modules中的“type”决定了它是那种类型的包,行为包对应“data”,材质包对应“resources”。不过与此同时,你需要让header和modules的UUID不同,同样的材质包的所有UUID也不能和行为包重合。如果真的出现相同的情况,则会导致只能导入其中一个包。
制作一个图标
为行为包制作图标的过程其实是和为资源包制作图标一样的, 可以在Creating Resource Packs 中查看细节. 当然,资源包和行为包图标一样也是可以的.
测试自己的行为包
现在可以尝试运行了!创建一个新的世界. 在创建世界页面的左边可以看到行为包选项. 点进去之后找到你导入的行为包并选中. 如果有弹窗就点确定, 行为包会导致这个世界不再能够获得成就:
请记住,每当您更改行为包时,您都需要创建一个新世界,因为游戏会在创建世界时制作行为包的副本。同样,如果您修改世界文件夹中包含的加载项,则可以避免这种情况。请记住,如果你这样做,你需要手动将它复制回com.mojang / behavior_packs文件夹,如果你想将这个包应用到其他世界。
设置JSON文件结构
现在我们的这个包所需的东西已经准备好了,让我们把这些json文件放进一个文件夹里. Now that our pack is set up, let’s set up the folder structure for our .json files.
- 在这个名为"tutorial_behavior_pack"的文件夹里,新建一个名为“entities”的文件夹.
- In the tutorial_behavior_pack folder, create a new folder called “entities”
The entities folder is where we will put all our JSON files for changing entity behaviors. Let’s start by grabbing pig.json from \Vanilla_Behavior_Pack\entities\pig.json and copying it into our entities folder. Once pig.json is copied over, open it up in the text editor of your choice.
Entity JSON Overview
Before we start modifying the JSON code, let’s take a brief look at how our entity.json files are set up.
The JSON files for Minecraft entities are broken into 4 different sections:
“minecraft:entity”
This is the main object for every mob, all the behaviors and events that follow are the values contained in this object.
Entity.json pseudocode:
Entity Layout Pseudocode:
{
"minecraft:entity": {
"format_version": 0.1,
"component_groups": { groups go here },
"components": { components go here },
"events": { events go here }
}
}
Note:
This is not valid JSON for Minecraft! This is just an example of how our entity json files are laid out to use as reference. If you take a look at pig.json you will notice that “component groups go here” is actually filled in with components.
Note 2 - “format_version”:
Format version is used to tell Minecraft what version of the JSON it should be trying to read. As we update our JSON and add new components, we need to tell the game what things it should be looking for. We do this by changing the format_version number.
DO NOT CHANGE THIS NUMBER! As the Minecraft JSON updates, you will need to update your JSON files to match what was added. However, if you do update your JSON, you will need to change this number to whatever the new number is. Alternatively, you can copy the new version of the JSON from the updated Vanilla files and then copy over the specific behaviors you had changed in the old version.
Component groups
Used to define groups of behavior, allow for easy removal/addition of multiple behaviors, useful for making states (i.e. Pig baby vs pig adult behaviors).
In pig.json there are 6 groups including pig_baby, pig_adult. Note that the behaviors in those groups will only run if the group is currently added to the entity. Thus, when the pig is a baby pig, the pig_baby component group will be added and running, but the pig_adult group will not be.
Components
Used to define individual behaviors that will run on every entity of this type.
Note that things like “minecraft:identifier” and “minecraft:type_family” are here because we want every type of pig to have these components.
Events
Events are used to add or remove component groups and are triggered by components we’ve added, or by Minecraft’s native code.
Events can use a randomize function to pick between different results (see “minecraft:entity_spawned” in pig.json). Note that the weights for randomize can add to over 100. This is because they are not percentages! If you set the weight for making an adult pig to 23 and then the weight for making a pig baby to 100 then you will have a 23/123 chance of getting an adult pig and a 100/123 chance to get a baby pig when a pig is spawned.
Components of note:
“minecraft:identifier”
This is required for every entity. This is how the game knows what entity this .json file will run behaviors for. The identifiers must be named appropriately! If you give an incorrect identifier you will either get unintended results or your entity may not work at all. If you aren’t sure what the identifier should be for an entity, please refer to the json file for that entity in the Vanilla Behavior Pack (remember json files are in /Vanilla_Behavior_Pack /entities/)
“minecraft:type_family”
This sets the family type for the entity. Family types are used for filtering entities for various components. See “minecraft:rideable” and “minecraft:breedable” for examples where type is used!
“minecraft:entity_spawned”
This is an event that is triggered when an entity of this type is spawned. In pig.json this has a randomize function that determines whether the pig is going to spawn as a baby or an adult pig.
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 grey):
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 tells 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 grey):
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 grey):
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 grey):
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.
Note on available behavior changes
Not all behaviors of a mob are data driven yet! You will not be able to change the details for special movement behaviors like flying (bat, ghast, blaze) or swimming (squid, guardian, guardian elder). This can also interfere with behaviors you might add to them. For example, giving the squid a melee attack will not work while it is in water as its swimming behavior is still hard coded. Additionally it is not possible to just spawn another mob or item at any time, for example the Chicken’s ability to lay eggs is not data driven yet and you will be unable to control it.
Look forward to these changes in the future!
Challenges
Now that you’ve gotten some basic experience, here are some challenges:
- Make zombies rideable by the player
- Have the player able to control direction with an item (hint: look at pig’s item_controllable and controlled_by_player)
- Make villagers transform into skeletons when given a bow (hint: look at wolf’s tameable and villagers transformation)
- Change the texture of the sun and moon
- Make a cow shoot the ghast’s projectile
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\USERNAME\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.
| 版本 |
| ||||||
|---|---|---|---|---|---|---|---|
| 开发 |
| ||||||
| 技术性 | |||||||
| 多人游戏 | |||||||
| 特色功能 | |||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
