Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created October 29, 2012 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadnk/3976879 to your computer and use it in GitHub Desktop.
Save aadnk/3976879 to your computer and use it in GitHub Desktop.
Count down in the MOTD field.
package com.comphenix.example;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.lang.time.DateUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class CountdownMod extends JavaPlugin implements Listener {
// The time the game starts
private Date launchTime;
@Override
public void onEnable() {
PluginManager manager = getServer().getPluginManager();
manager.registerEvents(this, this);
// Start the game in one hour.
launchTime = DateUtils.addHours(new Date(), 1);
}
@EventHandler
public void onServerListPing(ServerListPingEvent event) {
Date currentTime = new Date();
DateFormat formatter = SimpleDateFormat.getTimeInstance();
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
// See if the game has event started
if (launchTime == null) {
event.setMotd("Game has not started.");
} else {
// Note: Doesn't take into account leap seconds, ect.
long milliDelta = launchTime.getTime() - currentTime.getTime();
Date dateDelta = new Date(Math.abs(milliDelta));
if (milliDelta > 0) {
event.setMotd("Game will start in " + formatter.format(dateDelta));
} else {
// You can also print the duration of the game
//event.setMotd("Game has been running for " + formatter.format(dateDelta));
event.setMotd("Game has started.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment