完整版BedrockWiki镜像!

This commit is contained in:
boybook
2025-03-20 11:52:46 +08:00
parent 1994c41f01
commit bf9aa4b056
214 changed files with 9042 additions and 8867 deletions

View File

@@ -1,7 +1,7 @@
---
title: Molang
tags:
- intermediate
- 中级
mentions:
- yanasakana
- TheDoctor15
@@ -11,37 +11,56 @@ mentions:
- TheItsNameless
---
## Introduction
Pretty much everything evaluates to a number; if something doesn't evaluate to a number, you can use an `operator` to make it into one. You can basically just think of Molang as one big math equation.
# Molang
An equation evaluates to `true` when any number except `0` is returned. When I reference `returning`, I'm talking about the output of an equation. There is also a `return` statement, but I don't usually use it, and will therefore not be talking about it.
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
## Accessing Values
There are three main ways to access and use values in Molang (queries, variables and temp variables)
## 简介
几乎所有的表达式都会求值为一个数字。如果某个表达式的结果不是数字,可以使用`operator`运算符将其转换为数字。你可以将Molang简单理解为一个大型数学方程式。
- **Queries** are read only values returned by the game. You cannot set these values, only read them. (`query.example_query` | `q.example_query`)
当方程式返回除`0`以外的任何数字时,都会被判定为`true`(真)。本文中提到的"返回"指的是方程式的输出结果。虽然存在`return`语句,但通常不推荐使用,因此不做详细讨论。
- **Variables** are read and write values that you can manipulate, these can be set and read through Molang. (`variable.example_variable` | `v.example_variable`)
- There are also hard-coded variables which act practically the same way as queries, but can only be used in certain situations.
## 值访问方式
在Molang中主要有三种访问值的方式查询语句、变量和临时变量
- **Temp. Variables** are practically the same as variables, except they only exist in the current scope. (`temp.example_temp` | `t.example_temp`)
- A "scope" can refer to the current `for_each` or `loop` *or* just the current expression, if it's not used within either
- **查询语句** 是从游戏获取的只读值,不可修改只能读取(语法:`query.example_query` 或简写为 `q.example_query`
## Handling values
- **变量** 是可读写的值可以通过Molang进行修改语法`variable.example_variable` 或简写为 `v.example_variable`
- 部分硬编码变量的行为与查询语句类似,但仅在特定情境下可用
- **Logical Operators** can be used to convert non-numbers into 1s or 0s. These include: `==`, `!=`, `<`, `>`, `<=`, `>=`.
- Example.) "`q.get_equipped_item_name == 'stick'`" Will evaluate to `1`/`true` when holding a stick
- **临时变量** 的功能与普通变量相同,但仅在当前作用域内有效(语法:`temp.example_temp` 或简写为 `t.example_temp`
- "作用域"指当前的`for_each`循环、`loop`循环,若未在循环中使用则指当前表达式
- There is also a *second* set of *Logical Operators* which can be used to 'group' values into `and/or` statements, often used in cases where you need *multiple* things to evaluate to `true` or just *one out of many*. `&&` represents an `and` statement, and `||` represents an `or` statement.
- Example.) "`q.is_sneaking && q.is_using_item`" Will evaluate to `1`/`true` when sneaking *and* using an item
- Example.) "`q.is_sneaking || q.is_jumping`" // Evaluates to `1`/`true` when either jumping *or* sneaking
## 值处理
- **Parentheses**, `( )`, are also a major help when grouping values or performing math operations.
- Example.) "`q.is_sneaking && (q.get_equipped_item_name == "stick" || q.get_equipped_item_name == "diamond")`" Will evaluate to `1`/`true` when sneaking *and* holding either a stick *or* a diamond
- **逻辑运算符** 可以将非数字值转换为1或0包括`==`, `!=`, `<`, `>`, `<=`, `>=`
- 示例:`q.get_equipped_item_name == 'stick'` 当手持木棍时求值为`1`/`true`
- **Conditional Operators** can be used as `if/else` statements.
- A *binary* conditional operator refers to just using `?`. When this is used, it'll output your value or `0` depending on whether the given input value is `true`.
- Example.) "`q.is_sneaking ? 5`" Will output a `5` when sneaking, otherwise returning a `0`
- A *trinary* conditional operator refers to using `?` and `:`. When this is used, it'll output one of the two given values depending on whether your given input value is `true`.
- Example.) "`q.is_sneaking ? 10 : 3`" Will output a `10` when sneaking, otherwise returning a `3`
- **复合逻辑运算符** 用于构建"与/或"逻辑关系:
- `&&` 表示"与"`||` 表示"或"
- 示例:`q.is_sneaking && q.is_using_item` 潜行且使用物品时返回`1`/`true`
- 示例:`q.is_sneaking || q.is_jumping` 潜行或跳跃时返回`1`/`true`
- **圆括号** `( )` 在组合值或进行数学运算时非常实用
- 示例:`q.is_sneaking && (q.get_equipped_item_name == "stick" || q.get_equipped_item_name == "diamond")` 潜行时手持木棍或钻石返回`1`/`true`
- **条件运算符** 可实现类似`if/else`的逻辑:
- **二元条件运算符** `?` 根据输入值返回指定值或0
- 示例:`q.is_sneaking ? 5` 潜行时返回`5`,否则返回`0`
- **三元条件运算符** `? :` 根据条件返回两个指定值之一
- 示例:`q.is_sneaking ? 10 : 3` 潜行时返回`10`,否则返回`3`
::: code-group
```json [示例]
{
"format_version": "1.16.100",
"minecraft:entity": {
"components": {
"minecraft:movement": {
"value": "q.is_jumping ? 0.1 : 0.05" // 跳跃时移动速度变为0.1否则0.05
}
}
}
}
```
:::