Solved PlayerPickUpItemEvent

Discussion in 'Plugin Development' started by Anrza, Jul 12, 2014.

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

    Anrza

    I am trying to make it so that only the player who throw the snowball can pick the item that drops when it hits up.

    Code:java
    1. }
    2. @EventHandler
    3. public void onSnowballHit(ProjectileHitEvent event) {
    4. Projectile entity = (Projectile) event.getEntity();
    5. if (entity instanceof Snowball) {
    6. String uniqueId = event.getEntity().getUniqueId().toString();
    7. if (damageMap.containsKey(uniqueId));{
    8.  
    9. Projectile projectile = (Projectile) event.getEntity();
    10. @SuppressWarnings("deprecation")
    11. Player player = (Player) projectile.getShooter();
    12. String shooter = (String) player.toString();
    13. Location loc = event.getEntity().getLocation();
    14.  
    15. if (weaponMap.get(uniqueId).equals("IRON_AXE")) {
    16. ItemStack is = new ItemStack(Material.IRON_AXE);
    17. Item item = (Item) loc.getWorld().dropItem(loc, is);
    18. String unique = (String) item.getUniqueId().toString();
    19. item.setPickupDelay(100);
    20. damageMap.remove(uniqueId);
    21. weaponMap.remove(uniqueId);
    22. playerMap.put(unique, shooter);
    23. }
    24.  
    25. }
    26. }
    27. }
    28. @EventHandler
    29. public void onPickup(PlayerPickupItemEvent event) {
    30. String pickedItem = event.getItem().getUniqueId().toString();
    31. if (playerMap.containsKey(pickedItem)) {
    32. String uniqueId = (String) playerMap.get(pickedItem);
    33. String player = event.getPlayer().getName().toString();
    34. if (!uniqueId.equals(player)) {
    35. event.setCancelled(true);
    36. playerMap.remove(uniqueId);
    37. }
    38. }
    39. }
    40. }


    It wont let anyone pick up the item. If I change it to
    Code:java
    1.  
    2. event.setCancelled(true);
    3. if (uniqueId.equals(player)) {
    4. event.setCancelled(false);
    , everyone can pick up the item. I believe that the problem has to do with playerMap, which I defined like this:
    Code:java
    1. HashMap<String, String> playerMap = new HashMap<String, String>();

    However, I cannot identify what's wrong with my code.
     
Thread Status:
Not open for further replies.

Share This Page