Solved Make Items go in inventory after block is broken

Discussion in 'Plugin Development' started by ChicagoCuber, May 9, 2021.

  1. Offline

    ChicagoCuber

    So I'm pretty new to Spigot plugin development, and I am making a telekinesis enchantment similar to that in Hypixel Skyblock. The tutorial I am following, (Codedred's custom enchant episode) does work for most blocks. Except for the main thing that I created this enchant for, which is farming. For farming it gives the player one of the crop's normal drops instead of the normal minecraft drops, which is 1-4. I use carrots as an example. Here is my code:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onBlockBreak(BlockBreakEvent event) {
            if (event.getPlayer().getInventory().getItemInMainHand() == null)
                return;
            if (!event.getPlayer().getInventory().getItemInMainHand().hasItemMeta())
                return;
            if (!event.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchants.TELEKINESIS))
                return;
            if (event.getPlayer().getGameMode() == GameMode.CREATIVE || event.getPlayer().getGameMode() == GameMode.SPECTATOR)
                return;
            if (event.getPlayer().getInventory().firstEmpty() == -1)
                return;
    
            event.setDropItems(false);
            Player player = event.getPlayer();
            Block block = event.getBlock();
    
            Collection<ItemStack> drops = block.getDrops(player.getInventory().getItemInMainHand());
            if (drops.isEmpty())
                return;
            player.getInventory().addItem(drops.iterator().next());
            if (event.getBlock().getType() == Material.CARROT) {
                Block block1 = event.getBlock();
                block1.setType(Material.AIR);
                player.getInventory().addItem(new ItemStack(Material.CARROT, 2));
                //just a sample, I have chances and other things that I'm going to implement.
            }
        }
    The drops.iterator part does in fact work, I'm just trying to make it so the chances and extra items also go to the player's inventory. The code below is for a drop chance for Platinum, a 20% drop from ancient debris, and this code does function.
    Code:
    int max = 5;
    int min = 1;
    int random = (int )(Math.random() * 5 + 1);
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onBlockBreak(BlockBreakEvent event) {
            if (event.getBlock().getType() == Material.ANCIENT_DEBRIS) {
                Random rand = new Random();
                int value = rand.nextInt((max-min) + 1) + min;
                Block block = event.getBlock();
                block.setType(Material.AIR);
                if (value == 2) {
                    block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Platinum_Ingot.Platinum_Ingot));
                }
                event.setCancelled(true);
            }
    }
    Any help would be appreciated, thanks!
     
  2. Offline

    OkayName

    I haven't checked if this works, but it should in theory:

    Code:
    for (ItemStack i : event.getBlock().getDrops()) {
        if (event.getPlayer().getInventory().firstEmpty() == -1) {
            // Do something if their inventory is full
        } else {
            event.getPlayer().getInventory().addItem(i); // Add the item to their inventory
        }
    }
    
    Edit: I just reread your code and it seems like you already did that :/. I tested it though, and I get the usual amount instead of one. Im not sure why you only get one, but it might be caused by one of your other plugins.
     
    Last edited: May 10, 2021
  3. Offline

    ChicagoCuber

    I believe it is something with my plugins, I’ll send them when I get home!

    Nevermind, I think the issue was with me using drops.iterator instead of using event.getPlayer and ItemStack i, thanks for the help, really appreciate it!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 11, 2021

Share This Page