Right Click Event

Discussion in 'Plugin Development' started by Benpat9, Jul 10, 2014.

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

    Benpat9

    teej107
    ive done that but it doesnt do anything now im not sure if i misunderstood you but heres the updated code.

    Code:java
    1. @EventHandler
    2. public void OnReg2(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack Hand = event.getPlayer().getItemInHand();
    5. if(player.hasPermission("potionstaffs.regen")){
    6. System.out.println("Potions Staff " + player.getName() + " has permission");
    7. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    8. System.out.println("Potions Staff " + player.getName() + " Right Clicked");
    9. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "2")){
    10. System.out.println("Potions Staff " + player.getName() + " Success");
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    12. ItemMeta HandM = player.getItemInHand().getItemMeta();
    13. HandM.setDisplayName(ChatColor.RED + "Regen Staff - 1 Uses Left");
    14. Hand.setItemMeta(HandM);
    15. player.sendMessage(ChatColor.GREEN + "Whoosh");
    16. }
    17. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "1")){
    18. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    19. player.sendMessage(ChatColor.RED + "Your Staff Has Broken");
    20. player.getInventory().removeItem(player.getInventory().getItemInHand());
    21. }
    22. }
    23. }
    24. else{
    25. player.sendMessage(ChatColor.RED + "You do not have the required permission.");
    26. }
    27. }
     
  2. Offline

    teej107

    Benpat9 What do you mean it doesn't do anything?
     
  3. Offline

    Benpat9

    teej107 when i right click the staff it doesnt give me potion effect or say Whoosh
     
  4. Offline

    Si1lve3r

    The display name from the item is null, is that maybe the problem?
    When it checks
    Code:
    if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "2")){
    
    it gives null.
    Thats i think is the Problem.

    Code:java
    1. @EventHandler
    2. public void OnReg2(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack Hand = event.getPlayer().getItemInHand();
    5. if(player.hasPermission("potionstaffs.regen")){
    6.  
    7. System.out.println("Potions Staff " + player.getName() + " has permission");
    8.  
    9. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    10.  
    11. System.out.println("Potions Staff " + player.getName() + " Right Clicked");
    12.  
    13. if ( player.getItemInHand().getItemMeta().getDisplayName() == null) {
    14. ItemMeta HandM = player.getItemInHand().getItemMeta();
    15. HandM.setDisplayName(ChatColor.RED + "2");
    16. Hand.setItemMeta(HandM);
    17. }
    18.  
    19. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "2")){
    20.  
    21. System.out.println("Potions Staff " + player.getName() + " Success");
    22. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    23. ItemMeta HandM = player.getItemInHand().getItemMeta();
    24. HandM.setDisplayName(ChatColor.RED + "Regen Staff - 1 Uses Left");
    25. Hand.setItemMeta(HandM);
    26. player.sendMessage(ChatColor.GREEN + "Whoosh");
    27. } else if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "Regen Staff - 1 Uses Left")){
    28. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    29. player.sendMessage(ChatColor.RED + "Your Staff Has Broken");
    30. player.getInventory().removeItem(player.getInventory().getItemInHand());
    31. }
    32. }
    33. }
    34. else{
    35. player.sendMessage(ChatColor.RED + "You do not have the required permission.");
    36. }
    37. }

    That works, i think.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    teej107

    Benpat9 What does the console print out? You do print out stuff to the console for your debug code.
     
  6. Offline

    Benpat9

    teej107

    console:
    Code:
    12.07 23:04:29 [Server] INFO Potions Staff benpat9 Right Clicked
    12.07 23:04:29 [Server] INFO Potions Staff benpat9 has permission
    Code:
    Code:java
    1. @EventHandler
    2. public void OnReg2(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack Hand = event.getPlayer().getItemInHand();
    5. if(player.hasPermission("potionstaffs.regen")){
    6. System.out.println("Potions Staff " + player.getName() + " has permission");
    7. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    8. System.out.println("Potions Staff " + player.getName() + " Right Clicked");
    9. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "2")){
    10. System.out.println("Potions Staff " + player.getName() + " Success");
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    12. ItemMeta HandM = player.getItemInHand().getItemMeta();
    13. HandM.setDisplayName(ChatColor.RED + "Regen Staff - 1 Uses Left");
    14. Hand.setItemMeta(HandM);
    15. player.sendMessage(ChatColor.GREEN + "Whoosh");
    16. }
    17. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "1")){
    18. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    19. player.sendMessage(ChatColor.RED + "Your Staff Has Broken");
    20. player.getInventory().removeItem(player.getInventory().getItemInHand());
    21. }
    22. }
    23. }
    24. else{
    25. player.sendMessage(ChatColor.RED + "You do not have the required permission.");
    26. }
    27. }
     
  7. Offline

    teej107

    That means that this if test is false:
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.RED + "2"))


    Try removing the ChatColor.RED from the contains() method and see if it just has the '2' character in it.
     
  8. Offline

    Benpat9

    teej107 that worked but ive got the problem that now it will say it broke even when it should still have 1 use left (it will break after one use)

    updated code:
    Code:java
    1. @EventHandler
    2. public void OnReg2(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack Hand = event.getPlayer().getItemInHand();
    5. if(player.hasPermission("potionstaffs.regen")){
    6. System.out.println("Potions Staff " + player.getName() + " has permission");
    7. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    8. System.out.println("Potions Staff " + player.getName() + " Right Clicked");
    9. if(player.getItemInHand().getItemMeta().getDisplayName().contains("2")){
    10. System.out.println("Potions Staff " + player.getName() + " Success");
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    12. ItemMeta HandM = player.getItemInHand().getItemMeta();
    13. HandM.setDisplayName(ChatColor.RED + "Regen Staff - 1 Uses Left");
    14. Hand.setItemMeta(HandM);
    15. player.sendMessage(ChatColor.GREEN + "Whoosh");
    16. }
    17. if(player.getItemInHand().getItemMeta().getDisplayName().contains("1")){
    18. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 440, 1));
    19. player.sendMessage(ChatColor.RED + "Your Staff Has Broken");
    20. player.getInventory().removeItem(player.getInventory().getItemInHand());
    21. }
    22. }
    23. }
    24. else{
    25. player.sendMessage(ChatColor.RED + "You do not have the required permission.");
    26. }
    27. }
     
  9. Offline

    teej107

    Benpat9 That is because you have the code out of order. You check to see if it has 2 uses.
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().contains("2"))

    That if test returns true so all of the code in there is executed including setting the display name to have 1 use left. By the time your second check (the check for 1 use) comes around,
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().contains("1"))

    your display name already has been set to have 1 use left, therefore making that if statement true.

    You can fix this by making the second if statment and else if statement OR by switching the if statements around. I would prefer the first suggestion.
     
  10. Offline

    Benpat9

    teej107 ty i used the first suggestion and it worked, i was wondering if you could help me with my other problem of taking money when this made or would i have to take money using a command (e.g. the staff only works if you use /mage on it) or when first used :p
     
  11. Offline

    teej107

    Benpat9 I do not know what currency plugin you are using. Look up their API for hooking into that for your plugin or yes, use a command.
     
  12. Offline

    Si1lve3r

    Have anyone seen my post in the top? ^^
     
Thread Status:
Not open for further replies.

Share This Page