Teleport player to a player after player

Discussion in 'Plugin Development' started by Javaer, Mar 20, 2013.

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

    Javaer

    There is a way to get all players in the server and then teleport the player every time he do something to next player?
     
  2. Offline

    CaptainBern Retired Staff

    ?? so when a player hits a player he gets teleported to te next player in the list of all online players? if so whats the point of that?
     
  3. Offline

    Javaer

    No, I want when he right click with a pumpkin in his hand ( I know how to do that ) he will teleport to the next player in the server ( every time he doing this )
     
  4. Offline

    AlexLeporiday

    In short, yes of course.

    Bukkit.getOnlinePlayers() will return you a list of all players online. Only use this if you don't already have a pointer to a Server object already as you can get the online players from a server objects too.

    Teleporting a player is just as simple as any Entity has the teleport() method so you can take a player objects and do playerToTeleport.teleport(locationOfPlayerToTeleportTo) where locationOfPlayerToTeleportTo is a location object of the player you want to teleport to.

    As for "Every time they do something" you'll want to listen to some event or perhaps used the scheduler to teleport the player around on a timer. You have to choose your trigger event first before developing how you're going to do it.
     
  5. Offline

    CaptainBern Retired Staff

    oooh okey, i had something like that to but with stringlist in yml file, but i just choosed a random string...
     
  6. Offline

    finalblade1234

    Im not sure what you mean but this is how to get all online players:
    Code:
    for(Player all : Bukkit.getServer().getOnlinePlayers()){
                            //do stuff
                        }
     
  7. Offline

    Javaer

    Yes I know all the stuff you writed, thank you.
    But the problem is I don't know how to get the next player from the online list.
    Here is something that I did, don't working very well..
    Code:
     
    Player[] p1 = Bukkit.getServer().getOnlinePlayers();
    Player p = e.getPlayer();
    if(e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().equals(St)){
                for(int i = 0; i < p1.length; i++){
                    for(Player pa : p.getServer().getOnlinePlayers()){
                    List<Location> Locations = Arrays.asList(pa.getLocation());
                    if(!Spec.contains(pa.getName())){
                    if(pa != p){
                    p1[i].teleport(Locations.get(i));
                    }
                    }
                    }
                }
          }
     
  8. Offline

    CaptainBern Retired Staff

    he means like u haz all players, like player a = 1, player b = 2 , player c=3 and so go on, and when player a right clicks player b, player a will be teleported to player c, when he hits player c he will be teleported to player d who will have number 4...
     
  9. Offline

    Javaer

    When a player right click with something in the air.
    So how can I do that?
     
  10. Offline

    gomeow

    You could use a Queue
     
  11. Offline

    CaptainBern Retired Staff

    i think if u do:
    Player[] pl = Bukkit.getOnlinePlayers();
    and then if u do pl.get(0); will return the first player in the list, if u do pl.get(1) it will return the second...didnt tested this
     
  12. Offline

    Javaer

    Nope..
     
  13. Offline

    gomeow

  14. Offline

    CaptainBern Retired Staff

    oh sorry forgot about that, then maybe put them in a list..
     
  15. Offline

    Javaer

    I still don't know how to do it, Please help..
     
  16. Offline

    gomeow

    Like I said, use a Queue
     
  17. Offline

    Javaer

    Actually I don't realy know how to use a Queue, can I get a better explain for this?
     
  18. Offline

    CaptainBern Retired Staff

  19. Offline

    Javaer

  20. Offline

    CaptainBern Retired Staff

    you can mark the thread as solved now, and maybe give the people who helped you a like :3
     
    Javaer likes this.
  21. Offline

    Javaer

    Actually I havn't solved it..
    Here is my code:
    Code:
      @EventHandler
      public void Teleport(PlayerInteractEvent e){
          Player p = e.getPlayer();
          ItemStack St = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
          ItemMeta renameMeta = St.getItemMeta();
          renameMeta.setDisplayName("§8Teleporter");
          St.setItemMeta(renameMeta);
          if(e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().equals(St)){
              Queue<Player> qe = new LinkedList<Player>();
              for(Player Ap : p.getServer().getOnlinePlayers()){
                  if(!Spec.contains(Ap.getName())){
                  qe.add(Ap);
                  }
              }
              Player NextP = (Player) qe.remove();
              qe.remove(1);
              p.teleport(NextP);
          }
      }
    What it does is only teleport to one player in the server..
    I want to teleport a player next time other player and keep that in a loop.
    Please help?
     
  22. Offline

    CaptainBern Retired Staff

    i found something that maybe can interest you: http://forums.bukkit.org/threads/teleport-queue-help-with-code.95519/
     
  23. Offline

    Javaer

  24. Offline

    CaptainBern Retired Staff

    wait ill try some code..

    idk if it works....

    public class Teleport {

    @EventHandler
    public void Tele(PlayerInteractEvent e){

    List<Player> list = new ArrayList<Player>();
    HashMap<String, Integer> lol = new HashMap<String, Integer>();
    Player[] players = Bukkit.getOnlinePlayers();

    Player p = e.getPlayer();
    ItemStack St = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
    ItemMeta renameMeta = St.getItemMeta();
    renameMeta.setDisplayName("§8Teleporter");
    St.setItemMeta(renameMeta);
    if(e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().equals(St)){

    for(Player player : players){
    list.add(player);
    }
    if(lol.containsKey(p.getName())){
    Player t = list.get(lol.get(p.getName() + 1));
    p.teleport(t);
    }else{
    lol.put(p.getName(), 0);
    }

    }
    }
    }

    let me know if the code i gave you works...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  25. Offline

    gomeow

  26. Offline

    CaptainBern Retired Staff

    Details....
     
  27. Offline

    Javaer

    I tried to use your code ( btw <3 ) and it didn't do anything..
    Code:
      @EventHandler
      public void SpectatorTeleport(PlayerInteractEvent e){
          final Player p = e.getPlayer();
          ItemStack St = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
          ItemMeta renameMeta = St.getItemMeta();
          renameMeta.setDisplayName("§8Teleporter");
          St.setItemMeta(renameMeta);
          List<Player> list = new ArrayList<Player>();
          HashMap<String, Integer> lol = new HashMap<String, Integer>();
          Player[] players = Bukkit.getOnlinePlayers();
          if(e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().equals(St)){
          for(Player player : players){
          if(!Spec.contains(player.getName())){
          list.add(player);
          }
          }
          if(lol.containsKey(p.getName())){
          Player t = list.get(lol.get(p.getName() + 1));
          p.teleport(t);
          }else{
          lol.put(p.getName(), 0);
          }
          }
      }
     
  28. Offline

    CaptainBern Retired Staff

    Where you have the 2 }}, change it to one, and add another at the end of code
     
  29. Offline

    Javaer

    Code:
      @EventHandler
      public void SpectatorTeleport(PlayerInteractEvent e){
          final Player p = e.getPlayer();
          ItemStack St = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
          ItemMeta renameMeta = St.getItemMeta();
          renameMeta.setDisplayName("§8Teleporter");
          St.setItemMeta(renameMeta);
          List<Player> list = new ArrayList<Player>();
          HashMap<String, Integer> lol = new HashMap<String, Integer>();
          Player[] players = Bukkit.getOnlinePlayers();
          if(e.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().equals(St)){
          for(Player player : players){
          if(!Spec.contains(player.getName())){
          list.add(player);
          }
          if(lol.containsKey(p.getName())){
          Player t = list.get(lol.get(p.getName() + 1));
          p.teleport(t);
          }else{
          lol.put(p.getName(), 0);
          }
          }
          }
      }
    Error in console with this line: Player t = list.get(lol.get(p.getName() + 1));
     
  30. Offline

    CaptainBern Retired Staff

    And also you have to edit the part where it adds +1 to the list becus now it will never get the first person in the list and the hashmap value can be higher then the list size but you can fix dis by: if(lol.get(player.getname() > list.getSize()){
    Lol.get(player.getName()).set(-1);}. Now it wil get the first player in the list also

    Hmm im on my phone now, ill take a look on it tomorow, sorry :$

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page