mindcraft/test/test_check_blocks.js

128 lines
4.5 KiB
JavaScript

import mineflayer from 'mineflayer';
import { Vec3 } from 'vec3';
const bot = mineflayer.createBot({
host: 'localhost', // Replace with your server IP or hostname
port: 55916, // Replace with your server port
username: 'andy', // Replace with your bot's username
// password: 'your_bot_password' // Only if the server has online-mode=true
});
bot.on('spawn', async () => {
console.log("Bot spawned. Starting blueprint check...");
await new Promise((resolve) => setTimeout(resolve, 10000));
const blockAtLocation = await bot.blockAt(new Vec3(142, -60, -179));
console.log(blockAtLocation);
const results = await checkLevelBlueprint(bot, blueprintData.levels[0]);
console.log(results)
const matchesBlueprint = await checkBluepint(bot, blueprintData);
console.log(`Blueprint check result: \n`);
console.log(matchesBlueprint.mismatches)
bot.quit();
});
async function checkBluepint(bot, blueprintData) {
const materials = blueprintData.materials;
const levels = blueprintData.levels;
const mismatches = [];
const matches = [];
for (let i = 0; i < levels.length; i++) {
const levelData = levels[i];
const result = await checkLevelBlueprint(bot, levelData);
mismatches.push(...result.mismatches);
matches.push(...result.matches);
}
return {
"mismatches": mismatches,
"matches": matches
};
}
async function checkLevelBlueprint(bot, levelData) {
const startCoords = levelData.coordinates;
const placement = levelData.placement;
const mismatches = [];
const matches = [];
for (let zOffset = 0; zOffset < placement.length; zOffset++) {
const row = placement[zOffset];
for (let xOffset = 0; xOffset < row.length; xOffset++) {
const blockName = row[xOffset];
const x = startCoords[0] + xOffset;
const y = startCoords[1];
const z = startCoords[2] + zOffset;
try {
const blockAtLocation = await bot.blockAt(new Vec3(x, y, z));
if (!blockAtLocation || blockAtLocation.name !== blockName) {
mismatches.push({
level: levelData.level,
coordinates: [x, y, z],
expected: blockName,
actual: blockAtLocation ? bot.registry.blocks[blockAtLocation.type].name : 'air' // Assuming air if no block
});
} else {
matches.push({
level: levelData.level,
coordinates: [x, y, z],
expected: blockName,
actual: blockAtLocation ? bot.registry.blocks[blockAtLocation.type].name : 'air' // Assuming air if no block
});
}
} catch (err) {
console.error(`Error getting block at (${x}, ${y}, ${z}):`, err);
return false; // Stop checking if there's an issue getting blocks
}
}
}
return {
"mismatches": mismatches,
"matches": matches
};
}
const blueprintData = {
"materials": {
"oak_planks": 20,
"oak_door": 1,
"stone": 26,
},
"levels": [
{
"level": 0,
"coordinates": [142, -60, -179],
"placement":
[
["stone", "stone", "oak_door", "stone", "stone"],
["stone", "air", "air", "air", "stone"],
["stone", "air", "air", "air", "stone"],
["stone", "stone", "stone", "stone", "stone"]
]
},
{
"level": 1,
"coordinates": [142, -59, -179],
"placement":
[
["stone", "stone", "oak_door", "stone", "stone"],
["stone", "air", "air", "air", "stone"],
["stone", "air", "air", "air", "stone"],
["stone", "stone", "stone", "stone", "stone"]
]
},
{
"level": 2,
"coordinates": [142, -58, -179],
"placement":
[
["oak_planks", "oak_planks", "oak_planks", "oak_planks", "oak_planks"],
["oak_planks", "oak_planks", "oak_planks", "oak_planks", "oak_planks"],
["oak_planks", "oak_planks", "oak_planks", "oak_planks", "oak_planks"],
["oak_planks", "oak_planks", "oak_planks", "oak_planks", "oak_planks"]
]
}
]
};