-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b662ede
commit 0d94a3b
Showing
3 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
From: MC_XiaoHei <[email protected]> | ||
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; | ||
} |