Add minecraft bedrock server

This commit is contained in:
2024-09-09 13:28:08 -03:00
parent 54e5f3000c
commit 236b4b2960
4640 changed files with 688839 additions and 66 deletions

44
src/bedrock/bot.js Normal file
View File

@ -0,0 +1,44 @@
// Copyright (c) 2024 Guilherme Werner
// SPDX-License-Identifier: MIT
import bedrock from "bedrock-protocol";
import { parentPort, workerData } from 'worker_threads';
const { startBotIndex, endBotIndex } = workerData;
const movements = ['left', 'right'];
function getRandomMovement() {
return movements[Math.floor(Math.random() * movements.length)];
}
for (let i = startBotIndex; i < endBotIndex; i++) {
const bot = bedrock.createClient({
host: 'localhost',
port: 19132,
username: `Bot${i}`,
version: '1.20.0',
offline: true,
});
bot.on('time2', () => {
//const movement = getRandomMovement();
//const action = Math.random() > 0.5;
//bot.setControlState(movement, action);
//bot.setControlState('forward', true);
//bot.setControlState('sprint', true);
//bot.setControlState('jump', true);
});
bot.on('chat', (username, message) => {
if (username === bot.username) return;
bot.chat(message);
});
bot.on('kicked', (reason, loggedIn) => {
parentPort.postMessage(`Bot${i} foi expulso por: ${reason} | LoggedIn: ${loggedIn}`);
});
bot.on('error', (err) => {
parentPort.postMessage(`Bot${i} encontrou um erro: ${err}`);
});
}

24
src/bedrock/single.js Normal file
View File

@ -0,0 +1,24 @@
// Copyright (c) 2024 Guilherme Werner
// SPDX-License-Identifier: MIT
import bedrock from "bedrock-protocol";
const bot = bedrock .createClient({
host: 'localhost',
port: 19132,
username: 'Bot',
version: '1.20.0',
offline: true,
})
bot.once('spawn', () => {
//bot.setControlState('forward', true)
})
bot.on('chat', (username, message) => {
if (username === bot.username) return
bot.chat(message)
})
bot.on('kicked', console.log)
bot.on('error', console.log)

View File

@ -12,7 +12,30 @@ for (let i = 0; i < numThreads; i++) {
const startBotIndex = i * botsPerThread;
const endBotIndex = Math.min(startBotIndex + botsPerThread, numBots);
const worker = new Worker(path.resolve('src/bot.js'), {
const worker = new Worker(path.resolve('src/bedrock/bot.js'), {
workerData: { startBotIndex, endBotIndex }
});
worker.on('message', (message) => {
console.log(`Worker message: ${message}`);
});
worker.on('error', (error) => {
console.error(`Worker error: ${error}`);
});
worker.on('exit', (code) => {
if (code !== 0) {
console.error(`Worker stopped with exit code ${code}`);
}
});
}
for (let i = 0; i < numThreads; i++) {
const startBotIndex = i * botsPerThread;
const endBotIndex = Math.min(startBotIndex + botsPerThread, numBots);
const worker = new Worker(path.resolve('src/java/bot.js'), {
workerData: { startBotIndex, endBotIndex }
});