Solved For loop bug in Sign Update

Discussion in 'Plugin Development' started by Fouzer, Feb 15, 2016.

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

    Fouzer

    Code:
    package br.com.vidacraft.utilities;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    
    import br.com.vidacraft.core.Arena;
    import br.com.vidacraft.core.ArenaManager;
    import br.com.vidacraft.core.ArenaState;
    import br.com.vidacraft.utilities.coboid.Cuboid;
    import br.com.vidacraft.utilities.tasks.Task;
    
    public class SignUpdater {
    
        public static void signUpdate() {
            World world = Bukkit.getWorld("swlobby");
            for (Location loc : getSigns()) {
                signUpdater(loc);
                // signUpdater(loc);
    
            }
        }
    
        public static void signUpdater(Location bLocation) {
            World w = bLocation.getWorld();
            Block b = w.getBlockAt(bLocation);
            new Task(Task.REPEATING, 3 * 20) {
                public void update() {
                    if (b.getTypeId() == Material.SIGN_POST.getId() || b.getTypeId() == Material.WALL_SIGN.getId()) {
                        Sign sign = (Sign) b.getState();
                        for (Arena a : ArenaManager.getManager().getAllArenas()) {
                            for (int i = 0; i < ArenaManager.getManager().getArenasQuantity(); i++) {
                                sign.setLine(0, "------------");
                                sign.update();
                            }
                            for (ArenaState line1 : ArenaManager.getManager().getAllArenaStates()) {
                                if (line1 == ArenaState.WAITING) {
                                    sign.setLine(1, colorize("&2[Aguardando]"));
                                }
                                if (line1 == ArenaState.STARTING) {
                                    sign.setLine(1, "[Iniciando]");
                                }
                                if (line1 == ArenaState.RUNNING) {
                                    sign.setLine(1, "[No Jogo]");
                                }
                                if (line1 == ArenaState.RESTARTING) {
                                    sign.setLine(1, "[Reiniciando]");
                                }
                            }
    
                            sign.setLine(2, a.getName());
                            System.out.println(a.getName());
                            sign.update();
    
                            for (String line3 : ArenaManager.getManager().getAllArenaPlayersQuantity()) {
                                sign.setLine(3, line3);
                            }
                            sign.update();
    
                        }
                    }
                }
            };
    
        }
    
        public static void startUpdateSigns(Location block) {
            World w = block.getWorld();
            Block b = w.getBlockAt(block);
            if (b.getType() == Material.SIGN || b.getType() == Material.WALL_SIGN) {
                Sign s = (Sign) b.getState();
                List<ArenaState> states = ArenaManager.getManager().getAllArenaStates();
    
                if (states.get(0) == ArenaState.WAITING) {
                    s.setLine(2, "WAITING");
    
                }
    
                if (states.get(0) == ArenaState.WAITING) {
                    if (!s.getLine(2).equalsIgnoreCase("WAITING")) {
                        s.setLine(2, "WAITING");
                    }
    
                    int pl = ArenaManager.getManager().getAllArenaPlayersQuantity().size();
    
                    if (pl == 1) {
                        s.setLine(3, ChatColor.GREEN + String.valueOf(pl) + ChatColor.WHITE + "/4");
                    }
    
                    if (pl == 2) {
                        s.setLine(3, ChatColor.YELLOW + String.valueOf(pl) + ChatColor.WHITE + "/4");
                    }
    
                    if (pl == 3) {
                        s.setLine(3, ChatColor.RED + String.valueOf(pl) + ChatColor.WHITE + "/4");
                    }
    
                    if (pl == 4) {
                        s.setLine(3, ChatColor.DARK_RED + String.valueOf(pl) + ChatColor.WHITE + "/4");
                    }
                }
    
            }
    
        }
    
        public static List<Location> getSigns() {
            List<Location> signsLocs = new ArrayList<>();
            Location loc1 = new Location(Bukkit.getWorld("swlobby"), -665, 12, -2140);
            Location loc2 = new Location(Bukkit.getWorld("swlobby"), -664, 8, -2148);
            Cuboid cuboid = new Cuboid(loc1, loc2);
            for (Block block : cuboid.getBlocks()) {
                signsLocs.add(block.getLocation());
    
            }
            return signsLocs;
        }
    
        private static String colorize(String input) {
            return ChatColor.translateAlternateColorCodes('&', input);
        }
    
    }
    Problem: All the Updated Signs stay with the atributes of the last arena. Exemple: If I have arena1 to arena10, all the signs will stay with the name, state, players of the arena10.
    I think that I understand the problem, although I don't know how to solve it: I'm looping though all arenas for each sign, and then loop again through all the states, name, players. This will result in all arenas updating all the signs for themselves, but the only arena that will stay is the last one (since it is the last one to update).
    Print of the Signs in game: http://prnt.sc/a3sx9h
    Gist: https://gist.github.com/IanPedroV/075c970e363fe598520c
    Print of the arena names (Syso on the line 60 fo the gist): http://prnt.sc/a3taaj

    PS: The signUpdate() method is called on Enable, and the signUpdater is a repeating task that runs each 3 seconds until the server is shutdown. Sorry for an english mistakes, english is not my first language.
     
  2. Offline

    Zombie_Striker

    Unless you own that domian, please use a domain you own. Only assuming you don't since there is nothing really at the domain.

    Why not just loop through each individual arena, apply those stats for one sign, and move onto the next?
     
  3. Offline

    Fouzer

    Thanks for the reply. Vidacraft.com.br is actually my domain, I'm not using it wrong :p
    Abou the sugestion that you made, I'm going to apply it when I wake up, it's already 3:50 here :oops:

    Now that I'm awake...
    Not sure that I understand what you meant. the loop: for (Arena a : ArenaManager.getManager().getAllArenas()) loops a list of all arenas, so it picks up each indivual one and makes the change in the sign, I'm missing something here?

    I just understood what you said. Removing the for to get AllArenas on the signUpdater method, letting the same getting the line that corresponds to the arena name, and setting the order lines according to the atributes of this arena works perfectly! :D

    My final code:

    Code:
    package br.com.vidacraft.utilities;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    
    import br.com.vidacraft.core.Arena;
    import br.com.vidacraft.core.ArenaManager;
    import br.com.vidacraft.core.ArenaState;
    import br.com.vidacraft.utilities.coboid.Cuboid;
    import br.com.vidacraft.utilities.tasks.Task;
    
    public class SignUpdater {
    
        static List<Location> signsLocs = new ArrayList<>();
    
        public static void signUpdate() {
            getSigns();
    
            for (Location loc : signsLocs) {
                signUpdater(loc);
    
            }
        }
    
        public static void signUpdater(Location bLocation) {
            World w = bLocation.getWorld();
            Block b = w.getBlockAt(bLocation);
            new Task(Task.REPEATING, 3 * 20) {
                public void update() {
                    if (b.getTypeId() == Material.SIGN_POST.getId() || b.getTypeId() == Material.WALL_SIGN.getId()) {
                        Sign sign = (Sign) b.getState();
                        Arena a = ArenaManager.getManager().getArena(sign.getLine(2));
    
                        for (int i = 0; i < ArenaManager.getManager().getArenasQuantity(); i++) {
                            sign.setLine(0, "------------");
                        }
                        if (a.getGameState() == ArenaState.WAITING) {
                            sign.setLine(1, colorize("&2[Aguardando]"));
                        }
                        if (a.getGameState() == ArenaState.STARTING) {
                            sign.setLine(1, "[Iniciando]");
                        }
                        if (sign.getLine(1) == "RUNNING") {
                            sign.setLine(1, "[No Jogo]");
                        }
                        if (sign.getLine(1) == "RESTARTING") {
                            sign.setLine(1, "[Reiniciando]");
                        }
                        sign.setLine(2, a.getName());
                        sign.setLine(3, String.valueOf(a.getPlayers().size() + "/" + a.getMaximumPlayers()));
                        sign.update();
                    }
                }
            };
    
        }
    
        public static void getSigns() {
            Location loc1 = new Location(Bukkit.getWorld("swlobby"), -665, 12, -2140);
            Location loc2 = new Location(Bukkit.getWorld("swlobby"), -664, 8, -2148);
            Cuboid cuboid = new Cuboid(loc1, loc2);
            for (Block block : cuboid.getBlocks()) {
                Location loc = block.getLocation();
                World w = loc.getWorld();
                Block b = w.getBlockAt(loc);
                if (b.getTypeId() == Material.SIGN_POST.getId() || b.getTypeId() == Material.WALL_SIGN.getId()) {
                    signsLocs.add(block.getLocation());
                }
    
            }
        }
    
        private static String colorize(String input) {
            return ChatColor.translateAlternateColorCodes('&', input);
        }
    
    }
    
    Updated gist: https://gist.github.com/IanPedroV/075c970e363fe598520c

    Thanks @Zombie_Striker!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page