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
This commit is contained in:
Nathan Curran 2021-05-25 21:45:53 +10:00 committed by Ryan
parent 63069ff9ec
commit ff760a6c11
2 changed files with 46 additions and 46 deletions

View File

@ -1,6 +1,5 @@
package me.totalfreedom.totalfreedommod; package me.totalfreedom.totalfreedommod;
import java.io.File;
import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList; import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList;
import me.totalfreedom.totalfreedommod.config.YamlConfig; import me.totalfreedom.totalfreedommod.config.YamlConfig;
import me.totalfreedom.totalfreedommod.permissions.PermissionConfig; import me.totalfreedom.totalfreedommod.permissions.PermissionConfig;
@ -9,8 +8,11 @@ import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.util.FileUtil; import org.bukkit.util.FileUtil;
import java.io.File;
public class BackupManager extends FreedomService public class BackupManager extends FreedomService
{ {
@Override @Override
public void onStart() public void onStart()
{ {
@ -42,16 +44,16 @@ public class BackupManager extends FreedomService
config.load(); config.load();
// Weekly // Weekly
if (!config.isInt(save + ".weekly")) if (!config.isLong(save + ".weekly"))
{ {
performBackup(file, "weekly"); performBackup(file, "weekly");
config.set(save + ".weekly", FUtil.getUnixTime()); config.set(save + ".weekly", FUtil.getUnixTime());
} }
else 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"); performBackup(file, "weekly");
config.set(save + ".weekly", FUtil.getUnixTime()); config.set(save + ".weekly", FUtil.getUnixTime());
@ -65,16 +67,16 @@ public class BackupManager extends FreedomService
} }
// Daily // Daily
if (!config.isInt(save + ".daily")) if (!config.isLong(save + ".daily"))
{ {
performBackup(file, "daily"); performBackup(file, "daily");
config.set(save + ".daily", FUtil.getUnixTime()); config.set(save + ".daily", FUtil.getUnixTime());
} }
else 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"); performBackup(file, "daily");
config.set(save + ".daily", FUtil.getUnixTime()); config.set(save + ".daily", FUtil.getUnixTime());
@ -98,5 +100,4 @@ public class BackupManager extends FreedomService
final File newYaml = new File(backupFolder, file + "." + type + ".bak"); final File newYaml = new File(backupFolder, file + "." + type + ".bak");
FileUtil.copy(oldYaml, newYaml); FileUtil.copy(oldYaml, newYaml);
} }
} }

View File

@ -1,33 +1,5 @@
package me.totalfreedom.totalfreedommod.util; 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.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import org.apache.commons.io.FileUtils; 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.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.json.simple.JSONArray; 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; import static org.bukkit.Bukkit.getServer;
public class FUtil public class FUtil
@ -76,7 +60,7 @@ public class FUtil
"2e06e049-24c8-42e4-8bcf-d35372af31e6", // NotInSync "2e06e049-24c8-42e4-8bcf-d35372af31e6", // NotInSync
"f97c0d7b-6413-4558-a409-88f09a8f9adb" // videogamesm12 "f97c0d7b-6413-4558-a409-88f09a8f9adb" // videogamesm12
); );
public static final List<String> DEVELOPER_NAMES = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "Wild1145", "aggelosQQ", "scripthead", "CoolJWB", "elmon_", "speednt", "SupItsDillon", "Paldiu", "AwesomePinch", "TFTWPhoenix","abhithedev", "NotInSync", "videogamesm12"); public static final List<String> DEVELOPER_NAMES = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "Wild1145", "aggelosQQ", "scripthead", "CoolJWB", "elmon_", "speednt", "SupItsDillon", "Paldiu", "AwesomePinch", "TFTWPhoenix", "abhithedev", "NotInSync", "videogamesm12");
public static final Map<String, ChatColor> CHAT_COLOR_NAMES = new HashMap<>(); public static final Map<String, ChatColor> CHAT_COLOR_NAMES = new HashMap<>();
public static final List<ChatColor> CHAT_COLOR_POOL = Arrays.asList( public static final List<ChatColor> CHAT_COLOR_POOL = Arrays.asList(
ChatColor.DARK_RED, ChatColor.DARK_RED,
@ -274,7 +258,7 @@ public class FUtil
public static Response sendRequest(String endpoint, String method, List<String> headers, String body) throws IOException public static Response sendRequest(String endpoint, String method, List<String> headers, String body) throws IOException
{ {
URL url = new URL(endpoint); URL url = new URL(endpoint);
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method); connection.setRequestMethod(method);
@ -393,7 +377,8 @@ public class FUtil
} }
} }
private static final List<String> regxList = new ArrayList<String>(){{ private static final List<String> regxList = new ArrayList<String>()
{{
add("y"); add("y");
add("mo"); add("mo");
add("w"); add("w");
@ -403,11 +388,13 @@ public class FUtil
add("s"); add("s");
}}; }};
private static long a(String parse) { private static long a(String parse)
{
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
regxList.forEach(obj -> { regxList.forEach(obj -> {
if (parse.endsWith(obj)) { if (parse.endsWith(obj))
{
sb.append(parse.split(obj)[0]); sb.append(parse.split(obj)[0]);
} }
}); });
@ -415,7 +402,8 @@ public class FUtil
return Long.parseLong(sb.toString()); return Long.parseLong(sb.toString());
} }
private static TimeUnit verify(String arg) { private static TimeUnit verify(String arg)
{
TimeUnit unit = null; TimeUnit unit = null;
for (String c : regxList) for (String c : regxList)
{ {
@ -461,6 +449,16 @@ public class FUtil
return Date.from(instant); 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<OfflinePlayer> players) public static String playerListToNames(Set<OfflinePlayer> players)
{ {
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
@ -553,7 +551,7 @@ public class FUtil
{ {
Field field = checkClass.getDeclaredField(name); Field field = checkClass.getDeclaredField(name);
field.setAccessible(true); field.setAccessible(true);
return (T)field.get(from); return (T) field.get(from);
} }
catch (NoSuchFieldException | IllegalAccessException ignored) catch (NoSuchFieldException | IllegalAccessException ignored)
@ -635,7 +633,7 @@ public class FUtil
public static int randomInteger(int min, int max) public static int randomInteger(int min, int max)
{ {
int range = max - min + 1; int range = max - min + 1;
return (int)(Math.random() * range) + min; return (int) (Math.random() * range) + min;
} }
public static String randomString(int length) public static String randomString(int length)
@ -773,7 +771,7 @@ public class FUtil
{ {
c1values[i] = Math.round(c1values[i] + factor * (c2values[i] - c1values[i])); 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) public static boolean isValidIPv4(String ip)
@ -853,6 +851,7 @@ public class FUtil
public static class PaginationList<T> extends ArrayList<T> public static class PaginationList<T> extends ArrayList<T>
{ {
private final int epp; private final int epp;
public PaginationList(int epp) public PaginationList(int epp)
@ -870,7 +869,7 @@ public class FUtil
public int getPageCount() public int getPageCount()
{ {
return (int)Math.ceil((double)size() / (double)epp); return (int) Math.ceil((double) size() / (double) epp);
} }
public List<T> getPage(int page) public List<T> getPage(int page)