Code Monkey home page Code Monkey logo

mineflayerdoc's Introduction

MineflayerDoc

Docs

Mineflayer docs

Pathfinding docs

Setup

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}`)
  }
}

Example of things you can make!

mineflayerdoc's People

Contributors

asguho avatar victordigital avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.