husk at have downloadet NodeJs & vscode
- lav en tom mappe
- open mappen i vscode
- kør følgende commands i terminalen
npm init -y
npm i mineflayer
- herefter bare lav en tom javascript fil (f.eks. index.js)
- nu kan du prøve og lave din første minecraft bot!!
const mineflayer = require('mineflayer');
//creating the bot
const bot = mineflayer.createBot({
host: 'pvp.asguho.dk',
port: '25543',
username: 'player1',
})
//on the event spawn, sent in chat the message 'helloworld'
bot.on('spawn', () => {
bot.chat('hello world');
})
// Log errors and kick reasons:
bot.on('kicked', console.error);
bot.on('error', console.error);
const mineflayer = require('mineflayer');
//creating the bot
const bot = mineflayer.createBot({
host: 'pvp.asguho.dk',
port: '25543',
username: 'player1',
})
//on the event chat, if the messsage is hit, then it attacks
bot.on('chat', (username, message) => {
if(message == 'hit'){
bot.attack(bot.nearestEntity());
}
})
// Log errors and kick reasons:
bot.on('kicked', console.error);
bot.on('error', console.error);
husk at installere pathfinderen:
npm i mineflayer-pathfinder
for en list af goals her: goals
const mineflayer = require('mineflayer')
const { pathfinder, Movements, goals } = require("mineflayer-pathfinder");
//creating the bot
const bot = mineflayer.createBot({
host: 'pvp.asguho.dk',
port: '25543',
username: 'player1',
})
//loading the pathfinder plugin
bot.loadPlugin(pathfinder);
//on the event chat, if the messsage is come, then it pathfinds to the nearest entity
bot.on('chat', (username, message) => {
if(message == 'come'){
const target = bot.nearestEntity();
bot.pathfinder.setGoal(new goals.GoalNear(target.x, target.y, target.z, 1))
}
})
// Log errors and kick reasons:
bot.on('kicked', console.error)
bot.on('error', console.error)
har du aldrig arbejdet med events så læs her: Listening for an event
bot.on('physicsTick', () => {
//alt herinde vil blive kørt hvert tick, aka 20 gange i sekundet.
})
events er virkelig pratiske her er alle dem fra mineflayer: mineflayer events
husk at installere mineflayer-collectblock:
npm i mineflayer-collectblock
bot.loadPlugin(require('mineflayer-blockfinder').plugin);
bot.on('chat', async (username, message) => {
if(message == 'dig'){
const mcData = require('minecraft-data')(bot.version);
const dirtblock = bot.findBlock({
matching: mcData.blocksByName.oak_log.id,
maxDistance: 64
})
await bot.collectBlock.collect(dirtblock)
bot.chat('dirt collected')
}
})
har du aldrig prøvet async funtioner så læs her: promises
bot.on('chat', async (username, message) => {
if(message == 'craft'){
await craftItem(oak_planks, 1);
bot.chat('done crafting!!');
}
})
async function craftItem (name, amount) {
amount = parseInt(amount, 10)
const mcData = require('minecraft-data')(bot.version)
const item = mcData.itemsByName[name]
const craftingTableID = mcData.blocksByName.crafting_table.id
const craftingTable = bot.findBlock({
matching: craftingTableID
})
if (item) {
const recipe = bot.recipesFor(item.id, null, 1, craftingTable)[0]
if (recipe) {
bot.chat(`I can make ${name}`)
try {
await bot.craft(recipe, amount, craftingTable)
bot.chat(`did the recipe for ${name} ${amount} times`)
} catch (err) {
bot.chat(`error making ${name}`)
}
} else {
bot.chat(`I cannot make ${name}`)
}
} else {
bot.chat(`unknown item: ${name}`)
}
}