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 GitHub
parent d047cfc8ff
commit eedb7a5375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
@ -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<>();
@ -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)