Solved Error when testing integers value

Discussion in 'Plugin Development' started by Ago19, Jan 10, 2021.

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

    Ago19

    I'm coding a SkyBlock Plugin. I want the "island_progress" to increase by 1 every time the player breaks a block. Then when the island_progress reaches a specific value I want to change the "island_level" value.
    The "island_level" is an enumeration. The "island_progress" is an integer.

    Here's some code

    This checks when the island need to be upgraded. It's called every time a player breakes a block:
    Code:
    if(is.needToUpgrade() == true) {
                            is.upGrade();
                            player.sendMessage(prefix + "Your island levelled to:  " + is.getLevel());
                        }
    
    This is the needToUpgrade method:
    "progresso_livello" is the "island_progress". (It means the same, but the code is written with some Italian terms)
    Code:
        public boolean needToUpgrade() {
            switch(this.level) {
            case DESERT:
                if(this.progresso_livello >= UpgradeProgress[5]) {
                    return true;
                }
            case IDYLL:
                if(this.progresso_livello >= UpgradeProgress[7]) {
                    return true;
                }
            case JUNGLE:
                if(this.progresso_livello >= UpgradeProgress[4]) {
                    return true;
                }
            case NETHER:
                if(this.progresso_livello >= UpgradeProgress[6]) {
                    return true;
                }
            case OCEAN:
                if(this.progresso_livello >= UpgradeProgress[3]) {
                    return true;
                }
            case PLAINS:
                if(this.progresso_livello >= UpgradeProgress[0]) {
                    return true;
                }
            case TUNDRA:
                if(this.progresso_livello >= UpgradeProgress[2]) {
                    return true;
                }
            case UNDERGROUND:
                if(this.progresso_livello >= UpgradeProgress[1]) {
                    return true;
                }
            default:
                return false;   
            }
        }
    
    UpgradeProgress is an array of integers, and I use it to check if the Island needs un upgrade.
    If the player mined 200 blocks the island upgrades from PLAINS to UNDERGROUND, if he mined 400 block the island upgrades from UNDERGROUND to TUNDRA, and so on.
    Code:
    public static final int[] UpgradeProgress = {200, 400, 800, 1600, 3200, 6400, 12800, 25600};
    
    The code works finely until it reaches 400 block, then every time the player mines a block the island levels up, so when you mine the 401th block you reach TUNDRA, when you mine the 402th block you reach level OCEAN.

    What did I try to do to fix the problem?
    - I displayed all the values from the array to check that all of them were right.
    - I checked if the "island_progress" increased by 1 each time I break a block.
    - I found out that the problem appears ONLY after reaching 400 blocks mined

    If you need more explanations please ask
    Thanks
     
  2. Offline

    xpaintall

    Can you show us some more code? I understand your problem but I can't find the issue with this much code.
     
  3. Offline

    Ago19

    Yes, this is the EventHandler that replaces a block when it gets mines, (this plugin is actually a oneblock skyblock).
    Code:
        @EventHandler
        public void onMainBlockBreak(BlockBreakEvent event) {
                Player player = event.getPlayer();
                Location block_location = event.getBlock().getLocation();
                Block block = block_location.getBlock();
                if(hasIsland(player) == true) {
                    Island is = getPlayerIsland(player);
                    if(block_location.equals(is.mainBlock)) {
                        event.setCancelled(true);
                     
                      // Here we add 1 to the progress_level
                        is.addProgresso_Livello(1);
                        is.replaceBlock();
                        for(ItemStack i : block.getDrops()) {
                            is.mainBlock.getWorld().dropItem(is.spawnPlace, i);
                        }
                        ItemStack iS = player.getInventory().getItemInMainHand();
                    
                         // This part adds damage to the tool if the player mined with a tool
                        for(Material m : Tools) {
                            if(iS.getType().equals(m)) {
                                ItemMeta iM = iS.getItemMeta();
                                if(iM instanceof Damageable) {
                                    ((Damageable) iM).setDamage(((Damageable) iM).getDamage() + 1);
                                }
                                iS.setItemMeta(iM);
                                player.getInventory().setItemInMainHand(iS);
                            }
                        }
                    
                       // This is the part of the code previously shown
                        if(is.needToUpgrade() == true) {
                            is.upGrade();
                            player.sendMessage(prefix + "Your island upgraded to level:  " + is.getLevel());
                        }
                    }
                        
                } 
    
        }
    
    This is the tools list
    Code:
    public static Material[] Tools = {
                // Wooden Tools
                Material.WOODEN_SWORD, Material.WOODEN_SHOVEL, Material.WOODEN_PICKAXE, Material.WOODEN_AXE, Material.WOODEN_HOE,
                // Stone Tools
                Material.STONE_SWORD, Material.STONE_SHOVEL, Material.STONE_PICKAXE, Material.STONE_AXE, Material.STONE_HOE,
                // Gold Tools
                Material.GOLDEN_SWORD, Material.GOLDEN_SHOVEL, Material.GOLDEN_PICKAXE, Material.GOLDEN_AXE, Material.GOLDEN_HOE,
                // Iron Tools
                Material.IRON_SWORD, Material.IRON_SHOVEL, Material.IRON_PICKAXE, Material.IRON_AXE, Material.IRON_HOE,
                // Diamond Tools
                Material.DIAMOND_SWORD, Material.DIAMOND_SHOVEL, Material.DIAMOND_PICKAXE, Material.DIAMOND_AXE, Material.DIAMOND_HOE,
                // Netherite Tools
                Material.NETHERITE_SWORD, Material.NETHERITE_SHOVEL, Material.NETHERITE_PICKAXE, Material.NETHERITE_AXE, Material.NETHERITE_HOE
        };
    
    Now I'll post the upGrade method that levels up the Island
    Code:
        public void upGrade() {
            switch(level) {
            case DESERT:
                level = Levels.NETHER;
                break;
            case IDYLL:
                level = Levels.END;
                break;
            case JUNGLE:
                level = Levels.DESERT;
                break;
            case NETHER:
                level = Levels.IDYLL;
                break;
            case OCEAN:
                level = Levels.JUNGLE;
                break;
            case PLAINS:
                level = Levels.UNDERGROUND;
                break;
            case TUNDRA:
                level = Levels.OCEAN;
                break;
            case UNDERGROUND:
                level = Levels.TUNDRA;
                break;
            default:
                break;
        
            }
        }
    
    You can find the needToUpgrade method in the first post of this thread.

    As I said in the beginning the level is an enumeration
    Code:
        public enum Levels {PLAINS, UNDERGROUND, TUNDRA, OCEAN, JUNGLE, DESERT, NETHER, IDYLL, END, ERROR};
    
    The island variables (sorry I don't remember the name that variables take inside classes)
    Code:
    public String owner;
        public Location mainBlock;
        public Location spawnPlace;
        private int progresso_livello = 0;
        public String coworker = "n0cwk";
        public static final int max_coworkers = 2;
        private static int d = 10001;
        private Levels level;
        private    Random randomizer = new Random();
        private int rand = 0;
    
    In my opinion the error in in the needToUpgrade that returns true starting at 400 blocks, but I don't understand why.

    I'm showing you some random portions of code because I can't understand what is causing this problem. If you need more code please tell. I've showed you all the parts where the code edits the level of the island. Thanks


    PS: In the needToUpgrade I swapped all >= with == and it seem to work fine
     
    Last edited: Jan 10, 2021
  4. Offline

    xpaintall

    Do you get any errors in the console when you mine the 401th block? And can you post it?
     
Thread Status:
Not open for further replies.

Share This Page