Solved Monsters still attack player

Discussion in 'Plugin Development' started by voltywolty, Oct 2, 2021.

  1. Offline

    voltywolty

    I currently have a Set<UUID> that a player gets added to when they die. This should make it where monsters don't attack them, but it isn't working. Here is the code for the onEntityTarget for the monsters attacking players.

    Code:
    @EventHandler
        public void onEntityTarget(EntityTargetEvent event) {
            Player player = (Player) event.getEntity();
           
            if (isMonster.contains(player.getUniqueId())) {
                if (event.getTarget() instanceof Player) {
                    if (event.getEntity() instanceof Monster) {
                        event.setCancelled(true);
                        return;
                    }
                }
            }
            else if (!(event.getTarget() instanceof Player)) {
                return;
            }
        }
    Not really too sure where I'm going wrong, but if someone could help me out, that'd be great.
     
  2. Offline

    davidclue

    @voltywolty You cannot cast the entity to a player without checking it first, your code looks backwards just use it like this
    Code:
    @EventHandler
    public void onEntityTarget(EntityTargetEvent event) {
        if (event.getTarget() instanceof Player && event.getEntity() instanceof Monster && isMonster.contains(event.getTarget().getUniqueId())) event.setCancelled(true);
    }
     
  3. Offline

    voltywolty

    I'll try it out, thanks.
     

Share This Page