森林蝙蝠
本帖最后由 森林蝙蝠 于 2020-4-26 17:36 编辑

EventLib是一个基于fabric开发的小型mod,用于给fabric添加更多常见事件——这是它比起forge的一个重大缺陷。

ClientStartCallback:监听客户端启动的事件。
  1. ClientStartCallback.EVENT.register(client -> LOGGER.warn("Client Started!"));
复制代码

ClientStartCallback:监听客户端关闭的事件。
  1. ClientStopCallback.EVENT.register(client -> LOGGER.warn("Client Stopped!"));
复制代码
PlayerSleepCallback:监听玩家右键准备上床睡觉事件。
  1. PlayerSleepCallback.PLAYER_SLEEP_CALLBACK_EVENT.register((player,bedPos,time)->{
  2.                         if(player.inventory.isInvEmpty()) {
  3.                                 Entity entity=new SheepEntity(EntityType.SHEEP,player.world);
  4.                                 entity.setPosition(player.getBlockPos().getX(),player.getBlockPos().getY()+1,player.getBlockPos().getZ());
  5.                                 player.world.spawnEntity(entity);
  6.                         }
  7.                         return ActionResult.PASS;
  8.                 });
复制代码
EnchantmentCallback:监听玩家点击附魔事件。
  1. EnchantmentCallback.ENCHANTMENT_CALLBACK_EVENT.register(((playerEntity,itemStack,level) -> {
  2.                         if(itemStack.getTranslationKey().contains("gold"))
  3.                                 itemStack.setDamage(itemStack.getMaxDamage()/2);  //将被附魔物品耐久减半
  4.                         return ActionResult.PASS;
  5.                 }));
复制代码
PlayerLevelupCallback:玩家升级事件。
  1. PlayerLevelupCallback.PLAYER_LEVELUP_CALLBACK_EVENT.register((player,level)->{
  2.                         if(player.experienceLevel>30)
  3.                                 player.addPotionEffect(new StatusEffectInstance(StatusEffects.BLINDNESS,30,1));
  4.                         return ActionResult.PASS;
  5.                 });
复制代码
CommandStartCallback:命令得以执行事件。
  1. CommandStartCallback.COMMAND_START_CALLBACK_EVENT.register((source,command)->{
  2.                         if(source.getEntityOrThrow()!=null) {
  3.                                 Entity entity=source.getEntity();
  4.                                 if (command.contains("give") && entity instanceof PlayerEntity)
  5.                                         entity.kill();
  6.                                 return ActionResult.PASS;
  7.                         }
  8.                         return ActionResult.PASS;
  9.                 });
复制代码

BlockEntityConstructedCallback:世界中有BlockEntity生成事件。
  1. BlockEntityConstructCallback.BLOCK_ENTITY_CONSTRUCT_CALLBACK_EVENT.register((blockEntity,pos,world)->{
  2.                         if(blockEntity instanceof FurnaceBlockEntity && world!=null) {
  3.                                 world.getLevelProperties().setClearWeatherTime(0);
  4.                                 world.getLevelProperties().setRaining(true);
  5.                         }
  6.                         return ActionResult.PASS;
  7.                 });
复制代码
FishingCallback:钓鱼事件。
  1. FishingCallback.FISHING_CALLBACK_EVENT.register(((world, player) -> {
  2.                         if(world.getLevelProperties().isRaining())
  3.                                 player.dropItem(new ItemStack(Items.DIAMOND),false);
  4.                         return new TypedActionResult<>(ActionResult.PASS, LootContextTypes.CHEST);
  5.                 }));
复制代码
AdvancementCallback:成就完成事件(客户端)。
  1. AdvancementCallback.ADVANCEMENT_CALLBACK_EVENT.register(((playerEntity, world, advancementPacket) -> {
  2.                         advancementPacket.getAdvancementsToProgress().forEach(((identifier, advancementProgress) -> {
  3.                                 if(identifier.toString().equals("minecraft:story/mine_diamond"))
  4.                                         playerEntity.dropItem(Items.EMERALD_BLOCK);
  5.                         }));
  6.                         return ActionResult.PASS;
  7.                 }));
复制代码
ChunkLoadCallback:区块加载事件(客户端)。
  1. ChunkLoadCallback.CHUNK_LOAD_CALLBACK_EVENT.register((chunk -> {
  2.                         if(chunk!=null && chunk.getBiome(new BlockPos(200,80,200))== Biomes.SWAMP) {
  3.                                 Entity entity=new EnderDragonEntity(EntityType.ENDER_DRAGON,chunk.getWorld());
  4.                                 entity.setPosition(200,80,200);
  5.                                 chunk.addEntity(entity);
  6.                         }
  7.                         return ActionResult.PASS;
  8.                 }));
复制代码
DayNightCallback:昼夜交替事件。
  1. DayNightCallback.DAY_NIGHT_CALLBACK_EVENT.register(((world, player) -> {
  2.                         if(world.isThundering())
  3.                                 player.addPotionEffect(new StatusEffectInstance(StatusEffects.FIRE_RESISTANCE));
  4.                 }));
复制代码
EnterDimensionCallback:实体进入某个维度事件。
  1. EnterDimensionCallback.ENTER_DIMENSION_CALLBACK_EVENT.register(((dimension,entity, world, dimensionType) -> {
  2.                         if(!world.isClient && dimensionType.equals(DimensionType.THE_END) && entity instanceof LivingEntity)
  3.                                 entity.dropItem(Items.DIAMOND);
  4.                         return ActionResult.PASS;
  5.                 }));
复制代码
EntityDeathCallback:实体死亡事件。
  1. EntityDeathCallback.ENTITY_DEATH_CALLBACK_EVENT.register(((world, entity, playerEntity, pos) -> {
  2.                         if(entity instanceof WitherSkeletonEntity)
  3.                                 entity.dropItem(Items.ACACIA_BOAT);
  4.                         return ActionResult.PASS;
  5.                 }));
复制代码
EntityHealCallback:实体治愈事件。
  1. EntityHealCallback.ENTITY_HEAL_CALLBACK_EVENT.register(((entity, world, health) -> {
  2.                         if(entity instanceof CreeperEntity)
  3.                                 entity.setHealth(entity.getMaxBreath());
  4.                         return ActionResult.PASS;
  5.                 }));
复制代码
EntityMoveCallback:实体移动事件。
  1. EntityMoveCallback.ENTITY_MOVE_CALLBACK_EVENT.register(((world, self, direction) -> {
  2.                         if(self instanceof LivingEntity && direction.x>0 && direction.z>0 && direction.x<0.5 && direction.z<0.5)
  3.                                 self.dropItem(Items.ANVIL);
  4.                         return ActionResult.PASS;
  5.                 }
复制代码
EntitySpawnCallback:实体生成事件。
  1. EntitySpawnCallback.EVENT_SPAWN_CALLBACK.register(((world, player, self, pos) -> {
  2.                         if(self instanceof PigEntity)
  3.                                 self.dropItem(Items.BONE_MEAL);
  4.                         return ActionResult.PASS;
  5.                 }));
复制代码
ExplosionCallback:爆炸事件。
  1. ExplosionCallback.EXPLOSION_CALLBACK_EVENT.register(((world, pos) -> {
  2.                         if(pos.getY()>64)
  3.                                 world.getPlayers().forEach(player->{
  4.                                         if(!player.world.isClient)
  5.                                         player.sendMessage(new LiteralText("wo cao ni ma!"));
  6.                                 });
  7.                         return ActionResult.PASS;
  8.                 }));
复制代码
EntityTeleportCallback:实体传送事件。
  1. EntityTeleportCallback.ENTITY_TELEPORT_CALLBACK_EVENT.register(((entity, startPos, destPos) -> {
  2.                         if(entity instanceof EndermanEntity && startPos.getY()>40) {
  3.                                 Entity tnt=new TntEntity(entity.world,startPos.getX(),startPos.getY(),startPos.getZ(),null);
  4.                                 entity.world.createExplosion(tnt,startPos.getX(),startPos.getY(),startPos.getZ(),7, Explosion.DestructionType.DESTROY);
  5.                         }
  6.                         return ActionResult.PASS;
  7.                 }));
复制代码
FireworkCallback:焰火使用事件。
  1. FireworkCallback.FIREWORK_CALLBACK_EVENT.register((world,player,firework)->{
  2.                         if(world.getBiome(player.getBlockPos())==Biomes.MOUNTAINS && !world.isClient)
  3.                                 player.sendMessage(new LiteralText("ni ma zha le"));
  4.                         return ActionResult.PASS;
  5.                 });
复制代码
GuiScreenCallback:Gui事件,很明显,只属于客户端。
  1. GuiScreenCallback.GUI_SCREEN_CALLBACK_EVENT.register(screen -> {
  2.                         if(screen.isPauseScreen()) {
  3.                                 LOGGER.warn("PAUSED!");
  4.                         }
  5.                         return ActionResult.PASS;
  6.                 });
复制代码
MobSpawnerCallback:刷怪笼工作事件(客户端)。
  1. MobSpawnerCallback.MOB_SPAWNER_CALLBACK_EVENT.register((world, playerEntity, mobEntity) -> {
  2.                         if(world.isClient && mobEntity!=null&&mobEntity.getType()==EntityType.PIG)
  3.                                 playerEntity.sendMessage(new LiteralText("这不**"));
  4.                         return ActionResult.PASS;
  5.                 });
复制代码
OpenContainerCallback:玩家打开容器事件。
  1. OpenContainerCallback.OPEN_CONTAINER_CALLBACK_EVENT.register(playerEntity -> {
  2.                         if(!playerEntity.inventory.isInvEmpty())
  3.                                 playerEntity.getArmorItems().forEach(itemStack ->
  4.                                         itemStack.setDamage(itemStack.getMaxDamage()/2));
  5.                         return ActionResult.PASS;
  6.                 });
复制代码
PlayerChatCallback:聊天事件。
  1. PlayerChatCallback.PLAYER_CHAT_CALLBACK_EVENT.register((player, text) -> {
  2.                         if(text.asString().contains("nmsl"))
  3.                                 player.kill();
  4.                         return new TypedActionResult<>(ActionResult.PASS,text);
  5.                 });
复制代码
PlaySoundCallback:声音播放事件(客户端)。
  1. PlaySoundCallback.PLAY_SOUND_CALLBACK_EVENT.register((world, player, pos, event, category) -> {
  2.                         if(category.equals(SoundCategory.WEATHER)){
  3.                                 Entity entity=new LightningEntity(world,player.getBlockPos().getX()+0.5,player.getBlockPos().getY(),
  4.                                                 player.getBlockPos().getZ()+0.5,false);
  5.                                 world.spawnEntity(entity);
  6.                         }
  7.                         return ActionResult.PASS;
  8.                 });
复制代码
PortalSpawnCallback:传送门生成事件。
  1. PortalSpawnCallback.PORTAL_SPAWN_CALLBACK_EVENT.register((playerEntity, world) -> {
  2.                                 if(playerEntity.getMainHandStack().getItem()==Items.FLINT_AND_STEEL)
  3.                                         world.createExplosion(new TntEntity(EntityType.TNT,world),playerEntity.getBlockPos().getX(),
  4.                                                         playerEntity.getBlockPos().getY(),
  5.                                                         playerEntity.getBlockPos().getZ(),7,true, Explosion.DestructionType.DESTROY);
  6.                                 return ActionResult.PASS;
  7.                         });
复制代码
PreLoginCallback:准备登入事件。
  1. PreLoginCallback.PRE_LOGIN_CALLBACK_EVENT.register(((server, key, entity) -> {
  2.                         LOGGER.warn(server.getMaxPlayerCount());
  3.                         return ActionResult.PASS;
  4.                 }));
复制代码
RaidCallback:村庄侵袭开始事件。
  1. RaidCallback.RAID_CALLBACK_EVENT.register((raid, player) -> {
  2.                         if(raid.hasStarted())
  3.                                 player.inventory.armor.forEach(itemStack -> {
  4.                                         itemStack.addEnchantment(Enchantments.PROTECTION,4);
  5.                                         itemStack.addEnchantment(Enchantments.UNBREAKING,3);
  6.                                 });
  7.                         return ActionResult.PASS;
  8.                 });
复制代码
RidingCallback:玩家骑乘实体事件。
  1. RidingCallback.RIDING_CALLBACK_EVENT.register((player, horse) -> {
  2.                         if(horse.getDisplayName().toString().contains("nmsl"))
  3.                                 horse.kill();
  4.                         return ActionResult.PASS;
  5.                 });
复制代码
ShootingCallback:弹射物生成事件,由于拦截的是实体构造器,所以其实无法取消。
  1. ShootingCallback.SHOOTING_CALLBACK_EVENT.register((player,world,projectileEntity) -> {
  2.                         if(player instanceof PlayerEntity && world.isThundering()) {
  3.                                 projectileEntity.remove();
  4.                                 return ActionResult.FAIL;
  5.                         }
  6.                         return ActionResult.PASS;
  7.                 });
复制代码
TamedEntityCallback:有实体被驯服事件。
  1. TamedEntityCallback.TAMED_ENTITY_CALLBACK_EVENT.register((world, playerEntity, entity) -> {
  2.                         if(!entity.isFireImmune()) {
  3.                                 entity.setOnFireFor(8);
  4.                                 entity.addPotionEffect(new StatusEffectInstance(StatusEffects.HEALTH_BOOST));
  5.                         }
  6.                         return ActionResult.PASS;
  7.                 });
复制代码

使用方法:从这里(提取码yinm)下载dev版的eventlib,放入任意文件夹,然后在build.gradle的dependencies区域输入
  1. fileTree "你的保存位置\\eventlib-1.0.0-dev.jar"
复制代码
即可完成依赖项导入。
目前适用于Java8/11,1.14.4版本,准备向1.15迁移。
源码地址:https://github.com/forestbat/EventLib





ZX夏夜之风
  1. PlayerChatCallback.PLAYER_CHAT_CALLBACK_EVENT.register((player, text) -> {
  2.                         if(text.asString().contains("nmsl"))
  3.                                 player.kill();
  4.                         return new TypedActionResult<>(ActionResult.PASS,text);
  5.                 });
复制代码

nmsl瞩目
(害怕)

Fndream
我他喵研究了半天发送指令的Mixin事件触发,没想到这里居然找到了现成的,感谢鸭!!!