Allow fly for 5 seconds?

Discussion in 'Plugin Development' started by DrTURTLE2, Aug 24, 2013.

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

    DrTURTLE2

    This is code so far, stuck now though.

    Code:java
    1. @EventHandler
    2. public void Hawk(PlayerInteractEvent e){
    3. final Player player = (Player) e.getPlayer();
    4. if (Hawk.contains(player.getName()))
    5.  
    6.  
    7. if (e.getItem() != null)
    8. if (e.getItem().getType() == Material.FEATHER && e.getAction() == Action.RIGHT_CLICK_AIR) {
    9. if (players.contains(player.getName())) {
    10. player.sendMessage(ChatColor.RED + "You cannot fly yet!");
    11. } else {
    12. players.add(player.getName());
    13. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 100, 0));
     
  2. Offline

    naorpeled

    Code:java
    1.  
    2. p.setFly(true);
    3. getServer().getSchedualer.AsyncDelayedTask(this, new Runnable(){
    4. public void run()
    5. {
    6. p.setFly(false);
    7. }
    8. },50L)//I`m not sure about the 50.
    9.  

    I didnt test it but I`m sure that`s it.
     
  3. Offline

    DrTURTLE2

  4. Offline

    naorpeled

    Sec. are you making ktis?
     
  5. Offline

    DrTURTLE2


    yes.
     
  6. Offline

    naorpeled

    Code:java
    1.  
    2. p.setFlying(true);
    3. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    4. public void run()
    5. {p.setFlying(false);}
    6. },100L);
    7.  

    Here you go :p
     
    DrTURTLE2 likes this.
  7. Offline

    DrTURTLE2

  8. Offline

    Ultimate_n00b

    Why use a Runnable? Instead use a bukkit runnable:

    Code:java
    1. p.setFlying(true);
    2. new BukkitRunnable() {
    3. @Override
    4. public void run() {
    5. p.setFlying(false);
    6. }
    7. }.runTaskTimer(plugin, 100L);
     
  9. Offline

    naorpeled

    @DrTURTLE2 if you`ll need more help with your kits plugin, contact me :D
     
  10. Offline

    Plo124

    Ultimate_n00b
    That code will probably stop flying for the wrong person, because if 2 players try to fly at the same time, it will change the player variable, and the one who tried to fly first gets to keep fly.
     
  11. Offline

    Samthelord1

  12. Offline

    Ultimate_n00b

    What are you talking about..
     
  13. Offline

    Plo124

    If player A, and play B are on the server, and player A right clicks the fly tool, p is now set to player A, and in 5 seconds, player A will have his flight removed. Player B right clicks the fly tool, and wow, you just changed player A inside the runnable to disable player B's fly, and thus not disabling player A, instead of making a new runnable to disable player B
     
  14. Offline

    Ultimate_n00b

    But p is just a player inside the method :/ He changes every time the event is called
     
    tommycake50 likes this.
  15. Offline

    DrTURTLE2

    Ultimate_n00b Samthelord1

    This is code now, its super buggy, sometimes it allows fly, sometimes it doesnt. Sometimes it gives strength, sometimes is doesnt. I dont even...

    Code:java
    1. @EventHandler
    2. public void Hawk(PlayerInteractEvent e){
    3. final Player player = (Player) e.getPlayer();
    4. if (Hawk.contains(player.getName()))
    5.  
    6.  
    7. if (e.getItem() != null)
    8. if (e.getItem().getType() == Material.FEATHER && e.getAction() == Action.RIGHT_CLICK_AIR) {
    9.  
    10. if (players.contains(player.getName())) {
    11. player.sendMessage(ChatColor.RED + "You are on cooldown!");
    12. } else {
    13.  
    14. players.add(player.getName());
    15.  
    16. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 400, 0));
    17. new BukkitRunnable() {
    18. public void run() {
    19. players.remove(player.getName());
    20. player.sendMessage(ChatColor.GREEN + "You can now fly!");
    21. }
    22. }.runTaskLater(this, 400);
    23.  
    24.  
    25. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    26. public void run(){
    27. player.setAllowFlight(true);
    28. player.setFlying(true);
    29. player.addPotionEffect(new PotionEffect (PotionEffectType.INCREASE_DAMAGE, 100, 0));
    30. } },100L);
    31. }
    32. }
    33. }
    34.  
    35.  
    naorpeled
     
  16. Offline

    Ultimate_n00b

    Then debug the code? Do little tests to see why something isn't working.
     
  17. Offline

    DrTURTLE2

  18. Offline

    MgMaor

    Code:java
    1. void fly(final Player player){
    2. player.setFlying(true);
    3. Bukkit.getScheduler.scheduleSyncDelayedTask(this, new Runnable(){
    4. public void run(){
    5. player.setFlying(false);
    6. }
    7. }, 100);
    8. }
     
  19. Offline

    Ultimate_n00b

    He already has the code to do flying, and AGAIN just use a BukkitRunnable..
     
  20. Offline

    MrlolDestructo

    Ah I see where you're going with this. I had to do this sorta thing recently for a KitPVP server. I think this should work for you:
    Code:java
    1. @EventHandler
    2. public void Hawk(PlayerInteractEvent e) {
    3. if(Hawk.contains(p.getName())) {
    4. if(p.getItemInHand().getType() == Material.FEATHER) {
    5. if(!(players.contains(p.getName()))) {
    6. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 100, 0));
    7. p.setAllowFlight(true);
    8. players.add(p.getName());
    9. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    10. @Override
    11. public void run() {
    12. p.setAllowFlight(false);
    13. p.setFlying(false);
    14. }
    15. }, 100L);
    16. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    17. @Override
    18. public void run() {
    19. players.remove(p.getName());
    20. }
    21. }, /**Cooldown to let player fly again in ticks*/);
    22. } else {
    23. p.sendMessage(ChatColor.RED + "You cannot fly yet!");
    24. }
    25. }
    26. }
    27. }

    Untested, but should work. :) Hope that helps.
     
  21. Offline

    naorpeled

    Why did u give the strength effect in the schedualer? just check if the cooldown is over and if it is give them strength and flight and with schdeualer remove the flight.
     
  22. Offline

    DrTURTLE2

    MrlolDestructo

    Thanks, testing now :)

    MrlolDestructo

    Getting an error on the runtaskLater(plugin,

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

    tommycake50

    ... whether you use BukkitRunnable or not it makes no difference.
    BukkitRunnable just adds the cancel() method.
     
  24. Offline

    MrlolDestructo

    Have you made a plugin variable, and is it initialized?
     
Thread Status:
Not open for further replies.

Share This Page