Minecraft Server on a Rasberry PI and a bot which follows players
Btten var gjord med hjälp av Node.js och Mineflayer pluginen.
https://www.youtube.com/watch?v=ltWosy4Z0Kw&list=PLh_alXmxHmzGy3FKbo95AkPp5D8849PEV&index=1
Hur man sätter upp minecraft server: https://www.tomshardware.com/how-to/raspberry-pi-minecraft-server
Här är javascripten vi använde:
const mineflayer = require('mineflayer');
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder');
const bot = mineflayer.createBot({
host: '', // localhost eller server ip
port: 25565, // Standard porten för minecraft
auth: 'microsoft', // Use Microsoft authentication
username: '', // Din Minecraft Microsoft email
password: '' // Ditt Minecraft Microsoft password
});
bot.on('spawn', () => {
console.log('Bot has spawned in the game!');
});
// Load the pathfinder plugin
bot.loadPlugin(pathfinder);
bot.once('spawn', () => {
const mcData = require('minecraft-data')(bot.version);
const defaultMove = new Movements(bot, mcData);
bot.pathfinder.setMovements(defaultMove);
console.log("Bot has spawned! Type 'follow me' in chat to make it follow.");
});
bot.on('chat', (username, message) => {
if (username === bot.username) return;
if (message.toLowerCase() === "follow me") {
const target = bot.players[username]?.entity;
if (!target) {
bot.chat("I can't see you!");
return;
}
bot.chat(`Following ${username}...`);
bot.pathfinder.setGoal(new goals.GoalFollow(target, 1));
bot.on('physicTick', () => {
const currentBlock = bot.blockAt(bot.entity.position.offset(0, 1, 0));
if (currentBlock && currentBlock.name === 'ladder') {
bot.setControlState('jump', true);
} else {
bot.setControlState('jump', false);
}
});
}
if (message.toLowerCase() === "stop") {
bot.chat("Okay, I'll stop moving.");
bot.pathfinder.setGoal(null);
}
});
// CAMERA / webserver
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('a user connected');
bot.on('chat', (username, message) => {
socket.emit('chat', { username, message });
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});