Adding potion effects not working with OnPlayerToggleSprint?

Discussion in 'Plugin Development' started by khave, Oct 25, 2013.

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

    khave

    I got some problems with my code here, and I can not, for the life of me, figure out how to fix it

    It gives me the error at the player.addpotioneffect. It says: The method addPotionEffect(PotionEffect) in the type LivingEntity is not applicable for the arguments (PotionEffect)

    package me.potion.effect;

    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerToggleSprintEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffectType;

    public class PotionEffect extends JavaPlugin{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static PotionEffect plugin;

    @Override
    public void onDisable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    }

    @Override
    public void onEnable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion());
    }

    @EventHandler(priority=EventPriority.MONITOR)
    public void onPlayerToggleSprint(PlayerToggleSprintEvent event) {
    if (event.isCancelled()) return;
    Player player = event.getPlayer();
    if (!player.hasPermission("potioneffect.use")){
    player.sendMessage(ChatColor.RED + "You do not have permission. PotionEffects plugin made by khave.");
    return;
    }
    if(event.isSprinting()) {
    event.setCancelled(true);
    player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 5, 2)); //Applies poison 3 for 5 ticks (1/4 of a second)
    } else {
    player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 1000, 2));
    player.removePotionEffect(PotionEffectType.SPEED);
    }
    }
    }

    Thank you in advance!
     
  2. Offline

    Garris0n

    1. The issue is that your class is called PotionEffect, so instead of importing org.bukkit.potion.PotionEffect, it's trying to create a new instance of your class. Rename your class to something else and import PotionEffect.
    2. Bukkit automatically prints enable messages, and either way you should use getLogger().info("").
    3. Don't listen to the event in MONITOR if you have any plans to change it in any way(you cancel it).
    4. Use code tags when posting code...

    ...and do you really have to put your name in the permission deny message?
     
  3. Offline

    khave


    xD. Thanks for the help. I actually used the code brackets at first, but it messed up horribly and then I just didn't bother.

    EDIT:
    It's still being a cake to me. Now it's just saying: "The method addPotionEffect(PotionEffect) in the type LivingEntity is not applicable for the arguments (Main)"
     
  4. Offline

    Garris0n

    You'll have to change the "PotionEffect" back in that line now, and import Bukkit's PotionEffect.
     
  5. Offline

    khave

    Hello.
    I've now fixed the code completely and it displays no errors, but when I test it ingame, nothing happens. Please help? :)

    Code:
    Code:
    package me.potion.effect;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerToggleSprintEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main  extends JavaPlugin{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Main plugin;
     
    @Override
    public void onDisable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info("");
    }
     
    @Override
    public void onEnable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info("");
    }
     
        public void onPlayerToggleSprint(PlayerToggleSprintEvent event) {
                if (event.isCancelled()) return;
                Player player = event.getPlayer();
                if (!player.hasPermission("potioneffect.use")){
    }
                       if(event.isSprinting()) {
                                event.setCancelled(true);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                        } else {
                       player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                                player.removePotionEffect(PotionEffectType.SPEED);
                        }
                }
        }
    
     
  6. Offline

    Garris0n

    Well first, you need to register your events and use the @EventHandler annotation. Also, don't use the minecraft logger, and when you want to log anything use getLogger().info("stuff");. Your plugin does a permission check for the permission "potioneffect.use", however this check doesn't actually do anything as you close it right afterwards and continue. Also, it adds the potion effect whether they start sprinting or not, but if they don't it removes it afterwards. Instead, try doing if(player.hasPotionEffect(PotionEffectType.SPEED) and then remove it if that's true. None of this has come into play yet because you didn't register your events or use the @EventHandler annotation so the method is never called, but once you fix it you'll have to fix those things.
     
  7. Offline

    khave

    Now I feel like I've done everything you told me to and when I join the game and try to sprint, I sprint for a few seconds and then I stop sprinting.
    Note: I have tried removing the event.setCancelled, and yes it did fix the not sprinting part, but it did not give me the speed effect.

    Code:

    Code:
    package me.potion.effect;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerToggleSprintEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main  extends JavaPlugin implements Listener{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Main plugin;
    PluginDescriptionFile pdfFile;
     
    @Override
      public void onDisable() {
           this.pdfFile = getDescription();
           this.getLogger().info(this.pdfFile.getName() + " PotionEffects is now disabled!");
       }
    @Override
       public void onEnable() {
           PluginManager pm = getServer().getPluginManager();
           this.pdfFile = getDescription();
           this.getLogger().info(this.pdfFile.getName() + " Version: "+ this.pdfFile.getVersion());
           Bukkit.getServer().getPluginManager().registerEvents(this, this);    
    }
     
    @EventHandler
        public void onPlayerToggleSprint(PlayerToggleSprintEvent event) {
     
                if (event.isCancelled()) return;
                Player player = event.getPlayer();
                if (!player.hasPermission("potioneffect.use")){
               return;
    }
                       if(event.isSprinting()) {
                                event.setCancelled(true);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                        } else {
                       player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1));
                                player.removePotionEffect(PotionEffectType.SPEED);
                        }
                       if (player.hasPotionEffect(PotionEffectType.SPEED) == true){
                     player.removePotionEffect(PotionEffectType.SPEED);
                       }
                }
        }
    
     
  8. Offline

    Garris0n

    The issue is that you're adding it and then checking if they have it, when what I meant to do was add the check for it in the else area. Also, if you cancel it, they'll never stop sprinting because they never, so just remove that. What I'm saying is pretty confusing, so here's some code, but please ask about it if you don't understand anything or you won't learn.




    Code:java
    1. @EventHandler
    2. public void onPlayerToggleSprint(PlayerToggleSprintEvent event){ //The event
    3.  
    4. if (event.isCancelled()) //if the event is cancelled, don't bother with anything.
    5. return;
    6.  
    7. Player player = event.getPlayer(); //get the player
    8.  
    9. if (!player.hasPermission("potioneffect.use")) //if they don't have the permission, just leave
    10. return;
    11.  
    12. if(event.isSprinting()) //if they're sprinting...
    13. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 1)); //...add the potion effect
    14.  
    15. else if(player.hasPotionEffect(PotionEffectType.SPEED) == true) //if they're not sprinting but have the potion effect...
    16. player.removePotionEffect(PotionEffectType.SPEED); //...remove it
    17.  
    18. }
    19.  
     
  9. Offline

    khave

    Thank you! It works perfectly now, and yes I do understand it all ^^.
     
Thread Status:
Not open for further replies.

Share This Page