Skip to content

Commit a7120cf

Browse files
committed
fix: actually pull latest stable build
1 parent 64ef018 commit a7120cf

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/content/docs/misc/downloads-service.mdx

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,54 @@ PROJECT="paper"
7878
MINECRAFT_VERSION="\{LATEST_PAPER_RELEASE}"
7979
USER_AGENT="cool-project/1.0.0 ([email protected])"
8080

81-
# First check if the version exists
82-
VERSION_CHECK=$(curl -s -H "User-Agent: $USER_AGENT" https://fill.papermc.io/v3/projects/${PROJECT}/versions/${MINECRAFT_VERSION}/builds)
81+
# First check if the requested version has a stable build
82+
BUILDS_RESPONSE=$(curl -s -H "User-Agent: $USER_AGENT" https://fill.papermc.io/v3/projects/${PROJECT}/versions/${MINECRAFT_VERSION}/builds)
8383

8484
# Check if the API returned an error
85-
if echo "$VERSION_CHECK" | jq -e '.ok == false' > /dev/null 2>&1; then
86-
ERROR_MSG=$(echo "$VERSION_CHECK" | jq -r '.message // "Unknown error"')
85+
if echo "$BUILDS_RESPONSE" | jq -e '.ok == false' > /dev/null 2>&1; then
86+
ERROR_MSG=$(echo "$BUILDS_RESPONSE" | jq -r '.message // "Unknown error"')
8787
echo "Error: $ERROR_MSG"
8888
exit 1
8989
fi
9090

91-
# Get the download URL directly, or null if no stable build exists
92-
PAPERMC_URL=$(curl -s -H "User-Agent: $USER_AGENT" https://fill.papermc.io/v3/projects/${PROJECT}/versions/${MINECRAFT_VERSION}/builds | \
93-
jq -r 'first(.[] | select(.channel == "STABLE") | .downloads."server:default".url) // "null"')
91+
# Try to get a stable build URL for the requested version
92+
PAPERMC_URL=$(echo "$BUILDS_RESPONSE" | jq -r 'first(.[] | select(.channel == "STABLE") | .downloads."server:default".url) // "null"')
93+
FOUND_VERSION="$MINECRAFT_VERSION"
94+
95+
# If no stable build for requested version, find the latest version with a stable build
96+
if [ "$PAPERMC_URL" == "null" ]; then
97+
echo "No stable build for version $MINECRAFT_VERSION, searching for latest version with stable build..."
98+
99+
# Get all versions for the project (using the same endpoint structure as the "Getting the latest version" example)
100+
# The versions are organized by version group, so we need to extract all versions from all groups
101+
# Then sort them properly as semantic versions (newest first)
102+
VERSIONS=$(curl -s -H "User-Agent: $USER_AGENT" https://fill.papermc.io/v3/projects/${PROJECT} | \
103+
jq -r '.versions | to_entries[] | .value[]' | \
104+
sort -V -r)
105+
106+
# Iterate through versions to find one with a stable build
107+
for VERSION in $VERSIONS; do
108+
VERSION_BUILDS=$(curl -s -H "User-Agent: $USER_AGENT" https://fill.papermc.io/v3/projects/${PROJECT}/versions/${VERSION}/builds)
109+
110+
# Check if this version has a stable build
111+
STABLE_URL=$(echo "$VERSION_BUILDS" | jq -r 'first(.[] | select(.channel == "STABLE") | .downloads."server:default".url) // "null"')
112+
113+
if [ "$STABLE_URL" != "null" ]; then
114+
PAPERMC_URL="$STABLE_URL"
115+
FOUND_VERSION="$VERSION"
116+
echo "Found stable build for version $VERSION"
117+
break
118+
fi
119+
done
120+
fi
94121

95122
if [ "$PAPERMC_URL" != "null" ]; then
96123
# Download the latest Paper version
97124
curl -o server.jar $PAPERMC_URL
98-
echo "Download completed"
125+
echo "Download completed (version: $FOUND_VERSION)"
99126
else
100-
echo "No stable build for version $MINECRAFT_VERSION found :("
127+
echo "No stable builds available for any version :("
128+
exit 1
101129
fi
102130
```
103131

0 commit comments

Comments
 (0)