blob: 593e71a56a8168bb02f6cf16b0d7f3b7e4e0dad2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#!/bin/sh
set -e
lower() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}
upper() {
echo "$1" | tr '[:lower:]' '[:upper:]'
}
# global
MC_TYPE="$(lower "${MC_TYPE:-vanilla}")"
MC_VERSION="$(lower "${MC_VERSION:-latest}")"
# papermc
PAPERMC_BUILD="$(lower "${PAPERMC_BUILD:-latest}")"
# fabric
FABRIC_LOADER="$(lower "${FABRIC_LOADER:-latest}")"
FABRIC_INSTALLER="$(lower "${FABRIC_INSTALLER:-latest}")"
# other
QUIET=1
req() {
res=$(curl --fail --silent "$1")
code=$?
if [ $code -ne 0 ]; then
echo "null";
else
echo "$res";
fi
}
error() {
printf '\x1b[31m[ERROR]\t\x1b[0m%s\n' "$*" > /dev/stderr
exit 1;
}
log() {
if [ "$QUIET" -eq 0 ]; then
printf '\x1b[0m[LOG]\t%s\n' "$*" > /dev/stdout
fi
}
index_array() {
array="$1"
key="$2"
type="$3"
if [ "$key" = "latest" ]; then
index=$(echo "$array" | jq ". | index( last )");
else
index=$(echo "$array" | jq ". | index( \"$key\" )");
fi
if [ "$index" = "null" ]; then
error "Invalid $type: $key"
else
echo "$array" | jq -r ".[$index]";
fi
}
#https://api.papermc.io/v2/projects/velocity/versions/3.3.0-SNAPSHOT/
papermc() {
api="https://api.papermc.io/v2/projects"
log "Using papermc api v2"
get_version() {
log "Getting VERSION for '$MC_TYPE'"
versions=$(req "$api/$MC_TYPE" | jq -r '.versions')
if [ "$versions" = "null" ]; then
error "Invalid TYPE: $MC_TYPE (no versions)"
fi
MC_VERSION=$(index_array "$versions" "$MC_VERSION" "VERSION")
log "VERSION: $MC_VERSION"
}
get_build() {
log "Getting BUILD for '$MC_TYPE@$MC_VERSION'"
builds=$(req "$api/$MC_TYPE/versions/$MC_VERSION" | jq -r ".builds")
if [ "$builds" = "null" ]; then
error "Invalid VERSION: $MC_TYPE@$MC_TYPE (no builds)"
fi
PAPERMC_BUILD=$(index_array "$builds" "$PAPERMC_BUILD" "BUILD")
log "BUILD: $PAPERMC_BUILD"
}
get_version
get_build
echo "$api/$MC_TYPE/versions/$MC_VERSION/builds/$PAPERMC_BUILD/downloads/$MC_TYPE-$MC_VERSION-$PAPERMC_BUILD.jar"
}
fabric() {
api="https://meta.fabricmc.net/v2/versions"
log "Using fabric api v2"
get_fabric_impl() {
type="$1"
value="$2"
res=""
if [ "$value" = "latest" ]; then
res=$(req "$api/$type" | jq -r 'map(select(.stable == true) | .version) | reverse')
else
res=$(req "$api/$type" | jq -r 'map(.version) | reverse')
fi;
echo $res;
}
get_game() {
log "Getting VERSION for '$MC_TYPE'"
games=$(get_fabric_impl "game" "$MC_VERSION")
MC_VERSION=$(index_array "$games" "$MC_VERSION" "VERSION")
log "VERSION: $MC_VERSION"
}
get_loader() {
log "Getting LOADER for '$MC_TYPE'"
loaders=$(get_fabric_impl "loader" "$FABRIC_LOADER")
FABRIC_LOADER=$(index_array "$loaders" "$FABRIC_LOADER" "LOADER")
log "LOADER: $FABRIC_LOADER"
}
get_installer() {
log "Getting INSTALLER for '$MC_TYPE'"
installers=$(get_fabric_impl "installer" "$FABRIC_INSTALLER")
FABRIC_INSTALLER=$(index_array "$installers" "$FABRIC_INSTALLER" "INSTALLER")
log "INSTALLER: $FABRIC_INSTALLER"
}
get_game
get_loader
get_installer
echo "$api/loader/$MC_VERSION/$FABRIC_LOADER/$FABRIC_INSTALLER/server/jar"
}
vanilla() {
versions=$(req 'https://launchermeta.mojang.com/mc/game/version_manifest.json' | jq '.versions | map({ "id": .id, "type": .type, "url": .url }) | reverse');
map_type_impl() {
type="$1"
echo "$versions" | jq -r "map(select(.type == \"$type\"))"
}
get_version() {
log "Getting VERSION for '$MC_TYPE'"
version=""
case "$MC_VERSION" in
"latest")
version=$(map_type_impl "release" | jq -r 'last')
;;
"snapshot")
version=$(map_type_impl "snapshot" | jq -r 'last')
;;
*)
version=$(echo "$versions" | jq ".[] | select(.id == \"$MC_VERSION\")")
;;
esac
if [ "$version" = "null" ] || [ "$version" = "" ]; then
error "Invalid VERSION: $MC_VERSION"
fi
log "VERSION: $(echo "$version" | jq -r '.id')"
json=$(echo "$version" | jq -r '.url')
url=$(req "$json" | jq -r '.downloads.server.url')
if [ "$url" = "null" ]; then
error "Cannot get server jar for VERSION: $MC_VERSION"
fi
echo "$url"
}
get_version
}
get_url() {
log "TYPE: $MC_TYPE"
case "$MC_TYPE" in
"vanilla")
vanilla
;;
"paper" | "waterfall" | "velocity" | "folia")
papermc
;;
"fabric")
fabric
;;
*)
error "Invalid TYPE: $MC_TYPE"
;;
esac
}
usage() {
printf "usage: mcjar [options]\n"
}
help() {
printf "usage: mcjar [options]\n\n"
printf "options:\n"
printf "\t-h\tshow this message\n"
printf "\t-l\tshow logging (verbose)\n\n"
printf "\t-t TYPE\t\tset the mc server type\n"
printf "\t [vanilla,fabric,paper,folia,velocity,waterfall]\n\n"
printf "\t-v VERSION\tset the mc server version\n"
printf "\t [latest,snapshot,<version>]\n\n"
printf "environment:\n"
printf "\tMC_TYPE\t\tsets the mc server type (same as -t TYPE)\n"
printf "\tMC_VERSION\tsets the mc server version (same as -v VERSION)\n\n"
printf "\tPAPERMC_BUILD\tsets the papermc build\n\n"
printf "\tFABRIC_LOADER\t\tsets the fabric loder version\n"
printf "\tFABRIC_INSTALLER\tsets the fabric installer version\n\n"
}
while getopts "hlt:v:" arg > /dev/null; do
case $arg in
h)
help
exit 0
;;
l)
QUIET=0
;;
t)
MC_TYPE=${OPTARG}
;;
v)
MC_VERSION=${OPTARG}
;;
?)
error "unknown option"
exit 1
;;
esac;
done;
get_url
|