Skip to content

Commit

Permalink
Refactor chat message URLs in Dashboard component and add disconnect_…
Browse files Browse the repository at this point in the history
…on_player_proximity.js function
  • Loading branch information
SilkePilon committed Sep 20, 2024
1 parent 2e4572d commit ab6b128
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
64 changes: 64 additions & 0 deletions flow_functions/disconnect_on_player_proximity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { getBot } = require("../main.js");

function main(data) {
const bot = getBot();
const radius = data.radius;

if (radius <= 0) {
throw new Error("Invalid radius. Please provide a positive number.");
}

console.log(
`Starting background player proximity check with radius: ${radius} blocks`
);

function checkPlayerProximity() {
const nearbyPlayers = Object.values(bot.players).filter((player) => {
if (!player.entity) return false;
const distance = bot.entity.position.distanceTo(player.entity.position);
return distance <= radius && player.username !== bot.username;
});

if (nearbyPlayers.length > 0) {
const nearestPlayer = nearbyPlayers[0];
const distance = bot.entity.position.distanceTo(
nearestPlayer.entity.position
);
console.log(
`Player ${nearestPlayer.username} detected within ${distance.toFixed(
2
)} blocks. Disconnecting...`
);
bot.quit("Player detected nearby");
return true; // Player detected, bot disconnected
}

return false; // No players detected
}

function startBackgroundCheck() {
const intervalId = setInterval(() => {
try {
if (checkPlayerProximity()) {
clearInterval(intervalId);
}
} catch (error) {
console.error("Error in player proximity check:", error);
clearInterval(intervalId);
}
}, 1000); // Check every second

// Attach the intervalId to the bot instance for potential cleanup
bot.proximityCheckIntervalId = intervalId;

console.log("Background player proximity check started");
}

// Start the background check
startBackgroundCheck();

// Continue execution immediately
console.log("Node execution completed, background check is running");
}

module.exports = { main };
12 changes: 12 additions & 0 deletions flow_functions/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,17 @@
"z": "number"
},
"author": "YourGitHubUsername"
},
"DISCONNECT_ON_PLAYER_PROXIMITY": {
"name": "DISCONNECT_ON_PLAYER_PROXIMITY",
"file": "disconnect_on_player_proximity.js",
"id": "DISCONNECT_ON_PLAYER_PROXIMITY",
"label": "Disconnect on Player Proximity",
"hasInput": true,
"description": "Disconnects the bot when a player comes within a specified radius",
"input": {
"radius": "number"
},
"author": "Assistant"
}
}

0 comments on commit ab6b128

Please sign in to comment.