BlockBreakEvent help

Discussion in 'Spigot Plugin Development' started by FlawlessGaming, Jun 27, 2016.

  1. Hello all,

    I've just came back to Minecraft developing after a good 6 month break from everything and i can't remember a few things here and there. Can someone please help me with the following problem;

    I'm trying to register a EventHandler to register everytime i break sugarcane and then drop the sugarcane but with the name "Cocaine". I know I'm going to have to use ItemMeta's but i can't remember how to make it register and then drop the named sugarcane.

    Current Code:
    Code (Text):
    @EventHandler
        public void InteractWithSugarcane(BlockBreakEvent e){
            if (e.getBlock().getType() == Material.SUGAR_CANE_BLOCK);
           
     
  2. try something along the lines of this:

    ItemStack is = (get the itemstack thats dropped)
    ItemMeta im = is.getItemMeta();
    im.setDisplayname(String);
    is.setMetadata(im);

    Edit:
    Lmao! So tired i made this mistake:
    "ItemMeta im = is.getItemStack();"
     
    #2 aaron1998ish, Jun 27, 2016
    Last edited: Jun 27, 2016
    • Agree Agree x 2
  3. Set the block type to Material.AIR and cancel the event. Then just drop the sugarcane at the blocks location. You may also check that there are sugarcanes above, and drop more of them.
     
    • Agree Agree x 1
  4. This is more complicated than it sounds! Sugar cane blocks above the one you break will drop automatically, but they will not trigger BlockBreakEvent. Unless you do something to those blocks before they have a chance to drop, they will drop sugar cane the "vanilla" way.

    Before you set a display name on the items you want to drop, you will need to loop through the sugar cane stack (see: BlockFace) and set the blocks that WOULD have broken under vanilla mechanics to air. Then, drop all the sugar cane at once at the base block.
     
    • Agree Agree x 1
  5. Can you give me an example?
     
  6. To get the block directly above a given block:
    Code (Text):
    Block blockAbove = someBlock.getRelative(BlockFace.UP);
    You can use this strategy to loop through an entire stalk of sugar cane. You will need to start breaking the blocks at the TOP of the stalk, because otherwise, it will cause the blocks above the one you broke to drop.

    I shouldn't say more; I trust you can figure this out with the information we have provided! If you (or anyone else) is looking for an extra challenge, try doing the same thing for a chorus plant.
     
  7. Thank you.
     
  8. Don't forget code conventions, method names normally start with a lower case letter in Java.
     
    • Like Like x 1