Teleport a player to player in array

Discussion in 'Plugin Development' started by Konkz, Mar 22, 2014.

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

    Konkz

    Hey!

    I want to teleport player to next player in ArrayList.

    So if I right click an apple, I will teleport to player A, when I click it again to player B etc.

    How would I go about this?
     
  2. Offline

    MRPS

    • Simple loop with a counter which is specific to the player clicking the apple (maybe store that in a hashmap?), increment up by one to the size of the ArrayList, if i > ArrayList size, then set i to 0.
    • Check that the player you want to teleport to is != null and isOnline()
    • Cycle through players, p1.teleport(Bukkit.getPlayer(get player from list in here));
     
  3. Offline

    Konkz

    I tried but I could not get it to work, could I by any chance have an example? If not that's fine.
     
  4. Offline

    willeb96

    Store an index for each player which tells what player the player previously teleported to, then increase the number and teleport to that index, if the index isn't in the array, start over at 0.
     
  5. Offline

    Konkz

    MRPS willeb96

    I played with it for few hours but I still am unable to get it to work. I'm going to seem like an
    utter noob but could I possibly get an exemplar code?

    I feel so bad asking to be spoon fed, but I learn the best by looking at code; I am not one of those
    people that C&P but I read through it to understand.

    Thanks either way.
     
  6. Offline

    tommyhoogstra

    You could try using ArrayList.Count(i) and every time you teleport it increments i by 1.
    so p.teleport(ArrayList.count(i); untested, so noidea if itl work
     
  7. Offline

    Konkz


    But then if there are two spectators and they both use it the number will increment by two?
     
  8. Offline

    tommyhoogstra

    Yes, so if there were 2 spectators they both wont be able to teleport to the same player, this might be able to be accomplished with a hashmap for each player, no idea how though.

    Also it wouldnt be arraylist.count it would be arraylist.size(i), if you are still struggling when I wake up, ill have a go at a prototype
     
  9. Offline

    Konkz


    Exactly. I want it so when I click it once I teleport to player A then player B and the same order goes for another spectator.

    I know how to go about it, a bit, but I can't translate it from my head to code.
     
  10. Offline

    tommyhoogstra

    public HashMap<String, List<Integer>> jump = new HashMap<String, List<Integer>>();
    perhaps?
     
  11. Offline

    Konkz


    I know how to do the hashmap and increase the Integer in it by one but I am not sure how to teleport the player according to the Integer in HashMap.
     
  12. Offline

    tommyhoogstra

    p.teleport(jump.get(p.getName()); Should return the value of the integer from what I remember
     
  13. Offline

    MRPS

    Top of Event Listener:
    Code:
    private HashMap<String,Integer> currentPlayer = new HashMap<String,Integer>();
    In your click event:
    Code:
    Player target;
    // In your click event... if they click with an apple
    if(currentPlayer.get(p.getName)+1 > Bukkit.getOnlinePlayers().size()){
      // set the current player you are on, increment up every time you click
      currentPlayer.put(p.getName(),currentPlayer.get(p.getName)+1);
     
      target = Bukkit.getPlayer(Bukkit.getOnlinePlayers().get(currentPlayer.get(p.getName)));
      if((target.isOnline()) && (target != null)){
        // teleport to the next player
        p.teleport(target.getLocation());
      }
      else {
        // player not found...?
      }
    }
     
  14. Offline

    Konkz


    I've tried it and encountered a problem with
    Code:java
    1. target = Bukkit.getPlayer(Bukkit.getOnlinePlayers().get(currentPlayer.get(p.getName)));


    so I've changed it to

    Code:java
    1. target = Bukkit.getPlayer(currentPlayer.get(p.getName()));


    But now apparently p.getName() is an integer?
    Code:
    The method getPlayer(String) in the type Bukkit is not applicable for the arguments (Integer)
     
  15. Offline

    MRPS

    Konkz
    currentPlayer.get(p.getName()) is an integer, p.getName() is a String. When you get() from a HashMap<String,Integer>, you give it a String (the key) and it spits out the corresponding integer. You need to do:
    Code:
    target = Bukkit.getPlayer(Bukkit.getOnlinePlayers().get(currentPlayer.get(p.getName)));
    because that will get the number player you want, then get the player's name using the index from the list of all players online in your server - then it will use that String (player's name) to retrieve the player by name.
     
  16. Offline

    Konkz

    But then I get this error, hence I changed it:

    Code:
    Cannot invoke get(Integer) on the array type Player[]
     
  17. Offline

    MRPS

    try:

    Code:
    target = Bukkit.getOnlinePlayers()[currentPlayer.get(p.getName())];
     
Thread Status:
Not open for further replies.

Share This Page