Solved Problem teleporting players.

Discussion in 'Plugin Development' started by MadTheKing, Jul 30, 2019.

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

    MadTheKing

    Hello everyone, I'm having issues with my code not working. I'm new to bukkit and I haven't been having much luck teleporting players to random locations. I want it to teleport each online player to a new random location. Right now all my code does is teleport the command sender to the world spawn. I can't figure out why. If I try it on a single player instead of online players it works fine.

    Here's the information I have and my code:
    I'm on windows 10. x64
    I'm coding on 1.8.3 R0.1 SNAPSHOT bukkit.
    I'm not getting any errors when I reload in the consol nor am I getting any errors in intelliJ.
    Sorry in advance about the messy code.

    Code:
    package me.server.uhcmini.Commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class StartCommand implements CommandExecutor{
        private List<Player> onlinePlayers = new ArrayList();
    
        @Override
        public boolean onCommand (CommandSender cs, Command cms, String lbl, String[] args){
            if(lbl.equalsIgnoreCase("startcommand")){
                if (cs instanceof Player){
                    Player p = (Player)cs;
                    if (p.isOp()) {
    
                        Location uhcmini = p.getServer().getWorld("uhcmini").getSpawnLocation();
                        p.teleport(uhcmini);
    
                        Random r = new Random();
                        int x = r.nextInt(1000)+1;
                        int z = r.nextInt(1000)+1;
                        int y = p.getWorld().getHighestBlockYAt(x, z);
                        Location tploc = new Location(p.getWorld(),x,y,z);
    
                        for (Player po:Bukkit.getServer().getOnlinePlayers()){
                            onlinePlayers.add(po);
    
                            for (int i = 0; i> Bukkit.getOnlinePlayers().size(); i++){
                                onlinePlayers.get(i).teleport(tploc);
                            }
    
                        }
    
                        p.sendMessage(ChatColor.GREEN + "Working~~~~");
                    }
                }
            }
    
           return true;
        }
    
    }
    
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MadTheKing Why are you looping over the players twice? With different methods
    And i > onlinePlayers().size() will never be true
     
    MadTheKing likes this.
  3. Offline

    KarimAKL

    You can move this outside the method, then you won't be creating a new Random everytime the command is run.
    1. timtower already explained this.
    2. Why are you teleporting the first player twice? If it's because you need to get the player's world, then you can just set the world of 'tploc' to 'uhcmini.getWorld()'.
    Why are you saving the players? You aren't using the variable anywhere else. (It's private)
     
    MadTheKing likes this.
  4. Offline

    MadTheKing

    My bad everyone, I didn't need the double loops, that was totally useless. I was having a problem with it teleporting the players all to same location and turns out the problem was as some of you stated, in the Random location I was creating. Also yes I didn't need to store the players. Rn I'm using a foreach to teleport the players instead of a loop.

    Thank you all for the responses!
     
  5. Offline

    KarimAKL

    @MadTheKing Is this solved then? If that's the case, remember to set the title prefix to Solved. (Use 'Thread Tools' at the top right of your original post)
     
  6. Offline

    MadTheKing

    Yes, Solved. Thanks for the help :)
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page