Teleport all players online to different locations

Discussion in 'Spigot Plugin Development' started by Luk_3D, May 9, 2016.

  1. This is my class that reads the config, I wish that in the main class with a for loop players online are teleported one in each location. The positions are 15 but players can be the same or less. Sorry but I am unfamiliar with collections. Tks for the help :)

    Code (Text):
    package Murder;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    public class getconfig {
        private main plugin;
       
        public getconfig (main plugin) {
            this.plugin = plugin;
        }
       
        World world = Bukkit.getServer().getWorld("world");
       
        //Player spawns
        public Location getPlayerLoc1(){
                 
            Location PlayerLoc1 = new Location(world, plugin.getConfig().getInt("player1.x"),
                    plugin.getConfig().getInt("player1.y"), plugin.getConfig().getInt("player1.z"));
            return PlayerLoc1;
        }
        public Location getPlayerLoc2(){
             
            Location PlayerLoc2 = new Location(world, plugin.getConfig().getInt("player2.x"),
                    plugin.getConfig().getInt("player2.y"), plugin.getConfig().getInt("player2.z"));
            return PlayerLoc2;
        }
        public Location getPlayerLoc3(){
             
            Location PlayerLoc3 = new Location(world, plugin.getConfig().getInt("player3.x"),
                    plugin.getConfig().getInt("player3.y"), plugin.getConfig().getInt("player3.z"));
            return PlayerLoc3;
        }
        public Location getPlayerLoc4(){
             
            Location PlayerLoc4 = new Location(world, plugin.getConfig().getInt("player4.x"),
                    plugin.getConfig().getInt("player4.y"), plugin.getConfig().getInt("player4.z"));
            return PlayerLoc4;
        }
        public Location getPlayerLoc5(){
             
            Location PlayerLoc5 = new Location(world, plugin.getConfig().getInt("player5.x"),
                    plugin.getConfig().getInt("player5.y"), plugin.getConfig().getInt("player5.z"));
            return PlayerLoc5;
        }
        public Location getPlayerLoc6(){
             
            Location PlayerLoc6 = new Location(world, plugin.getConfig().getInt("player6.x"),
                    plugin.getConfig().getInt("player6.y"), plugin.getConfig().getInt("player6.z"));
            return PlayerLoc6;
        }
        public Location getPlayerLoc7(){
             
            Location PlayerLoc7 = new Location(world, plugin.getConfig().getInt("player7.x"),
                    plugin.getConfig().getInt("player7.y"), plugin.getConfig().getInt("player7.z"));
            return PlayerLoc7;
        }
        public Location getPlayerLoc8(){
             
            Location PlayerLoc8 = new Location(world, plugin.getConfig().getInt("player8.x"),
                    plugin.getConfig().getInt("player8.y"), plugin.getConfig().getInt("player8.z"));
            return PlayerLoc8;
        }
        public Location getPlayerLoc9(){
             
            Location PlayerLoc9 = new Location(world, plugin.getConfig().getInt("player9.x"),
                    plugin.getConfig().getInt("player9.y"), plugin.getConfig().getInt("player9.z"));
            return PlayerLoc9;
        }
        public Location getPlayerLoc10(){
             
            Location PlayerLoc10 = new Location(world, plugin.getConfig().getInt("player10.x"),
                    plugin.getConfig().getInt("player10.y"), plugin.getConfig().getInt("player10.z"));
            return PlayerLoc10;
        }
        public Location getPlayerLoc11(){
             
            Location PlayerLoc11 = new Location(world, plugin.getConfig().getInt("player11.x"),
                    plugin.getConfig().getInt("player11.y"), plugin.getConfig().getInt("player11.z"));
            return PlayerLoc11;
        }
        public Location getPlayerLoc12(){
             
            Location PlayerLoc12 = new Location(world, plugin.getConfig().getInt("player12.x"),
                    plugin.getConfig().getInt("player12.y"), plugin.getConfig().getInt("player12.z"));
            return PlayerLoc12;
        }
        public Location getPlayerLoc13(){
             
            Location PlayerLoc13 = new Location(world, plugin.getConfig().getInt("player13.x"),
                    plugin.getConfig().getInt("player13.y"), plugin.getConfig().getInt("player13.z"));
            return PlayerLoc13;
        }
        public Location getPlayerLoc14(){
             
            Location PlayerLoc14 = new Location(world, plugin.getConfig().getInt("player14.x"),
                    plugin.getConfig().getInt("player14.y"), plugin.getConfig().getInt("player14.z"));
            return PlayerLoc14;
        }
        public Location getPlayerLoc15(){
             
            Location PlayerLoc15 = new Location(world, plugin.getConfig().getInt("player15.x"),
                    plugin.getConfig().getInt("player15.y"), plugin.getConfig().getInt("player15.z"));
            return PlayerLoc15;
        }
       
        //Item spawns
    }
     
  2. What does your config file look like.
     
  3. The way I do it is the following...

    1) Save the locations, and the number of saved locations to a config file.
    2) Create a for loop going until 1 (if your saved location is 1) to the saved number of locations.
    3) Inside the loop I create a new location, and add it to a list. I then tp the player to the location, which I get using <listName>.get(0);
    4) I then remove the location

    That's the way I do it, it seems pretty efficient to me... I don't know if there are easier ways of doing it though, and it also depends on how you setup your config, and how you're getting values from it. Good luck! I hope this helps.
     
  4. Code (Text):
    ####################
    #     Player       #
    ####################

    player1:
      x: 0
      y: 0
      z: 0
    player2:
      x: 0
      y: 0
      z: 0
    player3:
      x: 0
      y: 0
      z: 0
    player4:
      x: 0
      y: 0
      z: 0
    player5:
      x: 0
      y: 0
      z: 0
    player6:
      x: 0
      y: 0
      z: 0
    player7:
      x: 0
      y: 0
      z: 0
    player8:
      x: 0
      y: 0
      z: 0
    player9:
      x: 0
      y: 0
      z: 0
    player10:
      x: 0
      y: 0
      z: 0
    player11:
      x: 0
      y: 0
      z: 0
    player12:
      x: 0
      y: 0
      z: 0
    player13:
      x: 0
      y: 0
      z: 0
    player14:
      x: 0
      y: 0
      z: 0
    player15:
      x: 0
      y: 0
      z: 0

    ####################
    #      Item        #
    ####################

    item1:
      x: 0
      y: 0
      z: 0
    item2:
      x: 0
      y: 0
      z: 0
    item3:
      x: 0
      y: 0
      z: 0
    item4:
      x: 0
      y: 0
      z: 0
    item5:
      x: 0
      y: 0
      z: 0
    item6:
      x: 0
      y: 0
      z: 0
    item7:
      x: 0
      y: 0
      z: 0
    item8:
      x: 0
      y: 0
      z: 0

    ####################
     
  5. loop through how ever many times with a basic for loop setting i as your denominator variable.

    then
    String part = player + String.valueof(i);
    String x = getConfig().getString("part.x");
     
  6.  
  7. Do you like this code?
    Code (Text):
    Collection<? extends Player> arrayOfPlayer = Bukkit.getOnlinePlayers();
                             int j = arrayOfPlayer.size();
                             for (int i = 0; i < j; i++)
                             {
                                 Player online = (Player)arrayOfPlayer.toArray()[i];
                                 switch (i) {
                                 case 0:
                                     online.teleport(getconfig.getPlayerLoc1());
                                     break;
                                 case 1:
                                     online.teleport(getconfig.getPlayerLoc2());
                                     break;            
                                 case 2:
                                     online.teleport(getconfig.getPlayerLoc3());
                                     break;          
                                 case 3:
                                     online.teleport(getconfig.getPlayerLoc4());
                                     break;          
                                 case 4:
                                     online.teleport(getconfig.getPlayerLoc5());
                                     break;          
                                 case 5:
                                     online.teleport(getconfig.getPlayerLoc6());
                                     break;    
                                 case 6:
                                     online.teleport(getconfig.getPlayerLoc7());
                                     break;
                                 case 7:
                                     online.teleport(getconfig.getPlayerLoc8());
                                     break;
                                 case 8:
                                     online.teleport(getconfig.getPlayerLoc9());
                                     break;
                                 case 9:
                                     online.teleport(getconfig.getPlayerLoc10());
                                     break;
                                 case 10:
                                     online.teleport(getconfig.getPlayerLoc11());
                                     break;
                                 case 11:
                                     online.teleport(getconfig.getPlayerLoc12());
                                     break;
                                 case 12:
                                     online.teleport(getconfig.getPlayerLoc13());
                                     break;
                                 case 13:
                                     online.teleport(getconfig.getPlayerLoc14());
                                     break;
                                 case 14:
                                     online.teleport(getconfig.getPlayerLoc15());
                                     break;
                                 }
     
  8. No. I really don't.

    Why getconfig? Instantiate your main class.
    Why do you make a new object just for Bukkit.getOnlinePlayers?
    What is wrong with your switch? You could make this method:
    Code (PHP):
     public Location getPlayerLoc(int number) {
        // SWITCH
    }
     
  9. I hate case/switch soo much!
    Just do this its ez and convenient:
    Code (Text):
    int i = 1;
    for(Player p : Bukkit.getOnlineplayers()) {
    p.teleport(new Location(getConfig().getString("player" + i + ".x"),getConfig().getString("player" + i + ".y"),getConfig().getString("player" + i + ".z"))); //Or whatever your config getting way is

    i++;

    if(i>15) {
    i = 0;
    }

    }
     
    • Like Like x 1
  10. case switch is great if you know how to use it properly, cleans up code and makes things a hell of alot easier.
     
    • Agree Agree x 2
  11. It fires each case, even if "it's not the case"
     
  12. Tks, I'll try it :)
     
  13. This is the correct instruction. Thanks so much
    p.teleport(new Location(world, getConfig().getInt("player" + i + ".x"), getConfig().getInt("player" + i + ".y"), getConfig().getInt("player" + i + ".z")));
     
  14. No it doesn't if you know how to use it...
     
    • Agree Agree x 2