Solved Get mobs that targets a specific player

Discussion in 'Plugin Development' started by Mr_Deniz, Dec 1, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    Mr_Deniz

    I'm making a vanish plugin and using code below to cancel mob target event:


    @EventHandler
    public void onEntityTargetLiving(org.bukkit.event.entity.EntityTargetLivingEntityEvent event)
    {
    if(hiddenPlayers.contains(event.getTarget().getUniqueId())//If player is invisible
    event.setCancelled(true); //Cancel the target event​
    }


    However this event does not work when mob follows a player and player uses the command after this event triggers. Is there any way to get creatures that targets a player?

    NOTE: Getting nearby entities is my current solution but MobFollowRange variable breaks this solution
     
    Last edited: Dec 2, 2018
  2. @Mr_Deniz

    I know 2 possible solutions:
    -Use the same event to keep track of every mob that is targeting a player. If you would do that, you should always check if the event.getTarget() returns a player and add the mob to some kind of map. When the player vanishes, you use the map to find all mobs that are currently targeting the player.

    -Whenever a player vanishes, get the list of all currently loaded entities in his world and check for every mob if it is targeting that player and set his target to null if so.

    The last option might seem expensive, but I think it won't be slow since I assume players won't vanish 100 times per second or something like that. The problem of the first option is that you should also remove despawned and killed mobs (or the memory useage will only increase...)
     
    Mr_Deniz likes this.
  3. Offline

    Mr_Deniz

    Thanks for your answer it really helped me! I'm gonna do HashMap<Player, Creature[]> variable and when player executes the command it checks the array (sets the targets to null) and then deletes it (HashMap.remove(Player)). Please reply if I made any mistake

    EDIT:

    After thinking again I'm gonna use second one because it would be not as laggy as the first one in big servers
     
Thread Status:
Not open for further replies.

Share This Page