Solved How get Silk touch broke Event?

Discussion in 'Plugin Development' started by rawiuwt, Jul 18, 2013.

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

    rawiuwt

    I want to get that player broke diamond ore with silk touch effect or not?
    I only see BlockBreakEvent but can't use to know that player broke this block with silk touch effect
     
  2. Offline

    etaxi341

    You could check if the Player has an Pickaxe in his Hand. After that you could check for the Enchantments at the Pickaxe. If the Enchantment is Silk touch you can do your stuff.

    rawiuwt Forgot to Tahg you

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    Jhtzb likes this.
  3. Offline

    rawiuwt

    Oh! you idea is so good.
    Code:java
    1. package io.github.rawiwut.sliktouch;
    2.  
    3. import org.bukkit.block.Block;
    4. import org.bukkit.enchantments.Enchantment;
    5. import org.bukkit.enchantments.EnchantmentWrapper;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockDamageEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class listener implements Listener {
    12. @EventHandler
    13. public ItemStack Iteminhand(BlockDamageEvent event) {
    14.  
    15. ItemStack inhand=event.getItemInHand();
    16. final Enchantment SILK_TOUCH = new EnchantmentWrapper(33);
    17. if(inhand.containsEnchantment(SILK_TOUCH))
    18. {
    19. int tobreak = event.getBlock().getTypeId();
    20. if(tobreak == 16)
    21. {
    22. //set item to drop
    23. }
    24. else if(tobreak == 21)
    25. {
    26. //set item to drop
    27. }
    28. else if(tobreak == 56)
    29. {
    30. //set item to drop
    31. }
    32. }
    33. return inhand;
    34. }
    35.  
    36. }

    This code is okay?
    How can I set entity when the block break?
     
    SumrioL likes this.
  4. Offline

    rawiuwt

  5. Offline

    etaxi341

    rawiuwt
    What entity? Do you want to spawn an Entity or what?
    And I would use the BlockBreakEvent and not the BlockDamageEvent

    Ahh with Entity you mean to drop an different item right?

    This is the code I would use for this:
    Code:java
    1. package io.github.rawiwut.sliktouch;
    2.  
    3. import org.bukkit.block.Block;
    4. import org.bukkit.enchantments.Enchantment;
    5. import org.bukkit.enchantments.EnchantmentWrapper;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class listener implements Listener {
    12. @EventHandler
    13. public ItemStack Iteminhand(BlockBreakEvent event) {
    14.  
    15. ItemStack inhand=event.getItemInHand();
    16. final Enchantment SILK_TOUCH = new EnchantmentWrapper(33);
    17. if(inhand.containsEnchantment(SILK_TOUCH))
    18. {
    19. int tobreak = event.getBlock().getTypeId();
    20. if(tobreak == 16)
    21. {
    22. event.setCancelled(true);
    23. block.setType(Material.AIR);
    24. event.getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.<HERE YOUR DROPITEM>, <HERE AN INT FOR THE AMOUNT OF DROPPED ITEMS>));
    25. }
    26. else if(tobreak == 21)
    27. {
    28. event.setCancelled(true);
    29. block.setType(Material.AIR);
    30. event.getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.<HERE YOUR DROPITEM>, <HERE AN INT FOR THE AMOUNT OF DROPPED ITEMS>));
    31. }
    32. else if(tobreak == 56)
    33. {
    34. event.setCancelled(true);
    35. block.setType(Material.AIR);
    36. event.getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.<HERE YOUR DROPITEM>, <HERE AN INT FOR THE AMOUNT OF DROPPED ITEMS>));
    37. }
    38. }
    39. return inhand;
    40. }
    41.  
    42. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    rawiuwt

    Yes i mean that.
    Thank you for you code.

    but this command does doesn't have @etaxi341
    Code:java
    1. event.getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.COAL,1));


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    etaxi341

    rawiuwt
    Oh I failed a little bit :D just add this line at the BlockBreakEvent:
    Code:java
    1. int block = event.getBlock();


    Then change it to event.getPlayer.getWorld insted of event.getWorld

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    rawiuwt likes this.
  8. Offline

    rawiuwt

    Ah! ok i only left 1 problem how to set ItemStack to lapis dye
     
  9. Offline

    etaxi341

    You need to work with damage values. So you drop the item ink_sack but with a damage value. I think it works like this:
    Code:java
    1. event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.INK_SACK,<DAMAGE VALUE>,1));
     
  10. Offline

    rawiuwt

    etaxi341 I do like that but it error
     
  11. Offline

    etaxi341

    Wait I look for it. Just wait one moment.

    I didn't tested it but it should work.
    Code:java
    1. ItemStack is = new ItemStack(Material.INK_SACK,1);
    2. is.setDurability(<DATA VALUE>);
    3. event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), is);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    rawiuwt

    Oh! I get it.
    Code:java
    1. event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.INK_SACK,1,(short)4));

    etaxi341 Thank you so much.
     
    etaxi341 likes this.
Thread Status:
Not open for further replies.

Share This Page