EventLib是一个基于fabric开发的小型mod,用于给fabric添加更多常见事件——这是它比起forge的一个重大缺陷。
ClientStartCallback:监听客户端启动的事件。
- ClientStartCallback.EVENT.register(client -> LOGGER.warn("Client Started!"));
ClientStartCallback:监听客户端关闭的事件。
- ClientStopCallback.EVENT.register(client -> LOGGER.warn("Client Stopped!"));
- PlayerSleepCallback.PLAYER_SLEEP_CALLBACK_EVENT.register((player,bedPos,time)->{
- if(player.inventory.isInvEmpty()) {
- Entity entity=new SheepEntity(EntityType.SHEEP,player.world);
- entity.setPosition(player.getBlockPos().getX(),player.getBlockPos().getY()+1,player.getBlockPos().getZ());
- player.world.spawnEntity(entity);
- }
- return ActionResult.PASS;
- });
- EnchantmentCallback.ENCHANTMENT_CALLBACK_EVENT.register(((playerEntity,itemStack,level) -> {
- if(itemStack.getTranslationKey().contains("gold"))
- itemStack.setDamage(itemStack.getMaxDamage()/2); //将被附魔物品耐久减半
- return ActionResult.PASS;
- }));
- PlayerLevelupCallback.PLAYER_LEVELUP_CALLBACK_EVENT.register((player,level)->{
- if(player.experienceLevel>30)
- player.addPotionEffect(new StatusEffectInstance(StatusEffects.BLINDNESS,30,1));
- return ActionResult.PASS;
- });
- CommandStartCallback.COMMAND_START_CALLBACK_EVENT.register((source,command)->{
- if(source.getEntityOrThrow()!=null) {
- Entity entity=source.getEntity();
- if (command.contains("give") && entity instanceof PlayerEntity)
- entity.kill();
- return ActionResult.PASS;
- }
- return ActionResult.PASS;
- });
BlockEntityConstructedCallback:世界中有BlockEntity生成事件。
- BlockEntityConstructCallback.BLOCK_ENTITY_CONSTRUCT_CALLBACK_EVENT.register((blockEntity,pos,world)->{
- if(blockEntity instanceof FurnaceBlockEntity && world!=null) {
- world.getLevelProperties().setClearWeatherTime(0);
- world.getLevelProperties().setRaining(true);
- }
- return ActionResult.PASS;
- });
- FishingCallback.FISHING_CALLBACK_EVENT.register(((world, player) -> {
- if(world.getLevelProperties().isRaining())
- player.dropItem(new ItemStack(Items.DIAMOND),false);
- return new TypedActionResult<>(ActionResult.PASS, LootContextTypes.CHEST);
- }));
- AdvancementCallback.ADVANCEMENT_CALLBACK_EVENT.register(((playerEntity, world, advancementPacket) -> {
- advancementPacket.getAdvancementsToProgress().forEach(((identifier, advancementProgress) -> {
- if(identifier.toString().equals("minecraft:story/mine_diamond"))
- playerEntity.dropItem(Items.EMERALD_BLOCK);
- }));
- return ActionResult.PASS;
- }));
- ChunkLoadCallback.CHUNK_LOAD_CALLBACK_EVENT.register((chunk -> {
- if(chunk!=null && chunk.getBiome(new BlockPos(200,80,200))== Biomes.SWAMP) {
- Entity entity=new EnderDragonEntity(EntityType.ENDER_DRAGON,chunk.getWorld());
- entity.setPosition(200,80,200);
- chunk.addEntity(entity);
- }
- return ActionResult.PASS;
- }));
- DayNightCallback.DAY_NIGHT_CALLBACK_EVENT.register(((world, player) -> {
- if(world.isThundering())
- player.addPotionEffect(new StatusEffectInstance(StatusEffects.FIRE_RESISTANCE));
- }));
- EnterDimensionCallback.ENTER_DIMENSION_CALLBACK_EVENT.register(((dimension,entity, world, dimensionType) -> {
- if(!world.isClient && dimensionType.equals(DimensionType.THE_END) && entity instanceof LivingEntity)
- entity.dropItem(Items.DIAMOND);
- return ActionResult.PASS;
- }));
- EntityDeathCallback.ENTITY_DEATH_CALLBACK_EVENT.register(((world, entity, playerEntity, pos) -> {
- if(entity instanceof WitherSkeletonEntity)
- entity.dropItem(Items.ACACIA_BOAT);
- return ActionResult.PASS;
- }));
- EntityHealCallback.ENTITY_HEAL_CALLBACK_EVENT.register(((entity, world, health) -> {
- if(entity instanceof CreeperEntity)
- entity.setHealth(entity.getMaxBreath());
- return ActionResult.PASS;
- }));
- EntityMoveCallback.ENTITY_MOVE_CALLBACK_EVENT.register(((world, self, direction) -> {
- if(self instanceof LivingEntity && direction.x>0 && direction.z>0 && direction.x<0.5 && direction.z<0.5)
- self.dropItem(Items.ANVIL);
- return ActionResult.PASS;
- }
- EntitySpawnCallback.EVENT_SPAWN_CALLBACK.register(((world, player, self, pos) -> {
- if(self instanceof PigEntity)
- self.dropItem(Items.BONE_MEAL);
- return ActionResult.PASS;
- }));
- ExplosionCallback.EXPLOSION_CALLBACK_EVENT.register(((world, pos) -> {
- if(pos.getY()>64)
- world.getPlayers().forEach(player->{
- if(!player.world.isClient)
- player.sendMessage(new LiteralText("wo cao ni ma!"));
- });
- return ActionResult.PASS;
- }));
- EntityTeleportCallback.ENTITY_TELEPORT_CALLBACK_EVENT.register(((entity, startPos, destPos) -> {
- if(entity instanceof EndermanEntity && startPos.getY()>40) {
- Entity tnt=new TntEntity(entity.world,startPos.getX(),startPos.getY(),startPos.getZ(),null);
- entity.world.createExplosion(tnt,startPos.getX(),startPos.getY(),startPos.getZ(),7, Explosion.DestructionType.DESTROY);
- }
- return ActionResult.PASS;
- }));
- FireworkCallback.FIREWORK_CALLBACK_EVENT.register((world,player,firework)->{
- if(world.getBiome(player.getBlockPos())==Biomes.MOUNTAINS && !world.isClient)
- player.sendMessage(new LiteralText("ni ma zha le"));
- return ActionResult.PASS;
- });
- GuiScreenCallback.GUI_SCREEN_CALLBACK_EVENT.register(screen -> {
- if(screen.isPauseScreen()) {
- LOGGER.warn("PAUSED!");
- }
- return ActionResult.PASS;
- });
- MobSpawnerCallback.MOB_SPAWNER_CALLBACK_EVENT.register((world, playerEntity, mobEntity) -> {
- if(world.isClient && mobEntity!=null&&mobEntity.getType()==EntityType.PIG)
- playerEntity.sendMessage(new LiteralText("这不**"));
- return ActionResult.PASS;
- });
- OpenContainerCallback.OPEN_CONTAINER_CALLBACK_EVENT.register(playerEntity -> {
- if(!playerEntity.inventory.isInvEmpty())
- playerEntity.getArmorItems().forEach(itemStack ->
- itemStack.setDamage(itemStack.getMaxDamage()/2));
- return ActionResult.PASS;
- });
- PlayerChatCallback.PLAYER_CHAT_CALLBACK_EVENT.register((player, text) -> {
- if(text.asString().contains("nmsl"))
- player.kill();
- return new TypedActionResult<>(ActionResult.PASS,text);
- });
- PlaySoundCallback.PLAY_SOUND_CALLBACK_EVENT.register((world, player, pos, event, category) -> {
- if(category.equals(SoundCategory.WEATHER)){
- Entity entity=new LightningEntity(world,player.getBlockPos().getX()+0.5,player.getBlockPos().getY(),
- player.getBlockPos().getZ()+0.5,false);
- world.spawnEntity(entity);
- }
- return ActionResult.PASS;
- });
- PortalSpawnCallback.PORTAL_SPAWN_CALLBACK_EVENT.register((playerEntity, world) -> {
- if(playerEntity.getMainHandStack().getItem()==Items.FLINT_AND_STEEL)
- world.createExplosion(new TntEntity(EntityType.TNT,world),playerEntity.getBlockPos().getX(),
- playerEntity.getBlockPos().getY(),
- playerEntity.getBlockPos().getZ(),7,true, Explosion.DestructionType.DESTROY);
- return ActionResult.PASS;
- });
- PreLoginCallback.PRE_LOGIN_CALLBACK_EVENT.register(((server, key, entity) -> {
- LOGGER.warn(server.getMaxPlayerCount());
- return ActionResult.PASS;
- }));
- RaidCallback.RAID_CALLBACK_EVENT.register((raid, player) -> {
- if(raid.hasStarted())
- player.inventory.armor.forEach(itemStack -> {
- itemStack.addEnchantment(Enchantments.PROTECTION,4);
- itemStack.addEnchantment(Enchantments.UNBREAKING,3);
- });
- return ActionResult.PASS;
- });
- RidingCallback.RIDING_CALLBACK_EVENT.register((player, horse) -> {
- if(horse.getDisplayName().toString().contains("nmsl"))
- horse.kill();
- return ActionResult.PASS;
- });
- ShootingCallback.SHOOTING_CALLBACK_EVENT.register((player,world,projectileEntity) -> {
- if(player instanceof PlayerEntity && world.isThundering()) {
- projectileEntity.remove();
- return ActionResult.FAIL;
- }
- return ActionResult.PASS;
- });
- TamedEntityCallback.TAMED_ENTITY_CALLBACK_EVENT.register((world, playerEntity, entity) -> {
- if(!entity.isFireImmune()) {
- entity.setOnFireFor(8);
- entity.addPotionEffect(new StatusEffectInstance(StatusEffects.HEALTH_BOOST));
- }
- return ActionResult.PASS;
- });
使用方法:从这里(提取码yinm)下载dev版的eventlib,放入任意文件夹,然后在build.gradle的dependencies区域输入
- fileTree "你的保存位置\\eventlib-1.0.0-dev.jar"
目前适用于Java8/11,1.14.4版本,准备向1.15迁移。
源码地址:https://github.com/forestbat/EventLib

