Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mounting for other entities and fix bot not dismounting when the vehicle is gone #3384

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions lib/plugins/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,26 +716,73 @@ function inject (bot) {

// attaching to a vehicle
bot._client.on('attach_entity', (packet) => {
if (packet.entityId !== bot.entity.id) return
const vehicle = bot.vehicle
if (packet.vehicleId === -1) {
bot.vehicle = null
bot.emit('dismount', vehicle)
} else {
bot.vehicle = bot.entities[packet.vehicleId]
bot.emit('mount')
const passenger = fetchEntity(packet.entityId)
const vehicle = packet.vehicleId === -1 ? null : fetchEntity(packet.vehicleId)

const originalVehicle = passenger.vehicle
if (originalVehicle !== null) {
const index = originalVehicle.passengers.indexOf(passenger)
originalVehicle.passengers = originalVehicle.passengers.splice(index, 1)
}
passenger.vehicle = vehicle
vehicle.passengers.push(passenger)

if (packet.entityId === bot.entity.id) {
const vehicle = bot.vehicle
if (packet.vehicleId === -1) {
bot.vehicle = null
bot.emit('dismount', vehicle)
} else {
bot.vehicle = bot.entities[packet.vehicleId]
bot.emit('mount')
}
}
})

bot._client.on('set_passengers', ({ entityId, passengers }) => {
if (passengers[0] !== bot.entity.id) return
const vehicle = bot.vehicle
if (entityId === -1) {
const passengerEntities = passengers.map((passengerId) => fetchEntity(passengerId))
const vehicle = entityId === -1 ? null : bot.entities[entityId]

for (const passengerEntity of passengerEntities) {
const originalVehicle = passengerEntity.vehicle
if (originalVehicle !== null) {
const index = originalVehicle.passengers.indexOf(passengerEntity)
originalVehicle.passengers = originalVehicle.passengers.splice(index, 1)
}
passengerEntity.vehicle = vehicle
if (vehicle !== null) {
vehicle.passengers.push(passengerEntity)
}
}

if (passengers.includes(bot.entity.id)) {
const originalVehicle = bot.vehicle
if (entityId === -1) {
bot.vehicle = null
bot.emit('dismount', originalVehicle)
} else {
bot.vehicle = bot.entities[entityId]
bot.emit('mount')
}
}
})

// dismounting when the vehicle is gone
bot._client.on('entityGone', (entity) => {
if (bot.vehicle === entity) {
bot.vehicle = null
bot.emit('dismount', vehicle)
} else {
bot.vehicle = bot.entities[entityId]
bot.emit('mount')
bot.emit('dismount', (entity))
}
if (entity.passengers) {
for (const passenger of entity.passengers) {
passenger.vehicle = null
}
}
if (entity.vehicle) {
const index = entity.vehicle.passengers.indexOf(entity)
if (index !== -1) {
entity.vehicle.passengers = entity.vehicle.passengers.splice(index, 1)
}
}
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"prismarine-block": "^1.17.0",
"prismarine-chat": "^1.7.1",
"prismarine-chunk": "^1.34.0",
"prismarine-entity": "^2.3.0",
"prismarine-entity": "qwqtoday/prismarine-entity#master",
"prismarine-item": "^1.14.0",
"prismarine-nbt": "^2.0.0",
"prismarine-physics": "^1.8.0",
Expand Down
Loading