Solved Need help with spawner breaking in certain zone.

Discussion in 'Plugin Development' started by deadlymaul, Jul 7, 2014.

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

    deadlymaul

    So I've got the following code:
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent e)
    3. {
    4. final Player player = e.getPlayer();
    5. if (player.hasPermission("SimpleSpawner.get"))
    6. {
    7. Material block = e.getBlock().getType();
    8. if (block.equals(Material.MOB_SPAWNER)) {
    9. if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH))
    10. {
    11. e.setExpToDrop(0);
    12. CreatureSpawner s = (CreatureSpawner)e.getBlock().getState();
    13. s.getWorld().dropItemNaturally(s.getLocation(), SimpleCommands.getSpawnerItem(1, s.getSpawnedType()));
    14. }
    15. }
    16. }
    17. }


    A quick explanation of what the code does: If a player has a pickaxe enchanted with Silk Touch and the permission listed in the code, he will be able to pickup spawners with the respective pickaxe. The code works fine, but the problem appears when PreciousStones is involved. Link to PS: http://dev.bukkit.org/bukkit-plugins/preciousstones/

    The exact problem is the following:

    If a player, that isn't member of the respective PreciousStone, comes with a silk touch pickaxe to a protection stone that he doesn't own, he can mine the spawner. The spawner, as a block, won't break, but it will always drop a spawner when mined. So he can mine the hell out of it and get endless amounts of that spawner.

    I have tried to hook in the line of code to PS so that I can disable the spawner blocks being dropped when mined by a player that is not member of the PS, but to no success. If anyone can help me sorting out this problem, would highly appreciate it. Here's a link to PS'es API:
    http://preciousstones.shoutwiki.com/wiki/Main_Page#Developers_API

    Thanks for your time
    deadlymaul
     
  2. Before you drop the item (in your code) you need to check if the event is canceled by using e.isCanceled()
     
  3. Offline

    MrMag518

    This, and I'd also use the highest event priority. (All this can be done using annotations)

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    3. public void onBlockBreak(BlockBreakEvent e) {
    4. Player player = e.getPlayer();
    5.  
    6. if(player.hasPermission("SimpleSpawner.get")) {
    7. Material block = e.getBlock().getType();
    8.  
    9. if(block == Material.MOB_SPAWNER) {
    10. if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
    11. e.setExpToDrop(0);
    12. CreatureSpawner s = (CreatureSpawner)e.getBlock().getState();
    13. s.getWorld().dropItemNaturally(s.getLocation(), SimpleCommands.getSpawnerItem(1, s.getSpawnedType()));
    14. }
    15. }
    16. }
    17. }
    18.  
     
  4. Offline

    deadlymaul

    Works perfect, thanks for the help mates.
     
Thread Status:
Not open for further replies.

Share This Page