BLOCKBREAKEVENT HELP ?

Discussion in 'Spigot Plugin Development' started by L00ting, Jul 14, 2018.

  1. Hello, Making a plugin when people break "wheat" with a perm the wheat will change name

    Code (Java):
    @EventHandler
        public void onHarvest(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if (p.hasPermission("ndrugs.weed")) {
                if (e.getBlock().getType().equals(org.bukkit.Material.WHEAT)) {
    This is what I've done atm. I dont know how no to change the item when they pick it up after they havested it ?
     
  2. Get the item that got dropped and change the item meta/lore or change the material.
     
  3. Code (Text):
    e.setDropItems(false);
    e.getBlock().getWorld().dropItem(e.getBlock().getLocation(), itemStack);
     
  4. add lines:

    Item item = (Item) Bukkit.getWorld("yourworld").getNearbyEntities(e.getBlock().getLocation(), 1, 1, 1);
    item.getItemStack().getItemMeta().setDisplayName("you have the permission !");

    My method: get the item from the block and change its name.

    The @pauhull method: create an item with the custom name
     
  5. Doesn't World#getNearbyEntities() return an array of entities? You can't guarantee no other entities are in the area, nor would you be able to cast an entity array to an Item. pauhall's method is much more reliable.. and actually works..
    Edit:// it returns a Collection, but my point still stands
     
    • Agree Agree x 1
  6. isnt it e.setcancelld(true) ?
     
  7. No, why would it be? Those lines of code remove the initial drops and instead drop a different itemstack (which isn't defined because you'd have to set it, of course).
     
  8. That would make the block indestructable