BlockBreakEvent help

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

Thread Status:
Not open for further replies.
  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.
     
  2. @awesomehenderson What do you have so far?

    The process:
    Listen for BlockBreakEvent
    Check the block type
    Get the itemstack and itememeta
    Change the displayName in ItemMeta
    Set the item meta
     
  3. @bwfcwalshy

    Code:
    @EventHandler
        public void InteractWithSugarcane(BlockBreakEvent e){
            if (e.getBlock().getType() == Material.SUGAR_CANE_BLOCK);
     
    Last edited: Jun 27, 2016
  4. @awesomehenderson Alright, the way I have done it is I got the ItemStack from the block drops, just make it an array and get the first item. Then I got the item meta, changed the name, and re set the meta. Then clear the drops and drop the item (I tried adding it to the drops but it didn't seem to work, dropping it does)
     
  5. @bwfcwalshy

    Can you show me the code? I haven't been around bukkit coding for around 6 months. I understand what your saying i just can't think about how to write it
     
  6. @awesomehenderson

    Code:
    @EventHandler
        public void onBreak(BlockBreakEvent e){
            if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK){
                //Change the drops to an array and get the first item, we can do this safely since we know this will only drop 1 item
                ItemStack is = (ItemStack) e.getBlock().getDrops().toArray()[0];
                ItemMeta im = is.getItemMeta();
                //Set the display name
                im.setDisplayName("Cocaine");
                is.setItemMeta(im);
               //Clear the drops so it wont drop the normal sugar cane
                e.getBlock().getDrops().clear();
                //Drop the item with the new ItemStack.
                e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), is);
            }
        }
    Actually, my code will not work as it should if you break the middle or bottom of a sugar cane with one on top. You will need to do a bit more in order to make it also work with them.
     
  7. Offline

    I Al Istannen

    @awesomehenderson
    Remove the ";" after the if. They make the if totally useless. Let me tell you why.
    There are two "types" of if statements. One is this:
    if(condition) {
    <code>
    }

    the other is this:
    if(condition)
    <one statement ending with ;>

    Let's look at an example for the second one:
    Code:
    if(condition)
        sysout("1"); sysout("2");
    This can be rewritten to:
    Code:
    if(condition)
        sysout("1");
    sysout("2");
    which is
    Code:
    if(condition) {
        sysout("1");
    }
    sysout("2");

    If you look at your statement, it can rewritten as this:
    Code:
    if (e.getBlock().getType() == Material.SUGAR_CANE_BLOCK) {}
    This means, that the if is worthless, as the code following it will be executed everytime.


    Then I would encourage you to follow java naming conventions and start methods (and variables) with a lower case letter, followed by camelCase. so "InteractWithSugarcane" would be "interactWithSugarcane".
     
  8. @bwfcwalshy
    What about the sugarcane on top? Sugarcane can grow to 3 levels.

    Would i use something like this?

    Code:
    Block blockAbove = someBlock.getRelative(BlockFace.UP);
     
Thread Status:
Not open for further replies.

Share This Page