本篇教程由作者设定未经允许禁止转载。

教程声明:作者也只是一个kjs新手,而且使用手机魔改,技术力低下,如果用错误请见谅

开始先展示代码

BlockEvents.rightClicked('epicadventure:altar_of_moying', event => {
    if (event.hand == "OFF_HAND") return;
    const player = event.player;
    if (!player) return;

    const mainHandItem = player.getMainHandItem();
    if (mainHandItem.getId() === 'cataclysm:void_core') {
        const moyingg = event.level.createEntity('cataclysm:ender_guardian');
        moyingg.setPosition(
            player.x + 5,
            player.y + 1,
            player.z + 5
        );
        moyingg.spawn();
    }
});

代码解释

BlockEvents.rightClicked

为KubeJS事件中的方块右键事件,而'epicadventure:altar_of_moying'为玩家右键的方块,可更换成你的方块

const player = event.player

定义player,也可以使用 const { player } = event,把player从event中解构出来

if (!player) return;

如果右键方块的不是玩家则返回

const mainHandItem = player.getMainHandItem();

const定义常量

if (mainHandItem.getId() === 'cataclysm:void_core')

检测玩家手中是否为指定物品,物品ID也可以换成你的物品ID

const moyingg = event.level.createEntity('cataclysm:ender_guardian');

定义常量,方便下文使用,生物ID可以替换成你自己想要召唤的生物ID

moyingg.setPosition(
            player.x + 5,
            player.y + 1,
            player.z + 5
        );

设置生物坐标,以玩家的坐标为基础上加减,x+数字就是玩家的x轴数值加上所输入的数值

moyingg.spawn();

生成生物,这个一定要有!!!

如果你想要在召唤生物后消耗物品,请在生物生成下加上这个

if (!player.isCreative()) {
            mainHandItem.shrink(1);
            player.setMainHandItem(mainHandItem);
        }

其中if(!player.isCreative()){……}

是检测玩家不是创造模式才消耗物品,如果你需要创造模式也消耗物品那么请删除这个if(!player.isCreative())和它后面包裹消耗物品的{}