Help with removing item on click!

Discussion in 'Plugin Development' started by THESABERSLAYER, Jan 19, 2014.

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

    THESABERSLAYER

    Hey everyone so I need some help with removing items when they right click it here is my code http://pastebin.com/R6PBmqnC it doesn't seem to work when I click it nothing happens. Any help would be great thanks!
     
  2. Offline

    RDNachoz

    Try changing null to Material.AIR
     
  3. Offline

    SuperOmegaCow

  4. Offline

    THESABERSLAYER

    SuperOmegaCow the EventHandler is registered though

    Also RDNachoz I did that and nothing happened still didnt get my potion effect or the text or anything

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    Niknea

    THESABERSLAYER instead of setting the item in there hand to null, remove the itemstack, it will also help as when the player has more then one it will only lose one and not all.
     
  6. Offline

    THESABERSLAYER

    Niknea can u please post the code to remove?
     
  7. Offline

    Niknea

    THESABERSLAYER create an itemstack that contains one red
    Mushroom. Then use this code
    Code:java
    1. player.getInventory.removeItem(<itemstack name>); [\syntax]
     
  8. Offline

    THESABERSLAYER

    Niknea my code works except for when there is only one mushroom left then it doesn't get rid of it
     
  9. Offline

    Sour

    Can you post the code that you currently have?
     
  10. Offline

    THESABERSLAYER

  11. Offline

    Sour

    Try This
    Code:java
    1. package me.MichaelSaber.Strong;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15.  
    16. public class Core extends JavaPlugin implements Listener {
    17.  
    18. public void onEnable() {
    19. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22.  
    23. @EventHandler
    24. @SuppressWarnings("deprecation")
    25. public void onPlayerInteract(PlayerInteractEvent e) {
    26. Player player = e.getPlayer();
    27. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    28. if (player.getItemInHand().getType().equals(Material.RED_MUSHROOM)) {
    29. player.sendMessage(ChatColor.GREEN + "You Are Now Invisble!");
    30. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 300, 300));
    31. ItemStack item = player.getItemInHand();
    32. item.setAmount(1);
    33. player.getInventory().removeItem(item);
    34. player.updateInventory();
    35. }
    36. }
    37. }
    38. }
     
  12. Offline

    STORM2354

    Sour when you take a stack it gets rid of all, how do you make it only get rid of one at a time?
     
  13. Offline

    THESABERSLAYER

    Sour thanks it works! :D
     
  14. Offline

    Garris0n

    Sour Please don't spoonfeed people entire classes (and on top of that not even say what you fixed)...Also, you can set the item in hand to null, that's how it's supposed to work.

    Besides, what you're doing is calling this method. That in turn does this to call this method. Which sets the item to null.
     
  15. Offline

    Sour

    An ItemStack does not necessarily mean that it contains 64 items. I set the amount to one, so it will only remove one item.


    No problem.


    Ya, probably shouldn't have handed it to him, but whatever.

    Also, does setting an item to null not remove the whole stack? He only wanted to remove one item, not all of them.
     
Thread Status:
Not open for further replies.

Share This Page