Removing a specific item ID from one's inventory after use

Discussion in 'Plugin Development' started by KaiPol, Nov 8, 2013.

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

    KaiPol

    Okay so I'm trying to make a plugin and this is what I have so far, I want the bone to make a lighting and make the anvil sound, and the stick to make an explosion and make the enderman scream sound.

    Here's my code:
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getType().getId();
            if(blockId == 280){
                Block block = player.getTargetBlock(null, 10);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.playSound(location,Sound.ENDERMAN_SCREAM, 1, 1);
                world.createExplosion(location, 5);
                blockId 280 delete
            } else if(blockId == 352){
                Block block = player.getTargetBlock(null, 10);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.playSound(location,Sound.ANVIL_LAND, 1, 1);
                world.strikeLightning(location);
            }
    I want it so that if I use the stick/bone it removes the stick/bone from my inventory, but if I have 2 sticks/bones it only removes one but keeps the other. I'm pretty new to plugin programming so this might be extremely simple and 'm just too stupid to figure it out.
     
  2. Offline

    sgavster

    KaiPol
    PHP:
    if(player.getInventory().contains(Material.YOUR_MATERIAL)) {
    player.getInventory().removeItem(new ItemStack(Material.YOUR_MATERIAL1));
    }
     
    KaiPol likes this.
  3. Offline

    Noxyro

    Basically with
    Code:java
    1. player.getInventory().clear(player.getInventory().getHeldItemSlot());

    and for your case
    Code:java
    1. if (player.getItemInHand().getAmount() == 1) {
    2. player.getInventory().clear(player.getInventory().getHeldItemSlot());
    3. } else {
    4. player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1);
    5. }
     
    KaiPol likes this.
  4. Offline

    KaiPol

    Thank you for both of your replies! Both ways worked!
     
  5. Offline

    TheCombatCA

    Can you mark this thread solved?
     
Thread Status:
Not open for further replies.

Share This Page