Please Help! Need help defining spawners

Discussion in 'Plugin Development' started by Smalltrout, Oct 15, 2014.

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

    Smalltrout

    Hello,

    I need help defining what type of spawner has been mined. I want it so you can gather spawners with silk touch. Right now it just drops a normal mob spawner. I want it to give you the same spawner you mined.
    Code:java
    1. package me.smalltrout.SilkSpawn;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.CreatureSpawner;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.BlockBreakEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class SilkSpawn extends JavaPlugin implements Listener {
    15.  
    16. @Override
    17. public void onDisable(){
    18. getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. @Override
    22. public void onEnable(){
    23. Bukkit.getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26.  
    27. @EventHandler
    28. public void onBlockBreak(BlockBreakEvent event){
    29. if (event.getBlock().getType() == Material.MOB_SPAWNER){
    30.  
    31. Player player = event.getPlayer();
    32. short durability = 90;
    33. CreatureSpawner csBlock = (CreatureSpawner)event.getBlock().getState();
    34.  
    35. if(player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)){
    36.  
    37. ItemStack dropSpawner = new ItemStack(Material.MOB_SPAWNER, 1, durability);
    38.  
    39. csBlock.getWorld().dropItemNaturally(csBlock.getLocation(), dropSpawner);
    40.  
    41. }
    42.  
    43.  
    44. }
    45.  
    46. }
    47. }
    48.  



    I am new to coding please help!

    Thanks!

    -Small
     
  2. Offline

    MeRPG

    Ahh, this. It is actually a bit more complicated than that.
    You can't actually define that, because that data is not kept as an item stack. Try... Lore. Try adding a lore, and on BlockPlaceEvent check for that lore.
     
  3. Offline

    ChipDev

    What about natural? NBT.
     
  4. Offline

    MeRPG

    Ahh, That might work. I'm not very familiar with the NBT tag system, so I can't be much help with that.


    Wait! I just noticed. Remove this code. You don't register events in onDisable.
    Code:java
    1. @Override
    2. public void onDisable(){
    3. getServer().getPluginManager().registerEvents(this, this);//THIS LINE
    4. }
     
  5. Offline

    fireblast709

    Smalltrout set the durability to the entity ID (you can get the EntityType from the CreatureSpawner, in turn this will give you access to getTypeId())
     
  6. Offline

    MeRPG

    fireblast709
    That works? I wish I knew that when I did something similar :oops:
     
  7. Offline

    Smalltrout

    Thanks! Could you give me an example of that? fireblast709
     
  8. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page