diff --git a/SUMMARY.md b/SUMMARY.md index c279988..09326bc 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -62,8 +62,6 @@ * [配方函数](advanced/recipe-function-and-action/recipe-function.md) * [配方事件](advanced/recipe-function-and-action/recipe-action.md) * [事件概论](advanced/event-overview/README.md) - * [常用类](advanced/event-overview/usual-classes.md) - * [基本框架](advanced/event-overview/basic-framework.md) * [一些忠告](advanced/event-overview/tips.md) * [ZenClass](advanced/zenclass.md) diff --git a/advanced/event-overview/README.md b/advanced/event-overview/README.md index 4f3b68a..6e2b384 100644 --- a/advanced/event-overview/README.md +++ b/advanced/event-overview/README.md @@ -3,3 +3,85 @@ description: Yep,你真的以为魔改仅仅只是个改合成的东西吗? --- # 事件概论 + +什么是事件?某个时间点上发生一件值得关注的事。你可以使用 CrT 在一件事发生时,额外执行别的行为。 + +```csharp +// onPlayerCrafted 当玩家合成时 +events.onPlayerCrafted(function(event){ + // ... +}); +``` + +在匿名函数内部可以编写你想要的行为,这个函数内部的代码将在玩家合成时才会执行,而不像一般配方修改的脚本那样在游戏加载时执行。 + +```csharp +// onPlayerCrafted 当玩家合成时 +events.onPlayerCrafted(function(event){ + print("A player crafted something."); +}); +``` + +比如这样,在玩家合成时,在日志打印信息。 + +## 具体游戏逻辑 + +不对啊,只是在日志打印个消息有啥用。我们需要在游戏有一个具体行为才行啊。我们便需要利用这个匿名函数的 event 参数,当这个事件发生时,这个函数执行时会传入这个参数,这个参数包含了这个事件发生的背景。简单来说,可以利用 event 参数的 ZenGetter 获取各种游戏对象,比如玩家、坐标、世界、方块等等,通过操作这些游戏对象,你就可以在合理的时机做你想要的新的游戏逻辑,限制你的只有想象力而已。 + +例如下面的例子,每次玩家合成时受到 1 点伤害。 + +```csharp +import crafttweaker.event.PlayerCraftedEvent; //导入玩家合成事件的类 + +// onPlayerCrafted 当玩家合成时 +// 由于 ZenScript 的限制,在这里你需要给 event 指定对应的事件类,否则你无法获取事件包含的信息 +// 这里就是 PlayerCraftedEvent 玩家合成事件,记得在脚本最前面导入该类 +events.onPlayerCrafted(function(event as PlayerCraftedEvent) { + // 获取是哪个玩家合成了物品 + var player = event.player; + // 让玩家受到 1 点魔法伤害 + player.attackEntityFrom(, 1.0f); +}); +``` + +## 取消事件 + +有的时候你希望一些原版行为不会发生,你可以使用 `cancel` 方法取消事件来阻止原版行为。 + +下面的例子为阻止苦力怕的生成。 + +```csharp +import crafttweaker.event.EntityJoinWorldEvent; //导入实体进入世界事件 + +// onPlayerCrafted 当玩家合成时 +// 由于 ZenScript 的限制,在这里你必须要给 event 参数指定对应的事件类,否则你无法获取事件包含的信息 +// 这里就是 PlayerCraftedEvent 玩家合成事件,记得在脚本最前面导入该类 +events.onEntityJoinWorld(function(event as EntityJoinWorldEvent) { + // 获取将被加入的实体的定义 + val definition = event.entity.definition; + // 如果该实体为玩家,definition 会返回 null,需要先进行判空 + // 然后再判断这个实体的 ID 是否是苦力怕 + if (!isNull(definition) && definition.id == "minecraft:creeper") { + // 取消该事件 + // 取消实体加入世界的结果自然是阻止实体加入世界 + event.cancel(); + } +}); +``` + +## 后记 + +本教程不可能列出所有可用的事件,以及所有游戏对象和可供使用的操作。你依旧需要官方 wiki 作为「工具书」来查找。 + +[可用事件列表](https://docs.blamejared.com/1.12/en/Vanilla/Events/IEventManager/) + +常用游戏对象: + +* [IPlayer](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Players/IPlayer/) :指定玩家,包含玩家的各种信息。IPlayer继承 [IEntityLivingBase](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntityLivingBase/#ientitylivingbase) 类 +* [IEntityLivingBase](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntityLivingBase/#ientitylivingbase):指定一个Mob 继承 [IEntity](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntity/) 类 +* [IEntity](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntity/) :指定一个实体 +* [IWorld](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/World/IWorld/#iworld):包含世界中的一个维度的信息,包含天气效果、方块信息、服务端客户端等信息。可以用来在世界中召唤一个实体 +* [IBlockPos](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/World/IBlockPos/) :指定世界中一个坐标,常用这个获取或更改一个IWorld的指定坐标的IBlockState +* [ICommandSender](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Commands/ICommandSender/) :指令发送者,如果想用 zs 发送指令需要用这个,但也要注意权限,推荐用直接用 `server` 关键词,表示服务器发送指令 +* [IBlock](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Blocks/IBlock/) :方块 +* [IBlockState](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Blocks/IBlockState/) :方块状态,真正在世界里的方块,包含方块定义和当前的状态,比如草方块是否被雪覆盖,炼药锅装了多少水。 diff --git a/advanced/event-overview/basic-framework.md b/advanced/event-overview/basic-framework.md deleted file mode 100644 index b91c270..0000000 --- a/advanced/event-overview/basic-framework.md +++ /dev/null @@ -1,68 +0,0 @@ -# 基本框架 - -最好先用`import crafttweaker.events.IEventManager;`导入有关包 - -你需要用一个函数描述事件,并告诉CraftTweaker要干什么。最重要的是**构建正确类型的事件**,否则你不能访问ZenGetter - -```csharp -import crafttweaker.events.IEventManager; //导入事件管理器 -import crafttweaker.event.PlayerCraftedEvent //导入玩家合成事件的类 - -//使用events全局关键词调用事件管理器,并使用一个ZenMethod指定使用的事件,在函数头将事件匹配至PlayerCraftedEvent -events.onPlayerCrafted(function(event as PlayerCraftedEvent){ - event.player.attackEntityFrom(, 1.0f); // 玩家受到 1 点魔法伤害 -}); -``` - -你可以使用这些事件: - -由于 CrT 的更新,这个列表并不完整,完整内容,见 [官方文档](https://docs.blamejared.com/1.12/en/Vanilla/Events/IEventManager/)。 - -| ZenMethod | 对应的类 | -| :--- | :--- | -| onAllowDespawn | [`crafttweaker.event.EntityLivingSpawnEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingSpawn/) | -| onBlockBreak | [`crafttweaker.event.BlockBreak`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/BlockBreak/) | -| onBlockHarvestDrops | [`crafttweaker.event.BlockHarvestDrops`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/BlockHarvestDrops/) | -| onCheckSpawn | [`crafttweaker.event.EntityLivingExtendedSpawnEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingSpawn/) | -| onCommand | [`crafttweaker.event.CommandEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/CommandEvent/) | -| onEnderTeleport | [`crafttweaker.event.EnderTeleportEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EnderTeleport/) | -| onEntityLivingAttacked | [`crafttweaker.event.EntityLivingAttackedEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingAttacked/) | -| onEntityLivingDeath | [`crafttweaker.event.EntityLivingDeathEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingDeath/) | -| onEntityLivingDeathDrops | [`crafttweaker.event.EntityLivingDeathDropsEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingDeathDrops/) | -| onEntityLivingFall | [`crafttweaker.event.EntityLivingFallEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingFall/) | -| onEntityLivingHurt | [`crafttweaker.event.EntityLivingHurtEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingHurt/) | -| onEntityLivingJump | [`crafttweaker.event.EntityLivingJumpEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingJump/) | -| onEntityLivingUseItem | [`crafttweaker.event.EntityLivingUseItemEvent.All`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/LivingEntityUseItem/) | -| onEntityLivingUseItemFinish | [`crafttweaker.event.EntityLivingUseItemEvent.Finish`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/LivingEntityUseItem/) | -| onEntityLivingUseItemStart | [`crafttweaker.event.EntityLivingUseItemEvent.Start`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/LivingEntityUseItem/) | -| onEntityLivingUseItemStop | [`crafttweaker.event.EntityLivingUseItemEvent.Stop`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/LivingEntityUseItem/) | -| onEntityLivingUseItemTick | [`crafttweaker.event.EntityLivingUseItemEvent.Tick`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/LivingEntityUseItem/) | -| onEntityStruckByLightning | [`crafttweaker.event.EntityStruckByLightningEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityStruckByLightning/) | -| onItemExpire | [`crafttweaker.event.ItemExpireEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/ItemExpire/) | -| onItemToss | [`crafttweaker.event.ItemTossEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/ItemToss/) | -| onPlayerAdvancement | [`crafttweaker.event.PlayerAdvancement`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerAdvancement/) | -| onPlayerAnvilRepair | [`crafttweaker.event.PlayerAnvilRepair`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerAnvilRepair/) | -| onPlayerAttackEntity | [`crafttweaker.event.PlayerAttackEntityEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerAttackEntity/) | -| onPlayerBonemeal | [`crafttweaker.event.PlayerBonemealEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerBonemeal/) | -| onPlayerBreakSpeed | [`crafttweaker.event.PlayerBreakSpeed`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerBreakSpeed/) | -| onPlayerBrewedPotion | [`crafttweaker.event.PlayerBrewedPotion`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerBrewedPotion/) | -| onPlayerChangedDimension | [`crafttweaker.event.PlayerChangedDimensionEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerChangedDimension/) | -| onPlayerCrafted | [`crafttweaker.event.PlayerCraftedEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerCrafted/) | -| onPlayerDeathDrops | [`crafttweaker.event.PlayerDeathDropsEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerDeathDrops/) | -| onPlayerDestroyItem | [`crafttweaker.event.PlayerDestroyItem`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerDestroyItem/) | -| onPlayerFillBucket | [`crafttweaker.event.PlayerFillBucketEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerFillBucket/) | -| onPlayerInteract | [`crafttweaker.event.PlayerInteractEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerInteract/) | -| onPlayerInteractBlock | [`crafttweaker.event.PlayerInteractBlockEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerInteractBlock/) | -| onPlayerInteractEntity | [`crafttweaker.event.PlayerInteractEntityEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerInteractEntity/) | -| onPlayerLoggedIn | [`crafttweaker.event.PlayerLoggedInEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerLoggedIn/) | -| onPlayerLoggedOut | [`crafttweaker.event.PlayerLoggedOutEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerLoggedOut/) | -| onPlayerOpenContainer | [`crafttweaker.event.PlayerOpenContainerEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerOpenContainer/) | -| onPlayerPickupItem | [`crafttweaker.event.PlayerPickupItemEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerPickupItem/) | -| onPlayerPickupXp | [`crafttweaker.event.PlayerPickupXpEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerPickupXp/) | -| onPlayerRespawn | [`crafttweaker.event.PlayerRespawnEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerRespawn/) | -| onPlayerSetSpawn | [`crafttweaker.event.PlayerSetSpawn`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerSetSpawn/) | -| onPlayerSleepInBed | [`crafttweaker.event.PlayerSleepInBedEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerSleepInBed/) | -| onPlayerSmelted | [`crafttweaker.event.PlayerSmeltedEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerSmelted/) | -| onPlayerTick | [`crafttweaker.event.PlayerTickEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerTick/) | -| onPlayerUseHoe | [`crafttweaker.event.PlayerUseHoeEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/PlayerUseHoe/) | -| onSpecialSpawn | [`crafttweaker.event.EntityLivingExtendedSpawnEvent`](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Events/Events/EntityLivingSpawn/) | diff --git a/advanced/event-overview/usual-classes.md b/advanced/event-overview/usual-classes.md deleted file mode 100644 index 0c95f84..0000000 --- a/advanced/event-overview/usual-classes.md +++ /dev/null @@ -1,15 +0,0 @@ -# 常用类 - - - -你需要监听一些事件,获得一些信息,若满足什么条件,执行一些操作。有一些类,是你经常用到的。 - -我不列出各种类的信息,wiki上都有。链接指向英文wiki。 - -* [IPlayer](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Players/IPlayer/) :指定玩家,包含玩家的各种信息。IPlayer继承 [IEntityLivingBase](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntityLivingBase/#ientitylivingbase) 类 -* [IEntityLivingBase](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntityLivingBase/#ientitylivingbase):指定一个Mob 继承 [IEntity](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntity/) 类 -* [IEntity](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Entities/IEntity/) :指定一个实体 -* [IWorld](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/World/IWorld/#iworld):包含世界中的一个维度的信息,包含天气效果、方块信息、服务端客户端等信息。可以用来在世界中召唤一个实体 -* [IBlockPos](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/World/IBlockPos/) :指定世界中一个坐标,常用这个获取或更改一个IWorld的指定坐标的IBlockState -* [ICommandSender](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Commands/ICommandSender/) :指令发送者,如果想用 zs 发送指令需要用这个,但也要注意权限,推荐用直接用 `server` 关键词,表示服务器发送指令 -* [IBlock](https://crafttweaker.readthedocs.io/en/latest/#Vanilla/Blocks/IBlock/) :方块