Solved Splitting an array to all contained playernames.

Discussion in 'Plugin Development' started by BeaztX, Dec 22, 2013.

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

    BeaztX

    Okay, Coding a minigame, did everything abit different this time, so I want the plugin to split an String of players like:
    Code:
    p1.teleport(new Location...
    p2.teleport...
    Would it be something like 'String spawns = bowgamelobby.nextInt(bowgamelobby.size()...' ?
     
  2. Offline

    mrkirby153

    BeaztX

    So, are you storing it in an array or in a string? Your title says Splitting an array and your post says splitting a string of players. I guess I'll post both ways to do it then.

    String[] (Like String[] players = {"player1", "player2", "player3"}; )
    Code:java
    1. for(String playerName : players /* A list of all your players */){
    2. if(Bukkit.getPlayerExact(playerName) == null) continue;
    3. Player player = Bukkit.getPlayerExact(playerName);
    4. player.teleport(/* Your location goes here */);
    5. }


    String (Like String players = "player1,player2,player3")
    Code:java
    1. for(String playerName : players.split(",")){
    2. if(Bukkit.getPlayerExact(playerName) == null) continue;
    3. Player player = Bukkit.getPlayerExact(playerName);
    4. player.teleport(/* Your location goes here*/);
    5. }


    Edit: Forgot the closing brackets :)
     
  3. Offline

    BeaztX

    well forgot to say what was really the issue my bad, should've posted the arraylist:
    Code:java
    1. ArrayList<String> bowgamelobby = new ArrayList<String>();
    2. for(String ? : ?) {
    3. if(Bukkit.getPlayerExact(?) == null) continue;
    4. Player ? = Bukkit.getPlayerExact(?);
    5. p.teleport(new Location(Bukkit.getWorld("world"), -1137.5, 57, -1974.5));
    6. p.teleport(new Location(Bukkit.getWorld("world"), -1153.5, 57, -1971.5));
    7. p.teleport(new Location(Bukkit.getWorld("world"), -1164.5, 57, -1972.5));
    8. p.teleport(new Location(Bukkit.getWorld("world"), -1170.5, 57, -1962.5));
    9. p.teleport(new Location(Bukkit.getWorld("world"), -1193.5, 57, -1955.5));
    10. p.teleport(new Location(Bukkit.getWorld("world"), -1192.5, 57, -1926.5));
    11. p.teleport(new Location(Bukkit.getWorld("world"), -1175.5, 57, -1910.5));
    12. p.teleport(new Location(Bukkit.getWorld("world"), -1163.5, 57, -1905.5));
    13. p.teleport(new Location(Bukkit.getWorld("world"), -1151.5, 57, -1908.5));
    14. p.teleport(new Location(Bukkit.getWorld("world"), -1135.5, 57, -1904.5));
    15. p.teleport(new Location(Bukkit.getWorld("world"), -1127.5, 57, -1911.5));
    16. p.teleport(new Location(Bukkit.getWorld("world"), -1115.5, 57, -1919.5));
    17. p.teleport(new Location(Bukkit.getWorld("world"), -1105.5, 57, -1928.5));
    18. p.teleport(new Location(Bukkit.getWorld("world"), -1102.5, 57, -1941.5));
    19. p.teleport(new Location(Bukkit.getWorld("world"), -1078.5, 58, -1958.5));
    20. p.teleport(new Location(Bukkit.getWorld("world"), -1094.5, 57, -1960.5));
    21. }
    22. }
    23. }


    lol the code fucked up when i pasted it but just the spacing mrkirby153

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

    Heracles421

    BeaztX
    Let me see if I understand, you want to teleport each player located inside the ArrayList to a different location?
     
  5. Offline

    BeaztX

    split the arraylist to playernames, then teleport each one to a location yes.
     
  6. Offline

    Heracles421

    BeaztX
    In that case, you don't need to split the ArrayList, as it's already splitted. An ArrayList devides into indexes when you add data to it. Therefore, we can simpy use a for loop to get the data inside the ArrayList.
    So, lets say that you want to add 3 players, Fee, Foo, Faa, and we're adding them to the ArrayList called players. We would do this:
    Code:java
    1. ArrayList<String> players = new ArrayList<String>();
    2.  
    3. players.add("Fee");
    4. players.add("Foo");
    5. players.add("Faa");


    After adding them, maybe we want to send them a message, it's done like this:
    Code:java
    1. for (String player : players){
    2. player.sendMessage("Hello!");
    3. }


    To teleport them, simply change the code inside the loop.
     
  7. Offline

    BeaztX

    well the issue is me trying to teleport 16 players to 16 DIFFERENT locations :/

    WAIIIIT. would something like bowgamelobby.length(1) something work?

    bowgamelobby.get(0); <-- would this work for somehow getting the player in a arraylist?
    Assist

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

    Heracles421

    BeaztX
    Yes, that can work too.
     
    BeaztX likes this.
  9. Offline

    BeaztX

    Thanks alot bro! :) I'm gonna follow + like ;), this would work right: ?
    Code:java
    1. String p1name = bowgamelobby.get(0);
    2. Player p1 = Bukkit.getPlayer(p1name);
    3. p1.teleport(new Location(Bukkit.getWorld("world"), -1137.5, 57, -1974.5));
     
  10. Offline

    Heracles421

    BeaztX
    Yup, that should work fine
     
Thread Status:
Not open for further replies.

Share This Page