Skip to content

Commit

Permalink
fix(bot): remove automatic connection in constructor to prevent recon…
Browse files Browse the repository at this point in the history
…nection loop
  • Loading branch information
gerred committed Dec 22, 2024
1 parent 7e91375 commit b2b1265
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
34 changes: 33 additions & 1 deletion .cursorrules
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,36 @@ When working with Mineflayer's API:

## Commit Rules

- Use conventional commits as the structure.
Commits must follow the Conventional Commits specification (https://www.conventionalcommits.org/):

1. Format: `<type>(<scope>): <description>`

- `<type>`: The type of change being made:
- feat: A new feature
- fix: A bug fix
- docs: Documentation only changes
- style: Changes that do not affect the meaning of the code
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing tests or correcting existing tests
- chore: Changes to the build process or auxiliary tools
- ci: Changes to CI configuration files and scripts
- `<scope>`: Optional, indicates section of codebase (e.g., bot, server, tools)
- `<description>`: Clear, concise description in present tense

2. Examples:

- feat(bot): add block placement functionality
- fix(server): resolve reconnection loop issue
- docs(api): update tool documentation
- refactor(core): simplify connection handling

3. Breaking Changes:

- Include BREAKING CHANGE: in the commit footer
- Example: feat(api)!: change tool response format

4. Body and Footer:
- Optional but recommended for complex changes
- Separated from header by blank line
- Use bullet points for multiple changes
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gerred/mcpmc",
"version": "0.0.5",
"version": "0.0.6",
"description": "A MCP server for interacting with Minecraft via Mineflayer",
"private": false,
"type": "module",
Expand Down
36 changes: 4 additions & 32 deletions src/core/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export class MineflayerBot extends EventEmitter implements MinecraftBot {
constructor(connectionParams: ConnectionParams) {
super();
this.lastConnectionParams = connectionParams;
this.setupBot();
}

async connect(host: string, port: number, username: string): Promise<void> {
Expand Down Expand Up @@ -155,26 +154,10 @@ export class MineflayerBot extends EventEmitter implements MinecraftBot {
this.isConnected = false;
this.movements = null;

// Only attempt reconnect if we're not already connecting and haven't exceeded attempts
if (
!this.isConnecting &&
this.reconnectAttempts < this.maxReconnectAttempts
) {
this.reconnectAttempts++;
this.sendJsonRpcNotification("bot.reconnecting", {
attempt: this.reconnectAttempts,
maxAttempts: this.maxReconnectAttempts,
});
setTimeout(() => {
if (!this.isConnected && !this.isConnecting) {
this.setupBot();
}
}, 5000 * this.reconnectAttempts);
} else {
this.sendJsonRpcError(-32003, "Max reconnection attempts reached", {
attempts: this.reconnectAttempts,
});
}
// Send a notification that the bot has disconnected
this.sendJsonRpcNotification("bot.disconnected", {
message: "Bot disconnected from server",
});
}

private sendJsonRpcNotification(method: string, params: any) {
Expand Down Expand Up @@ -1142,17 +1125,6 @@ export class MineflayerBot extends EventEmitter implements MinecraftBot {
}

private wrapError(message: string): never {
// If we're not connected and have connection params, try to reconnect first
if (!this.bot && this.lastConnectionParams && !this.isConnecting) {
this.connect(
this.lastConnectionParams.host,
this.lastConnectionParams.port,
this.lastConnectionParams.username
).catch((error) => {
console.error("Failed to reconnect:", error);
});
}

const response: ToolResponse = {
content: [
{
Expand Down

0 comments on commit b2b1265

Please sign in to comment.