资源与字体
全局 Resources 对象:读文件、读字体、读图、打日志。
路径
| 方法 | 说明 |
|---|
Resources.id(text) | 构造资源路径,等同 identifier() |
Resources.identifier(text) | 同上,全称写法 |
Resources.idRelative(text) | 相对当前脚本所在命名空间构造 |
Resources.idr(text) | 同上,简写 |
var path = Resources.id("mypack:textures/lcd.png");
读取
| 方法 | 返回 | 说明 |
|---|
readString(id) | String | 按文本读 |
readStream(id) | InputStream | 按流读 |
readBufferedImage(id) | BufferedImage | 读图片 |
readFont(id) | Font | 读字体文件 |
hasResource(id) | boolean | 是否存在 |
parseNbtString(text) | CompoundTag | 解析 NBT 字符串 |
readJsonResource(path) | String | 读 JSON 文本 |
listResources(prefix) | String[] | 按前缀列出资源 |
字体
| 方法 | 说明 |
|---|
readFont(id) | 从资源包读字体文件 |
getSystemFont(name) | 取系统字体 |
getFontRenderContext() | 取字体渲染上下文,用于测量文本尺寸 |
ensureStrFonts(text, font) | 为文本准备字体,处理缺字回退 |
clearFontCache() | 清空字体缓存 |
var f = Resources.readFont(Resources.id("mypack:font/lcd.ttf"));
var big = f.deriveFont(48.0);
中文字体缺字时用 ensureStrFonts(),它会处理回退,避免出现方块。
脚本组织
| 方法 | 说明 |
|---|
includeScript(pathOrId) | 引入另一个脚本文件 |
print(...) | 写日志,可传多个参数 |
getRTEVersion() | 取 RTE 版本号 |
getNTEVersion() | 取 NTE 版本号,未安装时可用于分支 |
Resources.print("车速", train.speedKmh(), "下一站", train.nextStationName());
print 在 render 里必须限流。
一辆 8 编组的车每秒能产生几百行,几辆车就能把 latest.log 撑到几百 MB。