9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-23 17:09:21 +00:00

3.0.0-beta1

This commit is contained in:
Xiao-MoMi
2023-04-16 22:54:42 +08:00
parent 670339210b
commit 5a300c1085
272 changed files with 8563 additions and 11816 deletions

View File

@@ -3,33 +3,58 @@ package net.momirealms.customcrops.helper;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import de.tr7zw.changeme.nbtapi.utils.VersionChecker;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.object.basic.ConfigManager;
import net.momirealms.customcrops.api.util.AdventureUtils;
import org.bukkit.Bukkit;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
public class VersionHelper {
private boolean isNewerThan1_19_R2;
private String version;
private boolean isNewerThan1_19_R3;
private String server_version;
private final CustomCrops plugin;
private final boolean isSpigot;
public VersionHelper(CustomCrops plugin) {
this.plugin = plugin;
isVersionNewerThan1_19_R2();
disableUseLessInfo();
this.initialize();
this.disableUseLessInfo();
this.isSpigot = plugin.getServer().getName().equals("CraftBukkit");
}
public boolean isVersionNewerThan1_19_R2() {
if (version == null) {
version = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
String[] split = version.split("_");
int main_ver = Integer.parseInt(split[1]);
if (main_ver >= 20) isNewerThan1_19_R2 = true;
else if (main_ver == 19) isNewerThan1_19_R2 = Integer.parseInt(split[2].substring(1)) >= 2;
else isNewerThan1_19_R2 = false;
if (server_version == null) {
initialize();
}
return isNewerThan1_19_R2;
}
public boolean isVersionNewerThan1_19_R3() {
if (server_version == null) {
initialize();
}
return isNewerThan1_19_R3;
}
private void initialize() {
server_version = plugin.getServer().getClass().getPackage().getName().split("\\.")[3];
String[] split = server_version.split("_");
int main_ver = Integer.parseInt(split[1]);
if (main_ver >= 20) isNewerThan1_19_R2 = (isNewerThan1_19_R3 = true);
else if (main_ver == 19) {
isNewerThan1_19_R2 = Integer.parseInt(split[2].substring(1)) >= 2;
isNewerThan1_19_R3 = Integer.parseInt(split[2].substring(1)) >= 3;
}
else isNewerThan1_19_R2 = false;
}
private void disableUseLessInfo() {
MinecraftVersion.disableBStats();
MinecraftVersion.disableUpdateCheck();
@@ -39,7 +64,7 @@ public class VersionHelper {
field.setAccessible(true);
MinecraftVersion minecraftVersion;
try {
minecraftVersion = MinecraftVersion.valueOf(version.replace("v", "MC"));
minecraftVersion = MinecraftVersion.valueOf(server_version.replace("v", "MC"));
} catch (IllegalArgumentException ex) {
minecraftVersion = MinecraftVersion.UNKNOWN;
}
@@ -62,4 +87,93 @@ public class VersionHelper {
throw new RuntimeException(e);
}
}
public void checkUpdate() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
URL url = new URL("https://api.polymart.org/v1/getResourceInfoSimple/?resource_id=2625&key=version");
URLConnection conn = url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(60000);
InputStream inputStream = conn.getInputStream();
String newest = new BufferedReader(new InputStreamReader(inputStream)).readLine();
String current = plugin.getDescription().getVersion();
inputStream.close();
if (!compareVer(newest, current)) {
AdventureUtils.consoleMessage(ConfigManager.lang.equalsIgnoreCase("chinese") ? "[CustomCrops] 当前已是最新版本" : "[CustomCrops] You are using the latest version.");
return;
}
if (ConfigManager.lang.equalsIgnoreCase("chinese")) {
AdventureUtils.consoleMessage("[CustomCrops] 当前版本: <red>" + current);
AdventureUtils.consoleMessage("[CustomCrops] 最新版本: <green>" + newest);
AdventureUtils.consoleMessage("[CustomCrops] 请到 <u>售后群<!u> 或 <u>https://polymart.org/resource/customcrops.2625<!u> 获取最新版本.");
}
else {
AdventureUtils.consoleMessage("[CustomCrops] Current version: <red>" + current);
AdventureUtils.consoleMessage("[CustomCrops] Latest version: <green>" + newest);
AdventureUtils.consoleMessage("[CustomCrops] Update is available: <u>https://polymart.org/resource/customcrops.2625<!u>");
}
} catch (Exception exception) {
Log.warn("Error occurred when checking update");
}
});
}
private boolean compareVer(String newV, String currentV) {
if (newV == null || currentV == null || newV.isEmpty() || currentV.isEmpty()) {
return false;
}
String[] newVS = newV.split("\\.");
String[] currentVS = currentV.split("\\.");
int maxL = Math.min(newVS.length, currentVS.length);
for (int i = 0; i < maxL; i++) {
try {
String[] newPart = newVS[i].split("-");
String[] currentPart = currentVS[i].split("-");
int newNum = Integer.parseInt(newPart[0]);
int currentNum = Integer.parseInt(currentPart[0]);
if (newNum > currentNum) {
return true;
} else if (newNum < currentNum) {
return false;
} else if (newPart.length > 1 && currentPart.length > 1) {
String[] newHotfix = newPart[1].split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
String[] currentHotfix = currentPart[1].split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
// hotfix2 & hotfix
if (newHotfix.length == 2 && currentHotfix.length == 1) return true;
// hotfix3 & hotfix2
else if (newHotfix.length > 1 && currentHotfix.length > 1) {
int newHotfixNum = Integer.parseInt(newHotfix[1]);
int currentHotfixNum = Integer.parseInt(currentHotfix[1]);
if (newHotfixNum > currentHotfixNum) {
return true;
} else if (newHotfixNum < currentHotfixNum) {
return false;
} else {
return newHotfix[0].compareTo(currentHotfix[0]) > 0;
}
}
} else if (newPart.length > 1) {
return true;
} else if (currentPart.length > 1) {
return false;
}
}
catch (NumberFormatException ignored) {
return false;
}
}
// if common parts are the same, the longer is newer
return newVS.length > currentVS.length;
}
public boolean isSpigot() {
return isSpigot;
}
public String getPluginVersion() {
return plugin.getDescription().getVersion();
}
}