Block.getWorld returns wrong chunk

Discussion in 'Plugin Development' started by minhperry, Nov 14, 2020.

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

    minhperry

    I was going to create a VeinMine plugin where if you break a block it will automatially mines all block around it within a 3*3*3 box. During the process i encountered a problem:
    Code:
    Code:
    public class VeinPick implements Listener {
       
        @EventHandler
        public void onMine(BlockBreakEvent ev) {
            Player p = ev.getPlayer();
            Block b = ev.getBlock();
            ItemStack mainhand = p.getInventory().getItemInMainHand();
            // gets broken block and item used
           
           // 3 if to check if the player holding right item
            if (mainhand.getType().equals(Material.DIAMOND_PICKAXE)) {
                if (mainhand.getItemMeta().hasDisplayName()) {
                    if (mainhand.getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Block Smasher")) {
                        // "bounding box" around the broken block. low and top are 2 corners of the box
                        Location low = b.getLocation().subtract(1,1,1);
                        Location top = b.getLocation().add(1,1,1);
                        // ArrayList of all block locations
                        ArrayList<Location> locs = new ArrayList<Location>();
                        // Iterate throught all locations, everything works fine till the end of this nested for loop
                        for (int x = low.getBlockX(); x <= top.getBlockX(); x++) {
                            for (int y = low.getBlockX(); y <= top.getBlockX(); y++) {
                                for (int z = low.getBlockX(); z <= top.getBlockX(); z++) {
                                    locs.add(new Location(b.getWorld(), x, y, z));
                                }
                            }
                        }
    
                        // p.sendmsg for debugging
                        for (Location l : locs) {
                            p.sendMessage(l.getBlock().toString());       
                        }
                       
                        p.sendMessage("block=" + b.getChunk().toString());
                        p.sendMessage("world=" + b.getWorld().toString());
                    }
                }
            }  
        }
    }
    upload_2020-11-14_16-32-0.png
    The problem is here, all blocks saved in the arraylist was saved with wrong chunk, the slime block was where the block was broken using the pickaxe
    What could be a potential way to fix this?
     

    Attached Files:

Thread Status:
Not open for further replies.

Share This Page