Send message on no ammo found?

Discussion in 'Plugin Development' started by mrsamcraft, Dec 24, 2013.

Thread Status:
Not open for further replies.
  1. Hello,

    So I've been messing around with my plugin most of the day while learning at the same time.
    I have managed to add a gun style code into my plugin, called snowball gun and I have set it so players will need ammo to be able to use it, I've got all that working.

    But I've found a bug where if the player has no ammo in his inventory it spams the console with errors, Saying it can't find the material type. I have tried what I thought would work but I can't seem to squash the bug... It might be easy? But i'm still learning myself java while coding my plugin.

    Here's the main code for the gun to shoot and stuff (Feel free to use if you wish!)

    Question is how can I get it to send a message if the player has no ammo (SULPHUR) in his inventory or on them. Im sorry if this is an easy/stupid question... But i'll never learn if I dont ask?

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e){
    3. if(e.getAction() == Action.RIGHT_CLICK_AIR){
    4. Player p = e.getPlayer();
    5. if(p.getInventory().getItemInHand().getType() == Material.DIAMOND_HOE){
    6. ItemStack i = p.getInventory().getItemInHand();
    7. if(i.getDurability() < 1562){
    8. i.setDurability((short)(i.getDurability() + 52.1));
    9. p.playSound(p.getLocation(), Sound.FIREWORK_BLAST, 1, 2);
    10. p.playSound(p.getLocation(), Sound.FIREWORK_BLAST, 5F, 2F);
    11. p.launchProjectile(Snowball.class);
    12. }else{
    13. p.sendMessage(ChatColor.RED + "Left click to reload!");
    14. p.playSound(p.getLocation(), Sound.ZOMBIE_WOODBREAK, 1, 2);
    15.  
    16. }
    17. }
    18. }else if(e.getAction() == Action.LEFT_CLICK_AIR){
    19. Player p = e.getPlayer();
    20. if(p.getInventory().getItemInHand().getType() == Material.DIAMOND_HOE){
    21. ItemStack i = p.getInventory().getItemInHand();
    22. if(i.getDurability() > 52.1){
    23. for(int x = 0 ; x <= 44 ; x++){
    24. ItemStack im = p.getInventory().getItem(x);
    25. if(im.getType() == Material.SULPHUR){
    26. im.setAmount(im.getAmount() - 1);
    27. i.setDurability((short) (i.getDurability() - 52.1));
    28. p.playSound(p.getLocation(), Sound.DOOR_OPEN, 1, 2);
    29. break;
    30. }
    31. }
    32. }else{
    33. p.sendMessage(ChatColor.RED + "Ammo full!");
    34. p.playSound(p.getLocation(), Sound.NOTE_STICKS, 1, 2);
    35. }
    36. }
    37. }
    38. }
    39.  
    40.  
    41. @EventHandler
    42. public void onEntityDamage1(EntityDamageByEntityEvent e) {
    43. if ((e.getDamager() instanceof Snowball)) {
    44. Snowball s = (Snowball)e.getDamager();
    45. if ((s.getShooter() instanceof Player)) {
    46. Player shooter = (Player)s.getShooter();
    47. if (shooter.getItemInHand().getType() == Material.DIAMOND_HOE)
    48. e.setDamage(getConfig().getDouble("Snowball Gun Damage", 10.0D));
    49. }
    50. }
    51. }


    Thanks,
    ~ Sam

    Merry Christmas and all the best for 2014!
     
  2. Offline

    pokuit

    if(player.getInventory().contains(Material.sulfur)) etc.
    Also another tip if your ever seeing a repeat of the same if statements try re-evaluating your code :)
     
Thread Status:
Not open for further replies.

Share This Page