Solved Adding items to a hopper.

Discussion in 'Spigot Plugin Development' started by AgustinEzequiel2, Jan 27, 2020.

Thread Status:
Not open for further replies.
  1. Hi, I'm making a plugin to save the enchantments of the weapons in an enchanted book after disenchanting them in the grindstone, for that I'm using a hopper.
    (I'm making this thread because I did not find any solution related to my problem in the forums)

    The problems start when I want to add this book (after creating it) inside a hopper. I can store it (the book) inside a chest, but not inside a hopper.

    I decided to go step by step, and try to add a diamond inside a hopper, using the BlockPlaceEvent event.

    Code (Java):
    package me.matsuneitor.grindstoneenchanter.Events;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Hopper;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;

    public class BlockPlace implements Listener {

        @EventHandler
        public void onBlocKPlace(BlockPlaceEvent event) {
            if(event.getBlockPlaced().getType().equals(Material.HOPPER)) {
                event.getPlayer().sendMessage(ChatColor.AQUA + "¡Diamonds!");
                Hopper hopper = (Hopper) event.getBlockPlaced().getState();
                ItemStack itemStack = new ItemStack(Material.DIAMOND);
                hopper.getInventory().addItem(itemStack);
                hopper.update();
            }
        }
    }
    I've already registered the event, when I place a hopper somewhere, I receive the message "Diamonds!" but the hopper's still empty.

    ¿What am I doing wrong? :confused:

    I'm using 1.15.2.
     
  2. Maybe try after adding the item to the inventory run setState to the block
     
  3. You need to use hopper.setBlockData(hopper.getBlockData()); before the update
     
    • Like Like x 1
  4. I couldn't find that method.

    I just tried it, but it still doesn't work.

    Thanks to both anyway. :)
     
  5. Did you try getBlockInventory()?

    Edit: nvm that one‘s only used for chests
     
    • Like Like x 1

  6. Try running it in a delayed task after 1-5 ticks for example.
     
    • Like Like x 1
  7. yup, that's for chests.

    I found the solution, changed from getInventory() to getSnapshotInventory() and now it works. Thanks y'all for the help.
     
    • Useful Useful x 1
Thread Status:
Not open for further replies.