HELP PLZ i need a way of making it so when you sprint you get the potion buff confused after running

Discussion in 'Spigot Plugin Development' started by dmb219910, Jun 11, 2016.

  1. Hey i need a way of making it so you get potion effect type confusion after 15 seconds of sprinting i tried adding a void run but that didnt work because i cant add extends java plugin to a listener is there a way for the timer to start in my main class when the player starts sprinting? My classes
    Code (Text):
    package me.dmb219910;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerToggleSprintEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;



    public class PlayerListener implements Listener{

        public PlayerListener(StartUp plugin) {

            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }

        @EventHandler
        public void OnSprint (PlayerToggleSprintEvent e ) {

            Player player = e.getPlayer();

            player.addPotionEffect((new PotionEffect(PotionEffectType.CONFUSION, 200, 10)));


        }


    }
     
    Code (Text):
    package me.dmb219910;

    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class StartUp extends JavaPlugin {

        @Override
        public void onEnable() {
        new PlayerListener(this);
        getLogger().info(ChatColor.GREEN + "Spring Effects HAVE BEEN SUCCESFULLY ENABLED!");



        }

            @Override
            public void onDisable() {

                getLogger().info(ChatColor.GREEN + "Spring Effects HAVE BEEN SUCCESFULLY DISABLED!");


            }

    }
     
  2. This is how I would do it.

    Code (Text):
    @EventHandler
        public void OnSprint (PlayerToggleSprintEvent e )
        {
            Player player = e.getPlayer();
            new BukkitRunnable()
            {
                @Override
                public void run() {
                    if (player.isSprinting())
                    {
                         player.addPotionEffect((new PotionEffect(PotionEffectType.CONFUSION, 200, 10)));
                    }
                }
            }.runTaskLater(this, 15 * 20);
        }
     
  3. That wont work i said that javaplugin extenstion only works on main class and not listener
     
  4. Then you need to pass an instance of your StartUp class. You can do this with either a getter in the StartUp class or by using the instance you pass to the PlayerListener constructor(Store it in a variable, call that variable).
     
  5. I would suggest, instead of using a runnable,
    use a long time stamp inside of a class that
    keeps track of this information for each player.

    (Above constructor)
    private long fifteensecondsfromnow = (System.currenttimemillis()+15000);

    Register PlayerMoveEvent
    Code (Text):
    @EventHandler
    public void move(PlayerMoveEvent e){
        Player p = e.getPlayer();
        if(!p.isSprinting()){
         // Keep updating this when the player is not sprinting
            fifteensecondsfromnow = (System.currenttimemillis()+15000);
        } else {
            if(System.currenttimemillis() >= fifteensecondsfromnow){
            //Give Confusion to the player.
        }
    }

    }
    You know, instead if having lots of runnables
    going if you have a high player base. ;)
     
  6. That's what I do, because you can't extend the main class.