Skip to content

Commit

Permalink
Add elytra flying support and rocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
lkwilson committed Aug 21, 2023
1 parent 9ecdf20 commit 15662d2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export interface Bot extends TypedEmitter<BotEvents> {
version: string
entity: Entity
entities: { [id: string]: Entity }
elytraFlying: boolean
fireworkRocketDuration: number
spawnPoint: Vec3
game: GameState
player: Player
Expand Down Expand Up @@ -260,6 +262,8 @@ export interface Bot extends TypedEmitter<BotEvents> {

wake: () => Promise<void>

elytraFly: () => Promise<void>

setControlState: (control: ControlState, state: boolean) => void

getControlState: (control: ControlState) => boolean
Expand Down
31 changes: 31 additions & 0 deletions lib/plugins/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function inject (bot, { hideErrors }) {
bot.heldItem = null
bot.usingHeldItem = false

bot.fireworkRocketDuration = 0

bot._client.on('entity_status', (packet) => {
if (packet.entityId === bot.entity.id && packet.entityStatus === 9 && !eatingTask.done) {
eatingTask.finish()
Expand Down Expand Up @@ -108,6 +110,35 @@ function inject (bot, { hideErrors }) {
hand: offHand ? 1 : 0
})
}

if (bot.elytraFlying) {
let heldItem
if (!offHand) {
heldItem = bot.heldItem
} else if (!bot.supportFeature('doesntHaveOffHandSlot')) {
const slot = bot.getEquipmentDestSlot('off-hand')
heldItem = bot.inventory.slots[slot]
}

if (heldItem != null && heldItem.name === 'firework_rocket') {
let flightDur = 1
if (heldItem.nbt != null) {
let nbt = heldItem.nbt
if (nbt.type === 'compound' && nbt.value.Fireworks != null) {
nbt = nbt.value.Fireworks
if (nbt.type === 'compound' && nbt.value.Flight != null) {
nbt = nbt.value.Flight
if (nbt.type === 'int') {
flightDur += nbt.value
}
}
}
}
const baseDuration = 10 * flightDur
const randomDuration = Math.floor(Math.random() * 6) + Math.floor(Math.random() * 7)
bot.fireworkRocketDuration = baseDuration + randomDuration
}
}
}

function deactivateItem () {
Expand Down
41 changes: 41 additions & 0 deletions lib/plugins/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,47 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {

bot.physics = physics

function getEffectLevel (mcData, effectName, effects) {
const effectDescriptor = mcData.effectsByName[effectName]
if (!effectDescriptor) {
return 0
}
const effectInfo = effects[effectDescriptor.id]
if (!effectInfo) {
return 0
}
return effectInfo.amplifier + 1
}

bot.elytraFlying = false

bot.elytraFly = async () => {
// todo: move to control state? but rocket isn't a control state
if (bot.elytraFlying) {
throw new Error('Already elytra flying')
} else if (bot.entity.onGround) {
throw new Error('Unable to fly from ground')
} else if (bot.entity.isInWater) {
throw new Error('Unable to elytra fly while in water')
}

const mcData = require('minecraft-data')(bot.version)
if (getEffectLevel(mcData, 'Levitation', bot.entity.effects) > 0) {
throw new Error('Unable to elytra fly with levitation effect')
}

const item = bot.inventory.slots[6]
if (item == null || item.name !== 'elytra') {
throw new Error('Elytra must be equip to start flying')
}
bot._client.write('entity_action', {
entityId: bot.entity.id,
actionId: 8,
jumpBoost: 0
})
bot.elytraFlying = true
}

bot.setControlState = (control, state) => {
assert.ok(control in controlState, `invalid control: ${control}`)
assert.ok(typeof state === 'boolean', `invalid state: ${state}`)
Expand Down

0 comments on commit 15662d2

Please sign in to comment.