Effect only visible players

Discussion in 'Spigot Plugin Development' started by OopsyPoopsy, Apr 29, 2017.

  1. Hello Spigot.

    In my practice plugin, there is multi-use arenas (Multiple people fighting in one arena but invisible to other players). Since the game mode is PotPvP, there is use of potions and the Instant Health & Poison, Slowness, etc effect the players even if the player who threw the potion is not visible to the players affected.

    I need the players who are not visible to the shooter of the potion to not be affected.

    Thank you.
     
  2. You should be able to use if player1.canSee(player2)


    Sent from my iPhone using Tapatalk
     
  3. I tried this but it didn't seem to work.

    Code (Java):

    @EventHandler

    public void onPot(PotionSplashEvent e) {

    Player p = (Player) e.getEntity();

    Player p2 = (Player) e.getAffectedEntities();

    if (!p.canSee(p2)) {

    e.setCancelled(true);
     
     
  4. Okay you casted a list or collection of entities to a single Player variable that won't work.

    You're going to need to loop through the affected entities

    for(Entity ent:e.getAffectedEntities())

    Then check if ent is an instanceof Player
    If so, then you can safely cast ent to player and do if p1 canSee p2

    You don't want to cancel the whole event either you want to remove that entity from the list. I suggest making a Set<Entity> and adding the entities you wish to remove to it

    Then once you're done checking all the affected do. e.getAffectedEntities().removeAll(yourSet)

    Sent from my iPhone using Tapatalk
     
  5. Code (Java):

    public static ArrayList<Player> affected = new ArrayList<Player>();


    @EventHandler

    public void onPot(PotionSplashEvent e) {

    Player p = (Player) e.getEntity();


    for (LivingEntity ent: e.getAffectedEntities()) {

    if (ent instanceof Player) {

    Player p2 = (Player) e.getAffectedEntities();

    affected.add(p2);



    if (!p.canSee(p2)) {

    e.getAffectedEntities().removeAll(affected);

    }

    }



    }

    }



    }
     
    Still Nothing :(
     
  6. You still casted the whole list of entities to a player.
    Use

    Player p2 = (Player) ent;


    Sent from my iPhone using Tapatalk
     
  7. Code (Java):

    public static ArrayList<Player> affected = new ArrayList<Player>();


    @EventHandler

    public void onPot(PotionSplashEvent e) {

    Player p = (Player) e.getEntity();


    for (LivingEntity ent: e.getAffectedEntities()) {

    if (ent instanceof Player) {

    Player p2 = (Player) ent;

    affected.add(p2);



    if (!p.canSee(p2)) {

    e.getAffectedEntities().removeAll(affected);

    }

    }



    }

    }



    }
     
    Still broke
     
  8. The way your pasting your code is making it very hard to read. Could you add it to a hastebin or something?
     
  9. what error is it giving you in the console?
    ConcurrentModificationError?

    you can not remove items from a list while you are iterating over it.

    1) delete the public static list at the top you do not need it, try to avoid declaring variables Public Static its frowned upon. This variable is only used for that method so it should be declared inside the method.

    2) BEFORE the for loop add this:
    Set <LivingEntity> cantSee = new HashSet<LivingEntity>();
    *I used a set here because its a bit faster than a List, there is no reason to have to access the livingentities inside it by their index so no need for a list.

    3) you should only add the players to the list if the original player can not see them,
    thus so when you do if(!p1.canSee(p2))

    THEN add them to a list,
    cantSee.add(ent);

    4) after the for loop is complete
    e.getAffectedEntities().removeAll(cantSee);

    **This is done AFTER because if you modify add/remove entities to the list while you are looping through it you will get a ConcurrentModificationError