Files
mc-bot-town/nodejs/controller/swing.js
2025-05-04 19:22:31 -04:00

75 lines
1.6 KiB
JavaScript

'use strict';
const conf = require('../conf');
const {sleep} = require('../utils');
function faceEntity(bot, entity) {
if (!entity) return; // Check if entity is valid
const targetPosition = entity.position.offset(0, entity.height * 0.5, 0); // Focus on the middle of the entity
bot.bot.lookAt(targetPosition);
}
class Swing{
constructor(args){
this.bot = args.bot;
this.target = args.target;
this.interval = args.interval;
this.intervalStop;
this.isDangerous = true;
this.isAction = true;
}
async init(){
this.onReadyListen = this.bot.on('onReady', async ()=>{
console.log('Swing.init onReady called');
try{
this.block = this.bot.findBlockBySign('guardian\nattack spot');
await this.goToSpot();
await this.swing();
}catch(error){
console.error('Error in Swing.init:', error)
}
});
return true;
}
unload(){
console.log('Swing.unload');
clearInterval(this.intervalStop);
this.intervalStop = null;
this.onReadyListen();
return true;
}
async goToSpot(){
await this.bot.goTo({
where: this.block,
range: 3,
});
}
async swing(){
this.intervalStop = setInterval(async ()=>{
try{
let entity = this.bot.bot.nearestEntity(entity =>{
// console.log('looking for entity name', entity.name, entity.name?.toLowerCase());
return entity.name?.toLowerCase() === "guardian"
});
if(entity && this.bot.isWithinRange(entity.position, 3)){
faceEntity(this.bot, entity);
await this.bot.bot.attack(entity);
}
}catch(error){
console.log('Swing.swing interval error:', error);
}
}, 500);
}
}
module.exports = Swing;