Solved Really weird bug

Discussion in 'Plugin Development' started by CheesyFreezy, Apr 26, 2015.

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

    CheesyFreezy

    Hi developers,

    I've been trying to fix this bug for in total almost 12 hours. I've been trying everything but i don't know why this is happening. What's wrong? I'm making a survival games plugin and there's a countdown when the game starts. Only the first player who joined the game sees the countdown message, and all the remaining players don't see any message.

    Please take some time and read everything. Thank you VERY much!

    Code:
    Code:
        @SuppressWarnings("deprecation")
        public void JoinGame(final Player player, final String arenaName) {
            Files files = new Files();
            final YamlConfiguration file = YamlConfiguration.loadConfiguration(files.SGArenas);
           
            player.setGameMode(GameMode.ADVENTURE);
            player.setLevel(0);
            player.setExp(0);
           
            inArena.put(player.getName(), arenaName);
            Data.lobbyPlayers.remove(player.getName());
            this.timer.put(arenaName, 30);
           
            setState(player, SGState.LOBBY);
            Core.scoreboard.SetScoreboard(player, ScoreboardType.SG);
           
            setClass(player, "Barbarian");
           
            if(file.getInt(arenaName + ".data.PLAYER_COUNT") >= 1) {
                file.set(arenaName + ".data.PLAYER_COUNT", file.getInt(arenaName + ".data.PLAYER_COUNT") + 1);
                try {
                    file.save(files.SGArenas);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                file.set(arenaName + ".data.PLAYER_COUNT", 1);
                try {
                    file.save(files.SGArenas);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            if(file.getInt(arenaName + ".data.PLAYER_COUNT") == file.getInt(arenaName + ".options.MAX_PLAYERS")) {
                Location signLocation = new Location(Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")), file.getInt(getArena(player) + ".locations.sign.x"), file.getInt(getArena(player) + ".locations.sign.y"), file.getInt(getArena(player) + ".locations.sign.z"));
                Sign sign = (Sign) Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")).getBlockAt(signLocation).getState();
               
                sign.setLine(0, ChatColor.RED + "" + ChatColor.BOLD + "[Full]");
                sign.update();
            } else {
                Location signLocation = new Location(Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")), file.getInt(getArena(player) + ".locations.sign.x"), file.getInt(getArena(player) + ".locations.sign.y"), file.getInt(getArena(player) + ".locations.sign.z"));
                Sign sign = (Sign) Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")).getBlockAt(signLocation).getState();
               
                sign.setLine(0, ChatColor.GREEN + "" + ChatColor.BOLD + "[Join]");
                sign.update();
            }
           
            Location lobby = player.getLocation();
            lobby.setWorld(Bukkit.getServer().getWorld(file.getString(arenaName + ".locations.lobby.world")));
            lobby.setX(file.getInt(arenaName + ".locations.lobby.x"));
            lobby.setY(file.getInt(arenaName + ".locations.lobby.y"));
            lobby.setZ(file.getInt(arenaName + ".locations.lobby.z"));
            lobby.setPitch(file.getInt(arenaName + ".locations.lobby.pitch"));
            lobby.setYaw(file.getInt(arenaName + ".locations.lobby.yaw"));
            player.teleport(lobby);
           
            ItemStack selector = new ItemStack(Material.NETHER_STAR, 1);
            ItemStack leave = new ItemStack(Material.PACKED_ICE, 1);
            ItemMeta selectorMeta = selector.getItemMeta();
            ItemMeta leaveMeta = leave.getItemMeta();
            selectorMeta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Character Selector" + Plugin.rightClick);
            leaveMeta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Leave Game" + Plugin.rightClick);
            selector.setItemMeta(selectorMeta);
            leave.setItemMeta(leaveMeta);
           
            if(!playerCollection.containsKey(inArena.get(player.getName()))) {
                ArrayList<String> playerList = new ArrayList<String>();
                playerList.add(player.getName());
                playerCollection.put(inArena.get(player.getName()), playerList);
            } else {
                ArrayList<String> playerList = playerCollection.get(arenaName);
                playerList.add(player.getName());
                playerCollection.put(inArena.get(player.getName()), playerList);
            }
           
            if(file.getInt(getArena(player) + ".data.PLAYER_COUNT") == file.getInt(getArena(player) + ".options.MIN_PLAYERS")) {
                for(String plist : getPlayers(arenaName)) {
                    Player players = Bukkit.getServer().getPlayer(plist);
                   
                    Core.dev.SetForcestart(arenaName, false, true);
                    StartCounter(players);
                }
            }
           
            for(String plist : getPlayers(arenaName)) {
                Player players = Bukkit.getServer().getPlayer(plist);
                players.sendMessage(Plugin.prefix + ChatColor.BLUE  + "" + ChatColor.BOLD + "> " + ChatColor.RESET + "" + ChatColor.BLUE + player.getName() + ChatColor.GRAY + " joined the game! " + ChatColor.BLUE + "(" + ChatColor.GRAY + file.getInt(arenaName + ".data.PLAYER_COUNT") + "/" + file.getInt(arenaName + ".options.MAX_PLAYERS") + ChatColor.BLUE + ")");
            }
           
            Location signLocation = new Location(Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")), file.getInt(getArena(player) + ".locations.sign.x"), file.getInt(getArena(player) + ".locations.sign.y"), file.getInt(getArena(player) + ".locations.sign.z"));
            Sign sign = (Sign) Bukkit.getServer().getWorld(file.getString(getArena(player) + ".locations.sign.world")).getBlockAt(signLocation).getState();
           
            sign.setLine(2, file.getInt(arenaName + ".data.PLAYER_COUNT") + "/" + file.getInt(arenaName + ".options.MAX_PLAYERS"));
            sign.update();
           
            player.getInventory().clear();
            player.getInventory().setHelmet(new ItemStack(Material.AIR));
            player.getInventory().setChestplate(new ItemStack(Material.AIR));
            player.getInventory().setLeggings(new ItemStack(Material.AIR));
            player.getInventory().setBoots(new ItemStack(Material.AIR));
            player.getInventory().setItem(0, selector);
            player.getInventory().setItem(8, leave);
            player.updateInventory();
        }
        @SuppressWarnings("deprecation")
        public void StartCounter(final Player players) {
            timer = new HashMap<String, Integer>();
            taskID = new HashMap<String, Integer>();
           
            if(Core.dev.getSkipCountdown() == false) {
                timerPhase.put(getArena(players), 1);
            } else {
                timerPhase.put(getArena(players), 2);
            }
            gracePeriodList.add(getArena(players));
           
            players.setMaxHealth(40);
            players.setHealth(40);
           
            players.getInventory().clear();
            players.getInventory().setHelmet(new ItemStack(Material.AIR));
            players.getInventory().setChestplate(new ItemStack(Material.AIR));
            players.getInventory().setLeggings(new ItemStack(Material.AIR));
            players.getInventory().setBoots(new ItemStack(Material.AIR));
            players.updateInventory();
           
            Core.scoreboard.SetScoreboard(players, ScoreboardType.SG);
           
            if(Core.dev.getEnableAbilityTimer() == true) {
                SGListener.abilityTimer.put(players.getName(),0);
            } else {
                SGListener.abilityTimer.put(players.getName(), 31);
            }
           
            Files files = new Files();
            final YamlConfiguration file = YamlConfiguration.loadConfiguration(files.SGArenas);
           
            Location loc = null;
           
            if(Core.sgManager.getArena(players) != null) {
                if(file.isConfigurationSection(Core.sgManager.getArena(players))) {
                    if(!Core.sgManager.isEmptyPlayers(Core.sgManager.getArena(players))) {
                        for(int i=0;i<Core.sgManager.getPlayers(Core.sgManager.getArena(players)).size();i++) {
                            if(file.getInt(Core.sgManager.getArena(players) + ".data." + players.getName()) == (i + 1)) {
                                int i2 = (i + 1);
                                String iString = Integer.toString(i2);
                               
                                loc = new Location(Bukkit.getServer().getWorld(file.getString(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".world")), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".x"), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".y"), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".z"));
                                loc.setPitch(players.getLocation().getPitch());
                                loc.setYaw(players.getLocation().getYaw());
                               
                                players.teleport(loc);
                            }
                        }
                    }
                }
            }
           
            Location signLocation = new Location(Bukkit.getServer().getWorld(file.getString(getArena(players) + ".locations.sign.world")), file.getInt(getArena(players) + ".locations.sign.x"), file.getInt(getArena(players) + ".locations.sign.y"), file.getInt(getArena(players) + ".locations.sign.z"));
            Sign sign = (Sign) Bukkit.getServer().getWorld(file.getString(getArena(players) + ".locations.sign.world")).getBlockAt(signLocation).getState();
           
            sign.setLine(0, ChatColor.RED + "" + ChatColor.BOLD + "[Started]");
            sign.update();
           
            if(Core.dev.getSkipCountdown() == false) {
                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The game starts in " + "20" + " seconds!");
                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
               
                canMove.add(players.getName());
                timer.put(getArena(players), 21);
            } else {
                canMove.remove(players.getName());
               
                timer.put(getArena(players), 31);
                timerPhase.put(getArena(players), 2);
               
                StartGame(players);
            }
            taskID.put(getArena(players), Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Plugin.core, new BukkitRunnable() {
                public void run() {
                    Core.scoreboard.SetScoreboard(players, ScoreboardType.SG);
                   
                    if(getPlayers(getArena(players)).get(0).equalsIgnoreCase(players.getName())) {
                        timer.put(getArena(players), timer.get(getArena(players)) - 1);
                       
                        if(timerPhase.get(getArena(players)) == 1) {
                            Core.scoreboard.SetScoreboard(players, ScoreboardType.SG);
                            if(timer.get(getArena(players)).intValue() == 10) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The game starts in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                            if(timer.get(getArena(players)).intValue() < 6 && timer.get(getArena(players)).intValue() > 0) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The game starts in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                           
                            if(timer.get(getArena(players)).intValue() == 0) {
                                players.playSound(players.getLocation(), Sound.LEVEL_UP, 10, 2);
                               
                                timerPhase.put(getArena(players), 2);
                                timer.put(getArena(players), 30);
                               
                                canMove.remove(getArena(players));
                               
                                StartGame(players);
                            }
                        } else if(timerPhase.get(getArena(players)) == 2) {
                            Core.scoreboard.SetScoreboard(players, ScoreboardType.SG);
                            if(timer.get(getArena(players)).intValue() == 10) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The grace period ends in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                            if(timer.get(getArena(players)).intValue() < 6 && timer.get(getArena(players)).intValue() > 0) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The grace period ends in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                           
                            if(timer.get(getArena(players)).intValue() == 0) {
                                players.playSound(players.getLocation(), Sound.LEVEL_UP, 10, 2);
                               
                                timerPhase.put(getArena(players), 3);
                                timer.put(getArena(players), 360);
                               
                                gracePeriodList.remove(getArena(players));
                               
                                SGListener.abilityTimer.put(players.getName(), 0);
                                PlayerUtils.sendActionBar(players, ChatColor.GREEN + "" + ChatColor.BOLD + "You can use your ability!");
                               
                                players.sendMessage(ChatColor.YELLOW + "The grace period ended! Now kill the other tributes!");
                                PlayerUtils.sendTitle(players, ChatColor.GOLD + "" + ChatColor.BOLD + "WARNING!", ChatColor.YELLOW + "The grace period ended! Now kill the other tributes!");
                            }
                        } else if(timerPhase.get(getArena(players)) == 3) {
                            Core.scoreboard.SetScoreboard(players, ScoreboardType.SG);
                            if(timer.get(getArena(players)).intValue() == 30) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The deathmatch starts in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                            if(timer.get(getArena(players)).intValue() < 11 && timer.get(getArena(players)).intValue() > 0) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The deathmatch starts in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                           
                            if(timer.get(getArena(players)).intValue() == 0) {
                                if(getPlayers(getArena(players)).get(0).equalsIgnoreCase(players.getName())) {
                                    timerPhase.put(getArena(players), 4);
                                    timer.put(getArena(players), 120);
                                }
                               
                                players.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Deathmatch started! Kill the remaining tributes!");
                                PlayerUtils.sendTitle(players, ChatColor.DARK_RED + "" + ChatColor.BOLD + "WARNING!", ChatColor.RED + "Deathmatch started! Kill the remaining tributes!");
                               
                                Location loc = null;
                                for(int i=0;i<Core.sgManager.getPlayers(Core.sgManager.getArena(players)).size();i++) {
                                    if(file.getInt(Core.sgManager.getArena(players) + ".data." + players.getName()) == (i + 1)) {
                                        int i2 = (i + 1);
                                        String iString = Integer.toString(i2);
                                               
                                        loc = new Location(Bukkit.getServer().getWorld(file.getString(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".world")), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".x"), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".y"), file.getDouble(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".z"));
                                        loc.setPitch(file.getInt(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".pitch"));
                                        loc.setYaw(file.getInt(Core.sgManager.getArena(players) + ".locations.spawn." + iString + ".yaw"));
                                       
                                        players.teleport(loc);
                                    }
                                }
                            }
                        } else if(timerPhase.get(getArena(players)) == 4) {
                            if(timer.get(getArena(players)).intValue() == 30) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The game ends in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                            if(timer.get(getArena(players)).intValue() < 11 && timer.get(getArena(players)).intValue() > 0) {
                                players.sendMessage(Plugin.prefix + ChatColor.BLUE + "The game ends in " + timer.get(getArena(players)).intValue() + " seconds!");
                                players.playSound(players.getLocation(), Sound.NOTE_PIANO, 10, 2);
                            }
                           
                            if(timer.get(getArena(players)).intValue() == 0) {
                                players.playSound(players.getLocation(), Sound.LEVEL_UP, 10, 2);
                               
                                players.getInventory().clear();
                                players.updateInventory();
                               
                                players.sendMessage(Plugin.prefix + ChatColor.YELLOW + "" + ChatColor.STRIKETHROUGH + "××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××");
                                players.sendMessage(Plugin.prefix + ChatColor.DARK_RED + "" + ChatColor.BOLD + "                Game ended!");
                                players.sendMessage(Plugin.prefix + ChatColor.RED + "" + ChatColor.BOLD + "            There is no winner!");
                                players.sendMessage(Plugin.prefix + ChatColor.YELLOW + "" + ChatColor.STRIKETHROUGH + "××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××");
                           
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Plugin.core, new Runnable() {
                                    public void run() {
                                        LeaveGame(players, getArena(players));
                                    }
                                }, 5 * 20);
                            }
                        }
                    }
                    if(Core.dev.getEnableAbilityTimer() == false) {
                        SGListener.abilityTimer.put(players.getName(), 0);
                    }
                   
                    if(timerPhase.get(getArena(players)) == 3) {
                        if(SGListener.abilityTimer.get(players.getName()) != 0) {
                            if(Core.dev.getEnableAbilityTimer() == false) {
                                SGListener.abilityTimer.put(players.getName(), 0);
                            } else {
                                SGListener.abilityTimer.put(players.getName(), SGListener.abilityTimer.get(players.getName()) - 1);
                            }
                        }
                       
                        if(SGListener.abilityTimer.get(players.getName()) == 0) {
                            PlayerUtils.sendActionBar(players, ChatColor.GREEN + "" + ChatColor.BOLD + "You can use your ability!");
                        }
                    }
                }
            }, 0, 20));
        }
     
  2. Offline

    mine-care

    1. where is this pece of code? you gave us > 300 lines that i am not able to read from my phone.
    2. follow java naming conventions!
    Dont compare booleans with == instead: if(boolean) for true and if(!boolean) for false.
    Dont abuse static :/
     
  3. Offline

    CheesyFreezy

    So that's the problem?
     
  4. Offline

    mine-care

    @CheesyFreezy i couldnt trace the issue, im on my phone so scroling and reading line by line is hard.
    the other stuff mentioned in my post are fixes that will make your code more efficient and will bring it in the path of naming conventions :- )
    Btw are there any errors?
     
  5. @CheesyFreezy When you check if something is false, it's easier just to do if(!boolean){ rather than if(boolean == false){

    EDIT: Your issue is your trying to send a message to a player array, you need to loop through the players and send them the message.
     
    CodePlaysMinecraft likes this.
  6. Offline

    CheesyFreezy

    I am doing that....right? What is the solution?
     
  7. Offline

    Maxx_Qc

    To send a message to everyone on the server you need to use this:
    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayer()) {
        //To do stuff
        //Use p.sendMessage to send a message to everyone
    }
    
     
  8. Offline

    Protophite

  9. Offline

    Maxx_Qc

    @Protophite I sincerely don't know which one is better.
     
  10. No, booth of you are wrong.
    @Protophite because it displays the message to all online players
    @Maxx_Qc same reason, but its better because its possible to send the messages to a specified player array.
     
  11. Offline

    Maxx_Qc

    @FisheyLP He wants everyone to see the message, but now only the first player see it.
     
  12. Offline

    CheesyFreezy

    @Maxx_Qc It should only send a message to the players who are in-game (getPlayers(arenaName) method).
    @Protophite Bukkit.broadcastMessage(String) is sending a message to all the online players
    @FisheyLP So what should I do?
     
  13. Offline

    Maxx_Qc

    So:
    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayer()) {
        if(p.getArena() == someArenaName) {
           //Use p.sendMessage to send a message to everyone in the arena
         }
    }
    
     
  14. Try to debug and use
    Code:java
    1. Bukkit.broadcastMessage(getPlayers(arenaName).length);

    or
    Code:java
    1. Bukkit.broadcastMessage(getPlayers(arenaName).length());

    or
    Code:java
    1. Bukkit.broadcastMessage(getPlayers(arenaName).size());

    idk what it is. depends on what getPlayers return.
     
  15. Offline

    CheesyFreezy

  16. Then it's obviously that it doesn't send it to other players, when only you are in the list ^^
     
  17. Offline

    Msrules123

    If it returned 1 and you were playing by yourself, your code should work.
     
  18. Offline

    CheesyFreezy

    @FisheyLP i debugged the arraylist and indeed, it only contained the name of the first player who joined
     
  19. Offline

    Protophite

    @CheesyFreezy

    Oh I thought that's what you wanted. All you have to do is iterate through the array and send a message to them.
     
  20. Offline

    CheesyFreezy

    I did that, but it still sends a message to only the first player in the arraylist. What am I doing wrong? D:
     
  21. Offline

    mythbusterma

    @CheesyFreezy

    Show us what you believe is "iterating" through the Players.
     
  22. Offline

    Protophite

    @CheesyFreezy
    If you are truly iterating through it, your array probably only contains that 1 player.
     
  23. Offline

    CheesyFreezy

    @mythbusterma:
    Code:
    for(String plist : getPlayers(getArena(players))) {
       Player joinedPlayers = Bukkit.getServer().getPlayer(plist);
    }
     
  24. Offline

    mythbusterma

    TheGamesHawk2001 likes this.
  25. Offline

    CheesyFreezy

    How should I do it then?
     
  26. Offline

    rbrick

    Ok...Wow. First thing I see off the bat is when ever a player joins a game, you write to a file...
     
  27. Offline

    CheesyFreezy

    That's to save the ID of a player to use it later in-game, like teleport location per player
     
  28. Offline

    dlange

    @CheesyFreezy Not a very good way of doing that... And also sort of useless. If you are going to let the game reset after the server restarts, then you should store them in an ArrayList or HashMap (Depending on how you store/get them)
     
  29. Offline

    CheesyFreezy

    Yes okay, i'll do that. But that's not why i created this thread....
     
  30. Offline

    CheesyFreezy

    BUMP. Still not fixed.... anybody got a solution for this?
     
Thread Status:
Not open for further replies.

Share This Page