前景需要#
最近在研究 Minecraft 服務端插件開發,有一個場景需要用到伺服器重啟時重置地圖
搜了一圈好像並沒有什麼現有的盡人意的插件,於是決定自己寫一個
探索#
最開始想直接用 Slime World Manager
的,但是一看倉庫
This repository was archived by the owner on Apr 21, 2025. It is now read-only.
呃呃,不過恰好又看到它的一個衍生 fork
https://github.com/InfernalSuite/AdvancedSlimePaper
看起來很有用,不過它的插件版好像不太喜歡我的伺服器(Paper 1.20.4 + Java 24)
反正就報缺類
但是呢他們好像又起了個分支去魔改 Paper 了,我不是很喜歡
沒辦法了,直接用土方法
實現#
我們都知道 Bukkit API 提供了 onEnable
和 onDisable
原本想的是直接在 onEnable
的時候備份還原一氣呵成,但是 Bukkit 的插件加載是比 world 後的
而且我們不能通過 Bukkit API 直接卸載默認世界(也就是 world
、world_nether
、world_end
)
用正常的 Bukkit.unloadWorld(Bukkit.getWorld("world"), false);
是沒法卸載的,即使沒報錯(實際上被忽略了)
所以我們不能在 onEnable
的時候還原
那有沒有更好的位置呢,相信聰明的你一定想到了,那就是 onDisable
處
所以我們就可以在 onEnable
的時候備份地圖,在每次 onDisable
的時候給複製回去
大致如下
private void copyWorld(Path source, Path target) throws IOException {
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path targetDir = target.resolve(source.relativize(dir));
Files.createDirectories(targetDir);
return FileVisitResult.CONTINUE;
}
});
}
然後在 onDisable
處調用就行了
欸你問我 world 被加載後那個 session.lock
被掛鎖了沒法複製?那我們跳過不就好了吗!
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.getFileName().toString().endsWith(".lock")) {
Files.copy(file, target.resolve(source.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
}
return FileVisitResult.CONTINUE;
}
然後你已經會寫這個邏輯了,快去寫個插件!
注意事項#
這個方法對於那些不好好關伺服器的人不友好(指不用 Ctrl+C
或者 /stop
的)
因為那樣無法觸發 onDisable
所以無法觸發還原事件(不過不用擔心,你下次正常關就能還原)
調用 copyWorld
個人認為應該保持同步(Bukkit 不就是同步卸載插件的嗎,所以應該不用擔心 race condition)
對於存檔過於大的伺服器可能需要數十秒才能完全 備份 / 還原 完成(所以要考驗你的硬碟 IO 啦!)
當然,以上只是最壞的解決方案,但是也最有效
END#
輪子我已經造好了,在遵循君子協議的情況下隨便 skid
https://github.com/NyaStudio/MapBackup
此文由 Mix Space 同步更新至 xLog
原始鏈接為 https://blog.xcnya.cn/posts/technology/128.html