Skip to content

Get with the times

Hempfest edited this page May 19, 2021 · 2 revisions

How much time has passed?

public final class ExamplePlugin extends BukkitCommand {

		protected ExamplePlugin() {
			super("test");
		}

		private final ConcurrentMap<UUID, Date> playerTime = new MapMaker().weakKeys().weakValues().makeMap();

		@Override
		public boolean execute(CommandSender sender, String commandLabel, String[] args) {

			if (!(sender instanceof Player)) {
				return true;
			}

			Player p = (Player) sender;
			Message msg = new Message(p);
			if (args.length == 0) {
				if (!playerTime.containsKey(p.getUniqueId())) {
					playerTime.put(p.getUniqueId(), new Date());
				}
			}

			if (args.length == 1) {
				if (args[0].equalsIgnoreCase("check")) {
					if (playerTime.containsKey(p.getUniqueId())) {
						if (TimeUtils.isHoursSince(playerTime.get(p.getUniqueId()), 2)) {
							msg.send("2 or MORE hours has passed");
						} else {
							msg.send("2 hours has NOT passed");
						}
						if (TimeUtils.isMinutesSince(playerTime.get(p.getUniqueId()), 5)) {
							msg.send("5 or MORE minutes has passed");
						} else {
							msg.send("5 minutes has NOT passed");
						}
						if (TimeUtils.isSecondsSince(playerTime.get(p.getUniqueId()), 25)) {
							msg.send("25 or MORE seconds has passed");
						} else {
							msg.send("25 seconds has NOT passed");
						}
					}
					return true;
				}
			}

			return false;
		}
	}

Time difference. (TimeWatch)

public class TestClass {

	private final Date date;
	
	public TestClass() {
		this.date = new Date(System.nanoTime());
	}
	
	public boolean isBetween(TimeUnit unit, long time) {
		return TimeWatch.start(date.getTime()).hasElapsed(unit, time);
	}

}
	public void t() {
		TestClass c = new TestClass();
		if (c.isBetween(TimeUnit.SECONDS, 5)) {
			JavaPlugin pl = JavaPlugin.getProvidingPlugin(getClass());
			pl.getLogger().info("- Yo! The time is correct, were spot on.");
		}
	}

Utility "TimeUtils.java"

Clone this wiki locally