RCON: Adding on last disconnect (#1395)

This commit is contained in:
chblodg 2022-03-03 18:47:30 -08:00 committed by GitHub
parent 07665d7571
commit a2499a8713
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 5 deletions

View file

@ -1670,6 +1670,7 @@ Feature is used run commands when the server starts, client connects, or client
```yaml
RCON_CMDS_STARTUP: |-
/gamerule doFireTick false
/pregen start 200
```
**On Client Connection:**
@ -1686,6 +1687,22 @@ Feature is used run commands when the server starts, client connects, or client
/gamerule doFireTick true
```
**On First Client Connect**
```yaml
RCON_CMDS_FIRST_CONNECT: |-
/pregen stop
```
**On Last Client Disconnect**
```yaml
RCON_CMDS_LAST_DISCONNECT: |-
/kill @e[type=minecraft:boat]
/pregen start 200
```
**Example of rules for new players**
Uses team NEW and team OLD to track players on the server. So move player with no team to NEW, run a command, move them to team OLD.
@ -1693,13 +1710,19 @@ Uses team NEW and team OLD to track players on the server. So move player with n
```yaml
RCON_CMDS_STARTUP: |-
/pregen start 200
/gamerule doFireTick false
/team add New
/team add Old
RCON_CMDS_ON_CONNECT: |-
/team join New @a[team=]
/give @a[team=New] diamond_block
/give @a[team=New] birch_boat
/team join Old @a[team=New]
RCON_CMDS_FIRST_CONNECT: |-
/pregen stop
RCON_CMDS_LAST_DISCONNECT: |-
/kill @e[type=minecraft:boat]
/pregen start 200
```
## Autopause

View file

@ -12,13 +12,19 @@ services:
# YAML Heredoc, be sure to use '|-' this will remove the first newline and final new line.
# This is versus '|' that will leaving with two empty strings at top and bottom.
RCON_CMDS_STARTUP: |-
/pregen start 200
/gamerule doFireTick false
/team add New
/team add Old
RCON_CMDS_ON_CONNECT: |-
/team join New @a[team=]
/give @a[team=New] diamond_block
/give @a[team=New] birch_boat
/team join Old @a[team=New]
RCON_CMDS_FIRST_CONNECT: |-
/pregen stop
RCON_CMDS_LAST_DISCONNECT: |-
/kill @e[type=minecraft:boat]
/pregen start 200
restart: unless-stopped
volumes:
mc: {}

View file

@ -3,6 +3,8 @@
: "${RCON_CMDS_STARTUP:=}"
: "${RCON_CMDS_ON_CONNECT:=}"
: "${RCON_CMDS_ON_DISCONNECT:=}"
: "${RCON_CMDS_FIRST_CONNECT:=}"
: "${RCON_CMDS_LAST_DISCONNECT:=}"
: "${RCON_CMDS_PERIOD:=10}"
# needed for the clients connected function residing in autopause
@ -44,7 +46,12 @@ do
run_command "$cmd"
done <<< "$RCON_CMDS_STARTUP"
fi
if [[ -z "$RCON_CMDS_ON_CONNECT" ]] && [[ -z "$RCON_CMDS_ON_DISCONNECT" ]]; then
if
[[ -z "$RCON_CMDS_ON_CONNECT" ]] &&
[[ -z "$RCON_CMDS_ON_DISCONNECT" ]] &&
[[ -z "$RCON_CMDS_FIRST_CONNECT" ]] &&
[[ -z "$RCON_CMDS_LAST_DISCONNECT" ]]
then
logRcon "No addition rcon commands are given, stopping rcon cmd service"
exit 0
fi
@ -52,19 +59,34 @@ do
fi
;;
XII)
# Main Loop looking for connections
CURR_CLIENTCONNECTIONS=$(java_clients_connections)
# When a client joins
if (( CURR_CLIENTCONNECTIONS > CLIENTCONNECTIONS )) && [[ "$RCON_CMDS_ON_CONNECT" ]]; then
logRcon "Clients have Connected, running connect cmds"
while read -r cmd; do
run_command "$cmd"
done <<< "$RCON_CMDS_ON_CONNECT"
# When a client leaves
elif (( CURR_CLIENTCONNECTIONS < CLIENTCONNECTIONS )) && [[ "$RCON_CMDS_ON_DISCONNECT" ]]; then
logRcon "Clients have Disconnected, running disconnect cmds"
while read -r cmd; do
run_command "$cmd"
done <<< "$RCON_CMDS_ON_DISCONNECT"
fi
# First client connection
if (( CURR_CLIENTCONNECTIONS > 0 )) && (( CLIENTCONNECTIONS == 0 )) && [[ "$RCON_CMDS_FIRST_CONNECT" ]]; then
logRcon "First Clients has Connected, running first connect cmds"
while read -r cmd; do
run_command "$cmd"
done <<< "$RCON_CMDS_FIRST_CONNECT"
# Last client connection
elif (( CURR_CLIENTCONNECTIONS == 0 )) && (( CLIENTCONNECTIONS > 0 )) && [[ "$RCON_CMDS_LAST_DISCONNECT" ]]; then
logRcon "ALL Clients have Disconnected, running last disconnect cmds"
while read -r cmd; do
run_command "$cmd"
done <<< "$RCON_CMDS_LAST_DISCONNECT"
fi
CLIENTCONNECTIONS=$CURR_CLIENTCONNECTIONS
;;
*)

View file

@ -11,6 +11,8 @@ IFS=$'\n\t'
: "${RCON_CMDS_STARTUP:=}"
: "${RCON_CMDS_ON_CONNECT:=}"
: "${RCON_CMDS_ON_DISCONNECT:=}"
: "${RCON_CMDS_FIRST_CONNECT:=}"
: "${RCON_CMDS_LAST_DISCONNECT:=}"
: "${RCON_CMDS_PERIOD:=10}"
shopt -s nullglob
@ -128,7 +130,13 @@ if isTrue "${ENABLE_AUTOSTOP}"; then
${SCRIPTS:-/}start-autostop
fi
if [[ "$RCON_CMDS_STARTUP" ]] || [[ "$RCON_CMDS_ON_CONNECT" ]] || [[ "$RCON_CMDS_ON_DISCONNECT" ]]; then
if
[[ "$RCON_CMDS_STARTUP" ]] ||
[[ "$RCON_CMDS_ON_CONNECT" ]] ||
[[ "$RCON_CMDS_ON_DISCONNECT" ]] ||
[[ "$RCON_CMDS_FIRST_CONNECT" ]] ||
[[ "$RCON_CMDS_LAST_DISCONNECT" ]]
then
log "Starting RCON commands"
# shellcheck source=start-rconcmds
${SCRIPTS:-/}start-rconcmds

View file

@ -6,11 +6,14 @@
: "${RCON_CMDS_STARTUP:=}"
: "${RCON_CMDS_ON_CONNECT:=}"
: "${RCON_CMDS_ON_DISCONNECT:=}"
: "${RCON_CMDS_FIRST_CONNECT:=}"
: "${RCON_CMDS_LAST_DISCONNECT:=}"
: "${RCON_CMDS_PERIOD:=10}"
: "${SERVER_PORT:=25565}"
export RCON_CMDS_STARTUP
export RCON_CMDS_ON_CONNECT
export RCON_CMDS_ON_DISCONNECT
export RCON_CMDS_LAST_DISCONNECT
export RCON_CMDS_PERIOD
export SERVER_PORT