From 0d94a3b79ce84001a2bdd343ff1987d66cdfd987 Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Sun, 6 Aug 2023 10:06:09 +0800 Subject: [PATCH] Add Leaves Auto Update (#97) --- ...004-Leaves-Server-Config-And-Command.patch | 14 ++- .../server/0115-Leaves-update-command.patch | 5 +- .../server/0117-Add-Leaves-Auto-Update.patch | 114 ++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 patches/server/0117-Add-Leaves-Auto-Update.patch diff --git a/patches/server/0004-Leaves-Server-Config-And-Command.patch b/patches/server/0004-Leaves-Server-Config-And-Command.patch index bd91f9b4..5915b024 100644 --- a/patches/server/0004-Leaves-Server-Config-And-Command.patch +++ b/patches/server/0004-Leaves-Server-Config-And-Command.patch @@ -128,10 +128,10 @@ index c9263cf970b82a2ee24d10202c21ac30bfd925dc..c20debb13d8496ad7c0ef1eaf5eae1c4 .withRequiredArg() diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java new file mode 100644 -index 0000000000000000000000000000000000000000..ba0daabf8104b2a16169b65544eeeb948f04233f +index 0000000000000000000000000000000000000000..1486bf1c37cbacb6643379cbd555f8a9e8bc54ba --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java -@@ -0,0 +1,958 @@ +@@ -0,0 +1,968 @@ +package top.leavesmc.leaves; + +import com.destroystokyo.paper.util.SneakyThrow; @@ -915,6 +915,16 @@ index 0000000000000000000000000000000000000000..ba0daabf8104b2a16169b65544eeeb94 + bstatsPrivacyMode = getBoolean("settings.misc.bstats-privacy-mode", bstatsPrivacyMode); + } + ++ public static boolean autoUpdate = false; ++ public static List autoUpdateTime = List.of("14:00","2:00"); ++ private static void autoUpdate() { ++ autoUpdate = getBoolean("settings.misc.auto-update.enable", autoUpdate); ++ autoUpdateTime = getList("settings.misc.auto-update.time", autoUpdateTime); ++ if (autoUpdate) { ++ LeavesLogger.LOGGER.warning("Auto-Update is not completely safe. Enabling it may cause data security problems!"); ++ } ++ } ++ + public static final class WorldConfig { + + public final String worldName; diff --git a/patches/server/0115-Leaves-update-command.patch b/patches/server/0115-Leaves-update-command.patch index 851e8bac..496ab0e4 100644 --- a/patches/server/0115-Leaves-update-command.patch +++ b/patches/server/0115-Leaves-update-command.patch @@ -26,10 +26,10 @@ index bdd4f2305e8336929bbf9bfaa4d369b3ac6702c0..78d3df6a15057d42466c266226f308a0 .flatMap(entry -> entry.getKey().stream().map(s -> Map.entry(s, entry.getValue()))) diff --git a/src/main/java/top/leavesmc/leaves/command/subcommands/UpdateCommand.java b/src/main/java/top/leavesmc/leaves/command/subcommands/UpdateCommand.java new file mode 100644 -index 0000000000000000000000000000000000000000..62068c4378e855f61c0e26ee3273212535224c2f +index 0000000000000000000000000000000000000000..293117a6c54fdce70d69b93fe80ce0216f9b3ea2 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/command/subcommands/UpdateCommand.java -@@ -0,0 +1,22 @@ +@@ -0,0 +1,21 @@ +package top.leavesmc.leaves.command.subcommands; + +import org.bukkit.ChatColor; @@ -42,7 +42,6 @@ index 0000000000000000000000000000000000000000..62068c4378e855f61c0e26ee32732125 + @Override + public boolean execute(CommandSender sender, String subCommand, String[] args) { + sender.sendMessage(ChatColor.GRAY + "Trying to update Leaves, see the console for more info."); -+ LeavesUpdateHelper.initAutoUpdate(); + LeavesUpdateHelper.tryUpdateLeaves(); + return true; + } diff --git a/patches/server/0117-Add-Leaves-Auto-Update.patch b/patches/server/0117-Add-Leaves-Auto-Update.patch new file mode 100644 index 00000000..abd19a17 --- /dev/null +++ b/patches/server/0117-Add-Leaves-Auto-Update.patch @@ -0,0 +1,114 @@ +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 952f9798aa2818eaac9b9f60acb2808f58e92e86..445e2bad15a3509f7f48f2c43657a4bdf0fea1b2 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.bot.agent.Actions; + import top.leavesmc.leaves.command.NoBlockUpdateCommand; + import top.leavesmc.leaves.profile.LeavesMinecraftSessionService; + import top.leavesmc.leaves.protocol.syncmatica.SyncmaticaProtocol; ++import top.leavesmc.leaves.util.LeavesUpdateHelper; + import top.leavesmc.leaves.util.MathUtils; + import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule; + import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules; +@@ -819,6 +820,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 fb0f4ac5a864a9f05b99ae9887ac110701021b2e..e348ae6c04e2d882d6c9998458dd0b81fb539caa 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; + }