Solved How to teleport a player that has his name stored in ArrayList

Discussion in 'Spigot Plugin Development' started by BoyRadoBG, Sep 11, 2019.

  1. I have a player that has has name stored in ArrayList<Player> bplayer = new ArrayList<>();
    Im getting his name with this
    Player bluep = (Player) this.plugin.bplayer;
    but when i try to do
    bluep.teleport(Bukkit.getWorld("world").getSpawnLocation());
    i get an error in the console caused by:
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.bukkit.entity.Player
    please help
     
    #1 BoyRadoBG, Sep 11, 2019
    Last edited: Sep 11, 2019
    • Funny Funny x 1
  2. SteelPhoenix

    Junior Mod

    a List holding Player objects is not the same as a Player, ergo you can't cast. Iterate over each element in the collection and then do what you want with each player.
     
    • Agree Agree x 1
  3. That you it works
    What i basicly did was that i changed the arraylist from:

    ArrayList<Player> bplayer = new ArrayList<>();
    to:

    List<Player> bplayer = new ArrayList<>();

    Then i changet the the payer from:

    Player bluep = (Player) this.plugin.bplayer;
    to:

    for (int i = 0; i < this.plugin.bplayer.size(); i++) {
    Player bluep = this.plugin.bplayer.get(i);
    bluep.teleport(Bukkit.getWorld("world_blue").getSpawnLocation());
    }
     
    #3 BoyRadoBG, Sep 11, 2019
    Last edited: Sep 11, 2019
  4. You are coding from ancient times, what my professor says when I google code

    Code (Java):
    for (Player p : bplayer) {
        p.teleport(); //finish method here
    }
    You can also use lambda
    Code (Java):
    bplayer.stream().forEach(p -> p.teleport()));
    Also that extra change isn’t necessary and it should be
    Code (Java):
     
     
    • Agree Agree x 1