Skip to content

事件触发器

Craftorithm 支持多种 Bukkit 事件作为触发器类型,覆盖玩家、实体、背包、方块等各类事件。

使用方式

在触发器 YAML 的 type 字段中使用事件名即可:

yaml
my_trigger:
  type: 'player_join'
  actions:
    - 'tell("&a欢迎!")'

事件列表

玩家事件

事件名说明上下文变量
player_join玩家加入
player_quit玩家退出
player_death玩家死亡killer_name
player_respawn玩家重生
player_interact玩家交互action
player_interact_entity玩家与实体交互entity_type
player_advancement完成进度advancement
player_level_change等级变化old_level, new_level
player_exp_change经验变化amount
player_toggle_sneak潜行切换sneaking
player_toggle_sprint疾跑切换sprinting
player_item_consume消耗物品item
player_item_held切换手持物品previous_slot, new_slot
player_animation玩家动画
player_bed_enter上床
player_bed_leave离床
player_bucket_fill装满桶-
player_bucket_empty倒空桶-
player_changed_world切换世界from
player_portal进入传送门
player_teleport传送cause
player_fish钓鱼state
player_shear_entity剪羊毛entity_type
player_edit_book编辑书本
player_statistic统计值变化statistic, value
player_swap_hand_items切换主副手
player_drop_item丢弃物品item
player_pickup_item拾取物品item
player_velocity速度变化
player_game_mode_change游戏模式变化new_game_mode
player_item_damage物品损坏item, damage
player_item_mend经验修补item, amount, repair_amount
player_recipe_discover发现配方recipe
player_take_campfire从营火取出物品item
player_move玩家移动-
player_command_preprocess命令预处理message
async_player_chat异步聊天message

实体事件

事件名说明上下文变量
damage_entity实体受伤damage, entity_type, entity_name
kill_entity实体被击杀entity_type, entity_name
entity_shoot_bow射箭force
entity_breed繁殖entity_type
entity_tame驯服entity_type
entity_potion_effect药水效果effect_type

背包事件

事件名说明上下文变量
inventory_click点击格子slot, click_type
inventory_close关闭背包
inventory_open打开背包
prepare_grindstone砂轮显示结果时
trade_select选择村民交易项index

方块事件

事件名说明上下文变量
block_break破坏方块block_type
block_place放置方块block_type

上下文变量

每个事件类型会自动注入相关变量到脚本上下文中,可通过 context("变量名") 函数访问。

event 对象 (1.13.0.0+)

从 1.13.0.0 版本开始,所有触发器的上下文中都新增了 event 对象,可以直接操作 Bukkit 事件对象。

event 对象是一个反射属性解析器,可以通过方法调用来访问事件的属性。

使用示例

yaml
on_craft_diamond_sword:
  type: 'crafting'
  recipes:
    - 'minecraft:diamond_sword'
  conditions:
    mode: script
    body:
      - 'if event.get("view").get("player").invoke("getName") == "Yufiria_"'
      - '  tell("是插件开发者正在进行合成!")'
      - 'endif'
  actions:
    - 'tell("&a合成成功!")'
    - 'give_level(100)'

访问方式

  • event.get("propertyName") - 获取事件的属性
  • .invoke("methodName") - 调用对象的方法
  • 链式调用支持:event.get("view").get("player").invoke("getName")

示例:检查击杀的实体类型

yaml
on_kill_zombie:
  type: 'kill_entity'
  conditions:
    - 'entity_type == "ZOMBIE"'
  actions:
    - 'tell("&a你击杀了一只僵尸!")'

示例:检查进度

yaml
on_diamond_advancement:
  type: 'player_advancement'
  conditions:
    - 'advancement == "minecraft:story/mine_diamond"'
  actions:
    - 'tell("&b你挖到了钻石!")'
    - 'give_money(1000)'

示例:检查聊天消息

yaml
on_keyword:
  type: 'async_player_chat'
  conditions:
    - 'perm("craftorithm.vip")'
  actions:
    - 'tell("&aVIP 发言!")'

基于 GPL-3.0 许可证发布