motd.sh and chooseIcon.sh

This commit is contained in:
Jonathan Jara 2025-04-17 16:19:02 -07:00
parent 635d5bb678
commit 5260bd6a24
3 changed files with 130 additions and 1 deletions

View File

@ -1,2 +1,45 @@
# Andromeda
Scripts for Minecraft Servers
This repository contains two scripts, chooseIcon.sh and motd.sh, that dynamically randomize the Minecraft server's icon and message of the day (MOTD) on startup.
## Purpose
- chooseIcon.sh: Randomly selects a PNG icon (icon*.png) from /home/minecraft/icons and sets it as the server-icon.png.
- motd.sh: Randomly selects a line from one of the motd*.txt files in /home/minecraft/motd and updates the motd field in your server.properties.
Requirements
- The icons must be stored in: /home/minecraft/icons
- The MOTD text files must be stored in: /home/minecraft/motd
- The Minecraft server.properties file must be located within the server directory or a subdirectory of where the script is called.
## Usage
To use these scripts, call them from your Minecraft server's start.sh script before the server starts.
If your start.sh file is outside the server directory (i.e., the server is in a subdirectory), you must provide the server directory as a parameter when calling motd.sh.
Example start.sh (located one level above the server folder):
```
#!/bin/bash
# Choose a random server icon
./chooseIcon.sh
# Set a random MOTD (pass the path to the server directory)
./motd.sh ./server
# Start the Minecraft server
cd server
java -Xmx2G -Xms1G -jar server.jar nogui
```
## Future Improvements
- Unique Icon and MOTD Per Server
Add logic to ensure no two concurrently running servers use the same icon or MOTD. This could be done by:
- Tracking assigned icons and MOTDs in temporary files (e.g., .used_icons, .used_motds)
- Automatically releasing resources when servers stop

17
chooseIcon.sh Normal file
View File

@ -0,0 +1,17 @@
mapfile -t ICON_FILES < <(find /home/minecraft/icons -type f -name "icon*.png")
NUM_ICONS=${#ICON_FILES[@]}
if [ $NUM_ICONS -eq 0 ]; then
echo "No icon PNG files found."
exit 1
fi
RAND_INDEX=$((RANDOM % NUM_ICONS))
SELECTED_FILE="${ICON_FILES[$RAND_INDEX]}"
echo "Number of icon PNGs found: $NUM_ICONS"
echo "Selected file: $SELECTED_FILE"
cp $SELECTED_FILE $PWD/server-icon.png

69
motd.sh Normal file
View File

@ -0,0 +1,69 @@
#!/bin/bash
# Function to find server.properties dynamically
find_server_properties() {
local search_dir="$1"
# Look for server.properties in the given directory or parent directories
while [ "$search_dir" != "/" ]; do
if [ -f "$search_dir/server.properties" ]; then
echo "$search_dir/server.properties"
return
fi
search_dir=$(dirname "$search_dir")
done
echo "server.properties not found!"
exit 1
}
# If the script is being called with a path, use that; otherwise, use the current directory
if [ -n "$1" ]; then
SERVER_PROPERTIES="$1/server.properties"
else
SERVER_PROPERTIES=$(find_server_properties "$(pwd)") # Find it starting from the current directory
fi
# Check if server.properties exists
if [ ! -f "$SERVER_PROPERTIES" ]; then
echo "server.properties not found in $SERVER_PROPERTIES"
exit 1
fi
# Find all text files for MOTD
mapfile -t MOTD_FILES < <(find /home/minecraft/motd -type f -name "motd*.txt")
NUM_MOTD=${#MOTD_FILES[@]}
if [ $NUM_MOTD -eq 0 ]; then
echo "No MOTD files found."
exit 1
fi
# Pick a random file
RAND_INDEX=$((RANDOM % NUM_MOTD))
SELECTED_FILE="${MOTD_FILES[$RAND_INDEX]}"
# Get number of lines
NUM_LINES=$(wc -l < "$SELECTED_FILE")
RAND_LINE=$((RANDOM % NUM_LINES + 1))
# Get the selected line
SELECTED_LINE=$(sed -n "${RAND_LINE}p" "$SELECTED_FILE")
echo "Number of MOTD FILES found: $NUM_MOTD"
echo "Number of lines in selected file: $NUM_LINES"
echo "Random line number: $RAND_LINE"
echo "Selected line: $SELECTED_LINE"
# Escape special characters
ESCAPED_LINE=$(echo "$SELECTED_LINE" | sed 's/\\/\\\\/g; s/:/\\:/g')
# Replace or add `motd=` in the found server.properties
if grep -q "^motd=" "$SERVER_PROPERTIES"; then
sed -i.bak "s/^motd=.*/motd=$ESCAPED_LINE/" "$SERVER_PROPERTIES"
else
echo "motd=$ESCAPED_LINE" >> "$SERVER_PROPERTIES"
fi
echo "Updated server.properties with new MOTD!"