setting conditions certain amount of time

Discussion in 'Plugin Development' started by bumppy5, Sep 30, 2014.

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

    bumppy5

    Lets say I want to make it so that someone can fly, but for only 30 seconds. I cannot figure out how to set the amount of time that the player is able to do it.


    Lets say that the fly code is:
    Code:
    player.setFlyAllow(true);
    How would I make it so that the player can fly for 30 seconds?
     
  2. Offline

    blobic123

    bumppy5
    Use http://wiki.bukkit.org/Scheduler_Programming & Map <Player, BukkitTask> to enable the fly then 30 seconds later disable the fly.

    An example:

    Your main class:
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public Map <Player, BukkitTask> flyingPlayers;
    4.  
    5. //Use setFly(Player, 30);
    6. public void setFly (Player player, Integer seconds) {
    7. seconds = seconds * 20; //Converting seconds to ticks
    8. player.setFlyAllow(true); //Enabling Fly
    9. BukkitTask task = new FlyingTimer(this,plugin, player).runTaskLater(this.plugin, seconds); //Creating Timer
    10. }
    11.  
    12. public void cancelTimer (Player player) {
    13. if (flyingPlayers.containsKey(player)) {
    14. flyingPlayers.get(player).cancel();
    15. }
    16. }
    17. }


    FlyTimer (A different class):
    Code:java
    1. public class FlyTimer extends BukkitRunnable {
    2.  
    3. private final JavaPlugin plugin;
    4. private final Player player;
    5.  
    6. public FlyTimer(JavaPlugin plugin, Player player) {
    7. this.plugin = plugin;
    8. this.player = player;
    9. }
    10.  
    11. @Override
    12. public void run() {
    13.  
    14. this.player.setFlyAllow(false);
    15. }
    16.  
    17. }
     
Thread Status:
Not open for further replies.

Share This Page