Error with schedules and array list players

Discussion in 'Plugin Development' started by AKMiner98, Dec 18, 2013.

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

    AKMiner98

    So I have this error:
    Code:
    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lorg.bukkit.entity.Player;
    at Coding.Main$1.run(Main.java:200) ~[?:?]
    Which is referring to:
    Code:
    Object[] o = Server1PL.toArray();
    Player[] join = (Player[]) o;
    join[0].sendMessage("Test");
    Server1L--;
    I would use
    Code:
    if(o instanceof Player[]){
    But it gives me no errors nor sends the message...

    Anyone got a idea?
     
  2. Offline

    xTrollxDudex

    AKMiner98
    Player objects aren't Objects, what are you trying to do?
     
  3. Offline

    AKMiner98

    I am trying to get the array list Server1PL certain lines and if they equal players it sends them messages.
    Which should work with
    Object[] o = Server1PL.toArray();
    if(o instanceof Player[]){
    Player[] join = (Player[]) o;
    join[0].sendMessage("Test");
    }
    But it does not send the message nor throw me a error.
    (Note this is all in a scheduled sync repeating task)
     
  4. Offline

    1Rogue

    Code:java
    1. Player[] arr = Server1PL.toArray(new Player[Server1PL.size()]);


    Also naming conventions, lowercase fields, etc.
     
  5. Offline

    AKMiner98

    1Rogue
    So I understand that you made arr to use.
    But I am confused on how to use it to check for the player in a certain line and send them a message.
     
  6. Offline

    1Rogue


    ....do whatever you were going to do with the object array before?
     
  7. Offline

    AKMiner98

    1Rogue
    *Facepalm*
    Thanks xD

    1Rogue
    Wait, when I try to use this, I get
    Code:java
    1. at Coding.Main$1.run(Main.java:200) ~[?:?]

    I am using arr[1]
    I also tried arr[0].

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

    The_Doctor_123

    AKMiner98
    You're trying to get a non-existing index in an array.
     
  9. Offline

    AKMiner98

    The_Doctor_123
    So am I searching for a certain player in the arraylist within arr[here]?
     
  10. Offline

    The_Doctor_123

    AKMiner98
    Err.. what? By the way.. array != ArrayList.
     
  11. Offline

    AKMiner98

    The_Doctor_123
    I was using
    join[0].sendMessage("Test");
    Before and 1Rogue said to just do what I was doing before with arr.
    I don't understand what I do with this line of code :p

    And what do you mean by array != ArrayList?
    You mean in this line of code:
    Code:java
    1. Player[] arr = Server1PL.toArray(new Player[Server1PL.size()]);

    Or what?
     
  12. Offline

    The_Doctor_123

    AKMiner98
    I honestly do not know why you're converting an ArrayList to an array in this case, that doesn't make much sense. Secondly, why are you getting a specific index? That basically gives you a random player. But anyway, your error states that you're getting an index that doesn't exist, I'm not so sure what's so hard to understand there.
     
  13. Offline

    AKMiner98

    The_Doctor_123
    With a player joining a game, they are added into the arraylist*.
    Instead of making the message broadcast to the entire server, I want it to send the message to the players in the arraylist. So they get the message but everyone else does not. Also when the game starts, I can use that random player that you were talking about (which is what I was going for) to be able to pick teams and do things without it being unfair.
    I don't understand with this line of code how I am supposed to do that.
    What index am I looking for? (Specific name, line number, etc.)
     
  14. Offline

    1Rogue

    so then just iterate through the values in the arraylist? Why bother converting it to a primitive data type?
     
  15. Offline

    AKMiner98

    1Rogue
    Problem is I don't know how to do that. I used to just use my original line of code and it worked.
    I don't know how to iterate through the values.
     
  16. Offline

    1Rogue

  17. Offline

    The_Doctor_123

    AKMiner98
    Iterate through the ArrayList and message the players. What you're doing only messages 1 player, if you're lucky, it's probably more likely to throw an Exception. Okay, I lied about the random part. I thought you were getting all online players for some reason. Just generate a random number with the length of the ArrayList and get the index with that number.

    Edit: ...

    Edit #2: Happen to know Java?
     
  18. Offline

    AKMiner98

    1Rogue
    That does not help at all.
    I don't need to go through numbers to pick players, I need to get a certain line in the arraylist, and if it equals a player, do things (like send messages, teleport, etc.)
     
  19. Offline

    The_Doctor_123

  20. Offline

    AKMiner98

    The_Doctor_123
    That would only work if I was searching for a certain player inside of the array list. Not getting the player in a certain line.
     
  21. Offline

    Gater12

    AKMiner98 You mean you want to get the player who's assigned number 69 in the list? Or something like that?
     
  22. Offline

    AKMiner98

    Gater12
    Yes. I want to get the ArrayList's line 69 (lol) and be able to send the messages or teleport them and stuff.
     
  23. Offline

    Gater12

    AKMiner98 Why would you want to do that?
     
  24. Offline

    The_Doctor_123

    AKMiner98
    You mean, you want a random value from the ArrayList? Look into the Random class. lol.. Random class, I'm so funny.
     
  25. Offline

    AKMiner98

    Gater12
    So I can get the players that are in the game (or the arraylist) and send them messages instead of broadcasting them to the whole server. Also, when the game starts, only those players in the arraylist teleport to the given location.

    If worse comes to worse, I can just use a lot of strings and make my work even longer, but I guess that's my luck :p
     
  26. Offline

    The_Doctor_123

    AKMiner98
    I'm extremely lost on what you're doing. Do you want a random value, or do you want the whole thing?
     
    Gater12 likes this.
  27. Offline

    Gater12

    AKMiner98 Wouldn't you just iterate through all the online players -> check if they are in the list -> then do something to them?
    Yes it involves contains() like The_Doctor said.
     
  28. Offline

    AKMiner98

    Gater12
    It's possible, but a little tricky.
    That is why I was using my old code to save me some time. I guess with the new updates, it no longer works or I am doing something wrong. I am just going to go over my code and try some stuff.
     
  29. Offline

    1Rogue


    Code:java
    1. for (Player p : Server1PL) {
    2. if (/*some qualifer for the player*/) {
    3. //send message
    4. }
    5. }


    I fail to see where this doesn't help at all. Did you even read the page I linked?
     
  30. Offline

    AKMiner98

    1Rogue
    I scrolled through it and saw it was a number counter and did not see where that could help me at all.
    I will do some toying with that code later, but for now, I have got to go do other work.

    Thanks for the help, I am sure I will have other questions for later, but I will just set this thread to solved
     
Thread Status:
Not open for further replies.

Share This Page