Solved Message getting sent too many times

Discussion in 'Spigot Plugin Development' started by Henrik, Jun 2, 2017.

  1. I am trying to make a minigame. When I want to send a message to the player it gets sent several times. I have no idea why this happens, and I have tried multiple things to make it works, without any success. I am storing the player in an arraylist when they join the server and then removing them when they leave. I think the player recieves the message as many times as there are players in the arraylist... I believe there is something I am missing out when it comes to arraylists and getting the player value. Anyone know what I am doing wrong?

    Code (Text):
    ArrayList<Player> players = new ArrayList<Player>();
     
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            event.setJoinMessage("");
            String UUID = player.getUniqueId().toString();
            if(!getConfig().contains("Players." + UUID));
                getConfig().set("Players." + UUID + ".Wins", 0);
                getConfig().set("Players." + UUID + ".Coins", 0);
                getConfig().set("Players." + UUID + ".Kills", 0);
                getConfig().set("Players." + UUID + ".Deaths", 0);
                getConfig().set("Players." + UUID + ".Rank", 0);
            players.add(player.getName());
        }
     
        @EventHandler
        public void onQuit(PlayerQuitEvent event) {
            event.setQuitMessage("");
            Player player = event.getPlayer();
            players.remove(player.getName());
        }
     
        public void Lobby() {
            for(Player all : Bukkit.getOnlinePlayers()) {
            all.teleport(new Location(Bukkit.getWorld("world"), 20, 60, 0));
            all.setGameMode(GameMode.ADVENTURE);
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
                        Game();
                    }
            }, 10*20L);
            }
        }
     
        public void Game() {
            chooseMap();
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
                        EndGame();
                    }
            }, 240*20L);
        }
     
        public void EndGame() {
         
        }
     
        public void chooseMap() {
            Random object = new Random();
            int map;
            for(int counter = 1; counter<=1;counter++) {
                map = 1+object.nextInt(1);
                if(map == 1){
                    for(Player player : players) {
                        World w = Bukkit.getServer().getWorld("village");
                        Random object1 = new Random();
                        int spawn;
                        for(int counter1 = 1; counter1<=1;counter1++) {
                            spawn = 1+object1.nextInt(16);
                            if(spawn == 1){
                                player.teleport(new Location(w, 0, 9, 0));
                            }
                            if(spawn == 2){
                                player.teleport(new Location(w, -4, 17, -26));
                            }
                            if(spawn == 3){
                                player.teleport(new Location(w, 5, 4, 22));
                            }
                            if(spawn == 4){
                                player.teleport(new Location(w, 8, 8, 14));
                            }
                            if(spawn == 5){
                                player.teleport(new Location(w, 21, 4, 1));
                            }
                            if(spawn == 6){
                                player.teleport(new Location(w, 18, 8, -23));
                            }
                            if(spawn == 7){
                                player.teleport(new Location(w, 6, 8, -25));
                            }
                            if(spawn == 8){
                                player.teleport(new Location(w, -5, 4, -20));
                            }
                            if(spawn == 9){
                                player.teleport(new Location(w, -14, 8, -26));
                            }
                            if(spawn == 10){
                                player.teleport(new Location(w, -8, 14, -14));
                            }
                            if(spawn == 11){
                                player.teleport(new Location(w, -13, 8, 10));
                            }
                            if(spawn == 12){
                                player.teleport(new Location(w, -23, 8, 14));
                            }
                            if(spawn == 13){
                                player.teleport(new Location(w, -7, 9, 25));
                            }
                            if(spawn == 14){
                                player.teleport(new Location(w, 18, 16, 10));
                            }
                            if(spawn == 15){
                                player.teleport(new Location(w, 18, 8, -6));
                            }
                            if(spawn == 16){
                                player.teleport(new Location(w, -1, 11, -14));
                            }
                        }
                         player.sendMessage("You have been teleported");
                        }
                }
            }
        }
     
    #1 Henrik, Jun 2, 2017
    Last edited: Jun 2, 2017
  2. Well, maybe just show the optional code, we don't have to see the whole class. Also point out where the messagr is send etc.
     
  3. I did remove most of the part of the code that was not necessary. The message is being sent to the player at the end of the chooseMap() method
     
  4. You're running the Game() method for each player.
     
    • Winner Winner x 1
  5. That really explains a lot, thank you so much!
     
  6. Ummm.... Naming Conventions please...
     
  7. woah there tiger!

    you're going to cause memory leaks.
    you're storing player objects, yet removing them the wrong way. you're trying to remove the player name from the list.

    Code (Java):

    ArrayList<Player> players = new ArrayList<Player>();
    players.remove(player.getName());
     
    edit: nope, in the first place, you're storing there name. so change the list type to string if you're only trying to store string types (the player name)