From 400995a3115094496a0cb0fa94a438c2182a2a09 Mon Sep 17 00:00:00 2001 From: Ethan Ethan Date: Thu, 11 Jan 2024 05:03:37 +0000 Subject: [PATCH] Update nodejs/controller/mc-bot.js --- nodejs/controller/mc-bot.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/nodejs/controller/mc-bot.js b/nodejs/controller/mc-bot.js index b4af70f..f381a77 100644 --- a/nodejs/controller/mc-bot.js +++ b/nodejs/controller/mc-bot.js @@ -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 {CJbot} = require('../model/minecraft'); +const {CJbot} = require('../model/minecraft'); const inventoryViewer = require('mineflayer-web-inventory'); const commands = require('./commands'); const {onJoin} = require('./player_list') -for(let name in conf.mc.bots){ - if(CJbot.bots[name]) continue; - let bot = new CJbot({name, host: conf.mc.host, ...conf.mc.bots[name]}); - CJbot.bots[name] = bot; +for(let name in conf.mc.bots){ //navigates to object in mc-bot-town/nodejs/conf/base.js + 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]}); //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. + //... 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 [name, toAdd] of Object.entries(commands[command])){ - bot.addCommand(name, toAdd) + 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])){ //looks at each key:value pair in the commands array. Object.entries returns an array for each keyvalue pair + 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. } }