Skip to content

Commit

Permalink
reforge other patch
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Jun 26, 2024
1 parent 3c991d1 commit 5a03bd3
Show file tree
Hide file tree
Showing 35 changed files with 141 additions and 287 deletions.
10 changes: 5 additions & 5 deletions patches/server/0006-Leaves-Server-Config-And-Command.patch
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ index 459f47244bdfeab63b5f16d780b0291d36310de8..a872421bc3e67fdcc929f104f4b085fb
.withRequiredArg()
diff --git a/src/main/java/org/leavesmc/leaves/LeavesConfig.java b/src/main/java/org/leavesmc/leaves/LeavesConfig.java
new file mode 100644
index 0000000000000000000000000000000000000000..9676585f170ffdde3ab96382efd9a1de078536a2
index 0000000000000000000000000000000000000000..2a7f9d9fdb72786b08b3cb216f5aa5e5eac3c967
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/LeavesConfig.java
@@ -0,0 +1,922 @@
Expand Down Expand Up @@ -956,7 +956,7 @@ index 0000000000000000000000000000000000000000..9676585f170ffdde3ab96382efd9a1de
+ public static boolean bstatsPrivacyMode = false;
+
+ @GlobalConfig(name = "force-minecraft-command", category = "misc")
+ public static boolean forceMinecraftCommand = true;
+ public static boolean forceMinecraftCommand = false;
+
+ @GlobalConfig(name = "leaves-packet-event", category = "misc")
+ public static boolean leavesPacketEvent = true;
Expand Down Expand Up @@ -1190,7 +1190,7 @@ index 0000000000000000000000000000000000000000..dccd32714b86d1fa3e0a9b10a23f765f
+}
diff --git a/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..b9e2bdb034dcd5119680ddea44a1eda11a0189b2
index 0000000000000000000000000000000000000000..ebd62b1d3a60d3e22e3849047293e1f62533eae7
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
@@ -0,0 +1,118 @@
Expand All @@ -1206,9 +1206,9 @@ index 0000000000000000000000000000000000000000..b9e2bdb034dcd5119680ddea44a1eda1
+import org.bukkit.permissions.Permission;
+import org.bukkit.permissions.PermissionDefault;
+import org.bukkit.plugin.PluginManager;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.command.subcommands.ConfigCommand;
+import org.jetbrains.annotations.Nullable;
+import org.leavesmc.leaves.command.subcommands.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: violetc <[email protected]>
Date: Mon, 31 Jul 2023 17:42:25 +0800
Subject: [PATCH] Leaves update command
Subject: [PATCH] Leaves Updater

---------

Co-authored-by: MC_XiaoHei <[email protected]>

diff --git a/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
index b9e2bdb034dcd5119680ddea44a1eda11a0189b2..facd180da9b7025cf7ae192cbe91317ff4b70472 100644
index ebd62b1d3a60d3e22e3849047293e1f62533eae7..e964f05c5c0f2d1d9b4c0a3459076e4b95f0f4e8 100644
--- a/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
+++ b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
@@ -13,6 +13,7 @@ import org.bukkit.plugin.PluginManager;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.command.subcommands.ConfigCommand;
+import org.leavesmc.leaves.command.subcommands.UpdateCommand;

import java.util.ArrayList;
import java.util.Arrays;
@@ -33,6 +34,7 @@ public final class LeavesCommand extends Command {
@@ -33,6 +33,7 @@ public final class LeavesCommand extends Command {
private static final Map<String, LeavesSubcommand> SUBCOMMANDS = Util.make(() -> {
final Map<Set<String>, LeavesSubcommand> commands = new HashMap<>();
commands.put(Set.of("config"), new ConfigCommand());
Expand Down Expand Up @@ -53,10 +48,10 @@ index 0000000000000000000000000000000000000000..7f94df607e8ffd48ab2cb7c90d520c2b
+}
diff --git a/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java b/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5ca151beea
index 0000000000000000000000000000000000000000..8620305f3330a856ad7445259f565bf8e281ea52
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/util/LeavesUpdateHelper.java
@@ -0,0 +1,240 @@
@@ -0,0 +1,268 @@
+package org.leavesmc.leaves.util;
+
+import com.google.common.base.Charsets;
Expand All @@ -68,6 +63,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+import net.minecraft.Util;
+import org.bukkit.Bukkit;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.LeavesConfig;
+import org.leavesmc.leaves.LeavesLogger;
+
+import java.io.BufferedReader;
Expand All @@ -78,13 +74,20 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+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.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;
Expand All @@ -99,24 +102,50 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ 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();
+ if (!workingDirFile.mkdir()) {
+ LeavesLogger.LOGGER.warning("Failed to create working directory: " + autoUpdateDir);
+ }
+ }
+
+ File corePathFile = new File(corePathFileName);
+ if (!corePathFile.exists()) {
+ try {
+ corePathFile.createNewFile();
+ if (!corePathFile.createNewFile()) {
+ throw new IOException();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ LeavesLogger.LOGGER.severe("Failed to create core path file: " + corePathFileName, e);
+ }
+ }
+
+ File leavesUpdateDir = new File(autoUpdateDir + File.separator + "leaves");
+ if (!leavesUpdateDir.exists()) {
+ leavesUpdateDir.mkdir();
+ if (!leavesUpdateDir.mkdir()) {
+ LeavesLogger.LOGGER.warning("Failed to create leaves update directory: " + leavesUpdateDir);
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+ }
+
Expand All @@ -125,12 +154,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ try {
+ if (!updateTaskStarted) {
+ updateTaskStarted = true;
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ downloadLeaves();
+ }
+ }).start();
+ new Thread(LeavesUpdateHelper::downloadLeaves).start();
+ }
+ } finally {
+ updateLock.unlock();
Expand All @@ -142,7 +166,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ String version = Bukkit.getVersion();
+
+ if (version.startsWith("null")) {
+ LeavesLogger.LOGGER.info("IDE?");
+ LeavesLogger.LOGGER.info("IDE? Can not update!");
+ updateTaskStarted = false;
+ return;
+ }
Expand All @@ -165,7 +189,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ Files.deleteIfExists(outFile);
+
+ boolean downloadFlag = false;
+ URL cdnUrl = new URL(buildInfo.cdnUrl);
+ URL cdnUrl = new URI(buildInfo.cdnUrl).toURL();
+ try (
+ final ReadableByteChannel source = Channels.newChannel(cdnUrl.openStream());
+ final FileChannel fileChannel = FileChannel.open(outFile, CREATE, WRITE, TRUNCATE_EXISTING)
Expand All @@ -177,17 +201,16 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ }
+
+ if (!downloadFlag) {
+ URL githubUrl = new URL(buildInfo.githubUrl);
+ URL githubUrl = new URI(buildInfo.githubUrl).toURL();
+ try (
+ final ReadableByteChannel source = Channels.newChannel(githubUrl.openStream());
+ final FileChannel fileChannel = FileChannel.open(outFile, CREATE, WRITE, TRUNCATE_EXISTING)
+ ) {
+ fileChannel.transferFrom(source, 0, Long.MAX_VALUE);
+ } catch (final IOException e) {
+ LeavesLogger.LOGGER.warning("Download " + buildInfo.fileName + " failed.");
+ LeavesLogger.LOGGER.warning("Download " + buildInfo.fileName + " failed.", e);
+ Files.deleteIfExists(outFile);
+ updateTaskStarted = false;
+ e.printStackTrace();
+ return;
+ }
+ }
Expand All @@ -207,14 +230,14 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(corePathFileName))) {
+ bufferedWriter.write(autoUpdateDir + File.separator + "leaves" + File.separator + buildInfo.fileName);
+ } catch (IOException e) {
+ e.printStackTrace();
+ LeavesLogger.LOGGER.warning("Fail to download leaves core", e);
+ updateTaskStarted = false;
+ return;
+ }
+
+ LeavesLogger.LOGGER.info("Leaves update completed, please restart your server.");
+ } catch (Exception e) {
+ e.printStackTrace();
+ LeavesLogger.LOGGER.severe("Leaves update failed", e);
+ }
+ } else {
+ LeavesLogger.LOGGER.warning("Can't get build info, stopping update.");
Expand All @@ -233,7 +256,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+
+ return toHexString(md5.digest()).equals(hash);
+ } catch (Exception e) {
+ e.printStackTrace();
+ LeavesLogger.LOGGER.warning("Fail to validate file " + file, e);
+ }
+ return false;
+ }
Expand All @@ -249,14 +272,14 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+
+ private static LeavesBuildInfo getLatestBuildInfo(String mcVersion, String gitHash) {
+ try {
+ HttpURLConnection connection = (HttpURLConnection) new URL("https://api.leavesmc.org/projects/leaves/versions/" + mcVersion + "/builds/latest").openConnection();
+ HttpURLConnection connection = (HttpURLConnection) new URI("https://api.leavesmc.org/projects/leaves/versions/" + mcVersion + "/builds/latest").toURL().openConnection();
+ connection.connect();
+ boolean useApiV2 = false;
+ if (connection.getResponseCode() / 100 != 2){
+ connection = (HttpURLConnection) new URL("https://api.leavesmc.org/v2/projects/leaves/versions/" + mcVersion + "/builds/latest").openConnection();
+ if (connection.getResponseCode() / 100 != 2) {
+ connection = (HttpURLConnection) new URI("https://api.leavesmc.org/v2/projects/leaves/versions/" + mcVersion + "/builds/latest").toURL().openConnection();
+ connection.connect();
+ useApiV2 = true;
+ };
+ }
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return LeavesBuildInfo.NULL;
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8))) {
+ JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
Expand All @@ -265,7 +288,7 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ if (channel.equals("default")) {
+ int build = obj.get("build").getAsInt();
+
+ JsonArray changes = obj.get("changes").getAsJsonArray();
+ JsonArray changes = obj.get("changes").getAsJsonArray();
+ boolean needUpdate = true;
+ for (JsonElement change : changes) {
+ if (change.getAsJsonObject().get("commit").getAsString().startsWith(gitHash)) {
Expand All @@ -284,11 +307,11 @@ index 0000000000000000000000000000000000000000..06ca57f0cb319a254ca1511e60f05e5c
+ return LeavesBuildInfo.NULL;
+ }
+ } catch (JsonSyntaxException | NumberFormatException e) {
+ e.printStackTrace();
+ LeavesLogger.LOGGER.warning("Fail to get latest build info", e);
+ return LeavesBuildInfo.NULL;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (IOException | URISyntaxException e) {
+ LeavesLogger.LOGGER.warning("Fail to get latest build info", e);
+ return LeavesBuildInfo.NULL;
+ }
+ }
Expand Down
12 changes: 2 additions & 10 deletions patches/server/0096-Force-peaceful-mode-switch.patch
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,10 @@ index 8045d6c9398d1c88595da6e41aa1ed27fb6fbad0..2f7de6dc538d147fd80885c1428112b7
if (!this.persistentDataContainer.isEmpty()) {
c.put("BukkitValues", this.persistentDataContainer.toTagCompound());
diff --git a/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
index facd180da9b7025cf7ae192cbe91317ff4b70472..2d871fafff90e2dbc8c4141f92cdb1544ccdce43 100644
index e964f05c5c0f2d1d9b4c0a3459076e4b95f0f4e8..b28ca267a841f33ba18fff5587a6b0e677dc1ea1 100644
--- a/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
+++ b/src/main/java/org/leavesmc/leaves/command/LeavesCommand.java
@@ -13,6 +13,7 @@ import org.bukkit.plugin.PluginManager;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.command.subcommands.ConfigCommand;
+import org.leavesmc.leaves.command.subcommands.PeacefulModeSwitchCommand;
import org.leavesmc.leaves.command.subcommands.UpdateCommand;

import java.util.ArrayList;
@@ -35,6 +36,7 @@ public final class LeavesCommand extends Command {
@@ -34,6 +34,7 @@ public final class LeavesCommand extends Command {
final Map<Set<String>, LeavesSubcommand> commands = new HashMap<>();
commands.put(Set.of("config"), new ConfigCommand());
commands.put(Set.of("update"), new UpdateCommand());
Expand Down
Loading

0 comments on commit 5a03bd3

Please sign in to comment.