diff --git a/README.md b/README.md index 78672fd0..48d1f35a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ # FeatherMorph -[[English](./README_en.md)] +[[English](./README_en.md)] [[Modrinth](https://modrinth.com/plugin/feathermorph)] 一个适用于Paper的变形插件。 @@ -20,7 +20,7 @@ ### 依赖关系 FeatherMorph至少需要下面这些依赖才能运行: -- Paper或基于Paper的1.19.3服务器 +- 一个Paper或基于Paper的服务器 - [LibsDisguises](https://www.spigotmc.org/resources/libs-disguises-free.81/) >= 10.0.32 @@ -32,10 +32,10 @@ FeatherMorph至少需要下面这些依赖才能运行: ### 构建 ```bash #!/usr/bin/env bash -git clone https://github.com/XiaMoZhiShi/MorphPlugin -cd MorphPlugin +git clone https://github.com/XiaMoZhiShi/FeatherMorph +cd FeatherMorph ./gradlew build --no-daemon ``` -生成的文件将位于`build/libs`中,`FeatherMorph-x.x.x.jar`就是构建出来的插件。 \ No newline at end of file +生成的文件将位于`build/libs`中,`FeatherMorph-x.x.x.jar`就是构建出来的插件。 diff --git a/src/main/java/xiamomc/morph/events/CommonEventProcessor.java b/src/main/java/xiamomc/morph/events/CommonEventProcessor.java index 9e77ac1c..a5801d44 100644 --- a/src/main/java/xiamomc/morph/events/CommonEventProcessor.java +++ b/src/main/java/xiamomc/morph/events/CommonEventProcessor.java @@ -76,23 +76,6 @@ public class CommonEventProcessor extends MorphPluginObject implements Listener private Bindable unMorphOnDeath; - //region Test - @EventHandler - public void onLoadComplete(ManagerFinishedInitializeEvent e) - { - var players = List.of("Icalingua", "Player111", "NekoCrystal"); - var ids = List.of("player:NekoCrystal", "null", "minecraft:allay"); - - for (var str : players) - { - var player = Bukkit.getPlayerExact(str); - if (player == null) continue; - if (ids.get(players.indexOf(str)).equals("null")) continue; - e.manager.morph(null, player, ids.get(players.indexOf(str)), null); - } - } - //endregion - @EventHandler public void onEntityDeath(EntityDeathEvent e) { diff --git a/src/main/java/xiamomc/morph/network/server/MorphClientHandler.java b/src/main/java/xiamomc/morph/network/server/MorphClientHandler.java index a84a2501..5e30b7b0 100644 --- a/src/main/java/xiamomc/morph/network/server/MorphClientHandler.java +++ b/src/main/java/xiamomc/morph/network/server/MorphClientHandler.java @@ -316,7 +316,7 @@ public void waitUntilReady(Player player, Runnable r) if (bool == null) { - logger.info("should remove for " + player.getName()); + //logger.info("should remove for " + player.getName()); return; } diff --git a/src/main/java/xiamomc/morph/updates/UpdateHandler.java b/src/main/java/xiamomc/morph/updates/UpdateHandler.java index 838c7a40..33be86cc 100644 --- a/src/main/java/xiamomc/morph/updates/UpdateHandler.java +++ b/src/main/java/xiamomc/morph/updates/UpdateHandler.java @@ -188,9 +188,10 @@ private void onUpdateReqFinish(CloseableHttpResponse response, int reqId, return; } - var currentVersion = plugin.getPluginMeta().getVersion(); - var latestVersion = matchMeta.versionNumber; - if (latestVersion == null) + var currentVersion = VersionHandling.toVersionInfo(plugin.getPluginMeta().getVersion()); + var latestVersion = VersionHandling.toVersionInfo(matchMeta.versionNumber); + + if (latestVersion.isInvalid()) { if (onFinish != null) onFinish.accept(CheckResult.FAIL); @@ -208,6 +209,16 @@ private void onUpdateReqFinish(CloseableHttpResponse response, int reqId, return; } + if (latestVersion.compare(currentVersion) == VersionHandling.CompareResult.OLDER) + { + logger.info("Your version is newer than released for %s!".formatted(Bukkit.getMinecraftVersion())); + + if (onFinish != null) + onFinish.accept(CheckResult.ALREADY_LATEST); + + return; + } + // 提醒服务器关于更新的消息 var serverOps = Bukkit.getOperators(); var sendTargets = new ObjectArrayList(); @@ -230,8 +241,9 @@ private void onUpdateReqFinish(CloseableHttpResponse response, int reqId, sendTargets.add(Bukkit.getConsoleSender()); this.msgPrimary = UpdateStrings.newVersionAvailable() - .resolve("current", currentVersion) - .resolve("origin", latestVersion); + .resolve("current", currentVersion.toString()) + .resolve("origin", latestVersion.toString()); + this.msgSecondary = UpdateStrings.update_here() .resolve("url", "https://modrinth.com/plugin/feathermorph"); diff --git a/src/main/java/xiamomc/morph/updates/VersionHandling.java b/src/main/java/xiamomc/morph/updates/VersionHandling.java new file mode 100644 index 00000000..003f6297 --- /dev/null +++ b/src/main/java/xiamomc/morph/updates/VersionHandling.java @@ -0,0 +1,114 @@ +package xiamomc.morph.updates; + +import org.jetbrains.annotations.NotNull; + +public class VersionHandling +{ + public static VersionInfo toVersionInfo(String str) + { + if (str == null) return VersionInfo.INVALID_VERSION; + + var strSpilt = str.split("\\."); + + var major = strSpilt.length >= 1 ? tryParse(strSpilt[0]) : -1; + var minor = strSpilt.length >= 2 ? tryParse(strSpilt[1]) : -1; + var patch = strSpilt.length >= 3 ? tryParse(strSpilt[2]) : -1; + var edition = strSpilt.length >= 4 ? strSpilt[3] : "Standard"; + + return new VersionInfo(major, minor, patch, edition); + } + + private static int tryParse(String str) + { + try + { + return Integer.parseInt(str); + } + catch (Throwable t) + { + return -1; + } + } + + public static record VersionInfo(int major, int minor, int patch, @NotNull String channel) + { + @Override + public String toString() + { + return "%s.%s.%s".formatted(major, minor, patch); + } + + public boolean isInvalid() + { + return this == INVALID_VERSION; + } + + @Override + public boolean equals(Object o) + { + if (!(o instanceof VersionInfo other)) return false; + + return major == other.major + && minor == other.minor + && patch == other.patch + && channel.equalsIgnoreCase(other.channel); + } + + /** + * 将此版本和另一版本比对 + * @param other + * @return + */ + public CompareResult compare(VersionInfo other) + { + if (!other.channel.equalsIgnoreCase(this.channel)) + return CompareResult.NOT_ON_SAME_CHANNEL; + + var majorCompare = integerCompare(other.major, this.major); + var minorCompare = integerCompare(other.minor, this.minor); + var patchCompare = integerCompare(other.patch, this.patch); + + if (majorCompare == CompareResult.EQUAL + && minorCompare == CompareResult.EQUAL + && patchCompare == CompareResult.EQUAL) + { + return CompareResult.EQUAL; + } + else if (majorCompare == CompareResult.NEWER + || minorCompare == CompareResult.NEWER + || patchCompare == CompareResult.NEWER) + { + return CompareResult.OLDER; + } + else if (majorCompare == CompareResult.OLDER + || minorCompare == CompareResult.OLDER + || patchCompare == CompareResult.OLDER) + { + return CompareResult.NEWER; + } + + return CompareResult.NOT_ON_SAME_CHANNEL; + } + + /** + * 比较两个数之间的版本关系 + * @param a + * @param b + * @return a 相较于 b 的关系,如果a更大就返回NEWER,反之返回OLDER + */ + private CompareResult integerCompare(int a, int b) + { + return a > b ? CompareResult.NEWER : (a == b ? CompareResult.EQUAL : CompareResult.OLDER); + } + + public static VersionInfo INVALID_VERSION = new VersionInfo(0, 0, 0, "Invalid"); + } + + public enum CompareResult + { + EQUAL, + NEWER, + OLDER, + NOT_ON_SAME_CHANNEL + } +}