Skip to content
On this page

事件

在 Katton 中,事件是一种响应游戏中各种操作和变化的强大手段。它们允许你在特定事件发生时执行自定义代码,例如玩家交互、世界更改或实体更新。

要监听事件,你可以使用 KattonEvents API。你可以在 Fabric API 文档 或者 NeoForge API 文档 中找到所有可用的事件。Katton 包装了Fabric等Mod加载器提供的事件,并将所有的参数都包装为了一个单独的事件参数记录类,方便你在事件处理函数中获取所需的信息。

开始监听事件

你可以在脚本中直接订阅对应事件回调,并在回调中编写你的业务逻辑。

简单示例

以下是一个监听 onPlayerJoin 事件并向玩家发送欢迎消息的简单示例。该示例与入门指南中的示例相同。

kotlin
//nessary imports for the script
import net.minecraft.network.chat.Component
import top.katton.api.KattonEvents

// The function with @ServerScriptEntrypoint is the entry point of the script.
@ServerScriptEntrypoint
fun main(){
   // Register an event listener for when a player joins the server
   KattonEvents.ServerPlayer.onPlayerJoin += onJoin@
   fun(arg: KattonEvents.ServerPlayer.PlayerArg){
      // Get the player who joined and send them a message
      val player = arg.player
      // As same as you would do in a normal mod!
      player.sendSystemMessage(Component.literal("Hello Katton"))
   }
}

每个事件都有自己的一组参数,这些参数提供关于该事件的信息,并允许你与之交互。你可以在API文档中查找每个事件的详细参数。

详细事件列表

在 Fabric 和 NeoForge 中,Katton 提供的事件稍有差异。下方提供了一份详细的列表。

Event CategoryEvent NameFabricNeoForge
Chunk and BlockonAfterBlockBreak-
onBeforeBlockBreak-
onBlockBreak-
onBlockEntityLoad
onBlockEntityUnload
onBlockPlace-
onCanceledBlockBreak-
onChunkDataLoad-
onChunkDataSave-
onChunkLevelTypeChange
onChunkLoad
onChunkSent-
onChunkUnload
onChunkUnWatch-
onChunkWatch-
onExplosionDetonate
onExplosionStart
Item ComponentonAllowEnchanting
onModifyComponent
onModifyEnchantment
ItemonUse
onUseOn
Living BehavioronAllowBed
onAllowNearbyMonsters
onAllowResettingTime
onAllowSettingSpawn
onAllowSleeping
onAnimalTame-
onBabySpawn-
onElytraAllow
onElytraCustom
onModifySleepingDirection
onModifyWakeUpPosition
onPlayerWakeUp
onSetBedOccupationState
onStartSleeping
onStopSleeping
Living Use ItemonUseItemFinish
onUseItemStart
onUseItemStop
onUseItemTick
Loot TableonLootTableAllLoad
onLootTableModify
onLootTableModifyDrops
onLootTableReplace
PlayeronAttackBlock-
onAttackEntity
onBlockInteract
onDestroyItem
onEntityInteract
onItemInteract
onLeftClickBlock-
onUseItemOn-
onUseWithoutItem-
Server Entity CombatonAfterKilledOtherEntity
onCriticalHit-
onShieldBlock
Server EntityonAfterEntityChangeLevel
onAfterEntityLoad-
onAfterPlayerChangeLevel
onEndermanAnger
onEntityLoad-
onEntityTeleport-
onEntityUnload
onEquipmentChange
ServeronAfterSave
onBeforeSave
onEndDatapackReload
onEndServerTick
onEndWorldTick
onLevelLoad-
onLevelSave-
onLevelUnload-
onServerStarted
onServerStarting
onServerStopped
onServerStopping
onStartDatapackReload
onStartServerTick
onStartWorldTick
onSyncDatapackContents
Server Living EntityonAfterDamage
onAfterDeath
onAllowDamage
onAllowDeath
onLivingDrops-
onLivingFall
onLivingHurt
onLivingJump-
onMobConversion
Server MessageonAllowChatMessage-
onAllowCommandMessage-
onAllowGameMessage-
onChatMessage-
onCommandMessage-
onGameMessage-
onServerChat-
Server Mob EffectonAfterAdd-
onAfterRemove-
onAllowAdd-
onAllowEarlyRemove-
onBeforeAdd-
onBeforeRemove-
onMobEffectAdd-
onMobEffectApplicable-
onMobEffectExpire-
onMobEffectRemove-
Server PlayeronAfterPlayerRespawn
onItemPickupPost-
onItemPickupPre-
onItemToss-
onPickFromBlock
onPickFromEntity
onPlayerCopy
onPlayerItemCrafted-
onPlayerItemSmelted-
onPlayerJoin
onPlayerLeave
onPlayerLoadFromFile-
onPlayerPickupXp
onPlayerSaveToFile-
onPlayerSpawnPhantoms-
onPlayerXpChange
onPlayerXpLevelChange
onStartTracking-
onStopTracking-