Solved For loops not continuing?

Discussion in 'Plugin Development' started by AdamDev, May 13, 2018.

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

    AdamDev

    Hi there, so there has been a problem in this code and I've redone and messed around with it many times but I can't seem to have this to work. Heres the for loop code:

    Code:
    public void resetMine(String name) {
            if (cm == null) {
                cm = new ConfigManager();
            }
            if (mm == null) {
                mm = new MessageManager();
            }
            Location corner1 = getCorner1(name);
            Location corner2 = getCorner2(name);
            Random rand = new Random();
    // These for loops right between these comments
            for (int x = corner1.getBlockX();x <= corner2.getBlockX();x++) {
                for (int y = corner1.getBlockY();y <= corner2.getBlockY();y++) {
                    for (int z = corner1.getBlockZ();z <= corner2.getBlockZ();z++) {
    // --
                        int r = rand.nextInt(100)+1;
                        HashMap<String,Integer> info = getBlockChance(name);
                        Location l = new Location(corner1.getWorld(),x,y,z);
                        for (Entry<String, Integer> set : info.entrySet()) {
                            String[] block = set.getKey().split(":");
                            int count = set.getValue();
                            String mat = block[0];
                            byte data = Byte.parseByte(block[1]);
                            int chance = Integer.parseInt(block[2]);
                            if (count == 0) {
                                info.remove(set.getKey());
                                continue;
                            }
                            if (r <= chance) {
                                setBlockType(l,mat,data);
                                count -= count;
                                set.setValue(count);
                                break;
                            }
                        }
                    }
                }
            }
            setResetTimer(name,getResetDelay(name));
            mm.sendResetMineMessage(name);
        }
    
    It's not the for loops don't activate, it starts and goes through the first location but ends at the one location. For example, the first corner is 100, 100, 100 and the second is 105, 105, 105. It executes only one of the blocks such as 100,100,100 and it stops there and doesn't remove any other blocks. Which to me doesn't make any sense. Unless I'm doing something wrong, please point it out.

    If you're wondering if there are any console errors, I check my console every time before I edit any of my code. There are absolutely no errors.

    Ask the questions that you may have, ill answer to the best of my ability.

    Thanks for the help!
    - AdamDev
     
  2. Online

    timtower Administrator Administrator Moderator

    @AdamDev Print corner1 and corner2, then print all the loop info.
     
  3. Offline

    AdamDev

    @timtower Well, corner1 and corner2 are coming from the config. I'll see what I could do when I get home.

    EDIT: So I have found my problem. It was a mistake in my code and I didn't but "corner2". I had put "corner1" in both my getCorner1(); and getCorner2(); methods. Let me fix this and see if the loops are fixed.

    EDIT #2: Yup, that was that cause. This thread was probably at most useless since that had happened but thanks for the help!

    I'll give you a update on what had happened:

    Code:
        public Location getCorner1(String name) {
            int x = plugin.getConfig().getInt("Mines."+name+".location.corner1.x");
            int y = plugin.getConfig().getInt("Mines."+name+".location.corner1.y");
            int z = plugin.getConfig().getInt("Mines."+name+".location.corner1.z");
            World w = Bukkit.getWorld(plugin.getConfig().getString("Mines."+name+".location.corner1.world"));
            Location loc = new Location(w,x,y,z);
            return loc;
        }
        public Location getCorner2(String name) {
            int x = plugin.getConfig().getInt("Mines."+name+".location.corner2.x");
    // ^^ used to be corner1 instead of corner2
            int y = plugin.getConfig().getInt("Mines."+name+".location.corner2.y");
    // ^^ used to be corner1 instead of corner2
            int z = plugin.getConfig().getInt("Mines."+name+".location.corner2.z");
    // ^^ used to be corner1 instead of corner2
            World w = Bukkit.getWorld(plugin.getConfig().getString("Mines."+name+".location.corner2.world"));
    //  ^^ used to be corner1 instead of corner2
            Location loc = new Location(w,x,y,z);
            return loc;
        }
    
    These two methods are now different. By not changing the corner numbers, it had caused a copy with both locations. Now changing those to corner2 had solved my problem.
     
    Last edited: May 14, 2018
Thread Status:
Not open for further replies.

Share This Page