How would I go about this?

Discussion in 'Plugin Development' started by Antigrate, Mar 18, 2014.

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

    Antigrate

    Hi, I'm wondering how I would go about checking that the player has chainmail armour equipped and integrating it into this:

    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event)
    3. {
    4. Player player = event.getPlayer();
    5.  
    6. if(player.getItemInHand().getType() == Material.GOLD_AXE && (event.getAction() == Action.RIGHT_CLICK_AIR))
    7. {
    8. player.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "WARRIOR: " + ChatColor.YELLOW + "" + ChatColor.BOLD + "USED WAR CRY");
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 2));
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 100, 2));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 220, 1));
    12. player.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, 2, 1);
    13. }
    14. }


    Thank you!
     
  2. Offline

    StealerSlain

    Hm... Why in if parameters you have player.getInventory()? For what?
    Code:java
    1. if(event.getAction == Action.RIGHT_CLICK_AIR){
    2. if(event.getPlayer().getItemInHand().getType() == Material.GOLD_AXE){
    3. //your code
    4. }
    5. }
     
  3. Offline

    Antigrate


    Shit oops my bad, was just testing stuff out haha! Will delete.
     
  4. Offline

    GameplayJDK

    Antigrate
    Code:java
    1.  
    2. @EventHandler
    3. public void onClick(PlayerInteractEvent event)
    4. {
    5. Player player = event.getPlayer();
    6.  
    7. if(player.getItemInHand().getType() == Material.GOLD_AXE && (event.getAction() == Action.RIGHT_CLICK_AIR))
    8. && player.getInventory().getHelmet().getType() == Material.WHATEVER) {
    9. player.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "WARRIOR: " + ChatColor.YELLOW + "" + ChatColor.BOLD + "USED WAR CRY");
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 2));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 100, 2));
    12. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 220, 1));
    13. player.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, 2, 1);
    14. }
    15. }
    16.  

    Here is the doc on how to get all or only specific parts of the armour equipped: http://jd.bukkit.org/rb/apidocs/org/bukkit/inventory/PlayerInventory.html#getArmorContents()
     
  5. Offline

    Squid_Boss

    So if a player has chainmail armor on, you want to add all those potion effects / sounds / messages?

    Here is how to check for armor:
    Code:java
    1. if (player.getInventory().getChestplate().getType() == Material.CHAINMAIL_CHESTPLATE) {
    2. //Add anything you want here
    3. }
     
  6. Offline

    Antigrate



    Hi, thanks for your reply!

    I did this:

    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event)
    3. {
    4. Player player = event.getPlayer();
    5.  
    6. if(player.getItemInHand().getType() == Material.GOLD_AXE && event.getAction() == Action.RIGHT_CLICK_AIR)
    7. && (player.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET);
    8. {
    9. player.sendMessage(ChatColor.BLUE + "" + ChatColor.BOLD + "WARRIOR: " + ChatColor.YELLOW + "" + ChatColor.BOLD + "USED WAR CRY");
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 2));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 100, 2));
    12. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 220, 1));
    13. player.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, 2, 1);
    14. }
    15. }


    But have errors in Eclipse for some reason:

    No exception of type boolean can be thrown; an exception type must be a subclass of Throwable
    Syntax error on token "&&", throw expected

    What's going on here? Thank you!

    EDIT: Accidentally added ; at the end for first error but still have the syntax error.
     
  7. Offline

    Squid_Boss

    Antigrate The error is because you close off your if statement, and then you try to add something else to it.

    You did:
    Code:java
    1. if(player.getItemInHand().getType() == Material.GOLD_AXE && event.getAction() == Action.RIGHT_CLICK_AIR)
    2. && (player.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET);

    But it should be this:
    Code:java
    1. if (player.getItemInHand().getType() == Material.GOLD_AXE && event.getAction() == Action.RIGHT_CLICK_AIR && player.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET) {
    2. //Stuff goes here
    3. }


    PS: When writing an if statement, you shouldn't NORMALLY have a semi colon, but there are exceptions..
     
  8. Offline

    GameplayJDK

    Antigrate
    Remove the ; after the if statement, if doesn't take a ; only in case its an inline statement.
    You should also fix the brackets of the if. They seem to be messed up..

    EDIT:
    Squid_Boss had the better answer
     
  9. Offline

    Antigrate


    Ah! Thank you very much, fixed!
     
  10. Offline

    StealerSlain

    Antigrate in what IDE you coding?.. Eclipse should automaticly checks for all errors.
     
  11. Offline

    Squid_Boss

    The brackets work fine, some choose to press enter then have the brackets, and others keep them on the same line, it's just a preference, not an error ;P
     
  12. Offline

    GameplayJDK

    @Squid_BossI meant these: >> ) << ^^
     
  13. Offline

    Squid_Boss

    Ah...I call "{}" brackets, and "()" parentheses ^_^
     
  14. Offline

    GameplayJDK

    Squid_Boss I think they're all "brackets" :D but parentheses sounds much better :p
     
  15. Offline

    Antigrate


    Yeah I'm using Eclipse, but it doesn't help that a) I'm super tired b) 3rd or so day of learning Java and don't know how to fix all errors yet, so I get help on here! :)
     
Thread Status:
Not open for further replies.

Share This Page