Blogs Tutorial

Whitelist command for everyone!

  • 386 views 0 today
  • 1
  • 0
norkwurz
Lvl 41Master Procrastinator
4
Use a Bash script and mcron to allow every player on your vanilla minecraft server to whitelist new players. This is somewhat of a hack to allow the server owner to use the vanilla server jar. Essentially the script reads the server console output via the nohup.out file and searches for the command !whitelist using regex. Usage is simple and explained on joining the server.


Dependencies:
  • A Linux system, in this tutorial i use mint/ubuntu and therefor the apt package manager.
  • Install gcc and mcrcon:



sudo apt-get update && sudo apt-get install -y gcc
reboot


cd /opt
git clone github.com/Tiiffi/mcrcon.git
cd mcrcon/
sudo make
sudo make install


  • allow minecraft ports through the firewall:


sudo ufw status
sudo ufw enable
sudo ufw allow 25565/tcp
sudo ufw allow 25565/udp
sudo ufw allow 25575/tcp
sudo ufw allow 25575/udp
sudo ufw status



  • modify your server.properties to allow rcon (password strongly recommended):

broadcast-rcon-to-ops=true

enable-rcon=true

rcon.password=not123456

rcon.port=25575



Main Scripts:



Navigate to your server directory and create the following 2 scripts. The first you will use to start the server from now on.



start_server.sh

#!/bin/bash
# Start the minecraft server.

rm -f nohup.out
nohup java -Xmx1024M -Xms1024M -jar minecraft_server.1.21.1.jar nogui &


server_pid=$!

# script that allows normal players to add players to the whitelist
./whitelist.sh "$server_pid" &
whitelist_scripting=$!




whitelist.sh
#!/bin/bash

# Allows every player to whitelist someone new.

server_pid=$1
info_msg="Invite players by typing [ !whitelist playername ] in chat."
spam_msg="Give it a moment before inviting another player."
wl_response_exists_msg="This player is already on the whitelist."
wl_response_added_msg="Player successfully invited!"
cooldown_file="player_cooldowns.txt"
wl_log="whitelist_log.txt"

rm -f $cooldown_file
rm -f $wl_log

# Send a command to the minecraft server.
rcon() {
  mcrcon -H 127.0.0.1 -P 25575 -p "not123456" "$1"
}

# Function to add a player to the cooldown file to prevent spam
add_to_cooldown() {
  player_name="$1"

  current_time=$(date +%s)
  echo "$player_name $current_time" >> "$cooldown_file"
}

# Function to check if a player is on cooldown
is_on_cooldown() {
  player_name="$1"
  cooldown_seconds=60

  last_command_time="$(grep "$player_name " "$cooldown_file" | tail -n 1 | awk '{print $2}')"
  if [[ $last_command_time ]]; then
    elapsed_seconds=$(($(date +%s) - last_command_time))
    if [[ $elapsed_seconds -lt $cooldown_seconds ]]; then
      return 0 # Player is still on cooldown
    else
      return 1 # Player is not on cooldown anymore
    fi
  else
    return 1 # Player is not on cooldown
  fi
}

# Wait for the server to start up completely
sleep 30

sleep_duration=10
nohub_len=$(wc -l < nohup.out)
while ps -p "$server_pid" > /dev/null; do
new_nohub_len=$(wc -l < nohup.out)
line_count=$((new_nohub_len - nohub_len))
unread_lines=$(tail -n $line_count nohup.out)

while read -r line; do
  # Check if a player just joined the server
  if [[ $line =~ ^\[[​0-9]{2}:[​0-9]{2}:[​0-9]{2}\]\ \[​Server\ thread/INFO\]:\ ([​A-Za-z0-9]+)\ joined\ the\ game$ ]]; then
    player_subject="${BASH_REMATCH[​1]}"
    rcon "tell $player_subject $info_msg"
  fi

  # Check if a player wants to add someone to the whitelist
  if [[ $line =~ ^\[[​0-9]{2}:[​0-9]{2}:[​0-9]{2}\]\ \[​Server\ thread/INFO\]:\ \<([​A-Za-z0-9]+)\>\ \!whitelist\ ([​A-Za-z0-9]+)$ ]]; then
    player_subject="${BASH_REMATCH[​1]}"
    player_object="${BASH_REMATCH[​2]}"

    # Check for spamming the whitelist command
    if is_on_cooldown "$player_subject"; then
      rcon "tell $player_subject $spam_msg"
    else

      # Check if the player is already whitelisted
      if [[ -n "$(grep " \"name\": \"$player_object\"" whitelist.json)" ]]; then
        rcon "tell $player_subject $wl_response_exists_msg"
      else
        rcon "whitelist add $player_object"
        rcon "tell $player_subject $wl_response_added_msg"

        # Add the player to the cooldown file
        add_to_cooldown "$player_subject"

        # Save who added whom to a file
        echo "$player_subject added $player_object to the whitelist" >> $wl_log
      fi
    fi
  fi
done <<< "$unread_lines"

nohub_len="$new_nohub_len"

sleep $sleep_duration
done

More like this

  Have something to say?

Welcome