Jump to content

Event Class not working


PadFoot2008

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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