Custom Enchant

Discussion in 'Plugin Development' started by Abaddon123, Jul 16, 2022.

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

    Abaddon123

    So i have this code that breaks a line of blocks in a mine


    Code:
    public void onBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            @SuppressWarnings("deprecation")
            Material pick = player.getInventory().getItemInHand().getType();
           
            if(pick == null || pick.equals(Material.AIR)) {
                return;
            }
           
            Location blockLoc = event.getBlock().getLocation();
           
            if(pick.equals(Material.DIAMOND_PICKAXE)) {
                Block nextBlock = blockLoc.add(1,0,0).getBlock();
         
                while (!nextBlock.getType().equals(Material.BEDROCK)) {
                    nextBlock.breakNaturally();
                    nextBlock = nextBlock.getLocation().add(1, 0, 0).getBlock();
                }
            }
        }
    Can someone explaim to me how can i make it break a layer of the mine?
     
  2. Offline

    gochi9

    So before removing the part we need to fix your code a little bit.

    If you see code that's deprecated it means, most of the time, that the code might work but you should not use it because there is something better to use. In your case you should use .getItemInMainHand() which is the right hand because players cannot use tools with their left hands so the tool used for breaking stuff will always be in your right (main) hand.

    Next I see that you check if the type is null which is very good but this code
    Code:
    Material pick = player.getInventory().getItemInHand().getType();
    will always throw a NullPointerException if you try to break with an empty hand. Why? Because the 'pick' (in your case .getItemInMainHand() and in my suggestion getItemInMainHand()) can be null so if you break with your fist then you are using .getType() on something null which will throw a NullPointerException so you must check if the item is null first and after get the material.

    Materials should be checked using == instead of equals. Also you could reduce the code instead of using
    Code:
    if(pick == null || pick.equals(Material.AIR))
    You are better of just getting the straight ItemStack and checking if that is null and check if it's material is not a diamond pickaxe
    Code:
    ItemStack pick = player.getInventory().getItemInMainHand();
    
    if(pick == null || pick.getType() != Material.DIAMOND_PICKAXE)
        return;
    So the full code will look a bit cleaner and you won't have to worry about the NullPointerException explained above

    Code:
        public void onBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            ItemStack pick = player.getInventory().getItemInMainHand();
    
            if(pick == null || pick.getType() != Material.DIAMOND_PICKAXE)
                return;
    
            Location blockLoc = event.getBlock().getLocation();
            Block nextBlock = blockLoc.add(1,0,0).getBlock();
    
            while (nextBlock.getType() != Material.BEDROCK) {
                nextBlock.breakNaturally();
                nextBlock = nextBlock.getLocation().add(1, 0, 0).getBlock();
            }
        }
    
    Now for the breaking a layer stuff. I think by layer you mean something like a square. Breaking a block and then removing every block in a 5 (example) block x,z radius not including y. Correct me if you need something else.

    You just need 2 simple loops and that's basically it, there's not much to explain here just looking at it and you'll see that it just loops from -size to size and adding the value to the middle location will result in a square.

    Code:
            int size = 5;
    
            for(int addX = -size; addX <= size; addX++)
                for(int addZ = -size; addZ <= size; addZ++)
                    //Add to location, get block and break it
    


    Final note, your 'line' only seems to go in a x+ direction which might not look good, you could get the direction (N, S, W, E) and break the line accordingly. Also you should add a limit to the line loop as it just seems to keep going.
     
    Last edited: Jul 17, 2022
  3. Offline

    Abaddon123

    Thank you so much , that really hepls but i need to break the blocks untill it hits bedrock not only on 5 block radius
     
  4. Offline

    gochi9

    The area part you want to go to bedrock? Like, for example, a 5x5 square that goes down until it hits bedrock?
     
  5. Offline

    Abaddon123

    The mine is a bedrock box and inside are blocks that player can mine i want to mine whole layer at once and stop when it hits the bedrock wall
     
    Last edited: Jul 17, 2022
  6. Offline

    gochi9

    Well using my example and setting the size to a larger value (like 50 or 100 or above) would break an entire layer, you would only need to check if the block is bedrock and if it is then just don't break that one and continue the loop.
     
  7. Offline

    Abaddon123

    Ty i'll try that

    Now i have this

    Code:
    public void onBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            @SuppressWarnings("deprecation")
            ItemStack pick = player.getInventory().getItemInHand();
    
            if(pick == null || pick.getType() != Material.DIAMOND_PICKAXE)
                return;
    
            int size = 50;
            int addX = block.getX();
            int addY = block.getY();
            int addZ = block.getZ();
            for(int x = addX -size; x < addX + size; x++) {
                for(int z = addZ -size; z < addZ + size; z++) {
                    Block newBlock = block.getWorld().getBlockAt(x,addY,z);
                    if(newBlock.getType() != Material.BEDROCK) {
                        newBlock.setType(Material.AIR);
                    }
                }
            }
        }
    But it breaks the blocks outside the bedrock border

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

Share This Page