How to define an item in an ItemPickupEvent

Discussion in 'Plugin Development' started by Java4Monkeys, Apr 18, 2014.

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

    Java4Monkeys

    Code:
    package me.BuissnessMonkey.CustomGapples;
     
    import java.io.IOException;
     
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class ItemListener implements Listener {
       
        @EventHandler
        public void onGapplePickup(PlayerPickupItemEvent e) throws IOException{
            Player p = (Player) e.getPlayer();
            if(e.getItem().getType().equals(Material.GOLDEN_APPLE));
            p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 100 , 2));
            p.playSound(p.getLocation(), Sound.LEVEL_UP, 2.0F, 1);
            p.getLocation().getWorld().playEffect(p.getLocation().add(0, 1, 0), Effect.STEP_SOUND, 322);
            e.getItem().remove();
            e.setCancelled(true);
           
           
            if(!e.getItem().getType().equals(Material.GOLDEN_APPLE));
            return ;
           
           
        }
       
     
    }
    
    Hey bukkit, I made an ItemPickupEvent to listen for when a player picks up a golden apple and it gives them regeneration but when a player picks up anything they get regeneration.
     
  2. Offline

    _Belknap_

    get rid of the !, because that basically means if they pickup any item besides a golden apple, it gives them regen.
     
  3. Offline

    nlthijs48

    Java4Monkeys You have no breakets behind the if statement that checks if it is a golden apple, change the ; to { and add a } before the if statement below.
     
  4. Offline

    Java4Monkeys

    It still did not work nlthijs48 I can't pick up anything now
     
  5. Offline

    sipsi133

    This should work:

    Code:java
    1. @EventHandler
    2. public void onGapplePickup(PlayerPickupItemEvent e) throws IOException{
    3. Player p = (Player) e.getPlayer();
    4. if(e.getItem().getType().equals(Material.GOLDEN_APPLE)) {
    5. //your code here
    6. }
    7.  
    8.  
    9. if(!e.getItem().getType().equals(Material.GOLDEN_APPLE)) {
    10. return;
    11. }
    12.  
    13.  
    14. }
     
  6. Offline

    Captain Dory

    Try this:

    PHP:
    @EventHandler
    public void pickupOrDelivery(PlayerPickupItemEvent e) { // get it c:
     
    ItemStack item = (ItemStacke.getItem();
    Player p e.getPlayer();
    if (
    item.getType() != Material.GOLDEN_APPLE) return;
     
    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION100 2));
    p.playSound(p.getLocation(), Sound.LEVEL_UP2.0F1);
    p.getLocation().getWorld().playEffect(p.getLocation().add(010), Effect.STEP_SOUND322);
    e.getItem().remove();
    e.setCancelled(true);
     
     
    }
    sipsi133 That won't work: Unless you cast it to ItemStack, Item.getType() return EntityType. Item is an entity, hence why it can use methods like setVelocity()
     
  7. Offline

    Java4Monkeys

    Captain Dory I don't get regen when I pick up anything now?

    BUMP!

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

    sipsi133

    Java4Monkeys
    Try replacing
    Code:
    ItemStack item = (ItemStack) e.getItem();
    with
    Code:
    ItemStack item = e.getItem().getItemStack();
     
  9. Offline

    Java4Monkeys

    sipsi133 Thank you so much, this was what I was looking for :D
     
Thread Status:
Not open for further replies.

Share This Page