#Is there any sort of onEntityTick event?
10 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
[➤](#1401419191374057603 message)
I'm trying to allow chickens to be sheared for feathers on a cooldown. My initial plan was to compare entity.age to a piece of persistent entity data set to:
entity.age + lastSuccesfulShearTick
to see if enough time has passed to allow another feather drop. This works while the player stays in the world, however, once a player logs out then back in, entity.age is reset to zero, while the persistent entity data remains.
This means that for a 5 minute cooldown, if the player stays in the world for 20 minutes, then shears a chicken and relogs, that chicken's shear cooldown will be 25 minutes, instead of the intended 20. Since the persistent data is set to 25 minutes worth of ticks, while entity.age is counting up from zero.
in a level you can get all entities using .entities
so you can use LevelEvents.tick and then event.level.entities
Can’t you just set a piece of persistent entity data to current tick + cooldown and then check if that value is < current tick when shearing
that's also a possibility
Thanks for the help guys. Great idea to use LevelEvents
Almost. CurrentTick resets to 0 on world restart. Meaning if you play for an hour, shear a chicken, then restart the world, your shear cooldown will be an hour. My eventually solution is basically the same as your idea, just without storing the current tick data directly:
const shearChickenBaseCooldown = 100 // in ticks
LevelEvents.tick(event => {
let levelEntities = event.level.entities
let chickenEntities = levelEntities.filter(entity => {
return entity.getName().getString() === 'Chicken'
})
if (chickenEntities.length) {
chickenEntities.forEach(chickenEntity => {
if (chickenEntity.persistentData.remainingShearCooldown > 0) {
chickenEntity.persistentData.remainingShearCooldown--
}
})
}
})
ItemEvents.entityInteracted('minecraft:shears', (event => {
let entity = event.getTarget()
let player = event.getPlayer()
if (entity.getName().getString() === 'Chicken') {
if (!entity.persistentData.remainingShearCooldown) {
console.info(`sheared chicken`)
entity.persistentData.remainingShearCooldown = shearChickenBaseCooldown
event.target.block.popItemFromFace(Item.of("minecraft:feather"), "up")
player.swing()
const damageSource = entity.damageSources().playerAttack(player)
entity.attack(damageSource, 0)
} else {
player.swing()
console.info(`shear chicken on cooldown`)
}
}
}))```
Ticket closed!