Compare commits

...

1 Commits

Author SHA1 Message Date
400995a311 Update nodejs/controller/mc-bot.js 2024-01-11 05:03:37 +00:00

View File

@ -1,22 +1,23 @@
'use strict'; 'use strict'; //enables Javascript's strict mode...enforces stricter error handling for cleaner code
const {sleep} = require('../utils'); const {sleep} = require('../utils'); //require() enables other files to be loaded in. (..) goes up one folder in the filepath
const conf = require('../conf'); const conf = require('../conf');
const {CJbot} = require('../model/minecraft'); const {CJbot} = require('../model/minecraft');
const inventoryViewer = require('mineflayer-web-inventory'); const inventoryViewer = require('mineflayer-web-inventory');
const commands = require('./commands'); const commands = require('./commands');
const {onJoin} = require('./player_list') const {onJoin} = require('./player_list')
for(let name in conf.mc.bots){ for(let name in conf.mc.bots){ //navigates to object in mc-bot-town/nodejs/conf/base.js
if(CJbot.bots[name]) continue; if(CJbot.bots[name]) continue; //Checks if the bot name in mc-bot-town/nodejs/conf/base.js is already present in the CJbot class (located in mc-bot-town-2/nodejs/model/minecraft.js). If it does, it skips over this if statement.
let bot = new CJbot({name, host: conf.mc.host, ...conf.mc.bots[name]}); let bot = new CJbot({name, host: conf.mc.host, ...conf.mc.bots[name]}); //If not,adds a new bot to the CJbot class, with data from conf.mc.host to provide the bot name, host (server), and other information.
CJbot.bots[name] = bot; //... expands an object into key:value pairs. conf.mc.bots[name] contains information regarding commands, autolog, etc.
CJbot.bots[name] = bot; //refers to line 29 in model/minecraft.js. In the CJbot class, an empty object named bots is created. This appends the bot name to this object, and is used to list all available bots.
for(let command of conf.mc.bots[name].commands || ['default']){ for(let command of conf.mc.bots[name].commands || ['default']){ //Navigates to the commands object in nodejs/conf/base.js and runs lines 19-21 on each command. If no command is found, a list of default commands are used.
for(let [name, toAdd] of Object.entries(commands[command])){ for(let [name, toAdd] of Object.entries(commands[command])){ //looks at each key:value pair in the commands array. Object.entries returns an array for each keyvalue pair
bot.addCommand(name, toAdd) bot.addCommand(name, toAdd) //adss the command to the specified bot in the class CJbot. addCommand is a function defined in model/minecraft.js line 329.
} }
} }