You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft Spigot World Edit API:如何实现区域复制粘贴?

关于Spigot中使用WorldEdit API实现区域复制粘贴的问题

现有代码的可用性分析

你提供的代码核心逻辑是可用的,但针对较新的WorldEdit版本(适配Spigot 1.16+)有几处可优化的细节:

  1. Operations.completeLegacy(copy):在WorldEdit 7.x及以上版本中,completeLegacy已被标记为过时,建议替换为Operations.complete(copy),二者功能一致但后者是当前推荐的调用方式。
  2. editSession.flushSession():由于使用了try-with-resources语法声明EditSession,它会在代码块结束时自动关闭并提交会话,手动调用该方法属于冗余操作,可直接移除。
  3. 代码中获取WorldEditPlugin实例但未实际使用,可直接通过WorldEdit.getInstance()获取核心实例,无需额外获取插件对象。

调整后的优化代码如下:

public void cloneWithWorldEdit(Location fromMin, Location fromMax, Location to) throws WorldEditException {
    com.sk89q.worldedit.world.World world = BukkitAdapter.adapt(fromMin.getWorld());

    // 定义源区域
    Region sourceRegion = new CuboidRegion(BukkitAdapter.asBlockVector(fromMin), BukkitAdapter.asBlockVector(fromMax));

    // 定义目标位置
    BlockVector3 toVector = BukkitAdapter.asBlockVector(to);

    // 创建编辑会话(try-with-resources自动管理关闭与提交)
    try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1)) {
        ForwardExtentCopy copy = new ForwardExtentCopy(editSession, sourceRegion, editSession, toVector);
        // 执行复制操作
        Operations.complete(copy);
    }
}

更简便的实现方式

如果仅需基础的区域复制粘贴,还可以利用WorldEdit的ClipboardAPI,写法更直观:

public void copyPasteWithClipboard(Location fromMin, Location fromMax, Location to) throws WorldEditException {
    com.sk89q.worldedit.world.World world = BukkitAdapter.adapt(fromMin.getWorld());
    BlockVector3 min = BukkitAdapter.asBlockVector(fromMin);
    BlockVector3 max = BukkitAdapter.asBlockVector(fromMax);

    // 创建剪贴板并复制源区域内容
    Clipboard clipboard = new CuboidClipboard(max.subtract(min).add(BlockVector3.ONE), min);
    try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1)) {
        // 将剪贴板内容粘贴到目标位置
        clipboard.paste(editSession, BukkitAdapter.asBlockVector(to), false);
    }
}

这种方式无需额外依赖模块,轻量且易读。若后续需要实现旋转、镜像等复杂复制功能,ForwardExtentCopy支持通过setRotation、setMirror等参数快速扩展,灵活性更高。

内容的提问来源于stack exchange,提问作者Zac Clarkson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:42:42