Remove an item on right click

Discussion in 'Plugin Development' started by coolmonkeyguy, Jun 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    coolmonkeyguy

    I have been trying and trying and I can't seem to get this to remove the item after a player right clicks it.


    Code:
    public void onPlayerInteract(PlayerInteractEvent e) {
     
        if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                    if (e.getPlayer().getItemInHand().getType().equals(Material.SUGAR)) {
                        e.getPlayer().sendMessage(ChatColor.BLUE + "[BBContraband]" + " " + ChatColor.GOLD + "Enjoy Those Drugs" );
                            e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED ,700,2));
                          e.getPlayer().getItemInHand().setType(Material.AIR);              }
     
  2. Offline

    chasechocolate

    Is the event getting fired?
     
  3. Offline

    coolmonkeyguy

    you mean updating the inventory?
     
  4. Offline

    chasechocolate

    No, when they click on sugar do they get speed?
     
  5. Offline

    coolmonkeyguy

    yes they get the speed. but the item does not get removed
     
  6. Offline

    chasechocolate

    I know it's deprecated, but try adding player.updateInventory() after you set their item in hand.
     
  7. Offline

    coolmonkeyguy

    how would i define player because i tryed Player player = Player sender but the player is not sending a command.
    how would I define it?
     
  8. Offline

    chasechocolate

    Code:java
    1. Player player = event.getPlayer();
     
  9. Offline

    coolmonkeyguy

    do i have to add @SuppressWarnings("deprecation")
    On the updateInventory?
     
  10. Offline

    chasechocolate

    Yeah.
     
  11. Offline

    coolmonkeyguy

    darn i loaded it up on my server and went to test it and the effects work but the item is still there.
    here is the code
    Code:
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
       
        if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                    if (e.getPlayer().getItemInHand().getType().equals(Material.SUGAR)) {
                        e.getPlayer().sendMessage(ChatColor.BLUE + "[BBContraband]" + " " + ChatColor.GOLD + "Enjoy Those Drugs" );
                            e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED ,700,2));
                            e.getPlayer().getItemInHand().setType(Material.AIR);
                            Player player = e.getPlayer();
                            player.updateInventory();
                    }
    }
    
     
  12. Offline

    slayr288

    coolmonkeyguy
    Here's some things that are wrong:
    You're checking if the action is right click air twice.
    You're defining player to a variable after you've been getting the player already.
    You're missing a bracket.
    This would delete the entire stack of sugar.

    Try this code:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                Player player = e.getPlayer();
                if (player.getItemInHand().getType().equals(Material.SUGAR)) {
                    player.sendMessage(ChatColor.BLUE + "[BBContraband]" + " " + ChatColor.GOLD + "Enjoy Those Drugs" );
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 2));
                    player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
                    player.updateInventory();
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page