UNPKG

yoni-mcscripts-lib

Version:

为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。

51 lines (50 loc) 1.54 kB
import { EventRegistry, eventManager, listenEvent } from "../index.js"; import { VanillaWorld, Minecraft } from "../../basis.js"; import { YoniScheduler, Schedule } from "../../schedule.js"; import { PlayerEvent } from "./PlayerEvent.js"; export class PlayerJoinedEvent extends PlayerEvent { constructor(player) { super(player); } async kickPlayer() { await this.player.kick("加入游戏被取消"); } } const eventRegistry = EventRegistry.register(PlayerJoinedEvent); const joiningPlayers = new Set(); const schedule = new Schedule({ type: Schedule.cycleTickSchedule, async: false, delay: 0, period: 1 }, function testPlayer() { if (joiningPlayers.size === 0) { stopDetect(); return; } let joinedPlayers = []; for (const onlinePlayer of VanillaWorld.getPlayers()) { if (joiningPlayers.has(onlinePlayer.name)) { joiningPlayers.delete(onlinePlayer.name); joinedPlayers.push(onlinePlayer); } } for (const joinedPlayer of joinedPlayers) { const event = new PlayerJoinedEvent(joinedPlayer); eventManager.callEvent(eventRegistry, event); } }); listenEvent(Minecraft.PlayerJoinAfterEvent, function onJoin(event) { joiningPlayers.add(event.playerName); startDetect(); }); function startDetect() { if (!schedule.isQueued()) { YoniScheduler.addSchedule(schedule); } } function stopDetect() { if (schedule.isQueued()) { YoniScheduler.removeSchedule(schedule); } }