Skip to content

Commit

Permalink
move used event cleaning to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixes committed May 2, 2021
1 parent 0b1b249 commit ffa0367
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function init(mineflayer) {
function CleanUpHurts() {
const min_time = new Date() - max_age_cleanup;
for (var i = last_hurts.length-1; i > 0 ; i--) { // running in reverse allows us to remove more than one element
if (last_hurts[i].time < min_time || last_hurts[i].used) {
if (last_hurts[i].time < min_time) {
last_hurts.splice(i,1);
}
}
Expand All @@ -62,7 +62,22 @@ function init(mineflayer) {
function CleanUpAttacks () {
const min_time = new Date() - max_age_cleanup;
for (var i = last_attacks.length-1; i > 0 ; i--) {
if (last_attacks[i].time < min_time || last_attacks[i].used) {
if (last_attacks[i].time < min_time) {
last_attacks.splice(i,1);
}
}
}

function CleanUsedEvents() {
// running in reverse allows us to remove more than one element
for (var i = last_hurts.length-1; i > 0 ; i--) {
if (last_hurts[i].used) {
last_hurts.splice(i,1);
}
}

for (var i = last_attacks.length-1; i > 0 ; i--) {
if (last_attacks[i].used) {
last_attacks.splice(i,1);
}
}
Expand Down Expand Up @@ -125,11 +140,12 @@ function init(mineflayer) {
CorrelateAttack(hurt_index,attack_index);
}
}
CleanUsedEvents();
}

/**
*
* @param {Entity} entity
* @param entity
* @param time
* @return {{time: *, used: boolean, entity: *}}
* @constructor
Expand All @@ -141,14 +157,14 @@ function init(mineflayer) {
bot.on("entityHurt",function (entity) {
//console.log("hurt");
const time = new Date();
last_hurts.push( MakeAttack(entity,time) );
last_hurts.push( MakeEvent(entity,time) );
CorrelateAttacks();
});

bot.on("entitySwingArm",function (entity) {
//console.log("armswing")
const time = new Date();
last_attacks.push( MakeAttack(entity,time) );
last_attacks.push( MakeEvent(entity,time) );
CorrelateAttacks();
});
}
Expand Down

0 comments on commit ffa0367

Please sign in to comment.