mirror of
https://github.com/guilhermewerner/tcc
synced 2025-06-16 23:15:08 +00:00
Add minecraft bedrock server
This commit is contained in:
44
src/bedrock/bot.js
Normal file
44
src/bedrock/bot.js
Normal 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
24
src/bedrock/single.js
Normal 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)
|
25
src/multi.js
25
src/multi.js
@ -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 }
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user