From eedb7a53759aa04504b81895939411674c4540ff Mon Sep 17 00:00:00 2001 From: Nathan Curran <30569566+Focusvity@users.noreply.github.com> Date: Tue, 25 May 2021 21:45:53 +1000 Subject: [PATCH] Use long instead of int and use offset to check time (FS-181) (#69) * Use long instead of int and use offset to check time (FS-181) * Remove star import --- .../totalfreedommod/BackupManager.java | 17 +++-- .../totalfreedommod/util/FUtil.java | 75 +++++++++---------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java b/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java index e70bbf36..d5f2b8e2 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/BackupManager.java @@ -1,6 +1,5 @@ package me.totalfreedom.totalfreedommod; -import java.io.File; import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList; import me.totalfreedom.totalfreedommod.config.YamlConfig; import me.totalfreedom.totalfreedommod.permissions.PermissionConfig; @@ -9,8 +8,11 @@ import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.util.FileUtil; +import java.io.File; + public class BackupManager extends FreedomService { + @Override public void onStart() { @@ -42,16 +44,16 @@ public class BackupManager extends FreedomService config.load(); // Weekly - if (!config.isInt(save + ".weekly")) + if (!config.isLong(save + ".weekly")) { performBackup(file, "weekly"); config.set(save + ".weekly", FUtil.getUnixTime()); } else { - int lastBackupWeekly = config.getInt(save + ".weekly"); + long lastBackupWeekly = config.getLong(save + ".weekly"); - if (lastBackupWeekly + 3600 * 24 * 7 < FUtil.getUnixTime()) + if (FUtil.parseLongOffset(lastBackupWeekly, "1w") < FUtil.getUnixTime()) { performBackup(file, "weekly"); config.set(save + ".weekly", FUtil.getUnixTime()); @@ -65,16 +67,16 @@ public class BackupManager extends FreedomService } // Daily - if (!config.isInt(save + ".daily")) + if (!config.isLong(save + ".daily")) { performBackup(file, "daily"); config.set(save + ".daily", FUtil.getUnixTime()); } else { - int lastBackupDaily = config.getInt(save + ".daily"); + long lastBackupDaily = config.getLong(save + ".daily"); - if (lastBackupDaily + 3600 * 24 < FUtil.getUnixTime()) + if (FUtil.parseLongOffset(lastBackupDaily, "1d") < FUtil.getUnixTime()) { performBackup(file, "daily"); config.set(save + ".daily", FUtil.getUnixTime()); @@ -98,5 +100,4 @@ public class BackupManager extends FreedomService final File newYaml = new File(backupFolder, file + "." + type + ".bak"); FileUtil.copy(oldYaml, newYaml); } - } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index 5afb206a..a269a296 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -1,33 +1,5 @@ package me.totalfreedom.totalfreedommod.util; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.Field; -import java.net.HttpURLConnection; -import java.net.URL; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.Instant; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.SplittableRandom; -import java.util.TimeZone; -import java.util.UUID; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import org.apache.commons.io.FileUtils; @@ -48,6 +20,18 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import org.json.simple.JSONArray; + +import java.io.*; +import java.lang.reflect.Field; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import static org.bukkit.Bukkit.getServer; public class FUtil @@ -76,7 +60,7 @@ public class FUtil "2e06e049-24c8-42e4-8bcf-d35372af31e6", // NotInSync "f97c0d7b-6413-4558-a409-88f09a8f9adb" // videogamesm12 ); - public static final List DEVELOPER_NAMES = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "Wild1145", "aggelosQQ", "scripthead", "CoolJWB", "elmon_", "speednt", "SupItsDillon", "Paldiu", "AwesomePinch", "TFTWPhoenix","abhithedev", "NotInSync", "videogamesm12"); + public static final List DEVELOPER_NAMES = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "Wild1145", "aggelosQQ", "scripthead", "CoolJWB", "elmon_", "speednt", "SupItsDillon", "Paldiu", "AwesomePinch", "TFTWPhoenix", "abhithedev", "NotInSync", "videogamesm12"); public static final Map CHAT_COLOR_NAMES = new HashMap<>(); public static final List CHAT_COLOR_POOL = Arrays.asList( ChatColor.DARK_RED, @@ -274,7 +258,7 @@ public class FUtil public static Response sendRequest(String endpoint, String method, List headers, String body) throws IOException { URL url = new URL(endpoint); - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); @@ -393,7 +377,8 @@ public class FUtil } } - private static final List regxList = new ArrayList(){{ + private static final List regxList = new ArrayList() + {{ add("y"); add("mo"); add("w"); @@ -403,11 +388,13 @@ public class FUtil add("s"); }}; - private static long a(String parse) { + private static long a(String parse) + { StringBuilder sb = new StringBuilder(); regxList.forEach(obj -> { - if (parse.endsWith(obj)) { + if (parse.endsWith(obj)) + { sb.append(parse.split(obj)[0]); } }); @@ -415,7 +402,8 @@ public class FUtil return Long.parseLong(sb.toString()); } - private static TimeUnit verify(String arg) { + private static TimeUnit verify(String arg) + { TimeUnit unit = null; for (String c : regxList) { @@ -461,6 +449,16 @@ public class FUtil return Date.from(instant); } + public static long parseLongOffset(long unix, String... time) + { + Instant instant = Instant.ofEpochMilli(unix); + for (String arg : time) + { + instant = instant.plusSeconds(verify(arg).get() * a(arg)); + } + return FUtil.getUnixTime(Date.from(instant)); + } + public static String playerListToNames(Set players) { List names = new ArrayList<>(); @@ -553,7 +551,7 @@ public class FUtil { Field field = checkClass.getDeclaredField(name); field.setAccessible(true); - return (T)field.get(from); + return (T) field.get(from); } catch (NoSuchFieldException | IllegalAccessException ignored) @@ -635,7 +633,7 @@ public class FUtil public static int randomInteger(int min, int max) { int range = max - min + 1; - return (int)(Math.random() * range) + min; + return (int) (Math.random() * range) + min; } public static String randomString(int length) @@ -773,7 +771,7 @@ public class FUtil { c1values[i] = Math.round(c1values[i] + factor * (c2values[i] - c1values[i])); } - return Color.fromRGB((int)c1values[0], (int)c1values[1], (int)c1values[2]); + return Color.fromRGB((int) c1values[0], (int) c1values[1], (int) c1values[2]); } public static boolean isValidIPv4(String ip) @@ -853,6 +851,7 @@ public class FUtil public static class PaginationList extends ArrayList { + private final int epp; public PaginationList(int epp) @@ -870,7 +869,7 @@ public class FUtil public int getPageCount() { - return (int)Math.ceil((double)size() / (double)epp); + return (int) Math.ceil((double) size() / (double) epp); } public List getPage(int page)