From 4037a7ae9a47466b959f3e8d65fe474cf183f34f Mon Sep 17 00:00:00 2001 From: liquidsquid1 <151129309+liquidsquid1@users.noreply.github.com> Date: Fri, 1 Mar 2024 20:18:45 +0000 Subject: [PATCH] added proper filters and organised the code a lil more --- src/app.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/app.js b/src/app.js index 33ba6ce..8083c13 100644 --- a/src/app.js +++ b/src/app.js @@ -88,28 +88,30 @@ function createNewBot(user) { }); } +function locateNearestPlayer(bot) { + let mobFilter = (e) => e.type === 'player'; + let player = bot.nearestEntity(mobFilter); + return player; +} + function killauraTick(bot, range) { - let entity = bot.nearestEntity(); - if (entity !== null) { - if (entity.type === "player") { - if (bot.entity.position.distanceTo(entity.position) <= range) { - let pos = entity.position.offset(0, entity.height, 0); - - if (rotations) { - bot.lookAt(pos, true); - } - bot.attack(entity); - } - } - } + entity = locateNearestPlayer(bot); + if (!entity) { return; } + let pos = entity.position.offset(0, entity.height, 0); + + if (bot.entity.position.distanceTo(entity.position) > range) { return; } + if (rotations) { bot.lookAt(pos, true); } + bot.attack(entity); + return entity; } function fightBotTick(bot, range) { killauraTick(bot, range); - let entity = bot.nearestEntity(); - if (entity !== null && entity.type === "player") { - bot.pathfinder.setGoal(new GoalFollow(entity, range)); - } + + entity = locateNearestPlayer(bot); + if (!entity) { return; } + + bot.pathfinder.setGoal(new GoalFollow(entity, range)); } function readyCommands(bot) { @@ -146,7 +148,7 @@ function readyCommands(bot) { bowBot = !bowBot; console.log("bowbot: " + bowBot); if (bowBot) { - bot.hawkEye.autoAttack(bot.hawkEye.getPlayer(), 'bow'); + bot.hawkEye.autoAttack(locateNearestPlayer(), 'bow'); } else { bot.hawkEye.stop(); }