Solved can't convert from Object to Player

Discussion in 'Plugin Development' started by Kyorax, Feb 5, 2014.

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

    Kyorax

    Hi everyone,
    I'd like to get all the player saved in one arraylist, clear that list and put them into another.
    I have created a method for this, however, it does not work:

    Code:java
    1. public static void startGame(ArrayList<Player> waiting, ArrayList<Player> ingame) {
    2.  
    3. Player[] pwait = (Player[])waiting.toArray();
    4. waiting.clear();
    5.  
    6. for(Player z: pwait) {
    7.  
    8. ingame.add(z);
    9. z.sendMessage("Test successful.");
    10.  
    11. }
    12.  
    13. startNewLevel();
    14.  
    15.  
    16. }


    I get this error, although there are only players in the arraylist:

    Code:
    Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lorg.bukkit.entity.Player;
    at commands.Execute.startGame(Execute.java:34) ~[?:?]
    at commands.CommandListener.onCommand(CommandListener.java:58) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-dev.jar:git-Bukkit-1.7.2-R0.2-36-g9f75167-b3005jnks]
    ... 13 more
     
  2. Offline

    xCyanide

    Kyorax
    Why are you coverting a list to an array?
     
  3. Offline

    Maurdekye

    xCyanide You'd have to loop through all the items individually and add them to the new list.
     
  4. Offline

    Kyorax

    Maurdekye xCyanide What is a better way to reach the same thing?

    Sorry thought too complicated.. xD
    it's just
    Code:java
    1. public static void startGame(ArrayList<Player> waiting, ArrayList<Player> ingame) {
    2.  
    3.  
    4.  
    5.  
    6.  
    7. for(Player z: waiting) {
    8.  
    9. ingame.add(z);
    10. z.sendMessage("Test successful.");
    11.  
    12. }
    13.  
    14. waiting.clear();
    15.  
    16. startNewLevel();
    17.  
    18.  
    19. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    Rocoty

    how about ingame.addAll(waiting)?
     
    Konkz likes this.
Thread Status:
Not open for further replies.

Share This Page