Need Help with Custom Item Drops For Certain Enchants

Discussion in 'Plugin Development' started by Br1cky, May 25, 2021.

  1. Offline

    Br1cky

    Ive made a custom Enchant plugin which is Called Smelting Touch as the Name suggest i want it to smelt the items you break with it but i cant get the process of finding the broken blocks smelted from

    Heres the not fully working part of my code:


    Code:
        @EventHandler()
        public void onBlockBreak(BlockBreakEvent event) {
        
            Player player = event.getPlayer();
            player.sendMessage("Block Broken");
        
            if(event.getPlayer().getInventory().getItemInMainHand() == null)
                return;
            if(!event.getPlayer().getInventory().getItemInMainHand().hasItemMeta())
                return;
            if(!event.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchant.SMELTINGTOUCH))
                return;
            if(event.getPlayer().getGameMode() == GameMode.CREATIVE || event.getPlayer().getGameMode() == GameMode.SPECTATOR)
                return;
        
            player.sendMessage("Checks done");
            event.setDropItems(false);
            Block block = event.getBlock();
            player.sendMessage(block+" Broken");
    
            Collection<ItemStack> drops = block.getDrops(player.getInventory().getItemInMainHand());
            if(drops.isEmpty())
                return;
            //Here seems to be the problem
            ItemStack result = null;
            for (Recipe recipe : Bukkit.getRecipesFor(drops.iterator().next())) {
                if (!(recipe instanceof FurnaceRecipe)) continue;
                result = recipe.getResult();
                block.getWorld().dropItemNaturally(block.getLocation(), result);
                break;
            }
        }
     
    Last edited: May 25, 2021
  2. Offline

    KarimAKL

    @Br1cky Did you try printing the recipe to see what it actually is?

    Also, why are you not using your 'player' variable instead of getting the player from the event?
    You should also make a variable for the player's inventory because you are referencing it several times.
     
  3. Offline

    Strahan

    I don't think you are using the right items there. I wasn't sure about your code, so I just made my test plugin to do what you want; drop smelted version of drops from a block instead of the regular. I didn't waste time with checking enchants or anything as it's proof of concept, but this worked great for me:
    Code:
    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
      e.setDropItems(false);
      Iterator<Recipe> it = Bukkit.recipeIterator();
      while (it.hasNext()) {
        Recipe recipe = it.next();
        if (!(recipe instanceof FurnaceRecipe)) continue;
        if (((FurnaceRecipe)recipe).getInput().getType() != e.getBlock().getType()) continue;
      
        e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), recipe.getResult());
      }
    }
     
    davidclue likes this.

Share This Page