Minecraft Integration - Documentation¶
Overview¶
The Minecraft module enables integration between MyCyclingCity and a Minecraft server. It synchronizes cyclists' coins between the Django database and Minecraft scoreboards, allowing players to use their earned coins in the Minecraft server.
Main Features¶
1. Coin Synchronization¶
- Bidirectional Sync: Coins are synchronized from MyCyclingCity to Minecraft and back
- Scoreboard Integration: Uses Minecraft scoreboards for coin display
- Real-time Updates: Changes are immediately transmitted to the Minecraft server
2. RCON Communication¶
- Remote Management: Communication with the Minecraft server via RCON (Remote Console)
- Scoreboard Management: Automatic creation and management of scoreboard objectives
- Player Score Updates: Real-time update of player scores
3. Outbox Pattern¶
- Asynchronous Processing: Events are stored in an outbox and processed asynchronously
- Error Handling: Automatic retry on errors
- Event Tracking: Full traceability of all synchronization events
4. WebSocket Support¶
- Real-time Communication: WebSocket endpoint for direct communication with Minecraft plugins
- Secure Authentication: Signature-based authentication
- Coin Spending: Support for direct coin spending from Minecraft
5. Snapshot System¶
- Status Capture: Periodic capture of scoreboard status
- Comparison: Comparison between database and Minecraft server
- Synchronization: Automatic synchronization on discrepancies
Configuration¶
Required Settings¶
Add the following settings to your .env file:
# RCON Connection
MCC_MINECRAFT_RCON_HOST=127.0.0.1
MCC_MINECRAFT_RCON_PORT=25575
MCC_MINECRAFT_RCON_PASSWORD=your-rcon-password
# Scoreboard Names
MCC_MINECRAFT_SCOREBOARD_COINS_TOTAL=player_coins_total
MCC_MINECRAFT_SCOREBOARD_COINS_SPENDABLE=player_coins_spendable
# Worker Intervals (in seconds)
MCC_MINECRAFT_WORKER_POLL_INTERVAL=1
MCC_MINECRAFT_RCON_HEALTH_INTERVAL=30
MCC_MINECRAFT_SNAPSHOT_INTERVAL=60
# Snapshot Settings
MCC_MINECRAFT_SNAPSHOT_UPDATE_DB_SPENDABLE=True
# Outbox Settings
MCC_MINECRAFT_OUTBOX_DONE_TTL_DAYS=7
MCC_MINECRAFT_OUTBOX_FAILED_TTL_DAYS=30
MCC_MINECRAFT_OUTBOX_MAX_EVENTS=50000
# WebSocket (optional)
MCC_MINECRAFT_WS_ENABLED=False
MCC_MINECRAFT_WS_SHARED_SECRET=your-secret-key
MCC_MINECRAFT_WS_ALLOWED_SERVER_IDS=server1,server2
RCON Setup in Minecraft Server¶
Enable RCON in server.properties:
Admin GUI¶
Access¶
Navigate to /admin/minecraft/ in the Django Admin Interface (superusers only).
Functions¶
Worker Control¶
- Start: Starts the Minecraft bridge worker
- Stop: Stops the worker
- Status: Shows the current worker status
Snapshot Worker¶
- Start: Starts the snapshot worker (periodic status capture)
- Stop: Stops the snapshot worker
- Status: Shows the snapshot worker status
Manual Actions¶
- Sync: Manual full synchronization of all players
- Snapshot: Manual update of the scoreboard snapshot
- RCON Test: Tests the RCON connection
- Cleanup: Cleans up old outbox events
Status Display¶
The Admin Interface shows:
- Worker Status: Whether the bridge worker is running
- Snapshot Status: Whether the snapshot worker is running
- Outbox Status: Number of pending, processing, and failed events
- RCON Status: Connection status to the Minecraft server
- Player List: All players with their coin values and snapshot status
Data Models¶
MinecraftOutboxEvent¶
Stores events for asynchronous processing:
event_type: Type of event (update_player_coins,sync_all)payload: JSON data of the eventstatus: Status (pending,processing,done,failed)attempts: Number of processing attemptslast_error: Last error message (if any)
MinecraftPlayerScoreboardSnapshot¶
Stores snapshots of scoreboard status:
player_name: Minecraft usernamecyclist: Link to Cyclist modelcoins_total: Total coins (from scoreboard)coins_spendable: Spendable coins (from scoreboard)source: Source of snapshot (rcon)captured_at: Timestamp of capture
MinecraftWorkerState¶
Stores worker status:
is_running: Whether the worker is runningpid: Process ID of the workerstarted_at: Start timelast_heartbeat: Last heartbeatlast_error: Last error message
Worker Processes¶
Bridge Worker¶
The bridge worker continuously processes events from the outbox:
Functions:
- Processes update_player_coins events
- Processes sync_all events
- Updates scoreboards via RCON
- Creates snapshots after updates
Start/Stop via Script:
Snapshot Worker¶
The snapshot worker periodically captures scoreboard status:
Functions:
- Reads all player scores from the Minecraft server
- Updates the snapshot table
- Synchronizes coins_spendable back to database (optional)
Start/Stop via Script:
scripts/minecraft.sh snapshot-start
scripts/minecraft.sh snapshot-stop
scripts/minecraft.sh snapshot-status
WebSocket Integration¶
Activation¶
Set in .env:
MCC_MINECRAFT_WS_ENABLED=True
MCC_MINECRAFT_WS_SHARED_SECRET=your-secret-key
MCC_MINECRAFT_WS_ALLOWED_SERVER_IDS=server1
Endpoint¶
WebSocket endpoint: ws://your-domain/ws/minecraft/events/
Supported Events¶
SPEND_COINS¶
Player spends coins in Minecraft:
{
"type": "SPEND_COINS",
"player": "PlayerName",
"amount": 100,
"server_id": "server1",
"signature": "hmac-signature"
}
Response:
Signature Calculation¶
The signature is calculated with HMAC-SHA256:
import hmac
import hashlib
import json
payload = {"type": "SPEND_COINS", "player": "PlayerName", "amount": 100}
message = json.dumps(payload, sort_keys=True)
signature = hmac.new(secret.encode(), message.encode(), hashlib.sha256).hexdigest()
Management Commands¶
Full Synchronization¶
Synchronizes all players with a Minecraft username.
Update Snapshot¶
Manually updates the scoreboard snapshot.
Cleanup Outbox¶
Cleans up old events from the outbox (automatically after TTL).
Player Linking¶
To link a cyclist with a Minecraft player:
- Navigate to MCC Core API & Models → Cyclists
- Select the cyclist
- Enter the Minecraft username in the
mc_usernamefield - Save
After linking, coins will be automatically synchronized.
Workflow¶
Coin Update from MyCyclingCity to Minecraft¶
- Cyclist earns coins (e.g., by cycling)
coins_totalorcoins_spendableis updated in the database- Event is queued in the outbox
- Bridge worker processes the event
- RCON command is sent to Minecraft server
- Scoreboard is updated
- Snapshot is created
Coin Spending from Minecraft to MyCyclingCity¶
- Player spends coins in Minecraft (e.g., via plugin)
- Plugin sends WebSocket event to MyCyclingCity
coins_spendableis reduced in the database- Event is queued in the outbox
- Bridge worker updates the scoreboard
- Snapshot is updated
Troubleshooting¶
RCON Connection Fails¶
- Check RCON settings in
server.properties - Test the connection in Admin GUI (
RCON Test) - Check firewall settings
- Ensure the Minecraft server is running
Worker Won't Start¶
- Check logs:
logs/minecraft_action.log - Ensure the script is executable:
chmod +x scripts/minecraft.sh - Check the Python environment
- Check the database connection
Coins Not Synchronizing¶
- Check the outbox in Admin GUI
- Check failed events
- Check logs:
logs/minecraft.log - Ensure the worker is running
- Check if the player has an
mc_username
Snapshot Shows Wrong Values¶
- Manually run a snapshot (Admin GUI)
- Check the RCON connection
- Check if scoreboards exist in Minecraft
- Run a full synchronization
Best Practices¶
- Regular Snapshots: Keep the snapshot worker running to detect discrepancies early
- Outbox Monitoring: Regularly monitor the outbox for failed events
- RCON Security: Use a strong RCON password
- WebSocket Security: Use a strong shared secret
- Backup: Regularly backup outbox events and snapshots
- Logging: Enable verbose logging for debugging
Related Documentation¶
- Admin GUI Manual - Overview of all admin functions
- Server Control - Server management
- Log File Viewer - View log files