Jump to content

Event Class not working


Recommended Posts

I tried to create an event class which shall check if a player is holding a torch and make zombies and skeleton scared of the player an run away, but IntelliJ is telling me that Class 'ModEvents' is never used and Method 'onPlayerTick(net.minecraftforge.event.TickEvent.PlayerTickEvent)' is never used and both the class name and the method name appear grayed out instead of yellow. I don't understand where I went wrong. Please help and do tell me if I need to provide any other pieces of my mod.

@Mod.EventBusSubscriber(modid = ImmersiveSurvival.MOD_ID)
public class ModEvents {
    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (event.side == LogicalSide.CLIENT && event.phase == TickEvent.Phase.END) {
            PlayerEntity player = event.player;
            if (player.getHeldItemMainhand().getItem() == Items.TORCH) {
                World world = player.world;
                AxisAlignedBB box = player.getBoundingBox().grow(8.0);
                List<MobEntity> mobs = world.getEntitiesWithinAABB(MobEntity.class, box);
                for (MobEntity mob : mobs) {
                    if (!mob.isPotionActive(FEAR_EFFECT) && (mob instanceof ZombieEntity || mob instanceof SkeletonEntity)) {
                        mob.addPotionEffect(new EffectInstance(FEAR_EFFECT, 20));
                        mob.setRevengeTarget(player);
                    }
                }
            }
            World world = player.world;
            AxisAlignedBB box = player.getBoundingBox().grow(128.0);
            for (Entity entity : world.getLoadedEntitiesWithinAABB(MobEntity.class, box)) {
                if (entity instanceof LivingEntity) {
                    LivingEntity livingEntity = (LivingEntity) entity;
                    if ((livingEntity instanceof ZombieEntity || livingEntity instanceof SkeletonEntity) && livingEntity.isPotionActive(FEAR_EFFECT)) {
                        if (livingEntity.getActivePotionEffect(FEAR_EFFECT).getDuration() == 0) {
                            livingEntity.setRevengeTarget(null);
                        }
                    }
                }
            }
        }
    }
}

Also, I created a Fear_Effect which doesnt really do anything except set a countdown so that I can set the target back to null. Please suggest a better way if you know one.

Link to comment
Share on other sites

Posted (edited)

Edited - Actually I think I've already done that in the line before the class declaration.
@Mod.EventBusSubscriber(modid = ImmersiveSurvival.MOD_ID)

And also in the Mod class.

MinecraftForge.EVENT_BUS.register(ModEvents.class);

But it's still not working, please please help.

Edited by PadFoot2008
Link to comment
Share on other sites

Only register the event handler one way, not both.

IntelliJ thinks the functions aren't called because the code that calls them is not normal code. You can safely ignore the warnings, as long as the methods are being called in-game (which you could check by setting a breakpoint and running debug).

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.