-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannouncements.sh
206 lines (164 loc) · 6.04 KB
/
announcements.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
#!/bin/bash
##############################################################
#
# Vanilla Minecraft Announcements Script
# 2015-11-10
#
# https://github.com/seifer44/minecraft-announcements
# Version 1.0.2
#
##############################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################
######################
# VARIABLES #
######################
# Log file location of the Minecraft service logs. Generally, this lives under
# $MINECRAFT/logs/latest.log
loglocation=/opt/minecraft/service/logs/latest.log
# The full path of the script. No trailing /
scriptlocation=/opt/minecraft/scripts
# The screen name of your Minecraft instance.
screenname=minecraft
######################
# FUNCTIONS #
######################
as_user() {
if [ $user == $serverusername ] ; then
bash -c "$1"
else
su - $serverusername -c "$1"
fi
}
######################
# CORE FUNCTIONALITY #
# STARTUP #
######################
if [ ! -d $scriptlocation/announcements.d ]
then
mkdir $scriptlocation/announcements.d
echo "Directory $scriptlocation/announcements.d was missing! Created it."
fi
if [ $(ls $scriptlocation/announcements.d | wc -l) -eq 0 ]
then
echo -e "Directory $scriptlocation/announcements.d has nothing in it! This script won't work unless you define some announcements!\nRead the README for more info. Exiting error code 1."
exit 1
fi
cd $scriptlocation/announcements.d
IFSorig=$IFS
IFS="
"
### We analyze all of the files with arguments here.
# If the file only has 2 lines, then just dump line 1 into the announcechecks array
# and line 2 as announcemsgs.
# If lines are greater than 2, then make the announcemsgs line the last line each time.
for i in $(ls)
do
echo "Analyzing file $i" # Debug
linenum=$(wc -l $i | cut -f1 -d " ")
if [ $linenum -gt 2 ]
then
echo " File $i has multiple arguments in it!" # Debug
echo " Number of lines: $linenum" # Debug
echo " Expressions: $(expr $linenum - 1)" # Debug
count=1
while [[ $count -lt $linenum ]]
do
announcechecks+=( "$(sed -n $count\p $i)" )
announcemsgs+=( "$(sed -n $linenum\p $i)" )
count=$(( $count + 1))
done
else
echo " File $i has a single argument!" # Debug
announcechecks+=( $(sed -n '1p' $i) )
announcemsgs+=( $(sed -n '2p' $i) )
fi
done
echo -e "\n#################\n"
count=0 # Debug
while [ $count -lt ${#announcechecks[*]} ] # Debug
do # Debug
echo "Argument & msg:" # Debug
echo " ${announcechecks[$count]}" # Debug
echo -e " ${announcemsgs[$count]}\n" # Debug
count=$(( $count + 1 ))
done # Debug
# Create a tmp file that gets emptied at startup and
# lists the maximum number of arguments for each bang arg
cat /dev/null > .announcements.sh.tmp
echo "Cleared .announcements.sh.tmp"
for i in ${announcechecks[*]}
do
if [[ $i == "!"* ]]
then
echo "Populating .announcements.sh.tmp with $i" # Debug
# Since ls always goes in alphabetical order, the higher numbers will come later.
# If we get a higher number, then simply trash the old entry.
# If the argument hasn't been entered in yet, then throw it in.
grep -q $(echo $i | cut -f1 -d " ") .announcements.sh.tmp
if [ $? -eq 0 ]
then
echo "Found a duplicate! Removed the lesser arg." # Debug
sed -i "/$(echo $i | cut -f1 -d ' ')/d" .announcements.sh.tmp
fi
echo $i >> .announcements.sh.tmp
fi
done
IFS=$IFSorig
echo -e "\n#################\n\nScript is now running. To apply any changes, restart the script.\nPress Control + C to exit the script." # Debug
######################
# LOOP FUNCTION #
######################
while read line
do
useronline=$(echo $line | cut -d " " -f4 | sed 's/\[.*//g' | sed 's/<//g' | sed 's/>//g')
firstarg=$(echo $line | cut -d " " -f5)
# Set second incoming line argument only if it starts with a bang
if [[ $(echo $line | cut -d " " -f5 ) == "!"* ]]
then
secondbangarg=$(echo $line | cut -d " " -f6)
if [ -z $secondbangarg ]
then
secondbangarg=1
fi
fi
# Run the incoming line against all of the checks we found above.
argnum=${#announcechecks[@]}
count=0
while [ $count -lt $argnum ]
do
# echo "Incoming line. Testing against \"${announcechecks[$count]}\"" # Debug
# Test if the pattern in each file matches the line that just came in.
if [[ "$line" == *${announcechecks[$count]} ]]
then
# echo " Found match!" # Debug
as_user "screen -p 0 -S $screenname -X stuff \"tellraw $useronline $(echo ${announcemsgs[$count]} | sed 's/"/\\"/g') $(printf \\r)\""
# echo " Max args for $firstarg: $(grep $firstarg .announcements.sh.tmp | cut -f2 -d " ")" # Debug
# If there is more than just 1 file for that ! argument, mention that!
if [ $(grep $firstarg .announcements.sh.tmp | wc -w) -eq 2 ]
then
if [ $secondbangarg -lt $(grep $firstarg .announcements.sh.tmp | cut -f2 -d " ") ]
then
# echo " Higher number of arguments found. Announcing." # Debug
# If you want to use a different message, make sure you escape EVERY " and ' except for the very first and last ".
as_user "screen -p 0 -S $screenname -X stuff \"tellraw $useronline $( echo "[\"\",{\"text\":\"There are more arguments for $firstarg\",\"color\":\"dark_green\"},{\"text\":\". Try $firstarg $((secondbangarg + 1)) for more info.\"}]"| sed 's/"/\\"/g')$(printf \\r)\""
fi
fi
fi
count=$(( $count + 1 ))
# echo " Line test $count" # Debug
done
done < <(tail -f -n 0 $loglocation)