How to check if an item has an specific enchantment?

Discussion in 'Spigot Plugin Development' started by Zendovo, Jun 1, 2017.

  1. Title says it all here is my try and code:
    Code (Text):
    package me.zendovio.fortuneblocks;

    import java.io.File;

    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.minecraft.server.v1_8_R3.Enchantment;

    public class Main
      extends JavaPlugin
      implements Listener
    {
      public void onEnable(){
        getServer().getPluginManager().registerEvents(new Break(this), this);
        if (!new File(getDataFolder(), "config.yml").exists())
        {
        saveDefaultConfig();
      }
    }

      public void onDisable(){
        getServer().getLogger().info("FortuneBlocks has been disabled!");
        saveConfig();
        reloadConfig();
      }

      @EventHandler
      public void onBreak(BlockBreakEvent e){
          Player player = e.getPlayer();
          Block block = (Block) e.getBlock();
          if ((player.hasPermission("fortuneblocks.use")) &&
                  (player.getGameMode().equals(GameMode.SURVIVAL))) {
              if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
              {
                  if ((this.getConfig().getIntegerList("Blocks").contains(block)) &&
                    (player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)))
                  {
                      int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                      e.getBlock().setType(Material.AIR);
                      e.getBlock().getDrops().clear();
               
                  }
                  }
              }
          }

      }

    And also need some other help
    How do I make the new drops like
    e.g.-Block broken is Diamond Block
    Fortune is 10. so the new drop will be multiplied by the fortune pls help

    This is my try

    Code (Text):
    //Added this
    String blockid = String.valueOf(block);
    //other stuff
                      Material bID = Material.valueOf(blockid);
                      ItemStack drop = new ItemStack(bID, fortune);
    //other stuff
     
    #1 Zendovo, Jun 1, 2017
    Last edited: Jun 1, 2017
  2. player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS) should work. Is your code just not working at all? You should start putting debug messages inbetween each if statement and checking if they are getting passed.

    As for your second question you already have the integer which is the fortune, so you take that integer and multiply it by the quantity of the blocks drop,
     
  3. Are you sure the contains enchantment code isn't working? Because in the same if statement you get an integer list of "Blocks" and then compare it to a Block object? I'm not sure if you can do this, if so I have never seen it done before. Check if an X, Y and Z value are equal to the ones of the block.
     
  4. I didn't even realise what was going on in that other if statement, nice catch @RubbaBoy
    if ((this.getConfig().getIntegerList("Blocks").contains(block)) &&


    If you have a list of integers then you are using block IDs which is depricated and you should move on to block names ASAP and you can't see if it contains the block, but b.getType().name() or since your code is yet to be fixed b.getTypeId()
     
    • Like Like x 1
  5. Oops, I forgot the context wasn't for coordinates :p

    If you want full customizability/block support add the data value to the config as well with what Ugleh said.
     
  6. Thanks for the reply and help! But it didn't work so then i removed the dropping blocks part to check if other things were working or not. But then also it didn't work. Then i removed the check for using the config to see if the block mined was listed in the config and it worked! I might need more help I will have to ask you again if it doesnt work.
     
  7. And yes i fixed all the silly things. And I also was using 2 classes earlier so i made a few mistakes.

    And now I only have problem with dropping and yes i have read the spigot docs if you want to say that :p. So my code seems to work till
    Code (Text):
      @EventHandler
      public void onBreak(BlockBreakEvent e){
          Player player = e.getPlayer();
          Block block = (Block) e.getBlock();
          String blockid = String.valueOf(block);
              //Maybe there is problem with these
          if ((player.hasPermission("fortuneblocks.use")) &&
                  (player.getGameMode().equals(GameMode.SURVIVAL))) {
              if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
              {
                  if(player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS))
                  {
                      int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                      e.getBlock().setType(Material.AIR);
                      e.getBlock().getDrops().clear();
                                      //Works till here clears the drop and sets it to air but no drops.
                      Material bID = Material.valueOf(blockid);
                      ItemStack drop = new ItemStack(bID, fortune);
                      player.getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
                  }
                  }
              }
          }
         
      }
     
  8. Ohhh, you must be making a mining plugin for a Prison server.
    I recommend that you do not drop the fortune equivalent of the blocks
    mined.

    Blocks that players don't gather or are unable to carry will sit there until
    a clear lag service removes them, which will lag your server.

    Insert the amount of blocks yielded from the fortune enchant into the
    players inventory directly.
     
    • Useful Useful x 1
  9. Yes I am making a plugin for prison! I know that you can add items to the inventory using Inventory.addItem(ItemStack)
    But, how do I use fortune on it so i take the level of fortune from from the pickaxe and then want the player to get as many blocks as the number of fortune.

    Will this work?
    Code (Text):
    player.getInventory().addItem(blockid, fortune);
    My main language is not English so even when I read the spigot docs sometimes I don't understand :(
    So sorry for my English

    Note: I do not want to get spoon feeded. I have posted this after the plugin was not working and couldn't figure out what to do.
     
    #9 Zendovo, Jun 1, 2017
    Last edited: Jun 1, 2017
  10. You will have to come up with your own calculation for this.
    Fortune has an effect on mining redstone ore, diamond ore, and others;
    and will add the correct amount to your inventory, but the same effect
    does not apply to blocks such as gold blocks, diamond blocks, and others; so you'll have to set every drop amount to
    one and make up your own calculation for figuring fortune yield.