npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mineflayer-projectile

v0.1.1

Published

Effectively determine the trajectory & angle of projectiles in mineflayer with Newtonian mechanics

Downloads

30

Readme

Features

  • Determine the optimal angle for a projectile with a high level physical basis
  • Predict the movement of a target given their velocity/acceleration
  • Get a projectile's intersection coordinates with blocks

Installation

  • Execute the following in your node project directory:
npm install mineflayer-projectile

API

Loading the Plugin

const projectile = require("mineflayer-projectile")

const bot = mineflayer.createBot( ... )

bot.loadPlugin(projectile.plugin)

Return Types

 // (https://www.npmjs.com/package/vec3)
class Vec3;

// yaw and pitch needed to hit a target with a projectile
Angle = {
  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 = new Projectile(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) {
  return velocity
}

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

const mineflayer = require(`mineflayer`)
const projectile = require(`mineflayer-projectile`)
const bot = mineflayer.createBot()
bot.loadPlugin(projectile.plugin)

let attack = false
let occupied = false

async function shoot(target) {
  // draw bow
  bot.activateItem()
  await new Promise(resolve => setTimeout(resolve, 1000))

  // lock on
  let angle = bot.projectile.getAngle(bot.projectile.types.bow, bot.entity.position, target.position)
  if (angle === null) return // too far away
  await bot.look(angle.yaw, angle.pitch, true)

  // release
  bot.deactivateItem()
}

bot.on("message", json => {
  let message = json.toString()
  if (message.includes("$attack")) {
    attack = !attack
    bot.chat(attack ? "Now autonomously firing towards the closest enemy!" : "Ceasing all operation!")
  }
})

bot.on("physicsTick", async () => {
  if (attack && !occupied) {
    let target = bot.nearestEntity(entity => entity.type === "player")
    occupied = true

    if (target) {
      await shoot(target)
    }

    occupied = false
  }
});