1.12.2 Adds 2 items instead of 1

Discussion in 'Spigot Plugin Development' started by Ded2332, Jul 13, 2021.

  1. Hello, trying to make it so that after a player has put a block it returns to inventory, but it comes back twice
    use this code:
    Code (Text):
    inv.addItem(new ItemStack(block.getType()));
    But if you put another material there, it will be added once.
    I don't understand why this happens.
     
  2. We need to see your entire listener. If you're listening for the PlayerInteractEvent, it may fire once for each hand, so that could explain it. But, please post your entire Listener.
     
    • Agree Agree x 1
  3. Code (Text):
      @EventHandler
        public void onPlace(BlockPlaceEvent event) {
            Block block = event.getBlock();
            Inventory inv = event.getPlayer().getInventory();
            if(block.getType() == Material.DIAMOND_BLOCK) {
                inv.addItem(new ItemStack(block.getType()));
            }
            if(block.getType() == Material.GOLD_BLOCK) {
              inv.addItem(new ItemStack(block.getType()));
            }
        }
     
  4. Is the block actually placed? If the event is cancelled and the block is not placed it will come back as 'twice'.
     
  5. do u want the block to be placed or just cancel the placing and return the item to the players inventory? if you add event.setCancelled(true) right before you add the item to the players inventory does that work? also not 100% but you might have to update the players inventory after you add to it with player.updateInventory()