You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
// (https://www.npmjs.com/package/vec3)classVec3;// yaw and pitch needed to hit a target with a projectileAngle={yaw: number,pitch: number}
Predefined Types & Constants
const{ Projectile, Constants }=require("mineflayer-projectile")/*A pre-defined type used for calculating projectile physics- "type" can be "bow", "crossbow", "potion", "expbottle", "trident", "throwable" (eggs, snowballs, pearls) or "firework" (fireworks shot from a crossbow)*/type=Constants["type"]/*Initialises a new projectile type used for calculating projectile physics- velocity (Number, optional) - The initial velocity, will assume 0 if unspecified (instantaneous)- gravity (Number, optional) - The gravity, will assume 0 if unspecified (linear)- chargeFunc (Void, optional) - A function returning the velocity after a set number of ticks (See below)- Returns: Projectile*/type=newProjectile(velocity,gravity,chargeFunc)/*A function returning a projectile's velocity after a number of ticks- ticks (Number) How long the projectile has been charging for (example: drawing a bow)*/chargeFunc=function(ticks){returnvelocity}
Methods
/*Predicts the target's position offset after a period of time- destination (Vec3) - Where the projectile is being fired- ticks (Number) - How long the target will move for (in ticks)- velocity (Vec3, optional) - How fast the target is moving- acceleration (Vec3, optional) - How fast the target's velocity is increasing- Returns: Vec3*/bot.projectile.getOffset(destination,ticks,velocity,acceleration)/*Gets the yaw and pitch required to hit a target- type (Projectile) - Projectile used for calculation- position (Vec3) - Where the projectile is being fired- destination (Vec3) - Where the projectile is landing- chargeTicks (Number, optional) - How long the projectile has been charging for (in ticks)- Returns: Angle*/bot.projectile.getAngle(type,position,destination,chargeTicks)/*Determines where the projectile will intersect with a block- type (Projectile) - Projectile used for calculation- position (Vec3) - Where the projectile is being fired- destination (Vec3) - Where the projectile is landing- chargeTicks (Number, optional) How long the projectile has been charging for (in ticks)- Returns: Vec3*/bot.projectile.getCollision(type,position,destination,chargeTicks)
Example
constmineflayer=require(`mineflayer`)constprojectile=require(`mineflayer-projectile`)constbot=mineflayer.createBot()bot.loadPlugin(projectile.plugin)letattack=falseletoccupied=falseasyncfunctionshoot(target){// draw bowbot.activateItem()awaitnewPromise(resolve=>setTimeout(resolve,1000))// lock onletangle=bot.projectile.getAngle(bot.projectile.types.bow,bot.entity.position,target.position)if(angle===null)return// too far awayawaitbot.look(angle.yaw,angle.pitch,true)// releasebot.deactivateItem()}bot.on("message",json=>{letmessage=json.toString()if(message.includes("$attack")){attack=!attackbot.chat(attack ? "Now autonomously firing towards the closest enemy!" : "Ceasing all operation!")}})bot.on("physicsTick",async()=>{if(attack&&!occupied){lettarget=bot.nearestEntity(entity=>entity.type==="player")occupied=trueif(target){awaitshoot(target)}occupied=false}});