How to add a warning to use again?

Discussion in 'Plugin Development' started by NewMasterBR, Oct 2, 2021.

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

    NewMasterBR

    Code:
        @EventHandler
        public void onFly(PlayerInteractEvent e) {
            Player p = e.getPlayer();
          
            if (e.getPlayer().getItemInHand().getType().equals(Material.FEATHER) && e.getPlayer().getItemInHand().hasItemMeta() &&
                    e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("phantom") && (
                    e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))) {
                if(cooldowns.containsKey(p.getName())) {
                    if (cooldowns.get(p.getName()) >= System.currentTimeMillis()){
                        long remainingTime = cooldowns.get(p.getName()) - System.currentTimeMillis();
                        p.sendMessage("You are on cooldown wait " + (TimeUnit.MILLISECONDS.toSeconds(remainingTime) + 1) + " seconds!");
                    return;
                    } else {
                        cooldowns.remove(p.getName());
                      
                    }
                }
                cooldowns.put(p.getName(), System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10));
    
                p.setAllowFlight(true);
                p.setFlying(true);
                Vector vector = p.getLocation().getDirection().setY(0.85D);
                p.setVelocity(vector);
                p.getWorld().playSound(p.getLocation(), Sound.ENTITY_WITHER_DEATH, 2.0F, 1.0F);
                p.sendMessage("debug");
              
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.sendMessage("§bYour skill ends in 3 seconds!");
                    }
                }.runTaskLater(plugin, 20 * 2);
              
                new BukkitRunnable() {
    
                    @Override
                    public void run() {
                        p.sendMessage("§bYour skill ends in 2 seconds!");
                    }
                }.runTaskLater(plugin, 20 * 3);
              
                new BukkitRunnable() {
    
                    @Override
                    public void run() {
                        p.sendMessage("§bYour skill ends in 1 second!");
                    }
                }.runTaskLater(plugin, 20 * 4);
    
                new BukkitRunnable() {
    
                    @Override
                    public void run() {
                        p.setFlying(false);
                        p.setAllowFlight(false);
                        p.sendMessage("§cWait for the cooldown time to use it again!");
                        p.getWorld().playSound(p.getLocation(), Sound.ENTITY_WITHER_SPAWN, 2.0F, 1.0F);
                      
                    }
                }.runTaskLater(plugin, 20 * 5);
                return;
            }
          
        } 
    Everything is going well with the code but I wanted to implement one last thing in the cooldown. I would like to get a "ready to use again" message as soon as I am removed from the cooldown. I've tried to fit Runnable but it didn't work :(
     
  2. Offline

    Kars

    You literally have the code already that messages the person 1 second before cooldown ends. How can you not figure out how to message them a second after that?
     
    davidclue likes this.
  3. Offline

    NewMasterBR

    I tried to fit another Runnable to let him know that the cooldown wait is over without the player clicking on the item to find out the time, but it didn't work. I can't see where to put the code
     
  4. Offline

    Kars

    @NewMasterBR you have 4 of these. Just add another one.
    Can't go wrong.
    PHP:
    new BukkitRunnable() {
        @
    Override
        
    public void run() {
            
    p.sendMessage("§bYour skill ends in 3 seconds!");
        }
    }.
    runTaskLater(plugin20 2);

    new 
    BukkitRunnable() {

        @
    Override
        
    public void run() {
            
    p.sendMessage("§bYour skill ends in 2 seconds!");
        }
    }.
    runTaskLater(plugin20 3);

    new 
    BukkitRunnable() {

        @
    Override
        
    public void run() {
            
    p.sendMessage("§bYour skill ends in 1 second!");
        }
    }.
    runTaskLater(plugin20 4);

    new 
    BukkitRunnable() {

        @
    Override
        
    public void run() {
            
    p.setFlying(false);
            
    p.setAllowFlight(false);
            
    p.sendMessage("§cWait for the cooldown time to use it again!");
            
    p.getWorld().playSound(p.getLocation(), Sound.ENTITY_WITHER_SPAWN2.0F1.0F);
         
        }
    }.
    runTaskLater(plugin20 5);
     
  5. Offline

    Tim_M

    This seems extremely inefficient. Its enough to just do something like this:
    Code:
    //HAVEN'T TESTED
    void count(int i)
    {
       if (i <= 0)
          return;
       Bukkit.getScheduler().runTaskLater(Main.instance, () ->{
       switch (i)
       {
          case 5: {//TODO 5 seconds left
          }
          case 4: {//TODO 4 seconds left
          }
          case 3: {//TODO 3 seconds left, ETC
          }
       }
    
       count(i - 1);
       }, 20L); //<- oops forgot ;
    }
    
    It would be especially good, because you wouldn't have so much code in your @EventHandler.
     
    Last edited: Oct 8, 2021
  6. Offline

    Kars

    Yea, the way you are doing it there won't work at all.
     
  7. Offline

    Tim_M

    I disagree, I used a similiar approach in my plugins and it works > : (
    Point is creating multiple schedules in a row can be shortened to a single function.

    PS. @Kars, I tested that exact thing and it works, runTaskLater does in fact work with lambda expressions if thats what you're going at.
     
    Last edited: Oct 6, 2021
  8. Offline

    Kars

    @Tim_M you are right. I missed the recursive part of your function.

    You still use if/else blocks for each amount of seconds, though. Should probably make that dynamic also.
     
  9. Offline

    Tim_M

    Fair enough, case switch is faster. Although if you wanted to make the fastest plugin you'd make them in C ;)
     
Thread Status:
Not open for further replies.

Share This Page