本站文本内容除另有声明外,转载时均必须注明出处。(详情…本站文本内容除另有声明外,转载时均必须注明出处。(详情…中文Minecraft Wiki是完全公开的。请勇于扩充与修正内容!中文Minecraft Wiki是完全公开的。请勇于扩充与修正内容!Minecraft中文Wiki微博正在更新!或许有兴趣去看看Minecraft中文Wiki微博正在更新!或许有兴趣去看看想与其他用户进行编辑上的沟通?社区专页正是为此创建的。想与其他用户进行编辑上的沟通?社区专页正是为此创建的。翻译或创建页面之前,不妨看看译名标准化Wiki条例页面。翻译或创建页面之前,不妨看看译名标准化Wiki条例页面。需要管理员的协助?在管理员告示板留言也许可以帮到您。需要管理员的协助?在管理员告示板留言也许可以帮到您。

基岩版beta MoLang文档

来自Minecraft Wiki
跳转至: 导航搜索
Information icon.svg
此特性为基岩版独有。


Ic translate.svg
此条目的(部分)内容需要被翻译。
你可以帮助我们来翻译此条目。
请勿使用机器翻译,劝阻无效将予以封禁处理。

版本:1.12.0.2[编辑 | 编辑源代码]

这是 Minecraft 发布的的一个测试版的文档。这个版本中的新特性、组件和功能都不是最终版本,并且可能在最终版本发布之前有所更改而不做任何提醒。 如果你的附加包在测试结束之后出现运行错误,请务必重新核对本文档。因为在这个测试版中所写的资源包和行为包都不确保能够在最终版本中运行。

为什么会有Molang?[编辑 | 编辑源代码]

Molang是一种基于表达式的简单语言,它被设计用于在运行时快速计算值。它的重点仅仅是在JavaScript不具备大规模性能的高性能系统中实现类似脚本的功能。 我们需要这些低级系统中的脚本功能,以支持最终用户修改功能,自定义实体,渲染和动画。



语法结构[编辑 | 编辑源代码]

语言结构主要基于简单的C风格语法,侧重于处理数学表达式。一般情况下,脚本由一个表达式组成,或者可以由需要中间值的几个表达式组成,有助于减少计算时间。

在所有情况下,脚本中最后一个表达式的值都提供了脚本的值。 在多表达式脚本中,除最后一个表达式之外的所有表达式都必须为变量赋值。 最后一个表达式也可以赋值给变量,但不需要,因为它的值被假定用作返回值。

[编辑 | 编辑源代码]

- 值的类型是具体指定的,否则为数值。
- 所有数值都是浮点数。
- 布尔值(如actor标志)分别转换为0.0或1.0的浮点值(值为false或true)。
- 对于布尔值,等于0.0的浮点值为false,不等于0.0的值为true。
- 对于数组索引,浮点数是C风格的强制转换为int,负值的浮点数被限制为零,大值的浮点数被数组大小所包围。
- 其他受支持的类型是纹理、材质和几何图形(如渲染控制器)。
- 发生错误通常返回值0.0。

变量[编辑 | 编辑源代码]

变量可归属于以下的域:

参数
范围 例子
temp 当前表达式 temp.foo = math.sin(query.anim_time); return temp.foo * temp.foo;
variable 可读写变量,通常与当前实体有关 variable.my_saved_var = variable.my_saved_var + 1;
query 只读变量,通常与当前实体有关 query.is_baby
geometry 当前渲染控制器 "geometry": "array.geos[query.is_sheared]"
material 当前渲染控制器 "materials": [ { "*": "material.default" }, { "leg*": "material.legs" } ]
texture 当前渲染控制器 "textures": ["array.skins[query.is_saddled]"]

关键字[编辑 | 编辑源代码]

下面列举出来的标识为将来使用保留

参数
关键字 描述
`float` `数字常量`
`( )` `作控制表达式术语赋值用的小括号`
`[ ]` `作数组通道用的中括号`
`query.function_name` `指向实体特性的通道`
`math.function_name` `各式数学函式(见下)`
`temp.variable_name` `储存当前表达式持续时间的中值`
`variable.variable_name` `为后续使用储存关联实体的值`
`geometry.texture_name` `对实体定义中被命名材质的参考`
`material.texture_name` `同上`
`texture.texture_name` `同上`
`! && < <= >= > == !=` `逻辑运算符`
`* / + -` `基础数学运算符`
`test ? if true : if false` `条件运算符`
`this` `在执行脚本前的当前变量(上下文敏感)`
`return` `对于复杂的表达式,它会对下述的语句进行估值并停止脚本的执行,返回计算出的值`

数学函数[编辑 | 编辑源代码]

`

参数
函数 描述
`math.abs(value)` `值的绝对值`
`math.sin(value)` `根据角度得出的正弦值`
`math.cos(value)` `根据角度得出的余弦值`
`math.exp(value)` `计算e的值的幂`
`math.ln(value)` `值的自然对数`
`math.pow(base, exponent)` `将基数提升为指数幂`
`math.sqrt(value)` `值的平方根`
`math.random(low, high)` `在最低和最高间的随机值`
`math.ceil(value)` `将值调高到最接近的整数`
`math.round(value)` `四舍五入到最近的整数`
`math.trunc(value)` `Round value towards zero`
`math.floor(value)` `将值调低到最接近的整数`
`math.mod(value, denominator)` `返回值除以分母的余数`
`math.min(A, B)` `返回A或B的最小值`
`math.max(A, B)` `返回A或B的最大值`
`math.clamp(value, min, max)` `Clamp value to between min and max inclusive`
`math.lerp(start, end, 0_to_1)` `Lerp from start to end via 0_to_1`
`math.lerprotate(start, end, 0_to_1)` `Lerp the shortest direction around a circle from start degrees to end degrees via 0_to_1`

类型,值和变量[编辑 | 编辑源代码]

总的来说,所有表达式的值均为浮点数。在渲染控制器中,一些表达式会引起材质或材料依照于其语境。所有的数组指数表达式均按浮点数来处理并在执行对数组的最后查阅时投入于整型数中。正数组会根据数组大小来索引换行,而负数组则索引固定的0。

简单与复杂表达式[编辑 | 编辑源代码]

简单表达式是一条语句,其值将返回到计算表达式的系统。例子:

math.sin(query.anim_time * 1.23)


复杂表达式是一个包含多个语句的表达式,每个语句以“;”结尾。每个语句按顺序进行计算。在当前实施中,最后一条语句要求使用返回关键字并定义表达式的结果值。例子:

temp.my_temp_var = Math.sin(query.anim_time * 1.23);
temp.my_other_temp_var = Math.cos(query.life_time + 2.0);
return temp.my_temp_var * temp.my_temp_var + temp.my_other_temp_var;


注意,在简单表达式中不允许使用“;”,而在复杂表达式中,每个语句都需要一个“;”,包括最后一个语句。

域示例[编辑 | 编辑源代码]

实体定义脚本[编辑 | 编辑源代码]

在定义文件中,有一个用作为预计算值的部分。这些在动画和渲染控制器处理前立刻执行,并储存在实体内。目的是预先计算任何你在将来想要重新在脚本、长时指数变量更新、或基本上任何每一个渲染刻中的一次性计算中所使用的贵重的和复杂的值。

"scripts": {
    "pre_animation": [
      "variable.my_constant = (Math.cos(query.modified_distance_moved * 38.17) * query.modified_move_speed;",
      "variable.my_constant2 = Math.exp(1.5);",
    ]
  },


动画和动画控制器文件[编辑 | 编辑源代码]

这些文件总是为用来控制播放哪一个动画和如何将骨骼动化的数值运算。"variable.variable_name"和"query.function_name"涉及到目前所渲染的实体。这些文件可通过任何语言,除了物质(material)、材质(texture)、和几何(geometry)类型。


渲染控制器[编辑 | 编辑源代码]

There are a few different kinds of expressions here, where context implies what is allowed. As with animations, the entity accessors refer to the current entity, however depending on the context one also has access to materials, textures, and geometries. There are two sections in a render controller:
-Array definitions (optional)
-Resource usage (required)
The array definition section allows you to create arrays of resources by resource type if you so desire. These can then be referenced in the resource usage section.

数组表达式

For each of the three resource types (materials, textures, and geometry), you can define an array of resources. The name of the resource is the nice-name from the definition file. Using materials as an example:

"arrays":
{
  "materials": {
    "array.my_array_1": ["material.a", "material.b", "material.c"],
    "array.my_array_2" : ["material.d", "material.e"],
    "array.my_array_3" : ["array.my_array_1", "material.my_array_2"],
    "array.my_array_4" : ["array.my_array_2", "material.my_array_3"],
    "array.my_array_5" : ["array.my_array_1", "material.my_array_1", "material.my_array_4"],
    "array.my_array_6" : ["array.my_array_1", "material.f"],
    ...
  },

Note that all elements of an array must be of the same type. eg: a texture array must only contain textures.

An array can reference any combination of zero or more arrays (including duplicates if desired) and/or zero or more materails (again, including duplicates if you like), and you can have as many arrays as you like, each with as many elements as you like. If an array includes arrays in its members, they do not need to be the same length. When indexing into an array in the resource usage section, you use numerical expressions. If the resulting number is negative, it will use zero as the index. Any non - negative index will converted to an integer, and will wrap based on the size of the array:

index = max(0, expression_result) % array_size



资源表达式

A resource expression must return a single resource of a specific type depending on the context.
For example, in the "geometry" section, you must produce an expression that will result in a single geometry. Some examples:

Always use a specific geometry
"geometry": "geometry.my_geo"
Cycle through an array of geometries at a rate of one per second
"geometry": "array.my_geometries[query.anim_time]"
Pick a geo based on an entity flag
"geometry": "query.is_sheared ? geometry.sheared : geometry.woolly"
Use specific geo when sleeping, otherwise flip through an array based on a cosine curve, using index zero for almost half the time while the cosine curve is negative
"geometry": "query.is_sleeping ? geometry.my_sleeping_geo : array.my_geos[math.cos(query.anim_time * 12.3 + 41.9) * 10 + 0.6]"


Resource Sections

Geometry 

The geometry section specifies which geometry to use when rendering. As you can specify as many render controllers as you like in the definition file, a single render controller is only concerned with how to render a single geometry. Note that a geometry can be arbitrarily complex using any number of bones and polygons.


Materials 

The materials section specifies how to map what material to what bone of the geometry. A single material is mapped to a whole bone. Material expressions are evaluated in the order listed. The first part of each statement is the name of the model part to apply the material to, and the second part is the material to use. The model part name can use * for wild - card matching of characters. For example :

  "materials": [
      { "*": "Material.default" },
      { "TailA": "array.hair_colors[variable.hair_color]" },
      { "Mane": "array.hair_colors[variable.hair_color]" },
      { "*Saddle*": "variable.is_leather_saddle ? material.leather_saddle : material.iron_saddle" }
    ],

- This will start by applying Material.default to all model parts.
- Next, it will set the material on a model part named "TailA" to the result of the expression "Array.hairColors[variable.hair_color]". This will look up some previously created variable on the entity named hair_color and use that to index into a material array called "array.hair_colors" defined in this render controller. This will overwrite the Material.default material set in the line above.
- Third, it will look up the same material as the expression is identical, and apply it to the "Mane" model part.
- Lastly, if will find any model part starting with, ending with, or containing "Saddle" (case sensitive) and change its material to either material.leather_saddle or material.iron_saddle depending on the previously set entity variable variable.is_leather_saddle.

查询函数[编辑 | 编辑源代码]

查询函数是一种允许在不同情况下对引擎中数值进行查询的布尔函数。它们可以在MoLang表达式中被运用以对位置变化、材质及动画等进行有效控制。例如,如果一个生物处于幼年:

"position": [ 0.0, "query.is_baby ? -8.0 : 0.0", "query.is_baby ? 4.0 : 0.0" ]

实体查询列表[编辑 | 编辑源代码]

名称 描述
query.all_animations_finished 仅在动画控制器中可用。若当前的动画控制器状态中所有的动画都被至少播放了一次,则返回1.0,否则返回0.0
query.anim_time 在当前动画开始播放时返回秒计时值,若未调用动画则返回0.0
query.any_animation_finished 仅在动画控制器中可用。若当前的动画控制器状态中任一个动画被至少播放了一次,则返回1.0,否则返回0.0
query.armor_color_slot 将盔甲槽指数设为参数,返回所请求槽位中盔甲的颜色
query.armor_material_slot 将盔甲槽指数设为参数,返回所请求槽位中盔甲的材料类型
query.armor_texture_slot 将盔甲槽指数设为参数,返回所请求槽位中盔甲的材质类型
query.blocking 若实体正在阻碍,则返回1.0,否则返回0.0
query.can_climb 若实体能够攀爬,则返回1.0,否则返回0.0
query.can_fly 若实体能够飞行,则返回1.0,否则返回0.0
query.can_power_jump 若实体能够蓄力跳跃,则返回1.0,否则返回0.0
query.can_swim 若实体能够游泳,则返回1.0,否则返回0.0
query.can_walk 若实体能够行走,则返回1.0,否则返回0.0
query.current_squish_value Returns the squish value for the current entity, or 0.0 if this doesn't make sense
query.delta_time Returns the time in seconds since the previous frame
query.frame_alpha Returns the ratio (from 0 to 1) of how much between AI ticks this frame is being rendered
query.ground_speed Returns the ground speed of the entity in metres/second
query.has_armor_slot Takes the armor slot index as a parameter, and returns 1.0 if the entity has armor in the requested slot, else it returns 0.0
query.has_collision Returns 1.0 if the entity has collisions enabled, else it returns 0.0
query.has_gravity Returns 1.0 if the entity is affected by gravity, else it returns 0.0
query.has_rider Returns 1.0 if the entity has a rider, else it returns 0.0
query.has_target Returns 1.0 if the entity has a target, else it returns 0.0
query.head_x_rotation Takes one argument as a parameter. Returns the nth head x rotation of the entity if it makes sense, else it returns 0.0
query.head_y_rotation Takes one argument as a parameter. Returns the nth head y rotation of the entity if it makes sense, else it returns 0.0
query.invulnerable_ticks Returns the number of ticks of invulnerability the entity has left if it makes sense, else it returns 0.0
query.is_angry Returns 1.0 if the entity is angry, else it returns 0.0
query.is_baby Returns 1.0 if the entity is a baby, else it returns 0.0
query.is_breathing Returns 1.0 if the entity is breathing, else it returns 0.0
query.is_bribed Returns 1.0 if the entity has been bribed, else it returns 0.0
query.is_carrying_block Returns 1.0 if the entity is carrying a block, else it returns 0.0
query.is_casting Returns 1.0 if the entity is casting, else it returns 0.0
query.is_charged Returns 1.0 if the entity is charged, else it returns 0.0
query.is_charging Returns 1.0 if the entity is charging, else it returns 0.0
query.is_chested Returns 1.0 if the entity has chests attached to it, else it returns 0.0
query.is_critical Returns 1.0 if the entity is critical, else it returns 0.0
query.is_dancing Returns 1.0 if the entity is dancing, else it returns 0.0
query.is_eating Returns 1.0 if the entity is eating, else it returns 0.0
query.is_elder Returns 1.0 if the entity is an elder version of it, else it returns 0.0
query.is_enchanted Returns 1.0 if the entity is enchanted, else it returns 0.0
query.is_fire_immune Returns 1.0 if the entity is immune to fire, else it returns 0.0
query.is_gliding Returns 1.0 if the entity is gliding, else it returns 0.0
query.is_grazing Returns 1.0 if the entity is grazing, or 0.0 if not
query.is_idling Returns 1.0 if the entity is idling, else it returns 0.0
query.is_ignited Returns 1.0 if the entity is ignited, else it returns 0.0
query.is_in_love Returns 1.0 if the entity is in love, else it returns 0.0
query.is_in_water Returns 1.0 if the entity is in water, else it returns 0.0
query.is_in_water_or_rain Returns 1.0 if the entity is in water or rain, else it returns 0.0
query.is_interested Returns 1.0 if the entity is interested, else it returns 0.0
query.is_invisible Returns 1.0 if the entity is invisible, else it returns 0.0
query.is_jumping Returns 1.0 if the entity is in water or rain, else it returns 0.0
query.is_laying_down Returns 1.0 if the entity is laying down, else it returns 0.0
query.is_laying_egg Returns 1.0 if the entity is laying an egg, else it returns 0.0
query.is_leashed Returns 1.0 if the entity is leashed to something, else it returns 0.0
query.is_lingering Returns 1.0 if the entity is lingering, else it returns 0.0
query.is_moving Returns 1.0 if the entity is moving, else it returns 0.0
query.is_on_ground Returns 1.0 if the entity is on the ground, else it returns 0.0
query.is_onfire Returns 1.0 if the entity is on fire, else it returns 0.0
query.is_orphaned Returns 1.0 if the entity is orphaned, else it returns 0.0
query.is_powered Returns 1.0 if the entity is powered, else it returns 0.0
query.is_pregnant Returns 1.0 if the entity is pregnant, else it returns 0.0
query.is_resting Returns 1.0 if the entity is resting, else it returns 0.0
query.is_riding Returns 1.0 if the entity is riding, else it returns 0.0
query.is_rolling Returns 1.0 if the entity is rolling, else it returns 0.0
query.is_saddled Returns 1.0 if the entity has a saddle, else it returns 0.0
query.is_scared Returns 1.0 if the entity is scared, else it returns 0.0
query.is_shaking Returns 1.0 if the entity is casting, else it returns 0.0
query.is_sheared Returns 1.0 if the entity is able to be sheared and is sheared, else it returns 0.0
query.is_shield_powered Returns 1.0f if the entity has an active powered shield if it makes sense, else it returns 0.0
query.is_silent Returns 1.0 if the entity is silent, else it returns 0.0
query.is_sitting Returns 1.0 if the entity is sitting, else it returns 0.0
query.is_sleeping Returns 1.0 if the entity is sleeping, else it returns 0.0
query.is_sneaking Returns 1.0 if the entity is sneaking, else it returns 0.0
query.is_sneezing Returns 1.0 if the entity is sneezing, else it returns 0.0
query.is_sprinting Returns 1.0 if the entity is sprinting, else it returns 0.0
query.is_standing Returns 1.0 if the entity is standing, else it returns 0.0
query.is_swimming Returns 1.0 if the entity is swimming, else it returns 0.0
query.is_tamed Returns 1.0 if the entity is tamed, else it returns 0.0
query.is_transforming Returns 1.0 if the entity is transforming, else it returns 0.0
query.is_using_item Returns 1.0 if the entity is using an item, else it returns 0.0
query.is_wall_climbing Returns 1.0 if the entity is climbing a wall, else it returns 0.0
query.key_frame_lerp_time Returns the ratio between the previous and next key frames
query.lie_amount Returns the lie down amount for the entity
query.life_time Returns the time in seconds since the current animation started, else 0.0 if not called within an animation
query.mark_variant Returns the entity's mark variant
query.max_trade_tier Returns the maximum trade tier of the entity if it makes sense, else it returns 0.0
query.model_scale Returns the scale of the current entity
query.modified_distance_moved Returns the total distance the entity has moved horizontally in metres (since the entity was last loaded, not necessarily since it was originally created) modified along the way by status flags such as is_baby or on_fire
query.modified_move_speed Returns the current walk speed of the entity modified by status flags such as is_baby or on_fire
query.overlay_alpha Do not use - this function is deprecated and will be removed
query.previous_squish_value Returns the previous squish value for the current entity, or 0.0 if this doesn't make sense
query.roll_counter Returns the roll counter of the entity
query.sit_amount Returns the current sit amount of the entity
query.sneeze_counter Returns the sneeze counter of the entity
query.spellcolor.b Returns the blue colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.spellcolor.g Returns the green colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.spellcolor.r Returns the red colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.standing_scale Returns the scale of how standing up the entity is
query.swell_amount Returns how swollen the entity is
query.swelling_dir Returns the swelling direction of the entity if it makes sense, else it returns 0.0
query.target_x_rotation Returns the x rotation required to aim at the entity's current target if it has one, else it returns 0.0
query.target_y_rotation Returns the y rotation required to aim at the entity's current target if it has one, else it returns 0.0
query.time_stamp Returns the current time stamp of the level
query.trade_tier Returns the trade tier of the entity if it makes sense, else it returns 0.0
query.unhappy_counter 返回实体的恼怒值
query.variant 返回实体的变量指数
query.wing_flap_position 返回实体的挥翅位置,若无效则返回0.0
query.wing_flap_speed 返回实体的挥翅速度,若无效则返回0.0
query.yaw_speed 返回实体的偏航速度