How do I make a timer

Discussion in 'Plugin Development' started by MJ535, Oct 4, 2020.

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

    MJ535

    How do I create a timer in a plugin?

    When the player types "/tenmc start," it will display a message and it will start a counting up timer. When the player types "/tenmc stop," it will stop the timer and display what the timer is at. The timer can also be triggered when a variable equals ten. How do I do this?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MJ535 Store the current time on start.
    Check the difference between current time and time from start on stop.
     
  3. Offline

    MJ535

    Would I use (System.currentTimeMillis() / 1000) and then compare the times in seconds?
     
    Last edited: Oct 4, 2020
  4. Offline

    timtower Administrator Administrator Moderator

    Get the difference in the normal unit, then convert to whatever you want.
     
  5. Offline

    Zettos

    Try this :

    First the API:

    Code:
    package de.worldextras.timer;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    
    import net.minecraft.server.v1_8_R3.IChatBaseComponent;
    import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
    
    public class Timer {
    
        private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(0);
        private boolean paused;
        private int elapsedTime;
       
       
        public void start(Player player) {
            executorService.scheduleAtFixedRate(() -> {
               
                if(!paused) {
                    elapsedTime += 100;
                }
               
               
               
                int hours = (elapsedTime / 3600000);
                int minutes = (elapsedTime / 60000) % 60;
                int seconds = (elapsedTime / 1000) % 60;
               
               
                StringBuilder message = new StringBuilder("§7Timer§8: §6§l");
                message.append(String.format("%02d", hours)).append(":");
                message.append(String.format("%02d", minutes)).append(":");
                message.append(String.format("%02d", seconds));
               
               
                if(paused) {
                    message.append(" §c(pausiert)");
                }
               
                sendActionBar(player, message.toString());
            }, 0, 100, TimeUnit.MILLISECONDS);
        }
       
        public void stop() {
            executorService.shutdownNow();
        }
       
        public void pause() {
            this.paused = true;
        }
       
        public void resume() {
            this.paused = false;
        }
       
       
        private void sendActionBar(Player player, String message) {
              PacketPlayOutChat packet = new PacketPlayOutChat(
                      IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + message + "\"}"), (byte) 2);
              ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packet);
            }
       
    }
    
    Than the commands:
    Code:
    package de.worldextras.timer;
    
    import java.util.Map;
    import java.util.UUID;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.google.common.collect.Maps;
    
    
    
    public class Command_Timer implements CommandExecutor{
       
        private static Map<UUID, Timer> timers = Maps.newHashMap();
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
            if(!(sender instanceof Player)){
                sender.sendMessage("§cYou have to be a Player");
                return true;
            }
            Player player = (Player)sender;
           
            if(!player.hasPermission("WorldExtras.timer")) {
                player.sendMessage("§cNo Permissions");
                return true;
            }
           
            if(args.length == 1 && args[0].equalsIgnoreCase("start")) {
                Timer timer = new Timer();
                timer.start(player);
                player.sendMessage(Cache.pr+"§bTimer started !");
                timers.put(player.getUniqueId(), timer);
                return true;
               
               
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("stop")) {
                Timer timer = timers.get(player.getUniqueId());
                timer.stop();
                player.sendMessage(Cache.pr+"§bTimer stopped !");
                return true;
               
               
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("pause")) {
                Timer timer = timers.get(player.getUniqueId());
                timer.pause();
                player.sendMessage(Cache.pr+"§bTimer paused !");
                return true;
               
               
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("resume")) {
                Timer timer = timers.get(player.getUniqueId());
                timer.resume();
                player.sendMessage(Cache.pr+"§bTimer Continue!");
                return true;
            }
           
            player.sendMessage(Cache.pr+"§cUse: /timer <start | stop | pause | resume>");
           
            return true;
        }
    
    }
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Zettos Missing a lot of things there to get it working though.
    You are using NMS for things, not needed for this.
    You assume that the timer is perfect.
    You are not checking if somebody has a timer.
    Cache.pr is undefined in this scope.
     
Thread Status:
Not open for further replies.

Share This Page