How gubaiovo/mcdr_chat_with_ai works
Project type and purpose
• This is an MCDReforged (“MCDR”) plugin written in Python. Its purpose is to connect a running Minecraft server to OpenAI-compatible models, allowing players to chat with an AI “villager” or NPC via in-game chat commands.
File structure
• Top-level files:
• LICENSE, README.md / README_CN.md
• mcdreforged.plugin.json – plugin manifest that MCDR uses to load the plugin
• requirements.txt – lists Python dependencies (e.g., openai)
• chat_with_ai/ directory (the plugin package):
• __init__.py – entry point, command registration with MCDR
• ConfigManager.py – loads and watches config/config.json
• DataManager.py – manages chat history, context per player
• config/ directory:
• config.json – plugin settings (API key, model, system prompt defaults)
• lang/ directory:
• en_us.yml, zh_cn.yml – localization strings for in-game messages
• doc/ directory: usage documentation
Key insights from README
• Uses the openai Python library to communicate with OpenAI API
• Supports commands like !!dsp msg, !!dsp history, !!dsp clear, etc.
• Fully config-driven: language packs, system and prefix presets, history persistence
Main components & interactions
a. Components
1. Minecraft Server + MCDReforged plugin host
2. chat_with_ai plugin
– Command Handler (registered via __init__.py)
– ConfigManager (reads config.json, reloads on change)
– DataManager (stores per-player conversation context)
– LangService (loads YAML language files)
3. OpenAI API (external HTTP service)
4. Local storage (file system for config.json, history in memory or disk)
b. Interactions / Data Flow
1. Player types “!!dsp msg Hello” in chat →
2. MCDReforged routes command to chat_with_ai.Command Handler →
3. Command Handler uses ConfigManager & LangService to build request context →
4. DataManager appends user message to conversation history →
5. Plugin calls OpenAI API with full message history →
6. OpenAI responds with AI message →
7. DataManager stores AI response →
8. Plugin sends response back to player via server chat
c. Architectural patterns
• Plugin / Extension pattern (MCDReforged host + plugin module)
• Manager pattern (ConfigManager, DataManager, LangService)
• Command pattern (each “!!dsp” command maps to a handler method)
d. Technologies & frameworks
• Python 3.x
• MCDReforged plugin API
• openai Python SDK
• YAML for localization
• JSON for configuration