How do I make a timer

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

  1. Offline


    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. Online

    timtower Moderator Moderator

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


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

    timtower Moderator Moderator

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


    Try this :

    First the API:

    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() {
        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);
    Than the commands:
    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;
    public class Command_Timer implements CommandExecutor{
        private static Map<UUID, Timer> timers = Maps.newHashMap();
        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();
                player.sendMessage("§bTimer started !");
                timers.put(player.getUniqueId(), timer);
                return true;
            if(args.length == 1 && args[0].equalsIgnoreCase("stop")) {
                Timer timer = timers.get(player.getUniqueId());
                player.sendMessage("§bTimer stopped !");
                return true;
            if(args.length == 1 && args[0].equalsIgnoreCase("pause")) {
                Timer timer = timers.get(player.getUniqueId());
                player.sendMessage("§bTimer paused !");
                return true;
            if(args.length == 1 && args[0].equalsIgnoreCase("resume")) {
                Timer timer = timers.get(player.getUniqueId());
                player.sendMessage("§bTimer Continue!");
                return true;
            player.sendMessage("§cUse: /timer <start | stop | pause | resume>");
            return true;
  6. Online

    timtower Moderator 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. is undefined in this scope.

Share This Page