Solved Get all online players if any are online

Discussion in 'Plugin Development' started by fritzcat12, May 10, 2015.

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

    fritzcat12

    Hello. I need help with getting all players currently online and making them god(invincible) because im making a survival games plugin. Here is my code:
    Code:
    public class Main(){
    List <Player> = Bukkit.getServer().getOnlinePlayers(); //this gives me an error saying: Type mismatch: cannot convert from Collection<capture#3-of ? extends Player> to List<Player>
    public void onPlayerDamageEvent(EntityDamageEvent e){
    //i dont know how to canel  the damage event plz help
    }
    }
    So yeah i need help fixing the list and cancelling the damage event.
    Thanks
     
  2. Offline

    Emalton

    @fritzcat12 You should name your list. For Example:
    Code:
    List <Player> PlayerList = Bukkit.getServer().getOnlinePlayers();
     
  3. Offline

    Konato_K

    @fritzcat12 Just cancel the event no matter what, if you want it to be for everyone then you don't need to worry about which player is it.
     
  4. Offline

    teej107

    ClassCastException Alert!!!!
     
  5. Offline

    MacDev

    Code:
    List<String> players = new ArrayList<String>();
    for (Player all : Bukkit.getOnlinePlayers()) {
    players.add(all.getName());
    }
    
    Use this to add online players.
    Code:
    @EventHandler
    public void onDamage(EntityDamageEvent e) {
    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (players.contains(p.getName())) {
    e.setCancelled(true);
    }
    }
    }
    Use this to prevent damage.

    Be sure to remove the player from the list onQuit.
     
  6. Offline

    Konato_K

    @MacDev No need to check if they are in the collection since the collection it's for all the players online.
     
  7. Offline

    FloppehDeesk

    Yeah, you can just do

    Code:java
    1.  
    2. @EventHandler
    3. public void onEntityDamageEvent(EntityDamageEvent e){
    4. if (e.getEntity() instanceof Player){
    5. e.setCancelled(true);
    6. }
    7. }
    8.  
     
  8. Offline

    RawCode

    online players always online.

    only possible way to get online players who not online is ill times async threads.
     
  9. Offline

    fritzcat12

    Wow guys thanks for your help! :D it works
     
Thread Status:
Not open for further replies.

Share This Page