Solved how not to break the bedrock

Discussion in 'Plugin Development' started by mAndAle, Jul 6, 2020.

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

    mAndAle

    hello to everyone, I would like to know how not to break the bedrock, I have already povato in various ways but without luck, so I ask for help to you. Here the code:

    Code:
    package org.mAndAle.Tokens.events;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    
    import net.md_5.bungee.api.ChatColor;
    
    
    public class Mining implements Listener{
    
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Block block = e.getBlock();
            Player player = e.getPlayer();
            ItemStack inhand = player.getInventory().getItemInMainHand();
            String mining = ChatColor.GRAY + "Mining I";
              
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
            int Xmin = x - 2;
            int Xmax = x + 3;
            int Ymin = y - 2;
            int Ymax = y + 3;
            int Zmin = z - 2;
            int Zmax = z + 3;
          
            if (inhand != null) {
                if (inhand.getType() == Material.DIAMOND_PICKAXE
                        || inhand.getType() == Material.GOLD_PICKAXE
                        || inhand.getType() == Material.IRON_PICKAXE
                        || inhand.getType() == Material.STONE_PICKAXE
                        || inhand.getType() == Material.WOOD_PICKAXE
                        ) {
                    if (inhand.hasItemMeta() && inhand.getItemMeta().hasLore()) {
                    if (inhand.getItemMeta().getLore().contains(mining)) {
                        for (int i = Xmin; i < Xmax; i++) {
                          
                            for (int j = Ymin; j < Ymax; j++) {
                              
                                for (int k = Zmin; k < Zmax; k++) {
                                    Block newBlock = block.getWorld().getBlockAt(i, j, k);
                               
                                    player.getInventory().addItem(new ItemStack(newBlock.getType()));
                                    newBlock.setType(Material.AIR);
                                    }
                                }
                            }
                        }
                    }
                }  
            }  
        }
    }
    
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @mAndAle Check if there is bedrock first, then check if the event is cancelled, if not cancelled: break the blocks.
     
  3. Offline

    mAndAle

    @timtower and how can i check if there is bedrock?
    p.s I want to find out if there are bedrock blocks in the block and if there aren't break them (only those)
     
  4. Online

    timtower Administrator Administrator Moderator

    The loop that you originally had there.
    But then twice.
    Once to search for bedrock.
    Once to break the blocks.
     
  5. Offline

    mAndAle

    @timtower
    ok, I think I understand what you mean, but in any case how do I understand "that the new block" has bedrock.
    let me explain better, how do I do that if it finds bedrock blocks "in the new block" it does not break them but on the contrary only breaks those that are not bedrock. (and using the main event if (block.getType == Materia.Bedrock) {
    e.setCancelled (true)
    })
    doesn't work because the new block does not take into account the blocks inside.
     
  6. Online

    timtower Administrator Administrator Moderator

    @mAndAle Because you only had 1 loop there.
     
  7. Offline

    mAndAle

    I still don't understand, could you make an example?
     
  8. Online

    timtower Administrator Administrator Moderator

    Post your current code please.
     
  9. Offline

    mAndAle

    then the result was much easier than expected (I try in the loop if there are bedrock(how you telled)) and that's it, sorry for the waste of time
     
Thread Status:
Not open for further replies.

Share This Page