Skip to content

Commit

Permalink
Refactor vehicle passenger handling and dismount event logic
Browse files Browse the repository at this point in the history
- Remove unnecessary semicolons
- Simplify dismount event emission
- Ensure consistent null and array checks for vehicle and passenger entities
  • Loading branch information
TheRealPerson98 committed Jan 30, 2025
1 parent 04fa8f4 commit d4265af
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/plugins/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,22 +842,22 @@ function inject (bot) {
if (vehicle && !vehicle.passengers) {
vehicle.passengers = []
}

for (const passengerEntity of passengerEntities) {
if (!passengerEntity) continue; // Skip if passenger entity is null
if (!passengerEntity) continue // Skip if passenger entity is null

if (!passengerEntity.passengers) {
passengerEntity.passengers = []
}

const originalVehicle = passengerEntity.vehicle
if (originalVehicle && originalVehicle.passengers && Array.isArray(originalVehicle.passengers)) {
const index = originalVehicle.passengers.indexOf(passengerEntity)
if (index !== -1) {
originalVehicle.passengers.splice(index, 1) // Fixed: use splice directly instead of reassignment
}
}

passengerEntity.vehicle = vehicle
if (vehicle && vehicle.passengers && Array.isArray(vehicle.passengers)) {
vehicle.passengers.push(passengerEntity)
Expand All @@ -878,11 +878,11 @@ function inject (bot) {

// dismounting when the vehicle is gone
bot._client.on('entityGone', (entity) => {
if (!entity) return;
if (!entity) return

if (bot.vehicle === entity) {
bot.vehicle = null
bot.emit('dismount', (entity))
bot.emit('dismount', entity)
}
if (entity.passengers && Array.isArray(entity.passengers)) {
for (const passenger of entity.passengers) {
Expand Down

0 comments on commit d4265af

Please sign in to comment.