Scaling Health

Scaling Health

23M Downloads

Too much entity ticking

Raycoms opened this issue ยท 0 comments

commented
@SubscribeEvent
public static void onLivingUpdate(LivingEvent.LivingTickEvent event) {
    LivingEntity entity = event.getEntity();
    //Return if players are empty on an integrated server, as the player needs a small delay to connect.
    if (entity.level.isClientSide || (entity.level.players().isEmpty() && !((ServerLevel)entity.level).getServer().isDedicatedServer()))
        return;

    // Tick mobs, which will calculate difficulty when appropriate and apply changes
    if (entity instanceof Mob)
        entity.getCapability(DifficultyAffectedCapability.INSTANCE).ifPresent(data ->
                data.tick((Mob)entity));

    if(entity instanceof TamableAnimal) {
        if(!((TamableAnimal) entity).isTame()) return;
            entity.getCapability(PetHealthCapability.INSTANCE).ifPresent(data ->
                    data.tick((TamableAnimal) entity));
    }

    if (entity instanceof Player && entity.level.getGameTime() % 20 == 0) {
        entity.getCapability(DifficultySourceCapability.INSTANCE).ifPresent(source -> {
            source.addDifficulty((float) SHDifficulty.changePerSecond());
        });
    }
}

This is done every single tick which could easily be done every 20 ticks for each entity and uses much less resources.
Difficulty adjustments don't happen that often.