[WIP]Speed effect on right click

Discussion in 'Plugin Development' started by Asticoli_express, Jul 18, 2013.

Thread Status:
Not open for further replies.
  1. Hello all, i am a brandly-new in java, and as a first plugin i wanted to make a dodging plugin which increases speed on right click. There are no "Errors" in the code but the plugin doesn't work on my server, please help me. This is the code :
    package mov;
    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class mov extends JavaPlugin{
    public void onPlayerInteract(PlayerInteractEvent event) {
    Player player = null;
    //RIGHTCLICK
    if(event.getAction() == Action.RIGHT_CLICK_AIR){
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4, 2));
    }
    }
    }
     
  2. Offline

    blablubbabc

    This would fit better in the default development section.

    However, you are missing some basics here:
    * your class isn't implementing Listener
    * you are not registering your Listener class in onEnable()
    * you will get a NullPointerExcpetion because player is null inside your event -> use event.getPlayer() instead
     
    1Rogue likes this.
  3. Offline

    TnT

    Moved to the appropriate forum.

    You should use
    Code:
    [code]
    tags. It makes your code more readable.
     
  4. Offline

    fanaticmw2

    Also, If I were you, I would add || event.getAction() == Action.RIGHT_CLICK_BLOCK.
     
  5. Offline

    ASHninja1997

    Asticoli_express
    The reason why its not working is because you need to define player. If the player is null then it can't find the play to add the potion effect to. Use this code.
    Code:
        Player player = e.getPlayer();
     
  6. I added all what you told me, there are no "Errors" but still doesn't work :
    Code:
    package mov;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class mov extends JavaPlugin {
        public void onDisable() {}
        public void onEnable() {
     
            class listener implements Listener {
                public void onPlayerInteract(PlayerInteractEvent event) {
                    Player player = event.getPlayer();
                    //RIGHTCLICK
                    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4, 2));
                    }
                }
            } 
        }
    }
     
  7. Offline

    themuteoneS

    1. Since you have your listener in another class you have to register your listener in onEnable() as so:
      Code:
      getServer().getPluginManager().registerEvents(new listener(), this);
    2. ALL of the events need to have a @EventHandler tag!
    3. I beg you to not have classes inside your onEnable() (or any function for that matter). Move class listener out of onEnable() and into the main class
    I recommend reading this: http://wiki.bukkit.org/Event_API_Reference
     
  8. this is what it looks like now, tell me if i am right
    Code:
    package mov;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class mov extends JavaPlugin implements Listener{
            public void onDisable() {}
            public void onEnable() {
                getServer().getPluginManager().registerEvents(new listener(), this);
                @EventHandler
                public void onPlayerInteract(PlayerInteractEvent event) {
                    Player player = event.getPlayer();
                    //RIGHTCLICK
                    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4, 2));
                    }
                }
            } 
        }
    
    Error ';' expected at line 18 (( public void onPlayerInteract(PlayerInteractEvent event) { ))

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

    kingstar64

    I think this works.

    Code:
    package mov;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class mov extends JavaPlugin implements Listener{
        public void onDisable() {
               
        }
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            //RIGHTCLICK
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4, 2));
            }
        }
    }
     
  10. Thanks, the are no compiler Errors now but the plugin still doesn't work :(
     
  11. Offline

    Techy4198

    let's see your plugin.yml
     
  12. plugin.yml :

    Code:
    name: mov
    main: mov
    version: 1
    author: asticoli_express
    
     
  13. Offline

    kingstar64

    You need to make a plugin.yml to let it work.
     
  14. i found the problem! it was a wrong path. Thanks to all of you, again and again i am so happy it works :)
     
  15. Offline

    Techy4198

  16. The plugin works now :)
     
Thread Status:
Not open for further replies.

Share This Page