Breaking Blocks

Discussion in 'Spigot Plugin Development' started by TrashSkid, Jun 3, 2018.

  1. So, I'm trying to create an enchant where when the player breaks a block with this enchant, it breaks the blocks in a plus pattern, I will upload a picture so you can see.

    This is part of my code, I have everything else in the enchant setup but i'm stumped here.



    Code (Text):
        @EventHandler
        public void onHit(EntityDamageByEntityEvent event){
            if(event.getDamager() instanceof Player) {
                Player player = (Player) event.getDamager();
           
                Item mainhand = player.getInventory().getItemInMainHand();
           
                if(mainhand.containsEnchantment(this)) {
    I know i'm a noob but I really need help xD
     

    Attached Files:

  2. First of all use BlockBreakEvent
    Then just add 1 to the Y coordinate, to the Z coordinate and to the X coordinate and the same with substraction
    Code (Java):
    Block block = e.getBlock();
    Block block2 = block.getLocation.add(1,0,0);
    Block block3 = block.getLocation.add(0,1,0);
    Block block4 = block.getLocation.add(0,0,1);
    Block block5 = block.getLocation.subtract(1,0,0);
    //And so on
     
  3. Like Creepy Code said, you need to use the block break event. Then from there, you get the item in their hand to check if it's enchanted. Then from there, you can get the block they broke and the blocks relative to them in all directions excluding diagonals. This will give you a plus pattern. You, however, need to take into consideration the direction they are facing and exclude that when getting relative blocks as that would make it a plus pattern, plus a block forward.
    Code (Java):

             b.getRelative(BlockFace.DOWN).setType(Material.AIR);
            b.getRelative(BlockFace.UP).setType(Material.AIR);
            //Out of these locations, you need to abstain the one which the player is facing.
            b.getRelative(BlockFace.EAST).setType(Material.AIR);
            b.getRelative(BlockFace.NORTH).setType(Material.AIR);
            b.getRelative(BlockFace.SOUTH).setType(Material.AIR);
            b.getRelative(BlockFace.WEST).setType(Material.AIR);
     
  4. Looking at your screenshot you just want a simple cross like this +
    You need to use BlockBreakEvent and get the blocks relative to the destroyed block using Block#getRelative
    and then iterate all these blocks and destroy them using Block#breakNaturally tool used as parameter.

    Sample:
    Code (Text):
    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
        Block[] blocks = new Block[] {
                e.getBlock().getRelative(BlockFace.NORTH),
                e.getBlock().getRelative(BlockFace.SOUTH),
                e.getBlock().getRelative(BlockFace.EAST),
                e.getBlock().getRelative(BlockFace.WEST)
        };
        for (Block block : blocks) {
            block.breakNaturally(e.getPlayer().getInventory().getItemInMainHand());
        }
    }
    But you can't just copy past this code, he will only work you are facing from up or down the block you've break.
    So for make it work you just have to check where the player is facing when destroying it and change the blocks array content with the correct BlockFace !

    Hope you understand, my english is bad af.
     
    • Agree Agree x 1