Solved for loops not working

Discussion in 'Plugin Development' started by karolis11234, Jul 22, 2014.

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

    karolis11234

    the code:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractEvent(PlayerInteractEvent event) {
    3. Player player = (Player) event.getPlayer();
    4. if (event.getClickedBlock() == null) {
    5. return;
    6. }
    7. else if (event.getClickedBlock().getState() instanceof Sign) {
    8. Sign sign = (Sign) event.getClickedBlock().getState();
    9. if (sign.getLine(0).equalsIgnoreCase("[Zaisti]")) {
    10. if (!boola) {
    11. if (listone.size() < 2 && listone.size() > 0) {
    12. player.sendMessage(ChatColor.GOLD + "Sekmingai prisijungete.");
    13. Bukkit.broadcastMessage("Dar reikia:"+ (8 - listone.size()) + "zmoniu kad prasidetu zaidimas!.");
    14. listone.add(player.getName());
    15. return;
    16. }
    17. else if (listone.size() == 0) {
    18. listone.add(player.getName());
    19. Bukkit.broadcastMessage(ChatColor.GOLD + "Susirinko pakankamas zmoniu skaicius zaidimas prasides po 30 sekundziu !");
    20. boola = true;
    21. sbManager = Bukkit.getScoreboardManager();
    22. sBoard = sbManager.getNewScoreboard();
    23. Objective obj = sBoard.registerNewObjective("test", "kill");
    24. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    25. obj.setDisplayName("Super Spleef");
    26. Score score = obj.getScore(ChatColor.GREEN + "Alive:");
    27. score.setScore(0);
    28. Score score1 = obj.getScore(ChatColor.GREEN + "Dead:");
    29. score1.setScore(0);
    30. for(Player player1 : Bukkit.getOnlinePlayers()) {
    31. if(listone.contains(player1)) {
    32. getServer().createWorld(new WorldCreator("world1"));
    33. World world10 = Bukkit.getWorld("world1");
    34. player1.setScoreboard(sBoard);
    35. double x2 = 343;
    36. double y2 = 64;
    37. double z2 = 45;
    38. Location loc10 = new Location(world10 , x2, y2, z2);
    39. player1.teleport(loc10);
    40. return;
    41. }
    42. }
    43. }
    44. }
    45. else if(boola){
    46. player.sendMessage(ChatColor.GOLD + "Zaidimas jau prasidejo!");
    47. }
    48. }
    49. }
    50. }

    my problem is that the players dont get the scoreboard showed and they dont get teleported to the location . can anyone help ?
     
  2. Offline

    Saladoc

    Code:java
    1. else if (listone.size() == 0) {
    2. // code that doesnt change listone
    3. if(listone.contains(player1))
    4.  

    I doubt many players are contained in an empty list.
    You probably want to check listone.size() > 0 or !listone.isEmpty()
     
  3. Offline

    AoH_Ruthless

    karolis11234
    Why do you even have a for loop?
    This is your code:
    - Checks if less than 2 and greater than 0 ?? Why not just check if it is one
    -- Code
    -- Returns


    - Checks if list is 0
    -- Adds player to list (now list size is 1)
    -- Code
    -- For loop; loops through all players
    --- Checks if players are in list
    --- Code

    There is only one player in the list ... you don't need a loop for this. And why don't you just loop through your list object? This is pretty basic Java.

    Saladoc
    In the check for if listone.size == 0, he adds the player to the list, making the list size one, rendering a for loop utterly useless because there is only one player ...
     
  4. Offline

    Saladoc

  5. Offline

    karolis11234

    i made this:
    Code:
    if (listone.size() < 2 && listone.size() > 0) {
    so i could check it if it works by myself and i wouldnt need more people.
     
  6. Offline

    AoH_Ruthless

    karolis11234
    That means the list size has to be one ... so ... you can just check if the list size is one instead. I don't think you know Java well enough to be working with Bukkit.
     
  7. Offline

    karolis11234

    im not so good with loops but in that part i just failed .

    i updated my code:
    Code:
        @EventHandler
            public void onPlayerInteractEvent(PlayerInteractEvent event) {
                Player player = (Player) event.getPlayer();
                if (event.getClickedBlock() == null) {
                    return;
                }
                else if (event.getClickedBlock().getState() instanceof Sign) {
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    if (sign.getLine(0).equalsIgnoreCase("[Zaisti]")) {
                        if (!boola) {
                            if (listone.size() < 2) {
                                player.sendMessage(ChatColor.GOLD + "Sekmingai prisijungete.");
                                Bukkit.broadcastMessage("Dar reikia:"+ (8 - listone.size()) + "zmoniu kad prasidetu zaidimas!.");
                                listone.add(player.getName());
                                return;
                                }
                            else if (listone.size() == 2) {
                                listone.add(player.getName());
                                Bukkit.broadcastMessage(ChatColor.GOLD + "Susirinko pakankamas zmoniu skaicius zaidimas prasides po 30 sekundziu !");
                                boola = true;
                                sbManager = Bukkit.getScoreboardManager();
                                sBoard = sbManager.getNewScoreboard();
                                Objective obj = sBoard.registerNewObjective("test", "kill");
                                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                                obj.setDisplayName("Super Spleef");
                                Score score = obj.getScore(ChatColor.GREEN + "Alive:");
                                score.setScore(0);
                                Score score1 = obj.getScore(ChatColor.GREEN + "Dead:");
                                score1.setScore(0);
                                for(Player player1 : Bukkit.getOnlinePlayers()) {
                                if(listone.contains(player1)) {
                                    getServer().createWorld(new WorldCreator("world1"));
                                    World world10 = Bukkit.getWorld("world1");
                                    player1.setScoreboard(sBoard);
                                    double x2 = 343;
                                    double y2 = 64;
                                    double z2 = 45;
                                    Location loc10 = new Location(world10 , x2, y2, z2);
                                    player1.teleport(loc10);
                                    return;
                                }
                                }
                                }
                        }
                        else if(boola){
                            player.sendMessage(ChatColor.GOLD + "Zaidimas jau prasidejo!");             
                        }
                        }
                    }
    }
    so can anyone explain what i need to do with my problem because i know now its not the loops fault but the checking of the arraylist. can anyone help ?

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

    karolis11234

Thread Status:
Not open for further replies.

Share This Page