Solved Check for every single content in Inventory

Discussion in 'Plugin Development' started by FlaveDrake, Jan 21, 2018.

Thread Status:
Not open for further replies.
  1. I've made a little piece of code to be able to shoot projectiles with the diamond shovel:

    Code:java
    1.  
    2. @EventHandler
    3. public void onShoot(PlayerInteractEvent e) {
    4. Player p = e.getPlayer();
    5. ItemStack i = p.getItemInHand();
    6. if (i.getType() == Material.DIAMOND_SPADE) {
    7. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    8. if (i.getAmount() != 1) {
    9. Projectile pj = p.launchProjectile(Egg.class);
    10. pj.setVelocity(p.getLocation().getDirection().normalize().multiply(2.5));
    11. i.setAmount(i.getAmount() - 1);
    12. } else if (i.getAmount() == 1) {
    13. ActionBarAPI.sendActionBar(p, "§cYour §3AK-47 §cis empty, reload it!");
    14. return;
    15. }
    16. } else if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
    17. if (p.getInventory().contains(Material.CLAY_BALL)) {
    18. for (ItemStack item : p.getInventory().getContents()) {
    19. if (i.getAmount() == 31) {
    20. return;
    21. } else if (i.getAmount() < 31) {
    22. ItemStack clay = new ItemStack(Material.CLAY_BALL, 30);
    23. if (item.getAmount() == clay.getAmount()) {
    24. p.getInventory().remove(item);
    25. i.setAmount(31);
    26. p.updateInventory();
    27. } else if (item.getAmount() > clay.getAmount()) {
    28. if (i.getAmount() != 1) {
    29. item.setAmount(item.getAmount() - (30 - i.getAmount()));
    30. i.setAmount(31);
    31. } else if (i.getAmount() == 1) {
    32. item.setAmount(item.getAmount() - 30);
    33. i.setAmount(31);
    34. }
    35. } else if (item.getAmount() >= 0 && item.getAmount() < 30) {
    36. i.setAmount(i.getAmount() + item.getAmount());
    37. p.getInventory().remove(item);
    38. p.updateInventory();
    39. }
    40. }
    41. }
    42. } else {
    43. ActionBarAPI.sendActionBar(p, "§cYou have not enough ammo!");
    44. }
    45. }
    46. }
    47. }
    48.  


    It should reload, when i'm left-clicking.

    But it only reloads when the "bullets" (Clay balls) are in the first slot of my inventory.


    Does anybody has some suggestions for me?
     
    Last edited: Jan 21, 2018
  2. Offline

    ipodtouch0218

    Use the first(Material) method to get the first instance of your bullet in the inventory. (first returns an int, the slot where it was found. Use Inventory#get(int) to get the itemstack.)
     
Thread Status:
Not open for further replies.

Share This Page