friendlyhj/ZenUtils

[Feature request] `IEntityDefinition.onUpdate(...)`

Closed this issue · 3 comments

Please add event listener for a specific entity definition onUpdate (or onTick) action.
This is required when i want to add special mechanics only for specific entity.

Right now, only way i found is to iterate all entities in world, using onWorldTick event.

https://github.com/Krutoy242/Enigmatica2Expert-Extended/blob/master/scripts/mods/betteranimalsplus_goose.zs#L19-L31

events.onWorldTick(function (e as crafttweaker.event.WorldTickEvent) {
  val world = e.world;
  if (world.remote || world.time % 10 != 0) return;
  
  for entity in world.getEntities() {
    if (!(entity instanceof IEntityAnimal) || !entity.alive) continue;
    val animal as IEntityAnimal = entity;
    if (!animal.canPickUpLoot) continue;
    if (animal.definition.id != 'betteranimalsplus:goose') continue;
    val base as IEntityLivingBase = animal;
    tickGoose(base);
  }
});

This sounds inefficient, since i need to use additional comparisons such as animal.definition.id != 'betteranimalsplus:goose' to just find appropriate entity. Another problem is that it required iterating all entities, and if there is 1000+ of them on server, but 0 desirable entities, iterations just consume CPU without actual doing anything.

If there would be function like this, it could save lot of lines of code. And i assume that this function would not be called if world have 0 entities of this type, so could save CPU time.

<entity:betteranimalsplus:goose>.onUpdate(function(e){ ... })

P.S. would be also cool if onUpdate() have optional argument step or interval, so i dont need to check world.time % 10 != 0 100 times for 100 entities.

<entity:minecraft:creeper>.onTick(function(entity) {
    // the function is only called on server side. No need to check world.remote
    print("time" ~ entity.world.time);
}, 50);

I implement it by using catenation. I'm not sure if it can imporve performance.

Thank you! I will try to rewrite my logic with new methods and see how's its going.

Implemented and tested - performance show good results! I tested on ~200 entities in world and there is only 0.2% TPS impact. Very good!

image