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/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 2c3becfa0e540ce1ae5a1357201666bbf3df665f..1d4d357465edf888f1d52755c382dcd8e015fc09 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -16,6 +16,7 @@ import top.leavesmc.leaves.profile.LeavesMinecraftSessionService; import top.leavesmc.leaves.util.MathUtils; import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule; import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules; +import top.leavesmc.leaves.util.LeavesUpdateHelper; import top.leavesmc.leaves.protocol.bladeren.BladerenProtocol.LeavesFeatureSet; import top.leavesmc.leaves.protocol.bladeren.BladerenProtocol.LeavesFeature; @@ -823,6 +824,7 @@ public final class LeavesConfig { private static void autoUpdate() { autoUpdate = getBoolean("settings.misc.auto-update.enable", autoUpdate); autoUpdateTime = getList("settings.misc.auto-update.time", autoUpdateTime); + LeavesUpdateHelper.init(); if (autoUpdate) { LeavesLogger.LOGGER.warning("Auto-Update is not completely safe. Enabling it may cause data security problems!"); } diff --git a/src/main/java/top/leavesmc/leaves/util/LeavesUpdateHelper.java b/src/main/java/top/leavesmc/leaves/util/LeavesUpdateHelper.java index 7476f6c5bf8c4572878159a74507193e3c4a6207..7b38b707869932bd81522c100cb430389d44ad00 100644 --- a/src/main/java/top/leavesmc/leaves/util/LeavesUpdateHelper.java +++ b/src/main/java/top/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 top.leavesmc.leaves.LeavesConfig; import top.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; }