UNPKG

yoni-mcscripts-lib

Version:

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

113 lines (112 loc) 4.56 kB
import { EntityUtils } from "../../../EntityUtils.js"; export function conditionEntityEventOptions(entities, options) { let condition; if (options.entities && options.entityTypes) condition = (entity) => { if (options.entityTypes.includes(entity.typeId)) return true; return options.entities.some(cEntity => EntityUtils.isSameEntity(cEntity, entity)); }; else if (options.entities && !options.entityTypes) condition = (entity) => { return options.entities.some(cEntity => EntityUtils.isSameEntity(cEntity, entity)); }; else if (!options.entities && options.entityTypes) condition = (entity) => { return options.entityTypes.includes(entity.typeId); }; else if (!options.entities && !options.entityTypes) return true; else throw new TypeError("unknown entity options"); for (const entity of entities) { if (condition(entity)) return true; } return false; } export function conditionEntityDataDrivenTriggerEventOptions(id, entities, options) { if (options.eventTypes && options.eventTypes.includes(id)) return true; return conditionEntityEventOptions(entities, options); } import { Minecraft } from "../../../basis.js"; import { EventRegistry } from "../../EventRegistry.js"; export function registerMinecraftEventOptionResolvers() { (function () { let registry = EventRegistry.getRegistry(Minecraft.EffectAddAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.entity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityDieAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.deadEntity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityHealthChangedAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.entity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityHitBlockAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.damagingEntity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityHitEntityAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.damagingEntity); entities.push(event.hitEntity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityHurtAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { const entities = []; entities.push(event.damageSource?.damagingEntity); entities.push(event.damageSource?.damagingProjectile); entities.push(event.hurtEntity); return conditionEntityEventOptions(entities, options); }; })(); (function () { let registry = EventRegistry.getRegistry(Minecraft.EntityRemoveAfterEvent); registry.extraOption = true; registry.extraOptionResolver = (event, options) => { if (!options || !(options.entityTypes || options.entities)) return true; let id = event.removedEntityId; let typeId = event.typeId; if (options.entities) for (const entity of options.entities) { if (entity.id === id) return true; } if (options.entityTypes) if (options.entityTypes.includes(typeId)) return true; return false; }; })(); }