From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Sun, 6 Aug 2023 09:57:53 +0800 Subject: [PATCH] Add Leaves Auto Update diff --git a/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java b/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java index 06ca57f0cb319a254ca1511e60f05e5ca151beea..061869a93704e9605378468af8343c64fa009580 100644 --- a/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java +++ b/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java @@ -7,8 +7,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import net.minecraft.Util; +import net.minecraft.world.level.gameevent.vibrations.VibrationSystem; import org.bukkit.Bukkit; +import org.checkerframework.checker.units.qual.C; import org.jetbrains.annotations.NotNull; +import org.leavesmc.leaves.LeavesConfig; import org.leavesmc.leaves.LeavesLogger; import java.io.BufferedReader; @@ -26,6 +29,12 @@ import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; +import java.time.Duration; +import java.time.LocalTime; +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; import static java.nio.file.StandardOpenOption.CREATE; @@ -40,7 +49,9 @@ public class LeavesUpdateHelper { private final static ReentrantLock updateLock = new ReentrantLock(); private static boolean updateTaskStarted = false; - public static void initAutoUpdate() { + private static final ScheduledExecutorService autoUpdateExecutor = Executors.newScheduledThreadPool(1); + + public static void init() { File workingDirFile = new File(autoUpdateDir); if (!workingDirFile.exists()) { workingDirFile.mkdir(); @@ -59,6 +70,24 @@ public class LeavesUpdateHelper { if (!leavesUpdateDir.exists()) { leavesUpdateDir.mkdir(); } + + if (LeavesConfig.autoUpdate) { + LocalTime currentTime = LocalTime.now(); + long dailyTaskPeriod = 24 * 60 * 60 * 1000; + + for (String time : LeavesConfig.autoUpdateTime) { + try { + LocalTime taskTime = LocalTime.of(Integer.parseInt(time.split(":")[0]), Integer.parseInt(time.split(":")[1])); + Duration task = Duration.between(currentTime, taskTime); + if (task.isNegative()) { + task = task.plusDays(1); + } + autoUpdateExecutor.scheduleAtFixedRate(LeavesUpdateHelper::tryUpdateLeaves, task.toMillis(), dailyTaskPeriod, TimeUnit.MILLISECONDS); + } catch (Exception ignored){ + LeavesLogger.LOGGER.warning("Illegal auto-update time ignored: " + time); + } + } + } } public static void tryUpdateLeaves() { @@ -66,12 +95,7 @@ public class LeavesUpdateHelper { try { if (!updateTaskStarted) { updateTaskStarted = true; - new Thread(new Runnable() { - @Override - public void run() { - downloadLeaves(); - } - }).start(); + new Thread(LeavesUpdateHelper::downloadLeaves).start(); } } finally { updateLock.unlock(); @@ -83,7 +107,7 @@ public class LeavesUpdateHelper { String version = Bukkit.getVersion(); if (version.startsWith("null")) { - LeavesLogger.LOGGER.info("IDE?"); + LeavesLogger.LOGGER.info("IDE? Can not update!"); updateTaskStarted = false; return; }