-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpocketmine.sh
executable file
·277 lines (247 loc) · 5.96 KB
/
pocketmine.sh
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
PHP='./bin/php'
PHP_OPTS='-d enable_dl=On -d date.timezone=Asia/Taipei'
PMMP='PocketMine-MP.php'
WORLD_WORLD='world'
WORLDS="$WORLD_WORLD"
MAP_DIR='worlds'
LOG_DIR='log'
LOG_FILE='console.log'
EVIL_KEY='C-c'
MAX_TRY='600'
SESSION='PocketMine'
PREFIX='C-a'
CMDS_BEFORE_STOP=''
#= functions ==================================================================
check_has_php(){
#. The PHP binary installed by PocketMine-MP_Installer.
if [[ -f ./php5/bin/php ]]; then
mkdir -p -m 0777 bin/
mv -f ./php5/bin/php ./bin/php
#rm -rf ./php5/
fi
#. do my stuff
if [[ -f ./bin/php ]]; then
PHP='./bin/php'
elif [[ -f $PHP ]]; then
PHP=$PHP
else
echo 'Install the PHP first!!'
echo 'See: https://github.com/PocketMine/PocketMine-MP/wiki/Setting-up-a-Server#wiki-linux'
exit 0
fi
}
check_has_tmux(){
if [[ -z $(which tmux) ]]; then
"Install the tmux first!!"
exit 0
fi
}
check_if_in_tmux(){
if [[ "$TERM" == 'screen' ]] || [[ -n $TMUX ]]; then
echo "Can't Create/Attach session in other tmux session!"
exit 1
fi
}
check_result(){
if [[ "$?" == 0 ]]; then
echo "$1: Done."
else
echo "$1: Fail!"
exit 1
fi
}
check_fail(){
if [[ "$?" != 0 ]]; then
echo 'Fail!'
exit 1
fi
}
bind_key(){
tmux set-option -t $SESSION prefix $PREFIX >/dev/null 2>&1
#. map Ctrl+c to prefix-key/detach to prevent PocketMine server from killing.
tmux set-option -t $SESSION prefix2 $EVIL_KEY >/dev/null 2>&1
tmux bind $EVIL_KEY detach
}
unbind_key(){
tmux unbind $EVIL_KEY 2>/dev/null
}
start_server(){
tmux new-session -d -s $SESSION
tmux new-window -t "$SESSION":1 -n Console \
"$PHP $PHP_OPTS $PMMP"
check_result 'Start'
tmux kill-window -t "$SESSION":0
bind_key
}
attach_console(){
check_if_in_tmux
tmux attach-session -t "$SESSION" #\; set-option -g prefix $PREFIX
}
detach_clients(){
tmux detach -s "$SESSION" 2>/dev/null
}
send_command(){
tmux send -t $SESSION "$1" ENTER 2>/dev/null
}
send_ctrl_key(){
tmux send -t $SESSION "$1" 2>/dev/null
}
clean_cmd_line(){
#. PMMP can't handle ctrl/meta/up/down/left/right keys current,
#. need a solution to clear the command line.
#. right way
#send_ctrl_key 'C-e' #. move to end of line (END)
#for (( i = 0; i < 50; i++ )); do
# send_ctrl_key 'C-h' #. clean the command line (BACKSPACE)
#done
#. dirty way
send_command 'iamjustaspam'
}
session_exist(){
#. print message if not exist
tmux has-session -t $SESSION 2>&1
}
stop_server(){
echo 'Please wait ...'
detach_clients
unbind_key
clean_cmd_line
try=0
#. save data
send_command "save-all"
while [[ -z $(session_exist) ]]; do
#. send custom commands
#. TODO: handle the args.
if [[ ! -z "$CMDS_BEFORE_STOP" ]]; then
for cmd in CMDS_BEFORE_STOP; do
send_command "$cmd"
done
fi
#. stop the server
send_command "stop"
sleep 1
try=$(($try+1))
if [[ "$try" -ge "$MAX_TRY" ]]; then
echo "Stop: Fail."
exit 1
fi
done
echo 'Stop: Done.'
}
kill_server(){
detach_clients
unbind_key
clean_cmd_line
tmux kill-session -t "$SESSION" 2>/dev/null
check_result 'Stop'
}
purge_maps(){
rm -rf $MAP_DIR 2>/dev/null
}
rename_maps(){
for dir in "$WORLDS"; do
mv $MAP_DIR/$dir $MAP_DIR/$dir.$(date +"%s") 2>/dev/null
done
}
start_server_if_need(){
if [[ -z $(session_exist) ]]; then
echo "session exist: $SESSION"
else
start_server
fi
}
today(){
date +"%Y%m%d_%s"
}
log_rotate(){
log=$LOG_DIR/server.$(today).log
mkdir -p $LOG_DIR
cat $LOG_FILE > $log
echo "Log rotate to $log!" > $LOG_FILE
}
confirm(){
read -p "Are you sure? (yes/no): " answer
case $answer in
yes)
foo=bar
;;
*)
echo 'Abort.'
exit 0
;;
esac
}
strip_color(){
cat -v $1 | sed -e 's#\^\[\[\([0-9]*;*\)*m##g'
}
usage(){
echo '
____ _ _ __ __ _
| _ \ ___ ___| | _____| |_| \/ (_)_ __ ___
| |_) / _ \ / __| |/ / _ \ __| |\/| | | `_ \ / _ \
| __/ (_) | (__| < __/ |_| | | | | | | | __/
|_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___|_Ctrl
'
echo -e "Usage: $(basename $0) [CMD]\n"
echo -e "Available CMDs:"
echo -e " start\t\t\tStart PocketMine server."
echo -e " attach\t\tAttach PocketMine server console."
echo -e " console\t\tAlias for attach."
echo -e " stop\t\t\tStop PocketMine server. (graceful)"
echo -e " restart\t\tRestart PocketMine server. (graceful)"
echo -e " kill\t\t\tKill the PocketMine server."
echo -e " cmd \"MY COMMAND\"\tSend command to PocketMine server."
echo -e " plainlog \"LOGFILE\"\tStrip color code from log file."
echo -e " log-rotate\t\tLog rotate."
echo -e " remake-world\t\tRegenerate worlds and keep old worlds. (need restart)"
echo -e " purge-world\t\tRegenerate worlds. (need restart)"
}
#= main program ===============================================================
cd $(dirname $0)
check_has_php
check_has_tmux
case $1 in
start)
start_server_if_need
;;
attach|console)
attach_console
;;
stop)
stop_server
;;
restart)
stop_server
start_server
;;
kill)
kill_server
;;
cmd)
detach_clients
clean_cmd_line
send_command "$2"
;;
log-rotate)
log_rotate
;;
remake-world)
confirm
stop_server
rename_maps
start_server
;;
purge-world)
confirm
stop_server
purge_maps
start_server
;;
plainlog)
strip_color "$2"
;;
*)
usage
;;
esac