Source Code of Java Class with Developer Comments
// Declares the Package and all Imports
package de.craiz.uptime;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
// Declares JavaPlugin as Super Class and makes this class a Command Executor
public class UpTime extends JavaPlugin implements CommandExecutor {
// Command Method:
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// Checks if the command is uptime.
// If not: Do Nothing
if(cmd.getName().equalsIgnoreCase("uptime")) {
// Checks if the Sender (Console or Player) has Permission to execute this command
if(sender.hasPermission("xuptime.see") | sender.hasPermission("xuptime.started")) {
// Checks if the commands has any arguments
if(args.length == 0) {
// Checks if the Sender has Permission to see the UpTime
if(sender.hasPermission("xuptime.see")) {
// Calculates for how long (in millis) the server has been running by simply doing
// Time Now (in millis) (since 1970) - Time when the Server started (in millis) (since 1970)
long on = System.currentTimeMillis() - stat;
// Creates a new SimpleDateFormat Instance with the Pattern:
// Hours : Minutes : Second
// e.g.: 11:35:23
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
// Sets the TimeZone
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
// Formats for the long defined above and splits it with : as regex
String[] df = sdf.format(new Date(on)).split(":");
// This String is representing the Hour:
String h = df[0];
// This String is representing the Minute:
String m = df[1];
// This String is representing the Second:
String s = df[2];
// Formats a String, replaces all valid placeholders and sends it to the Sender
String msg = uptime.replace("{h}", h).replace("{m}", m).replace("{s}", s);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
} else {
// Sets the Default TimeZone (To avoid issues)
TimeZone.setDefault(TimeZone.getTimeZone(timezone));
// Creates a new SimpleDateFormat Instance with the Pattern:
// Day : Month : Year : Hour : Minute : Second : Milli Second
// e.g.: 21:12:2016:08:58:25:285
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy:HH:mm:ss:SSS");
// Sets the TimeZone (Again to avoid issues)
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
// Formats for time (in millis) (since 1970) when the server started and splits it with : as regex
String[] df = sdf.format(new Date(stat)).split(":");
// This String is representing the Day:
String d = df[0];
// This String is representing the Month:
String m = df[1];
// This String is representing the Year:
String y = df[2];
// This String is representing the Hour:
String h = df[3];
// This String is representing the Minute:
String min = df[4];
// This String is representing the Second:
String sec = df[5];
// This String is representing the Milli Second
String ms = df[6];
// This String is representing the TimeZone
String timezone = this.timezoner;
// Formats a String, replaces all valid placeholders and sends it to the sender
String msg = this.started.replace("{d}", d).replace("{m}", m).replace("{y}", y).replace("{h}", h).replace("{min}", min).replace("{sec}", sec).replace("{ms}", ms).replace("{timezone}", timezone);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
}
// Checks if the Command has any arguments
} else if(args.length == 1) {
// Checks if the Commands Argument is started
if(args[0].equalsIgnoreCase("started")) {
// Checks if the Sender has Permission for this Command
if(sender.hasPermission("xuptime.started")) {
// See above:
TimeZone.setDefault(TimeZone.getTimeZone(timezone));
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy:HH:mm:ss:SSS");
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
String[] df = sdf.format(new Date(stat)).split(":");
String d = df[0];
String m = df[1];
String y = df[2];
String h = df[3];
String min = df[4];
String sec = df[5];
String ms = df[6];
String timezone = this.timezoner;
String msg = this.started.replace("{d}", d).replace("{m}", m).replace("{y}", y).replace("{h}", h).replace("{min}", min).replace("{sec}", sec).replace("{ms}", ms).replace("{timezone}", timezone);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
} else {
// See above:
long on = System.currentTimeMillis() - stat;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
String[] df = sdf.format(new Date(on)).split(":");
String h = df[0];
String m = df[1];
String s = df[2];
String msg = uptime.replace("{h}", h).replace("{m}", m).replace("{s}", s);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
}
}
} else {
// This happens if the Command was not executed correctly
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', usage));
}
} else {
// This happens if the Sender had not enough Permissions
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', noperm));
}
}
// Returns true so no Usage Message is printed by Bukkit
return true;
}
// This happens on Startup:
public void onEnable() {
// Loads the Config:
loadConfig();
// Saves the Config Values into Variables:
readConfig();
// Sets this Class as the Executor of UpTime
this.getCommand("uptime").setExecutor(this);
// Declares the StartUp Time (in millis) (since 1970)
stat = System.currentTimeMillis();
// Sets the Default TimeZone (To avoid issues)
TimeZone.setDefault(TimeZone.getTimeZone(timezone));
}
// This Long is representing the StartUp Time (in millis) (since 1970)
private long stat = 0L;
// This String is representing the Help Url
// Useless since it's literally final and used by only 1 method
private String helpurl = "http://forum.decraiz.bplaced.net";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String uptime = "&9This server is running for &e{h} &9hours &e{m} &9minutes and &e{s} &9seconds.";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String started = "&9This server is running since &e{d}/{m}/{y} &9at &e{h}:{min}:{sec}.{ms} &9in the &e{timezone} &9TimeZone";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String timezone = "GMT";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String timezoner = "GMT";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String usage = "&cUsage: /uptime (started)";
// Placeholder for a Config Value but has it's default Value in case of an error when overwriting from Config
private String noperm = "&cSorry, but you don't have permission to perform this action.";
private File getCfgF() {
// Returns the Config File
// Creation is done by YamlConfiguration
return new File("plugins/xUpTime", "config.yml");
}
private FileConfiguration getCfg() {
// Returns the Config Configuration and creates the file (if not done yet)
return YamlConfiguration.loadConfiguration(getCfgF());
}
private boolean loadConfig() {
FileConfiguration cfg = getCfg();
// Allows to create Values / Header
cfg.options().copyDefaults(true);
cfg.options().copyHeader(true);
// Sets Header:
cfg.options().header("Visit "+helpurl+" for help");
// Sets Default Values:
cfg.addDefault("Settings.TimeZone.Java", "GMT");
cfg.addDefault("Settings.TimeZone.Display", "GMT");
cfg.addDefault("Message.UpTime", "&9This server is running for &e{h} &9hours &e{m} &9minutes and &e{s} &9seconds.");
cfg.addDefault("Message.Started", "&9This server is running since &e{d}/{m}/{y} &9at &e{h}:{min}:{sec}.{ms} &9in the &e{timezone} &9TimeZone");
cfg.addDefault("Error.Usage", "&cUsage: {usage}");
cfg.addDefault("Error.NoPermission", "&cSorry, but you don't have permission to perform this action.");
try {
// Saves the File
cfg.save(getCfgF());
} catch (IOException e) {
// Prints (in case of an error) a detailed report about it
e.printStackTrace();
// Returns False because of Error
return false;
}
// Returns True because of no Error
return true;
}
private void readConfig() {
FileConfiguration cfg = getCfg();
// Overwrites all Variables with their Config Values
this.timezone = cfg.getString("Settings.TimeZone.Java");
this.timezoner = cfg.getString("Settings.TimeZone.Display");
this.uptime = cfg.getString("Message.UpTime");
this.started = cfg.getString("Message.Started");
this.usage = cfg.getString("Error.Usage");
this.noperm = cfg.getString("Error.NoPermission");
}
}
-
View User Profile
-
Send Message
Posted Dec 21, 2016Bukkit Paste Version