Help with adding a potion effect to a player

Discussion in 'Plugin Development' started by CreeperFan12346, May 25, 2014.

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

    CreeperFan12346

    Hello. I've tried to make a plugin that changes the effects of a Golden Apple, but it doesn't work. All it does so far is add speed for 1 second. Here's my code:
    Code:java
    1. public class Candy extends JavaPlugin implements Listener{
    2.  
    3.  
    4. public void onEnable(){
    5. getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8.  
    9.  
    10. @EventHandler
    11. public void onPlayerItemConsume(PlayerItemConsumeEvent e){
    12. if(e.getItem().getType() == Material.GOLDEN_APPLE ){
    13.  
    14. }
    15. Player p = e.getPlayer();
    16. ItemMeta pm = (ItemMeta) e.getItem().getItemMeta();
    17. if(pm.getDisplayName().equalsIgnoreCase("Candy")){
    18. p.sendMessage(ChatColor.YELLOW + "You ate " + ChatColor.GOLD + "candy! Enjoy your effects. ;)");
    19. p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 10, 3));
    20. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 10));
    21. p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 0));
    22. }
    23. if(!p.hasPermission("candy.eat")){
    24. p.sendMessage(ChatColor.RED + "You are not Super+! Donate for it here " + ChatColor.GREEN + " [url]http://vontagemc.enjin.com/shop[/url]");
    25.  
    26.  
    27.  
    28. }
    29.  
    30. }
    31. }
    32.  
    Any help would be appreciated. Thanks!
     
  2. Offline

    rsod

    First of all, how does your item looks like? And where the code that creates apple?
    Secondary,
    Code:
    
    
    [LIST]
    
    
    [*]
    
    p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 10, 3));
    
    
    
    
    [*]
    
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 10));
    
    
    
    
    [*]
    
    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 0));



    [*]will be almost unnoticiable, since duration is in ticks. 20 ticks = 1 second, 10 ticks = 0,5 sec, so you giving effects for less then 0.5 seconds.


    [/LIST]

    p.s. I'm not going to fix large spaces in my posts anymore. It's this forum's bug and not my problem.

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

    CreeperFan12346

    It will just be a golden apple. Also, it doesn't create an apple. You have to manually get an apple.
     
  4. Offline

    TheOatBaron

    CreeperFan12346
    Try to organize things better, I had to do a lot of reworking.
    Code:java
    1. @EventHandler
    2. public void onPlayerItemConsume(PlayerItemConsumeEvent e){
    3. if(e.getItem().getType().equals(Material.GOLDEN_APPLE) && e.getItem().hasItemMeta() && e.getItem().getItemMeta().hasDisplayName() && e.getItem().getItemMeta().getDisplayName().equals("Candy")){
    4. Player p = e.getPlayer();
    5. if(p.hasPermission("candy.eat")){
    6. p.sendMessage(ChatColor.RED + "You are not Super+! Donate for it here " + ChatColor.GREEN + "[url]http://vontagemc.enjin.com/shop[/url]");
    7. e.setCancelled(true);
    8. }else{
    9. p.sendMessage(ChatColor.YELLOW + "You ate " + ChatColor.GOLD + "candy! Enjoy your effects.");
    10. p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 10, 3));
    11. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 10));
    12. p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 0));
    13. }
    14. }
    15. }
     
  5. Offline

    Gater12

    CreeperFan12346
    Second parameter of contructor of PotionEffect is time in ticks. So it's really acting for half a second.
     
  6. Offline

    CreeperFan12346

    Oh.

    I've tried a lot. Still doesn't work.

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

    CoderMusgrove

    Are you changing any of your code at all? Take in part as they have said, the 2nd parameter represents the server ticks. 20 equals one second. So if you want speed to last for 10 seconds, do this:
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10 * 20, 10));
     
  8. Offline

    AoH_Ruthless

    CreeperFan12346
    Cancel the event and then add the potion effects and remove the golden apple from the player's inventory.
     
  9. Offline

    CreeperFan12346

    How would I do that? (I'm a beginner at java)
     
  10. Offline

    CreeperFan12346

    Bump

    I've tried it, it doesn't work.

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

    CoderMusgrove

    I think I found your issue. You make an if statement checking if it's a golden apple that was consumed, but you don't put anything inside of it. Instead, you put it outside of the if statement, and it doesn't get ran.
    Code:java
    1. if(e.getItem().getType() == Material.GOLDEN_APPLE ){
    2.  
    3. }

    Throw all of your code into that if statement.
     
  12. Offline

    CreeperFan12346

    I tried that as well. Still doesn't work.
     
  13. Offline

    CoderMusgrove

    Does the display name of the golden apple equal any capitalization variation of Candy? That is literally my last guess (for now).
     
  14. Offline

    CreeperFan12346

    Nope.
     
  15. Offline

    Prosfis

    Is the name of the golden apple colored? Is the name of the golden apple surely "candy". You said that you do not create the item, but they have to get it. The item won't just name itself. If the name is colored, then that must be taken into account when comparing the current item with "Candy".
     
  16. Offline

    redytedy

    Assuming they have an apple with the exact name "Candy", then do the code TheOatBaron did, but swap what is inside the if and else statements, and fix the effects. It should work.

    Code:java
    1. @EventHandler
    2. public void onPlayerItemConsume(PlayerItemConsumeEvent e){
    3. if(e.getItem().getType().equals(Material.GOLDEN_APPLE) && e.getItem().hasItemMeta() && e.getItem().getItemMeta().hasDisplayName() && e.getItem().getItemMeta().getDisplayName().equals("Candy")){
    4. Player p = e.getPlayer();
    5. if(p.hasPermission("candy.eat")){
    6. p.sendMessage(ChatColor.YELLOW + "You ate " + ChatColor.GOLD + "candy!"+ ChatColor.YELLOW+ " Enjoy your effects.");
    7. p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 3, 200));
    8. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 200));
    9. p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 1, 200));
    10.  
    11. }else{
    12. p.sendMessage(ChatColor.RED + "You are not Super+! Donate for it here " + ChatColor.GREEN + "[url]http://vontagemc.enjin.com/shop[/url]");
    13. e.setCancelled(true);
    14. }
    15. }
    16. }


    ^ Use that code
     
Thread Status:
Not open for further replies.

Share This Page