Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.20.2' into 1.20.2-folia
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 12, 2023
2 parents 97d5053 + 445e744 commit 42abddc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 28 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# FeatherMorph

[[English](./README_en.md)]
[[English](./README_en.md)] [[Modrinth](https://modrinth.com/plugin/feathermorph)]

一个适用于Paper的变形插件。

Expand All @@ -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 <!--[^ld]-->

<!-- [^ld]: 我们建议使用Jenkins上版本至少为#1154的构建,Spigot页面上的最新版本并不支持1.19.3。-->
Expand All @@ -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`就是构建出来的插件。
生成的文件将位于`build/libs`中,`FeatherMorph-x.x.x.jar`就是构建出来的插件。
17 changes: 0 additions & 17 deletions src/main/java/xiamomc/morph/events/CommonEventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ public class CommonEventProcessor extends MorphPluginObject implements Listener

private Bindable<Boolean> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 17 additions & 5 deletions src/main/java/xiamomc/morph/updates/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<CommandSender>();
Expand All @@ -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");

Expand Down
114 changes: 114 additions & 0 deletions src/main/java/xiamomc/morph/updates/VersionHandling.java
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 42abddc

Please sign in to comment.