Advertisement
KC9UZR

Ultmate Mincraft Server Bash Script

Jun 15th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 28.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Minecraft Server Management Script
  4. # Supports installation, updates, and mod management
  5.  
  6. set -e
  7.  
  8. # Configuration
  9. SERVER_DIR="$HOME/minecraft-server"
  10. JAVA_VERSION="21"  # Minecraft 1.21+ requires Java 17+
  11. MINECRAFT_VERSION="1.21.4"  # Latest stable version
  12. FORGE_VERSION="51.0.33"  # Compatible Forge version
  13. SERVER_JAR="minecraft_server.${MINECRAFT_VERSION}.jar"
  14. FORGE_JAR="forge-${MINECRAFT_VERSION}-${FORGE_VERSION}-installer.jar"
  15. SERVER_RAM="4G"  # Adjust based on your system
  16.  
  17. # Colors for output
  18. RED='\033[0;31m'
  19. GREEN='\033[0;32m'
  20. YELLOW='\033[1;33m'
  21. BLUE='\033[0;34m'
  22. NC='\033[0m' # No Color
  23.  
  24. # Top 10 popular Minecraft mods (URLs and filenames)
  25. declare -A MODS=(
  26.     ["JEI"]="https://www.curseforge.com/api/v1/mods/238222/files/latest/download"
  27.     ["OptiFine"]="https://optifine.net/adloadx?f=OptiFine_1.21.4_HD_U_J2.jar"
  28.     ["JourneyMap"]="https://www.curseforge.com/api/v1/mods/32274/files/latest/download"
  29.     ["Iron Chests"]="https://www.curseforge.com/api/v1/mods/228756/files/latest/download"
  30.     ["Biomes O' Plenty"]="https://www.curseforge.com/api/v1/mods/220318/files/latest/download"
  31.     ["Tinkers Construct"]="https://www.curseforge.com/api/v1/mods/74072/files/latest/download"
  32.     ["Applied Energistics 2"]="https://www.curseforge.com/api/v1/mods/223794/files/latest/download"
  33.     ["Thermal Expansion"]="https://www.curseforge.com/api/v1/mods/69163/files/latest/download"
  34.     ["Waystones"]="https://www.curseforge.com/api/v1/mods/245755/files/latest/download"
  35.     ["Storage Drawers"]="https://www.curseforge.com/api/v1/mods/223852/files/latest/download"
  36. )
  37.  
  38. # Logging function
  39. log() {
  40.     echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
  41. }
  42.  
  43. error() {
  44.     echo -e "${RED}[ERROR]${NC} $1" >&2
  45. }
  46.  
  47. success() {
  48.     echo -e "${GREEN}[SUCCESS]${NC} $1"
  49. }
  50.  
  51. warn() {
  52.     echo -e "${YELLOW}[WARNING]${NC} $1"
  53. }
  54.  
  55. # Check if Java is installed and install if needed
  56. install_java() {
  57.     log "Checking Java installation..."
  58.    
  59.     if command -v java &> /dev/null; then
  60.         JAVA_VER=$(java -version 2>&1 | head -n1 | cut -d'"' -f2 | cut -d'.' -f1)
  61.         if [ "$JAVA_VER" -ge "$JAVA_VERSION" ]; then
  62.             success "Java $JAVA_VER is already installed"
  63.             return 0
  64.         else
  65.             warn "Java $JAVA_VER found, but Java $JAVA_VERSION+ is required"
  66.         fi
  67.     fi
  68.    
  69.     log "Installing OpenJDK $JAVA_VERSION..."
  70.     sudo apt update
  71.     sudo apt install -y openjdk-${JAVA_VERSION}-jdk
  72.    
  73.     if command -v java &> /dev/null; then
  74.         success "Java installed successfully"
  75.     else
  76.         error "Failed to install Java"
  77.         exit 1
  78.     fi
  79. }
  80.  
  81. # Create server directory structure
  82. setup_directories() {
  83.     log "Setting up server directories..."
  84.    
  85.     mkdir -p "$SERVER_DIR"
  86.     mkdir -p "$SERVER_DIR/mods"
  87.     mkdir -p "$SERVER_DIR/backups"
  88.     mkdir -p "$SERVER_DIR/logs"
  89.    
  90.     success "Directory structure created"
  91. }
  92.  
  93. # Download Minecraft server
  94. download_server() {
  95.     log "Downloading Minecraft server $MINECRAFT_VERSION..."
  96.    
  97.     cd "$SERVER_DIR"
  98.    
  99.     # Download vanilla server
  100.     if [ ! -f "$SERVER_JAR" ]; then
  101.         wget -O "$SERVER_JAR" "https://piston-data.mojang.com/v1/objects/450698d1863ab5180c25d7c804ef0fe6369dd1ba/server.jar"
  102.         success "Minecraft server downloaded"
  103.     else
  104.         log "Server jar already exists"
  105.     fi
  106.    
  107.     # Accept EULA
  108.     echo "eula=true" > eula.txt
  109.     log "EULA accepted"
  110. }
  111.  
  112. # Download and install Forge
  113. install_forge() {
  114.     log "Installing Minecraft Forge..."
  115.    
  116.     cd "$SERVER_DIR"
  117.    
  118.     # Download Forge installer
  119.     if [ ! -f "$FORGE_JAR" ]; then
  120.         wget -O "$FORGE_JAR" "https://maven.minecraftforge.net/net/minecraftforge/forge/${MINECRAFT_VERSION}-${FORGE_VERSION}/forge-${MINECRAFT_VERSION}-${FORGE_VERSION}-installer.jar"
  121.     fi
  122.    
  123.     # Install Forge
  124.     java -jar "$FORGE_JAR" --installServer
  125.    
  126.     success "Forge installed successfully"
  127. }
  128.  
  129. # Download mods
  130. download_mods() {
  131.     log "Downloading popular mods..."
  132.    
  133.     cd "$SERVER_DIR/mods"
  134.    
  135.     for mod_name in "${!MODS[@]}"; do
  136.         log "Downloading $mod_name..."
  137.         # Note: This is a simplified approach. In reality, you'd need to handle
  138.         # CurseForge API keys and proper mod downloading
  139.         warn "Mod downloading requires manual setup due to API restrictions"
  140.         warn "Please visit CurseForge and download: $mod_name"
  141.     done
  142.    
  143.     # Create a list of recommended mods
  144.     cat > ../recommended_mods.txt << EOF
  145. Recommended Mods for your Minecraft Server:
  146. 1. JEI (Just Enough Items) - Recipe viewer
  147. 2. OptiFine - Performance optimization
  148. 3. JourneyMap - Minimap and waypoints
  149. 4. Iron Chests - Additional storage options
  150. 5. Biomes O' Plenty - New biomes
  151. 6. Tinkers Construct - Tool crafting
  152. 7. Applied Energistics 2 - Advanced storage
  153. 8. Thermal Expansion - Tech mod
  154. 9. Waystones - Fast travel
  155. 10. Storage Drawers - Compact storage
  156.  
  157. Download these mods from CurseForge and place them in the mods/ directory.
  158. EOF
  159.    
  160.     success "Mod information saved to recommended_mods.txt"
  161. }
  162.  
  163. # Create server configuration
  164. create_server_config() {
  165.     log "Creating server configuration..."
  166.    
  167.     cd "$SERVER_DIR"
  168.    
  169.     # Create server.properties if it doesn't exist
  170.     if [ ! -f "server.properties" ]; then
  171.         cat > server.properties << EOF
  172. #Minecraft server properties
  173. server-port=25565
  174. level-name=world
  175. gamemode=survival
  176. difficulty=normal
  177. allow-nether=true
  178. max-players=20
  179. online-mode=true
  180. white-list=false
  181. spawn-protection=16
  182. motd=Welcome to your Minecraft Server!
  183. EOF
  184.     fi
  185.    
  186.     success "Server configuration created"
  187. }
  188.  
  189. # Create start script
  190. create_start_script() {
  191.     log "Creating server start script..."
  192.    
  193.     cd "$SERVER_DIR"
  194.    
  195.     # Find the forge server jar
  196.     FORGE_SERVER_JAR=$(find . -name "forge-*-${MINECRAFT_VERSION}-${FORGE_VERSION}.jar" | head -1)
  197.    
  198.     if [ -z "$FORGE_SERVER_JAR" ]; then
  199.         FORGE_SERVER_JAR="forge-${MINECRAFT_VERSION}-${FORGE_VERSION}.jar"
  200.     fi
  201.    
  202.     cat > start_server.sh << EOF
  203. #!/bin/bash
  204.  
  205. # Minecraft Server Start Script
  206. cd "$SERVER_DIR"
  207.  
  208. # Check if Forge server exists, otherwise use vanilla
  209. if [ -f "$FORGE_SERVER_JAR" ]; then
  210.     echo "Starting Forge server..."
  211.     java -Xmx${SERVER_RAM} -Xms1G -jar "$FORGE_SERVER_JAR" nogui
  212. else
  213.     echo "Starting vanilla server..."
  214.     java -Xmx${SERVER_RAM} -Xms1G -jar "$SERVER_JAR" nogui
  215. fi
  216. EOF
  217.    
  218.     chmod +x start_server.sh
  219.     success "Start script created"
  220. }
  221.  
  222. # Backup function
  223. backup_server() {
  224.     log "Creating server backup..."
  225.    
  226.     BACKUP_NAME="minecraft-backup-$(date +%Y%m%d-%H%M%S).tar.gz"
  227.    
  228.     cd "$SERVER_DIR/.."
  229.     tar -czf "minecraft-server/backups/$BACKUP_NAME" \
  230.         --exclude='minecraft-server/backups' \
  231.         --exclude='minecraft-server/logs' \
  232.         minecraft-server/
  233.    
  234.     success "Backup created: $BACKUP_NAME"
  235. }
  236.  
  237. # Update server
  238. update_server() {
  239.     log "Updating Minecraft server..."
  240.    
  241.     # Create backup before updating
  242.     backup_server
  243.    
  244.     # Update server jar (this would need version checking logic)
  245.     warn "Server updates require manual version checking"
  246.     warn "Check https://minecraft.net for the latest version"
  247.    
  248.     success "Update check completed"
  249. }
  250.  
  251. # Show server status
  252. server_status() {
  253.     log "Checking server status..."
  254.    
  255.     if pgrep -f "minecraft.*server" > /dev/null; then
  256.         success "Minecraft server is running"
  257.         echo "PID: $(pgrep -f 'minecraft.*server')"
  258.     else
  259.         warn "Minecraft server is not running"
  260.     fi
  261. }
  262.  
  263. # Display help
  264. show_help() {
  265.     echo "Minecraft Server Management Script"
  266.     echo ""
  267.     echo "Usage: $0 [COMMAND]"
  268.     echo ""
  269.     echo "Commands:"
  270.     echo "  install     - Install Java, download server, and set up mods"
  271.     echo "  start       - Start the Minecraft server"
  272.     echo "  stop        - Stop the Minecraft server"
  273.     echo "  restart     - Restart the Minecraft server"
  274.     echo "  status      - Show server status"
  275.     echo "  backup      - Create a server backup"
  276.     echo "  update      - Update the server"
  277.     echo "  mods        - Show recommended mods list"
  278.     echo "  menu        - Show interactive menu"
  279.     echo "  help        - Show this help message"
  280.     echo ""
  281.     echo "Server directory: $SERVER_DIR"
  282. }
  283.  
  284. # Clear screen function
  285. clear_screen() {
  286.     clear
  287. }
  288.  
  289. # Press any key to continue
  290. press_any_key() {
  291.     echo ""
  292.     echo -e "${YELLOW}Press any key to continue...${NC}"
  293.     read -n 1 -s
  294. }
  295.  
  296. # Display main menu
  297. show_menu() {
  298.     while true; do
  299.         clear_screen
  300.         echo -e "${BLUE}╔══════════════════════════════════════════════════════╗${NC}"
  301.         echo -e "${BLUE}${NC}          ${GREEN}Minecraft Server Management Menu${NC}          ${BLUE}${NC}"
  302.         echo -e "${BLUE}╠══════════════════════════════════════════════════════╣${NC}"
  303.         echo -e "${BLUE}${NC}                                                      ${BLUE}${NC}"
  304.         echo -e "${BLUE}${NC}  ${YELLOW}1)${NC} Install Server & Mods                         ${BLUE}${NC}"
  305.         echo -e "${BLUE}${NC}  ${YELLOW}2)${NC} Start Server                                  ${BLUE}${NC}"
  306.         echo -e "${BLUE}${NC}  ${YELLOW}3)${NC} Stop Server                                   ${BLUE}${NC}"
  307.         echo -e "${BLUE}${NC}  ${YELLOW}4)${NC} Restart Server                                ${BLUE}${NC}"
  308.         echo -e "${BLUE}${NC}  ${YELLOW}5)${NC} Server Status                                 ${BLUE}${NC}"
  309.         echo -e "${BLUE}${NC}  ${YELLOW}6)${NC} Create Backup                                 ${BLUE}${NC}"
  310.         echo -e "${BLUE}${NC}  ${YELLOW}7)${NC} Update Server                                 ${BLUE}${NC}"
  311.         echo -e "${BLUE}${NC}  ${YELLOW}8)${NC} View Recommended Mods                         ${BLUE}${NC}"
  312.         echo -e "${BLUE}${NC}  ${YELLOW}9)${NC} Server Configuration                          ${BLUE}${NC}"
  313.         echo -e "${BLUE}${NC}  ${YELLOW}10)${NC} View Server Logs                            ${BLUE}${NC}"
  314.         echo -e "${BLUE}${NC}  ${YELLOW}11)${NC} Server Information                           ${BLUE}${NC}"
  315.         echo -e "${BLUE}${NC}                                                      ${BLUE}${NC}"
  316.         echo -e "${BLUE}${NC}  ${RED}0)${NC} Exit                                          ${BLUE}${NC}"
  317.         echo -e "${BLUE}${NC}                                                      ${BLUE}${NC}"
  318.         echo -e "${BLUE}╚══════════════════════════════════════════════════════╝${NC}"
  319.         echo ""
  320.        
  321.         # Show current server status in menu
  322.         if pgrep -f "minecraft.*server" > /dev/null; then
  323.             echo -e "${GREEN}● Server Status: RUNNING${NC} (PID: $(pgrep -f 'minecraft.*server'))"
  324.         else
  325.             echo -e "${RED}● Server Status: STOPPED${NC}"
  326.         fi
  327.        
  328.         if [ -d "$SERVER_DIR" ]; then
  329.             echo -e "${GREEN}● Server Directory: EXISTS${NC} ($SERVER_DIR)"
  330.         else
  331.             echo -e "${RED}● Server Directory: NOT FOUND${NC}"
  332.         fi
  333.        
  334.         echo ""
  335.         echo -n -e "${YELLOW}Enter your choice [0-11]: ${NC}"
  336.         read choice
  337.        
  338.         case $choice in
  339.             1)
  340.                 clear_screen
  341.                 log "Starting server installation..."
  342.                 install_java
  343.                 setup_directories
  344.                 download_server
  345.                 install_forge
  346.                 download_mods
  347.                 create_server_config
  348.                 create_start_script
  349.                 success "Installation completed!"
  350.                 echo ""
  351.                 echo "Next steps:"
  352.                 echo "1. Download mods from the recommended_mods.txt file"
  353.                 echo "2. Place mod files in $SERVER_DIR/mods/"
  354.                 echo "3. Start the server from the menu"
  355.                 press_any_key
  356.                 ;;
  357.             2)
  358.                 clear_screen
  359.                 start_server
  360.                 press_any_key
  361.                 ;;
  362.             3)
  363.                 clear_screen
  364.                 stop_server
  365.                 press_any_key
  366.                 ;;
  367.             4)
  368.                 clear_screen
  369.                 log "Restarting server..."
  370.                 stop_server
  371.                 sleep 2
  372.                 start_server
  373.                 press_any_key
  374.                 ;;
  375.             5)
  376.                 clear_screen
  377.                 server_status
  378.                 if [ -d "$SERVER_DIR" ]; then
  379.                     echo ""
  380.                     echo "Server Directory: $SERVER_DIR"
  381.                     if [ -f "$SERVER_DIR/server.properties" ]; then
  382.                         echo "Server Port: $(grep '^server-port' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  383.                         echo "Max Players: $(grep '^max-players' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  384.                         echo "Game Mode: $(grep '^gamemode' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  385.                     fi
  386.                 fi
  387.                 press_any_key
  388.                 ;;
  389.             6)
  390.                 clear_screen
  391.                 if [ ! -d "$SERVER_DIR" ]; then
  392.                     error "Server not installed. Please install first."
  393.                 else
  394.                     backup_server
  395.                     echo ""
  396.                     echo -e "${GREEN}Available backups:${NC}"
  397.                     ls -la "$SERVER_DIR/backups/" 2>/dev/null | grep ".tar.gz" || echo "No backups found"
  398.                 fi
  399.                 press_any_key
  400.                 ;;
  401.             7)
  402.                 clear_screen
  403.                 update_server
  404.                 press_any_key
  405.                 ;;
  406.             8)
  407.                 clear_screen
  408.                 if [ -f "$SERVER_DIR/recommended_mods.txt" ]; then
  409.                     cat "$SERVER_DIR/recommended_mods.txt"
  410.                     echo ""
  411.                     echo -e "${YELLOW}Mod Installation Instructions:${NC}"
  412.                     echo "1. Visit https://www.curseforge.com/minecraft/mc-mods"
  413.                     echo "2. Search for each mod by name"
  414.                     echo "3. Download the .jar files for Minecraft $MINECRAFT_VERSION"
  415.                     echo "4. Place all .jar files in: $SERVER_DIR/mods/"
  416.                 else
  417.                     error "Mods list not found. Please install server first."
  418.                 fi
  419.                 press_any_key
  420.                 ;;
  421.             9)
  422.                 clear_screen
  423.                 show_config_menu
  424.                 ;;
  425.             10)
  426.                 clear_screen
  427.                 show_logs_menu
  428.                 ;;
  429.             11)
  430.                 clear_screen
  431.                 show_server_info
  432.                 press_any_key
  433.                 ;;
  434.             0)
  435.                 clear_screen
  436.                 echo -e "${GREEN}Thank you for using Minecraft Server Manager!${NC}"
  437.                 echo -e "${BLUE}Server directory: $SERVER_DIR${NC}"
  438.                 exit 0
  439.                 ;;
  440.             *)
  441.                 clear_screen
  442.                 error "Invalid option. Please choose 0-11."
  443.                 sleep 2
  444.                 ;;
  445.         esac
  446.     done
  447. }
  448.  
  449. # Configuration menu
  450. show_config_menu() {
  451.     while true; do
  452.         clear_screen
  453.         echo -e "${BLUE}╔══════════════════════════════════════════════════════╗${NC}"
  454.         echo -e "${BLUE}${NC}             ${GREEN}Server Configuration Menu${NC}              ${BLUE}${NC}"
  455.         echo -e "${BLUE}╚══════════════════════════════════════════════════════╝${NC}"
  456.         echo ""
  457.        
  458.         if [ -f "$SERVER_DIR/server.properties" ]; then
  459.             echo -e "${GREEN}Current Configuration:${NC}"
  460.             echo "Server Port: $(grep '^server-port' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  461.             echo "Max Players: $(grep '^max-players' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  462.             echo "Game Mode: $(grep '^gamemode' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  463.             echo "Difficulty: $(grep '^difficulty' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  464.             echo "MOTD: $(grep '^motd' $SERVER_DIR/server.properties | cut -d'=' -f2)"
  465.             echo ""
  466.         else
  467.             warn "Server not installed or configuration not found."
  468.             echo ""
  469.         fi
  470.        
  471.         echo -e "${YELLOW}1)${NC} Edit Server Properties File"
  472.         echo -e "${YELLOW}2)${NC} Change Server Port"
  473.         echo -e "${YELLOW}3)${NC} Change Max Players"
  474.         echo -e "${YELLOW}4)${NC} Change Game Mode"
  475.         echo -e "${YELLOW}5)${NC} Change Server MOTD"
  476.         echo -e "${YELLOW}6)${NC} Reset to Default Configuration"
  477.         echo -e "${RED}0)${NC} Back to Main Menu"
  478.         echo ""
  479.         echo -n -e "${YELLOW}Enter your choice [0-6]: ${NC}"
  480.         read config_choice
  481.        
  482.         case $config_choice in
  483.             1)
  484.                 if command -v nano &> /dev/null; then
  485.                     nano "$SERVER_DIR/server.properties"
  486.                 elif command -v vim &> /dev/null; then
  487.                     vim "$SERVER_DIR/server.properties"
  488.                 else
  489.                     error "No text editor found. Please install nano or vim."
  490.                     press_any_key
  491.                 fi
  492.                 ;;
  493.             2)
  494.                 echo -n "Enter new server port (default 25565): "
  495.                 read new_port
  496.                 if [[ "$new_port" =~ ^[0-9]+$ ]] && [ "$new_port" -ge 1024 ] && [ "$new_port" -le 65535 ]; then
  497.                     sed -i "s/^server-port=.*/server-port=$new_port/" "$SERVER_DIR/server.properties"
  498.                     success "Server port changed to $new_port"
  499.                 else
  500.                     error "Invalid port number. Must be between 1024 and 65535."
  501.                 fi
  502.                 press_any_key
  503.                 ;;
  504.             3)
  505.                 echo -n "Enter max players (1-100): "
  506.                 read max_players
  507.                 if [[ "$max_players" =~ ^[0-9]+$ ]] && [ "$max_players" -ge 1 ] && [ "$max_players" -le 100 ]; then
  508.                     sed -i "s/^max-players=.*/max-players=$max_players/" "$SERVER_DIR/server.properties"
  509.                     success "Max players changed to $max_players"
  510.                 else
  511.                     error "Invalid number. Must be between 1 and 100."
  512.                 fi
  513.                 press_any_key
  514.                 ;;
  515.             4)
  516.                 echo "Game modes:"
  517.                 echo "1) Survival"
  518.                 echo "2) Creative"
  519.                 echo "3) Adventure"
  520.                 echo "4) Spectator"
  521.                 echo -n "Choose game mode [1-4]: "
  522.                 read gamemode_choice
  523.                 case $gamemode_choice in
  524.                     1) gamemode="survival" ;;
  525.                     2) gamemode="creative" ;;
  526.                     3) gamemode="adventure" ;;
  527.                     4) gamemode="spectator" ;;
  528.                     *) error "Invalid choice"; press_any_key; continue ;;
  529.                 esac
  530.                 sed -i "s/^gamemode=.*/gamemode=$gamemode/" "$SERVER_DIR/server.properties"
  531.                 success "Game mode changed to $gamemode"
  532.                 press_any_key
  533.                 ;;
  534.             5)
  535.                 echo -n "Enter new MOTD: "
  536.                 read new_motd
  537.                 sed -i "s/^motd=.*/motd=$new_motd/" "$SERVER_DIR/server.properties"
  538.                 success "MOTD changed to: $new_motd"
  539.                 press_any_key
  540.                 ;;
  541.             6)
  542.                 echo -n "Reset configuration to defaults? [y/N]: "
  543.                 read confirm
  544.                 if [[ "$confirm" =~ ^[Yy]$ ]]; then
  545.                     create_server_config
  546.                     success "Configuration reset to defaults"
  547.                 else
  548.                     log "Configuration reset cancelled"
  549.                 fi
  550.                 press_any_key
  551.                 ;;
  552.             0)
  553.                 break
  554.                 ;;
  555.             *)
  556.                 error "Invalid option. Please choose 0-6."
  557.                 sleep 2
  558.                 ;;
  559.         esac
  560.     done
  561. }
  562.  
  563. # Logs menu
  564. show_logs_menu() {
  565.     while true; do
  566.         clear_screen
  567.         echo -e "${BLUE}╔══════════════════════════════════════════════════════╗${NC}"
  568.         echo -e "${BLUE}${NC}                 ${GREEN}Server Logs Menu${NC}                   ${BLUE}${NC}"
  569.         echo -e "${BLUE}╚══════════════════════════════════════════════════════╝${NC}"
  570.         echo ""
  571.        
  572.         echo -e "${YELLOW}1)${NC} View Live Server Log (last 50 lines)"
  573.         echo -e "${YELLOW}2)${NC} View Full Server Log"
  574.         echo -e "${YELLOW}3)${NC} View Latest Crash Report"
  575.         echo -e "${YELLOW}4)${NC} Clear Server Logs"
  576.         echo -e "${YELLOW}5)${NC} Monitor Live Server Output"
  577.         echo -e "${RED}0)${NC} Back to Main Menu"
  578.         echo ""
  579.         echo -n -e "${YELLOW}Enter your choice [0-5]: ${NC}"
  580.         read logs_choice
  581.        
  582.         case $logs_choice in
  583.             1)
  584.                 clear_screen
  585.                 if [ -f "$SERVER_DIR/logs/server.log" ]; then
  586.                     echo -e "${GREEN}Last 50 lines of server log:${NC}"
  587.                     echo "----------------------------------------"
  588.                     tail -n 50 "$SERVER_DIR/logs/server.log"
  589.                 else
  590.                     warn "Server log not found. Server may not have been started yet."
  591.                 fi
  592.                 press_any_key
  593.                 ;;
  594.             2)
  595.                 if [ -f "$SERVER_DIR/logs/server.log" ]; then
  596.                     less "$SERVER_DIR/logs/server.log"
  597.                 else
  598.                     warn "Server log not found."
  599.                     press_any_key
  600.                 fi
  601.                 ;;
  602.             3)
  603.                 clear_screen
  604.                 crash_report=$(find "$SERVER_DIR" -name "crash-*" -type f 2>/dev/null | head -1)
  605.                 if [ -n "$crash_report" ]; then
  606.                     echo -e "${GREEN}Latest crash report:${NC}"
  607.                     echo "----------------------------------------"
  608.                     cat "$crash_report"
  609.                 else
  610.                     success "No crash reports found!"
  611.                 fi
  612.                 press_any_key
  613.                 ;;
  614.             4)
  615.                 echo -n "Clear all server logs? [y/N]: "
  616.                 read confirm
  617.                 if [[ "$confirm" =~ ^[Yy]$ ]]; then
  618.                     rm -f "$SERVER_DIR/logs/"*.log 2>/dev/null
  619.                     rm -f "$SERVER_DIR/"crash-*.txt 2>/dev/null
  620.                     success "Server logs cleared"
  621.                 else
  622.                     log "Log clearing cancelled"
  623.                 fi
  624.                 press_any_key
  625.                 ;;
  626.             5)
  627.                 if pgrep -f "minecraft.*server" > /dev/null; then
  628.                     clear_screen
  629.                     echo -e "${GREEN}Monitoring live server output. Press Ctrl+C to stop.${NC}"
  630.                     echo "----------------------------------------"
  631.                     tail -f "$SERVER_DIR/logs/server.log" 2>/dev/null || echo "Log file not found"
  632.                 else
  633.                     error "Server is not running"
  634.                     press_any_key
  635.                 fi
  636.                 ;;
  637.             0)
  638.                 break
  639.                 ;;
  640.             *)
  641.                 error "Invalid option. Please choose 0-5."
  642.                 sleep 2
  643.                 ;;
  644.         esac
  645.     done
  646. }
  647.  
  648. # Show server information
  649. show_server_info() {
  650.     echo -e "${BLUE}╔══════════════════════════════════════════════════════╗${NC}"
  651.     echo -e "${BLUE}${NC}               ${GREEN}Server Information${NC}                 ${BLUE}${NC}"
  652.     echo -e "${BLUE}╚══════════════════════════════════════════════════════╝${NC}"
  653.     echo ""
  654.    
  655.     echo -e "${GREEN}System Information:${NC}"
  656.     echo "Java Version: $(java -version 2>&1 | head -n1 | cut -d'"' -f2)"
  657.     echo "Operating System: $(uname -s) $(uname -r)"
  658.     echo "Available Memory: $(free -h | grep '^Mem:' | awk '{print $2}')"
  659.     echo "Free Disk Space: $(df -h $HOME | tail -1 | awk '{print $4}')"
  660.     echo ""
  661.    
  662.     echo -e "${GREEN}Server Configuration:${NC}"
  663.     echo "Minecraft Version: $MINECRAFT_VERSION"
  664.     echo "Forge Version: $FORGE_VERSION"
  665.     echo "Server RAM Allocation: $SERVER_RAM"
  666.     echo "Server Directory: $SERVER_DIR"
  667.     echo ""
  668.    
  669.     if [ -d "$SERVER_DIR" ]; then
  670.         echo -e "${GREEN}Installation Status:${NC}"
  671.         [ -f "$SERVER_DIR/$SERVER_JAR" ] && echo "✓ Minecraft Server: Installed" || echo "✗ Minecraft Server: Not Found"
  672.         [ -f "$SERVER_DIR/forge-${MINECRAFT_VERSION}-${FORGE_VERSION}.jar" ] && echo "✓ Forge: Installed" || echo "✗ Forge: Not Found"
  673.         [ -d "$SERVER_DIR/mods" ] && echo "✓ Mods Directory: Created ($(ls $SERVER_DIR/mods/*.jar 2>/dev/null | wc -l) mods installed)" || echo "✗ Mods Directory: Not Found"
  674.         [ -f "$SERVER_DIR/server.properties" ] && echo "✓ Configuration: Created" || echo "✗ Configuration: Not Found"
  675.         echo ""
  676.        
  677.         echo -e "${GREEN}Directory Size:${NC}"
  678.         du -sh "$SERVER_DIR" 2>/dev/null | awk '{print "Total Size: " $1}'
  679.     else
  680.         echo -e "${RED}Server not installed${NC}"
  681.     fi
  682. }
  683.  
  684. # Stop server
  685. stop_server() {
  686.     log "Stopping Minecraft server..."
  687.    
  688.     if pgrep -f "minecraft.*server" > /dev/null; then
  689.         pkill -f "minecraft.*server"
  690.         sleep 5
  691.        
  692.         if pgrep -f "minecraft.*server" > /dev/null; then
  693.             warn "Forcefully killing server..."
  694.             pkill -9 -f "minecraft.*server"
  695.         fi
  696.        
  697.         success "Server stopped"
  698.     else
  699.         warn "Server is not running"
  700.     fi
  701. }
  702.  
  703. # Start server
  704. start_server() {
  705.     log "Starting Minecraft server..."
  706.    
  707.     if pgrep -f "minecraft.*server" > /dev/null; then
  708.         warn "Server is already running"
  709.         return 1
  710.     fi
  711.    
  712.     cd "$SERVER_DIR"
  713.    
  714.     if [ -f "start_server.sh" ]; then
  715.         nohup ./start_server.sh > logs/server.log 2>&1 &
  716.         success "Server started in background. Check logs/server.log for output"
  717.     else
  718.         error "Start script not found. Run 'install' first."
  719.         return 1
  720.     fi
  721. }
  722.  
  723. # Main script logic
  724. case "${1:-menu}" in
  725.     install)
  726.         log "Installing Minecraft server..."
  727.         install_java
  728.         setup_directories
  729.         download_server
  730.         install_forge
  731.         download_mods
  732.         create_server_config
  733.         create_start_script
  734.         success "Installation completed!"
  735.         echo ""
  736.         echo "Next steps:"
  737.         echo "1. Download mods from the recommended_mods.txt file"
  738.         echo "2. Place mod files in $SERVER_DIR/mods/"
  739.         echo "3. Run '$0 start' to start the server"
  740.         ;;
  741.     start)
  742.         start_server
  743.         ;;
  744.     stop)
  745.         stop_server
  746.         ;;
  747.     restart)
  748.         stop_server
  749.         sleep 2
  750.         start_server
  751.         ;;
  752.     status)
  753.         server_status
  754.         ;;
  755.     backup)
  756.         backup_server
  757.         ;;
  758.     update)
  759.         update_server
  760.         ;;
  761.     mods)
  762.         if [ -f "$SERVER_DIR/recommended_mods.txt" ]; then
  763.             cat "$SERVER_DIR/recommended_mods.txt"
  764.         else
  765.             error "Mods list not found. Run 'install' first."
  766.         fi
  767.         ;;
  768.     menu)
  769.         show_menu
  770.         ;;
  771.     help|--help|-h)
  772.         show_help
  773.         ;;
  774.     *)
  775.         show_menu
  776.         ;;
  777. esac
  778.  
  779.  
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement