addMap (Filepath, Alpha)
通过本地 BIN 文件路径创建地图。这是最常用的高效导入方式。
long long addMap(std::string path, bool useAlpha = true);
const addMap = ll.import("CustomMap", "addMap");
let uuid = addMap("./test.bin");
log(`导入地图,UUID: ${uuid}`);
addMapNoAlpha (Filepath)
创建不带 Alpha 通道的地图,性能更高,适合不透明图像。
long long addMapNoAlpha(std::string path);
const addMapNoAlpha = ll.import("CustomMap", "addMapNoAlpha");
let uuid = addMapNoAlpha("plugins/MyMap/data/test_no_alpha.bin");
setMap (Filepath, MapId,
Alpha)
修改指定mapid的地图画数据⚠️ 调用后必须发送方块更新包才能刷新客户端显示。
long long setMap(std::string path, int mapId, bool alpha = true);
const setMap = ll.import("CustomMap", "setMap");
// 更新地图 ID 为 12345 的像素
setMap("plugins/MyMap/data/update.bin", 12345, true);
delMap (UUID)
删除指定 UUID 的地图数据,释放服务器内存。
bool delMap(long long uuid);
const delMap = ll.import("CustomMap", "delMap");
const success = delMap(12345);
if (success) log("地图删除成功");
getMapList ()
返回服务器内存中加载的所有 CustomMap UUID 数组。
std::vector<long long> getMapList();
const getMapList = ll.import("CustomMap", "getMapList");
const list = getMapList();
log(`当前世界共有 ${list.length} 张地图`);
list.forEach(uuid => log(`地图 UUID: ${uuid}`));
Python 自动化转换脚本
将 PNG 图片切片并转换为 CustomMap 识别的原始二进制格式(128x128 像素)。
# 需要安装 opencv-python
import cv2
import numpy as np
import os
def convert_png_to_bins(png_path, output_dir, tile_size=128):
"""
将 PNG 图片转换为 Minecraft 地图 BIN 文件
参数:
png_path: 输入 PNG 文件路径
output_dir: 输出目录
tile_size: 每个 BIN 文件的尺寸 (默认 128x128)
"""
# 创建输出目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 读取图片
img = cv2.imread(png_path, cv2.IMREAD_UNCHANGED)
if img is None:
raise ValueError(f"无法读取图片: {png_path}")
# 统一为 RGBA 格式
if len(img.shape) == 2: # 灰度图
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGBA)
elif img.shape[2] == 3: # BGR
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)
elif img.shape[2] == 4: # BGRA
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)
height, width = img.shape[:2]
# 计算需要的行列数
cols = (width + tile_size - 1) // tile_size
rows = (height + tile_size - 1) // tile_size
# 创建白色背景
padded_height = rows * tile_size
padded_width = cols * tile_size
white_bg = np.zeros((padded_height, padded_width, 4), dtype=np.uint8)
white_bg[:, :] = [255, 255, 255, 255] # 白色不透明
white_bg[:height, :width] = img
# 分割并保存每个 tile
for row in range(rows):
for col in range(cols):
# 提取 tile
y_start = row * tile_size
y_end = (row + 1) * tile_size
x_start = col * tile_size
x_end = (col + 1) * tile_size
tile = white_bg[y_start:y_end, x_start:x_end]
# 保存为 BIN 文件
bin_filename = f"frame_{row}_{col}.bin"
bin_path = os.path.join(output_dir, bin_filename)
# 转换为字节并保存
with open(bin_path, 'wb') as f:
f.write(tile.tobytes())
print(f"已生成: {bin_filename} ({tile_size}x{tile_size})")
print(f"\n转换完成!共生成 {rows * cols} 个 BIN 文件")
print(f"尺寸: {cols}列 × {rows}行")
print(f"每个 BIN: {tile_size}x{tile_size} 像素")
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("用法: python png_to_bin.py ")
sys.exit(1)
png_path = sys.argv[1]
output_dir = sys.argv[2]
try:
convert_png_to_bins(png_path, output_dir)
except Exception as e:
print(f"错误: {e}")
sys.exit(1)
JavaScript 完整调用示例
包含方块更新包的完整地图更新流程。
// 导入所有 CustomMap API
const addMap = ll.import("CustomMap", "addMap");
const addMapNoAlpha = ll.import("CustomMap", "addMapNoAlpha");
const setMap = ll.import("CustomMap", "setMap");
const delMap = ll.import("CustomMap", "delMap");
const getMapList = ll.import("CustomMap", "getMapList");
// 发送方块更新数据包(必须)
function sendBlockEntityData(x, y, z, dim, playerName) {
const block = mc.getBlock(x, y, z, dim);
if (!block) return;
const bs = new BinaryStream();
bs.writeBlockPos(block.pos);
bs.writeCompoundTag(block.getBlockEntity().getNbt());
const player = mc.getPlayer(playerName);
if (player) {
player.sendPacket(bs.createPacket(56)); // 56 = BlockEntityData
}
}
// 更新地图函数
function updateMap(path, x, y, z, dim, player) {
if (!File.exists(path)) return false;
const m = path.match(/frame_(\d+)_(\d+)\.bin/);
if (!m) return false;
const row = parseInt(m[1]);
const col = parseInt(m[2]);
const mapId = 100 + row * 10 + col;
// 设置像素
setMap(path, mapId, true);
// ✅ 强制客户端刷新
sendBlockEntityData(
x + col,
y + (3 - row),
z,
dim,
player
);
return true;
}
// 使用示例
log("CustomMap API 示例启动...");
const maps = getMapList();
log(`当前地图总数: ${maps.length}`);