PlayerInteractEvent Need Help xD

Discussion in 'Plugin Development' started by bjsnow, Oct 15, 2012.

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

    bjsnow



    • How would I make it so Before giving the iron sword it would check if the player had 10 Gold_ingots




    • then if the player had that many it would remove the ingots and give the player the iron sword?




    • I need some help xD



    Code:java
    1.  
    2. @EventHandler(priority=EventPriority.HIGH)
    3. public void onPlayerUse(PlayerInteractEvent event){
    4. Player p = event.getPlayer();
    5.  
    6. if(p.getItemInHand().getType() == Material.GOLD_INGOT && event.getClickedBlock().getType() == Material.IRON_BLOCK){
    7. p.sendMessage("You Have Purchased an Iron_Sword");
    8. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    9. }
    10. else if(p.getItemInHand().getType() == Material.BLAZE_ROD){
    11. //Do whatever
    12. }
    13.  
    14. }
    15. [/syntax=java]
    16.  
    17. [USER=17954]np98765[/USER]
    18. Maybe some help?
    19.  
    20. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    gjossep

    Al try to help, but your code is messy, you put it in a list. And i don't know what the problem is. Try putting your code between [.syntax=java](with out the dot).

    Thanks
     
  3. Offline

    bjsnow

    gjossep
    done
     
  4. Offline

    Woobie

    Pretty sure this wont work, but try it :D
    Code:JAVA
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onPlayerUse(PlayerInteractEvent event){
    3. Player p = event.getPlayer();
    4.  
    5. if(p.getInventory().getItemInHand() == Material.GOLD_INGOT(10)){
    6. if(event.getClickedBlock().getType() == Material.IRON_BLOCK){
    7. p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10));
    8. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    9. p.sendMessage("You Have Purchased an Iron_Sword");
    10.  
    11. } else {
    12. p.sendMessage("You need 10 gold ingots to buy this item!");
    13.  
    14. }
    15. }
    16. }
    17. }
     
  5. Offline

    Lolmewn

    Ohh, I added code for this somewhere! Can't remember where though :O
     
  6. Offline

    bjsnow

    Code:JAVA
    1.  
    2. If this works I <3 You xS
    3.  
    4. [code]
    5. @EventHandler(priority=EventPriority.HIGH)
    6. public void onPlayerUse(PlayerInteractEvent event){
    7. Player p = event.getPlayer();
    8.  
    9. if(p.getInventory().getItemInHand() == Material.GOLD_INGOT(10)){
    10. if(event.getClickedBlock().getType() == Material.IRON_BLOCK){
    11. p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10));
    12. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    13. p.sendMessage("You Have Purchased an Iron_Sword");
    14.  
    15. } else {
    16. p.sendMessage("You need 10 gold ingots to buy this item!");
    17.  
    18. }
    19. }
    20. }
    21. [/code]
    22. Theres an error on if(p.getInventory().getItemInHand() == Material.GOLD_INGOT(10)){ Cant seem to be able to fix it ;( cannot find symbol xD
    23.  
    24. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  7. you need to import org.bukkit.Material;
     
  8. Offline

    the_merciless

    That else statement will not work where it is.

    Just swap the 2 if statements around.

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player p = event.getPlayer();

    if(event.getClickedBlock().getType() == Material.IRON_BLOCK){
    if(p.getInventory().getItemInHand() == Material.GOLD_INGOT(10)){
    p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10));
    p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    p.sendMessage("You Have Purchased an Iron_Sword");

    } else {
    p.sendMessage("You need 10 gold ingots to buy this item!");

    }
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page