Solved How can I get a event for 5x5 pickaxe?

Discussion in 'Plugin Development' started by mAndAle, Jun 7, 2020.

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

    mAndAle

    Anyone have an idea for 5x5 pickaxe?

    It really help me a lot pls
     
  2. Offline

    KarimAKL

    @mAndAle Listen to the BlockBreakEvent, then destroy the blocks around the main block.
     
  3. Offline

    mAndAle

    An example to take the other blocks?
     
  4. Online

    timtower Administrator Administrator Moderator

    @mAndAle 5x5 as in 5x5x5 or as in a single layer?
     
  5. Offline

    mAndAle

    5x5x5
     
  6. Offline

    KarimAKL

    @mAndAle Loop the area based on the broken block's location.
     
  7. Offline

    mAndAle

    For example this it would be good?

    Code:
    for(int x = -1;x <= 1;x++){
      for(int y = -1;y <= 1;y++){
        for(int z = -1;z <= 1;z++){
     
  8. Offline

    KarimAKL

    @mAndAle Almost, you need to make the coordinates relative to the block's x, y and z.
     
  9. Offline

    mAndAle

    Can you make me an example please?
     
  10. Offline

    KarimAKL

    @mAndAle
    Code:Java
    1. Block block = ...; // Your block
    2. int x = block.getX();
    3. int y = block.getY();
    4. int z = block.getZ();
    5. int offset = 5;
    6.  
    7. for (int i = x - offset; i < x + offset; i++) {
    8. for (int j = y - offset; j < y + offset; j++) {
    9. for (int k = z - offset; k < z + offset; k++) {
    10. Block newBlock = block.getWorld().getBlockAt(i, j, k);
    11. // Destroy the new block
    12. }
    13. }
    14. }
     
  11. Offline

    mAndAle

    For example if i would take any blocks i can type:

    Code:
    e.getBlock();
    ?

    And then if i would that the material is add to players invntory but there isn't drop everywhere?
     
    Last edited: Jun 9, 2020
  12. Offline

    KarimAKL

    Yes.

    Cancel the event, then add the broken block's material into the player's inventory. Do the same for every looped block as well.
     
  13. Offline

    mAndAle

    I'm getting this error:

    Code:
    For (ItemStack item : p.getInventory().getContents()){
        If (item != null && item.getType() == Material.Diamond_Pickaxe)
      // another code
    }
    }
    The event do always, why?


    In another code there is your code
     
  14. Online

    timtower Administrator Administrator Moderator

    @mAndAle Your "For" should be "for"
    Your capitalization is something that needs to be checked.
     
  15. Offline

    mAndAle

    I'm writing with telefon because I'm out now but it is good my capitalizion in my plugin
     
  16. Online

    timtower Administrator Administrator Moderator

    Then we need the error that you are getting.
     
  17. Offline

    mAndAle

    This is the problem I don't get any errors

    When I return to home I'll post all class
     
  18. Offline

    KarimAKL

    @mAndAle Why're you looping the player's inventory?
     
  19. Offline

    mAndAle

    Beacause if item that broke the block is diamond pickaxe That have a certain lore
     
  20. Offline

    KarimAKL

    @mAndAle Okay, what's the problem with it then?
     
  21. Online

    timtower Administrator Administrator Moderator

    Check the item in the hand then.
     
    KarimAKL likes this.
  22. Offline

    mAndAle

    How i can?
     
  23. Online

    timtower Administrator Administrator Moderator

    p.getItemInHand() ?
    Or something similar at least.
     
  24. Offline

    KarimAKL

    @timtower @mAndAle I believe it's player.getInventory().getItemInMainHand() for >=1.9 and player.getInventory().getItemInHand() for <=1.8.
    For 1.8 and below, you can also use Player#getItemInHand() without it being deprecated.
     
  25. Offline

    mAndAle


    Thanks, i figured it out, but there's one problem the block isn't 5x5x5, how can i geti 5x5x5?
     
  26. Online

    timtower Administrator Administrator Moderator

    Then post your code
     
  27. Offline

    mAndAle

    Here the code:
    Code:
    public class Mining implements Listener{
    
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Block block = e.getBlock();
            Player player = e.getPlayer();
            ItemStack inhand = player.getInventory().getItemInMainHand();
              
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
            int offset = 5;
          
            if (inhand != null && inhand.getType() == Material.DIAMOND_PICKAXE) {
                if (inhand.hasItemMeta() && inhand.getItemMeta().hasLore()) {
                    if (inhand.getItemMeta().getLore().contains("Mining")) {
                        for (int i = x - offset; i < x + offset; i++) {
                          
                            for (int j = y - offset; j < y + offset; j++) {
                              
                                for (int k = z - offset; k < z + offset; k++) {
                                    Block newBlock = block.getWorld().getBlockAt(i, j, k);
                                    newBlock.setType(Material.AIR);
                                }
                            }
                        }
                    }
                }
            }
          
        }
    }
    
    And another question if i want to put the block's drop into inventory off the player how can?
     
  28. Online

    timtower Administrator Administrator Moderator

    @mAndAle Because your offset is 5 in both directions.
    Try using an xMin, xMax.
    xMin = x -2, xMax = x+2
    Then you have better variable names as well, I suggest xIndex or something like that.
    i, j and k don't tell a lot when using coordinates.
     
  29. Offline

    mAndAle

    for the another question any input?

    And an example for this?

    this is wouble be right:

    Code:
    public void onBreak(BlockBreakEvent e) {
            Block block = e.getBlock();
            Player player = e.getPlayer();
            ItemStack inhand = player.getInventory().getItemInMainHand();
               
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
            int Xmin = x -2;
            int Xmax = x + 2;
            int offset = 5;
           
            if (inhand != null && inhand.getType() == Material.DIAMOND_PICKAXE) {
                if (inhand.hasItemMeta() && inhand.getItemMeta().hasLore()) {
                    if (inhand.getItemMeta().getLore().contains("Mining")) {
                        for (int i = Xmin; i < Xmax; i++) {
                           
                            for (int j = y - offset; j < y + offset; j++) {
                               
                                for (int k = z - offset; k < z + offset; k++) {
                                    Block newBlock = block.getWorld().getBlockAt(i, j, k);
                                    newBlock.setType(Material.AIR);
                                }
                            }
                        }
                    }
                }
            }
           
        }
    }
    
    
    Solo i do for all variables?
     
  30. Online

    timtower Administrator Administrator Moderator

    @mAndAle That question did not exist yet when I saw it. And not sure about it either.
    The x is right yes. Should give a 5*5 area
     
Thread Status:
Not open for further replies.

Share This Page