API

CraftScript 参考

所有可用的关键字、运算符、函数和成员。

关键字

关键字 用途
let / var / const 声明变量(const 不能重新赋值)。
fun 具名函数和 lambda fun(x) { }。
class, extends, this, super 类与继承。
enum 枚举。
import, from, as 模块。
if, else, while, for … in, break, continue, return 控制流。
and, or, not(或 &&, ||, !) 逻辑运算。
true, false, null 字面量。
mod, item, block, recipe 内容声明。
ore, tree, patch, structure, biome, dimension, terrain, biome_map, carver 世界生成声明。见世界生成和用代码生成。
on … priority 事件处理器。
command … requires … alias … describe 命令。
every, after + ticks/seconds/minutes 计时器。

注释:// 行、# 行、/* 块 */。语句以换行(或 ;)分隔。

运算符

运算符 含义
+ - * / % 算术运算。只要一侧是字符串,+ 就做拼接;它也能连接列表。
== != < <= > >= 比较。id 与字符串表示同一个 id 时视为相等。
= += -= *= /= 赋值。
?? 左侧为 null 时取默认值。
?. 安全访问:返回 null 而不是出错。
a..b 包含两端的数字范围。
x[i] 列表下标(负数从末尾倒数)、映射的键、字符串的字符。

假值:null、false、0、""、空列表或空映射。

内置函数

函数 说明
print(...) 写入服务器日志。
log(...) 与 print 相同,但带上模组名称。
str(x) 转换为字符串。
num(x) 转换为数字(或 null)。
int(x) 取整数部分。
round, floor, ceil, abs, sqrt 数学运算。
min(...), max(...) 数字的最小值/最大值。
sin, cos, atan2, pi() 三角函数,单位为弧度。
len(x) 字符串、列表或映射的长度。
type(x) 类型名称。
is(x, Class) 检查类或枚举。
random(), random(n), random(a, b) 0–1 的小数、0–n 的整数、a–b 的整数。
chance(p) 以概率 p(0–1)返回 true。
pick(list) 随机元素。
now() 当前毫秒数。
id("ns:path") 创建一个标识符。
item(id, count?, name?) 创建一个 ItemStack。
color(text) 把 & 转换为颜色代码。
broadcast(msg) 向所有玩家发送消息。
run(command) 以控制台身份执行游戏命令。兜底手段:命令格式属于游戏,可能会变。
player(name) 在线玩家或 null。
players() 玩家列表。
world(id?) 世界(默认为主世界)。
mod_loaded(id) 另一个模组是否已加载?
config(name, values?, synced?) 声明或读取模组的设置文件。
listen(channel, fun(data, player)) 在模组频道上接收数据。
send(player, channel, data) 从服务器向玩家发送数据。
send_to_server(channel, data) 从客户端向服务器发送数据。
server 服务器。
tick 当前刻。
game_version, namespace Minecraft 版本和模组的命名空间。

字符串成员

length、upper()、lower()、trim()、contains(s)、starts_with(s)、ends_with(s)、split(sep)、replace(a, b)、repeat(n)、substring(from, to?)、is_empty。

列表成员

size、is_empty、first、last、add(x)/push(x)、remove(x)、remove_at(i)、contains(x)、index_of(x)、join(sep)、reverse()、sort()、clear()、map(f)、filter(f)、each(f)、any(f)、all(f)、sum()。

映射成员

keys()、values()、has(k)、get(k, default?)、remove(k)、size;还可以用 map.key 或 map["key"] 直接访问。

游戏对象

名称使用 snake_case(max_health),与 Kotlin 中的成员相同(maxHealth)。枚举以字符串形式使用(player.game_mode = "creative")。各对象的详细说明见世界与玩家。

类型 属性 方法
Player name, uuid, health, max_health, food_level, game_mode, inventory, held_item, is_op, is_sleeping, has_crafting_client, location, world, custom_name, is_alive, data send, action_bar, title, give, set_held_item, play_sound, teleport, heal, damage, has_permission, kick, set_rotation, add_tag, has_tag, set_cooldown, has_cooldown
World id, players, time, is_raining, data block_at, set_block(可带状态), spawn, drop_item, explode, strike_lightning, spawn_particle, play_sound, light_at, container_at, set_sign_text, set_chunk_loaded, entities, living_near, shoot, summon
Block type, x, y, z, world, location, is_solid, data set_type, relative
Entity uuid, type, world, location, custom_name, is_alive, data, direction, vehicle teleport, remove, set_rotation, add_tag, has_tag
LivingEntity Entity 的全部属性, health, max_health damage, heal, set_ai, add_effect, remove_effect, has_effect, effects, add_modifier, remove_modifier, attribute, set_attribute, equipment, set_equipment
Inventory size get, set, add, count, clear
Location world, x, y, z, yaw, pitch, block_x, block_y, block_z add, distance_to
data(存储) keys;data["key"] 可读写 get, set, get_int, get_number, get_string, get_boolean, remove, clear
ItemStack item, count, name, lore, data, enchantments with_count
Server players, worlds, motd, tps, is_dedicated player, world, broadcast, stop, run_command

世界生成对象

世界生成声明中的函数在区块生成时所收到的东西。在用代码生成页面中有说明。

对象 属性 方法
world(结构 build) x, y, z, seed, min_y, max_y block, state, is_air, is_in, can_stay, set(可带状态), inside, surface, top, flow, structures, place_file
plan(结构 plan) chunk_x, chunk_z, seed, min_y height, biome, column, file_size
site(结构 build) x, y, z, radius, min_y, max_y, data, seed
world(terrain) seed, min_y, height biome
world(biome_map) seed tagged
ctx(carver) seed, start_x, start_z, chunk_x, chunk_z, min_y, max_y carve
at(生物群系 surface) x, y, z, depth_above, depth_below, surface_depth, steep noise

命令参数类型

player、int、number、bool、string(一个词)、text(该行的剩余部分)、id。加上 ? 即为可选。

限制

  • 每次调用(事件、命令或计时器)最多 2,000,000 步。
  • 最多 200 层嵌套调用(递归)。
  • 范围最多 1,000,000 个元素。