Solved Same location do not return true when compared

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

  1. Offline

    Ago19

    I know could be ambiguos so I'm going to explain

    When a player breakes a block I want to check if the block broken is in a specific location.
    The code is the following:
    Code:
    @EventHandler
        public void onMainBlockBreak(BlockBreakEvent event) {
          
          
          
            Location block_location = event.getBlock().getLocation();
            Player player = event.getPlayer();
            player.sendMessage("Broken block");
          
            if(hasIsland(player) == true) {
                player.sendMessage("Debug Message");
                Island is = getPlayerIsland(player);
                if(block_location.equals(is.mainBlock)) {
                    is.addProgresso_Livello(1);      
                    is.replaceBlock();
                    player.sendMessage("Progress +1");
                }
              
            }      
          
        }
    
    In Island.java
    public Location mainBlock;

    Here is the replaceBlock method:

    Code:
    
        public boolean replaceBlock () {
            Random randomizer = new Random();
            int rand = randomizer.nextInt(getReplacableBlocks().size());
            mainBlock.getBlock().setType((Material) getReplacableBlocks().toArray()[rand]);
           
            return true;
        }
    
    

    Here the getReplacableBlock:

    Code:
    
        public HashSet<Material> getReplacableBlocks() {
           
            HashSet<Material> replaceable = new HashSet<Material>();
           
            switch(level) {
            case DESERT:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks), OceanBlocks), Utils.addArray(JungleBlocks, DesertBlocks)));
                break;
            case END:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks), OceanBlocks), JungleBlocks), DesertBlocks), NetherBlocks), IdyllBlocks), EndBlocks));
                break;
            case ERROR:
                replaceable = null;
                break;
            case IDYLL:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks), OceanBlocks), JungleBlocks), DesertBlocks), NetherBlocks), IdyllBlocks));
                break;
            case JUNGLE:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks), Utils.addArray(OceanBlocks, JungleBlocks)));
                break;
            case NETHER:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks), OceanBlocks), JungleBlocks), DesertBlocks), NetherBlocks));
                break;
            case OCEAN:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), Utils.addArray(TundraBlocks, OceanBlocks)));
                break;
            case PLAINS:
                replaceable = Utils.arrayToHashSet(PlainBlocks);
                break;
            case TUNDRA:
                replaceable = Utils.arrayToHashSet(Utils.addArray(Utils.addArray(PlainBlocks, UndergroundBlocks), TundraBlocks));
                break;
            case UNDERGROUND:
                replaceable = Utils.arrayToHashSet(Utils.addArray(PlainBlocks, UndergroundBlocks));
                break;
            default:
                break;
           
            }
           
            return replaceable;
        }
    
    
     
    Last edited: Jan 6, 2021
  2. Offline

    CraftCreeper6

    @Ago19
    You didn't actually say what the problem is.

    Although one thing I notice already is that you are constantly remaking the Random variable in the replaceBlock method. If you call this method multiple times in a 'tick' then it's likely to return exactly the same blocks each time because of how the Random class in Java works. I suggest you create a class variable to store the Random instance.
     
  3. Offline

    Ago19

    @CraftCreeper6
    The problem is that the new block is never replaced when I break it. This plugin has to replace the block you broke with another one. Every time you break that block it has to be replaced. The onMainBlockBreak method knows when a block is broken, then it checks if the block is "is.mainBlock" (this is a Location). When I break the block on the Chat appears "Broken Block" and "Debug Message" but not the "Progress +1" message.
    The problem is in the following line (I think)
    Code:
    if(block_location.equals(is.mainBlock))
    
    I think it returns false and that's why "is.replaceBlock()" is not called and thus the block is not replaced.
     
    Last edited: Jan 6, 2021
  4. Offline

    Ago19

    I solved the problem. The error was somewhere else in the code. I changed the value of the mainBlock location so it didn't return true.
    Setting this thread to Solved
     

Share This Page