Item dropping from GUI

Discussion in 'Spigot Plugin Development' started by nayongbin108, Jun 28, 2018.

  1. I have inventory click event and when I close the inventory the item that I double clicked on comes out of the inventory. How do I fix this? This is my onClick method
    Code (Java):
    @EventHandler
        public void onClick(InventoryClickEvent e)
        {
            String stripname = ChatColor.stripColor(e.getClickedInventory().getName());
            if(stripname.equals("Upgrade Spawner".toUpperCase())&&e.getClick()==ClickType.DOUBLE_CLICK)
            {
                if(e.getCursor()!=null)
                {
                    if(e.getCursor().getType()!=null)
                    {
                        Material item = e.getCursor().getType();
                        if(item.equals(Material.STAINED_GLASS_PANE)) e.setCancelled(true);
                        else if(item.equals(Material.COAL))
                        {
                            if(checkBalance(e.getWhoClicked(), config.getInt("Coal Cost")))
                            {
                                takeAwayMoney(e.getWhoClicked(), config.getInt("Coal Cost"));
                                Map<String, ItemStack> data = us.getSpawnerData();
                                data.put(Utils.locationToString(us.getPlayers().get(e.getWhoClicked())), new ItemStack(Material.COAL));
                                Utils.putInConfig("Spawners", data);
                                e.getWhoClicked().closeInventory();
                                e.getWhoClicked().sendMessage(ChatColor.GREEN + "Iron Golems now drop coal!");
                            } else
                            {
                                e.getWhoClicked().closeInventory();
                                e.getWhoClicked().sendMessage(ChatColor.RED + "You do not have the requirment amount of money! You need " + getMoneyNeeded(e.getWhoClicked(), config.getInt("Coal Cost")) + " more!");
                            }
                        }
                    }
                }
            }
        }
     
  2. You can use e.setCancelled(true);

    So the player won't be able to remove the item from inventory
     
  3. Where should I put it?
     
  4. if(checkBalance(e.getWhoClicked(), config.getInt("Coal Cost")))
    {
    e.setCancelled(true); takeAwayMoney(e.getWhoClicked(), config.getInt("Coal Cost"));
    Map<String, ItemStack> data = us.getSpawnerData();
    data.put(Utils.locationToString(us.getPlayers().get(e.getWhoClicked())), new ItemStack(Material.COAL));
    Utils.putInConfig("Spawners", data);
    e.getWhoClicked().closeInventory();
    e.getWhoClicked().sendMessage(ChatColor.GREEN + "Iron Golems now drop coal!");
    } else
    {
    e.setCancelled(true); e.getWhoClicked().closeInventory();
    e.getWhoClicked().sendMessage(ChatColor.RED + "You do not have the requirment amount of money! You need " + getMoneyNeeded(e.getWhoClicked(),
    config.getInt("Coal Cost")) + " more!");
    }
     
  5. What can I do to make it one click and a drag?
     
  6. What u mean? Like you can drag it but if u place it on Ur in or drop it denies?
     
  7. How it is in mineplex were you click on a item and move it around until it the gui closes
     
  8. That's called lag I think because I played on mineplex for a while and it's normal like hypixel and other gui event cancel but if I'm mistaken send me a video to be sure what to tell you thanks
     
    • Funny Funny x 1
  9. Sounds like lag... How do I make it one click then... Right now it is doing two clickes
     
    • Funny Funny x 1
  10. What u mean if u canceled the event it cancels at first...
     
  11. Code (Java):
    @EventHandler
        public void onClick(InventoryClickEvent e)
        {
            String stripname = null;
            if(e.getClickedInventory().getName()!=null) stripname = ChatColor.stripColor(e.getClickedInventory().getName());
            if(stripname.equals("Upgrade Spawner".toUpperCase())&&e.getClick()==ClickType.DOUBLE_CLICK)
            {
                if(e.getCursor()!=null)
                {
                    if(e.getCursor().getType()!=null)
                    {
                        Material item = e.getCursor().getType();
                        if(item.equals(Material.STAINED_GLASS_PANE)) e.setCancelled(true);
                        else if(item.equals(Material.COAL))
                        {
                            if(checkBalance(e.getWhoClicked(), config.getInt("Coal Cost")))
                            {
                                e.setCancelled(true);
                                takeAwayMoney(e.getWhoClicked(), config.getInt("Coal Cost"));
                                Map<String, ItemStack> data = us.getSpawnerData();
                                data.put(Utils.locationToString(us.getPlayers().get(e.getWhoClicked())), new ItemStack(Material.COAL));
                                Utils.putInConfig("Spawners", data);
                               
                                e.getWhoClicked().closeInventory();
                                e.getWhoClicked().sendMessage(ChatColor.GREEN + "Iron Golems now drop coal!");
                            } else
                            {
                                e.setCancelled(true);
                                e.getWhoClicked().closeInventory();
                                e.getWhoClicked().sendMessage(ChatColor.RED + "You do not have the requirment amount of money! You need " + getMoneyNeeded(e.getWhoClicked(), config.getInt("Coal Cost")) + " more!");
                            }
                        }
                    }
                }
            }
        }
    The item is still dropping to the ground
     
  12. For data ull need a new evenr
    @EventHandler
    public void onDrop(PlayerDropItemEvent e) {
    //in this case import the bykkit ones

    ItemStack dropped = e.getItemStack();//or utem dropped not sure
    if(dropped == Material.Coal){
    e.setCancelled(true) ;
    }
    }


    But atention this would block everyone from dropping all coal so i would tecomend u to set and itme meta on the inventory and then do this


    @EventHandler
    public void onDrop(PlayerDropItemEvent e) {
    ItemStack dropped = e.getItemStack();
    ItemMeta droppedmeta = dropped.getItemMeta();

    if(dropped == Material.Coal){
    if(droppedmeta.getDisplayName() == "item name" {
    e.setCancelled(true) ;
    }
    }
    }
     
  13. Please, for the love of all that's holy, use [CODE][/CODE] blocks.
     
    • Agree Agree x 3
    • Funny Funny x 1
  14. That will place the item in the player inventory