Try   HackMD

前言

版上或者網路上其實有非常多一鍵安裝伺服器或API的方法, 但以我新手的視角來看, 我比較想要先一步一步了解, 再用那些無敵的工具, 畢竟我都還沒開始走, 就可以飛了感覺也怪怪的.
BuildTools 其實就是Spigot整合了基本上大部分的API, 包括伺服器jar檔等等, 而使用它安裝有好有壞, 但如果只是安裝Server的話, 我覺得倒是目前最方便的工具了.

本文

Spigot 下載官網
首先根據前置條件, 需要Git, 並且根據文章所說, 不同版本的Minecraft 也會對應不同版本的JDK
(這邊如果是JRE8的話, 他給的下載地址是Orcale版本的, 其他的則是Adopt版本)

1.17版本以下 1.17(包括.1) 1.17以上
JRE8 OpenJDK 16以上 OpenJDK 17以上

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

前置處理完後接下來到Spigot官網上下載BuildTools.jar, 並放到自己喜歡的位置.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

接下來到檔案的文件夾中右鍵打開Terminal.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

然後根據文檔所說的, 需要哪個版本就在命令後面加上

--rev <version>

例如我需要的是1.19.4 那我所要打的指令就是

java -jar BuildTools.jar --rev 1.19.4

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

在Terminal打完指令後, 應該會跑很長一串
(註 在這邊偵測到我沒安裝到Git, 直接幫我安裝PortableGit = =')
(註 這邊因為我是使用Window Terminal 所以它會自動幫我安裝一個PortableGit的軟體, 不過也只是安裝在當前目錄而已, 不喜歡直接Delete就好)
(註 如果在安裝Git的時候, 把這個打勾, 這時候在wt裡面就會看到多了一個Git的設定檔, 用那個打開並執行一開始的命令, 就不用再額外安裝PortableGit了)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

等指令跑完後, Terminal應該會長這樣.
可以看到總共耗時1分46秒, 馬馬虎虎吧~

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

接下來看到資料夾, 應該會長這樣.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

根據官網描述, Spigot API 文檔應該是包含在Spigot\Spigot-API\target<version>.jar

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

可以看到在這資料夾有兩個jar檔, 一個為<version>shaded.jar, 一個為<version>.jar檔
(註, shaded檔案其實我還沒研究完)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Minecraft Server建立

根據Spigot Server Installation, 我們新增一個名為start.txt檔, 並把文章中的命令直接複製貼上
(註 這邊要記得修改spigot<version>.jar中的版本. 像我的是1.19.4 那就應該更改成spigot-1.19.4.jar
(註 -Xms#G -Xmx#G 為最小使用內存 跟最大使用內存 記得也要設定, 我是都設定為2G
(註 版上有很多一鍵安裝Server的方法, 但其實我完全沒有研究, 之後有研究在放出來)
(註 這邊推薦Notepad++, 就跟D槽女神一樣, 誰用誰知道)

@echo off
java -Xms#G -Xmx#G -XX:+UseG1GC -jar spigot<vserion>.jar nogui
pause

我後來在Paper官網上看到一段超酷的Java 執行檔優化設定, 於是我就把他搬過來了, 因為有點複雜, 很多我也不懂, 所以我晚點研究出來我在貼出來詳解, 先放代碼, 一樣要修改spigot<vserion>.jar

java -Xms2G -Xmx2G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar spigot<vserion>.jar --nogui

然後把.txt檔改成.bat檔 並雙擊開啟,
(註 這邊打開的時候, 會是醜醜的舊版Terminal, 網路上找了一堆後, 統整了一下資料,
額外新建一個txt檔在當前目錄下, 並且填入以下這段, 然後保存成.bat檔)

wt -d "C:\Users\kou\Desktop\Minecraft Server" -p "Windows PowerShell" C:\Users\kou\Desktop\Minecraft Server\start.bat
exit

全部做完後, 你當前的目錄應該會長這樣.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

之後雙擊.bat檔開啟, 成功的話, 畫面應該長這樣

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

最後一段上面說, 要首先同意EULA, 如果要看更多訊息請到eula.txt檔裡面
於是我們接者打開eula.txt檔

[13:50:19] [ServerMain/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

我們看到文檔裡面說, 如果要同意, 請改成TURE, 那沒啥好說的, 把她修改成TRUE後, 再次重新執行bat檔.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

eula=true

成功執行後, 畫面應該為這樣

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這中間我很好奇有一段, 他寫者任何地址都可以訪問本機的25565端口, 我想知道這是真的還是假的.

[13:54:50] [Server thread/INFO]: Starting Minecraft server on *:25565

(這邊新手的我, 本來想講解一下localhost, 127.0.0.1, 0.0.0.0
, 跟*的差別, 但其實這樣講起來會有點太複雜, 所以如果需要的, 我之後再做一篇教學,
但簡單來說, 以我多年混吃等死的經驗, 我是最不希望看到localhost跟127.0.0.1
(在做ssh tunnel的時候, 很常會因為localhost會自動解析成IPv6的地址::1, 導致反向代理連不過來. 之前被這件事弄了好幾天.QQ

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

我們新建一個Terminal 並輸入以下命令, 可以看到有兩段顯示出來了.
剛剛的*.25565 實際上電腦裡面解析成兩條

netstat -ano | findstr "25565"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這邊解釋一下這兩條意思,
0.0.0.0:25565 表示該服務接受任何可用的IPv4地址發送到25565端口的連接,
而[::]:25565則表示接受任何可用的IPv6地址發送到25565端口的連接,
而最後面的數字 21128表示在這台電腦中的程序 PID碼

TCP         0.0.0.0:25565          0.0.0.0:0              LISTENING       21128
TCP         [::]:25565             [::]:0                 LISTENING       21128

我們接者輸入以下命令來確認是哪個程序在監聽這個端口

tasklist|findstr "21128"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

可以發現對應的是java.exe, 畢竟Minecraft 本身就是由Java打造的, 我們到工作管理員 -> 詳細, 其實也能看到PID碼, 如下圖

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

好的, 這時候我們其實伺服器已經開起來了, 回到Minecraft 並輸入以下代碼, 就能直接進入了伺服器裡面了.

127.0.0.1:25565

參考

  1. 127.0.0.1 與 0.0.0.0 的區別

IDEA中匯入SPigot API

這裡用最基本的匯入方式, 不用任何Maven, Gradle, 如果需要這些相關的教學, 我已經大致做好了, 可以參考以下連結, 不過我建議還是使用Maven, 或Gradle, 畢竟這種感覺就跟明明可以坐飛機, 但你選擇游泳, 一樣可以到, 只是會比較累億點點而已.

  1. Maven
  2. Gradle

首先我們打開IDEA,

IDEA中匯出Project to jar file.