Use new Java 8 time API

This commit is contained in:
filoghost 2021-01-24 12:01:32 +01:00
parent b2b03f3e40
commit b5852116d4
3 changed files with 17 additions and 11 deletions

View File

@ -14,8 +14,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.text.SimpleDateFormat; import java.time.Instant;
import java.util.Date; import java.time.format.DateTimeFormatter;
public class DeathHolograms extends JavaPlugin implements Listener { public class DeathHolograms extends JavaPlugin implements Listener {
@ -39,7 +39,7 @@ public class DeathHolograms extends JavaPlugin implements Listener {
Hologram hologram = HologramsAPI.createHologram(this, event.getEntity().getEyeLocation()); Hologram hologram = HologramsAPI.createHologram(this, event.getEntity().getEyeLocation());
hologram.appendTextLine(ChatColor.RED + "Player " + ChatColor.GOLD + event.getEntity().getName() + ChatColor.RED + " died here!"); hologram.appendTextLine(ChatColor.RED + "Player " + ChatColor.GOLD + event.getEntity().getName() + ChatColor.RED + " died here!");
hologram.appendTextLine(ChatColor.GRAY + "Time of death: " + new SimpleDateFormat("H:m").format(new Date())); hologram.appendTextLine(ChatColor.GRAY + "Time of death: " + DateTimeFormatter.ofPattern("H:mm").format(Instant.now()));
} }
} }

View File

@ -20,12 +20,13 @@ import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
/** /**
* Just a bunch of static variables to hold the settings. * Just a bunch of static variables to hold the settings.
@ -40,7 +41,7 @@ public class Configuration {
public static boolean updateNotification; public static boolean updateNotification;
public static ChatColor transparencyColor; public static ChatColor transparencyColor;
public static SimpleDateFormat timeFormat; public static DateTimeFormatter timeFormat;
public static int bungeeRefreshSeconds; public static int bungeeRefreshSeconds;
public static boolean useRedisBungee; public static boolean useRedisBungee;
@ -197,13 +198,18 @@ public class Configuration {
} }
try { try {
timeFormat = new SimpleDateFormat(config.getString(ConfigNode.TIME_FORMAT.getPath())); timeFormat = DateTimeFormatter.ofPattern(config.getString(ConfigNode.TIME_FORMAT.getPath()));
timeFormat.setTimeZone(TimeZone.getTimeZone(config.getString(ConfigNode.TIME_ZONE.getPath())));
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
timeFormat = new SimpleDateFormat("H:mm"); timeFormat = DateTimeFormatter.ofPattern("H:mm");
Log.warning("Time format not valid in the configuration, using the default."); Log.warning("Time format not valid in the configuration, using the default.");
} }
try {
timeFormat = timeFormat.withZone(ZoneId.of(config.getString(ConfigNode.TIME_ZONE.getPath())));
} catch (DateTimeException e) {
Log.warning("Time zone not valid in the configuration, using the default.");
}
if (bungeeRefreshSeconds < 1) { if (bungeeRefreshSeconds < 1) {
Log.warning("The minimum interval for pinging BungeeCord's servers is 1 second. It has been automatically set."); Log.warning("The minimum interval for pinging BungeeCord's servers is 1 second. It has been automatically set.");
bungeeRefreshSeconds = 1; bungeeRefreshSeconds = 1;

View File

@ -12,7 +12,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.Date; import java.time.Instant;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
@ -37,7 +37,7 @@ public class PlaceholdersRegister {
})); }));
register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, () -> { register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, () -> {
return Configuration.timeFormat.format(new Date()); return Configuration.timeFormat.format(Instant.now());
})); }));
register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.toStringList( register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.toStringList(