#entity.tick

7 messages · Page 1 of 1 (latest)

upbeat jewel
#

Adapted from @proven pagoda's 'entity.tick' implementation

const LivingEntity = Java.loadClass('net.minecraft.world.entity.LivingEntity')

function updateEntity(entity, level) {
    //check if the entity is removed; if the entity is a LivingEntity, optionally check if it is dying
    if (entity.removed || (typeof entity == LivingEntity && entity.deadOrDying)) {
    return;
    } else {
        //set scheduleDelay equal to the amount of time in ticks you would like a loop to take
        scheduleDelay = 20
        
        //RUN CODE HERE

    // Loops in scheduleDelay ticks
    Utils.server.scheduleInTicks(scheduleDelay,_ => {
      updateEntity(entity, level)
    })
    }
}

EntityEvents.spawned((e) => {
      //replace entityType with the entity/entities you want to "tick"
      if (e.entity.getType().includes('entityType')) {
        updateEntity(e.entity, e.level)
    }
})
upbeat jewel
#

just an update to this really quick; this could be a lot cleaner if you pass scheduledelay as an argument instead of defining it in the update code, and it's much much cleaner if you just pass in your update code as an anonymous method to updateEntity so that if you have multiple entities to update, you can define different behaviours in the spawn event from the same method

#

i'll append another example here later, for people like me who are quite frankly not competent at interpreting explanations like that and just.. Want To See The Source

upbeat jewel
#
let $Vec3 = Java.loadClass('net.minecraft.world.phys.Vec3')
let LivingEntity = Java.loadClass('net.minecraft.world.entity.LivingEntity')

EntityEvents.spawned((e) => {
    let level = e.level
    let entity = e.entity
    /*
    example script for making zombies chase after and attack cows
    very sloppy, no detection for if the zombies can actually *see* the cows, just here to serve as an example
    */
    if (entity.getType().includes('minecraft:zombie')) {
        /*
        make sure all the variables that you will access in your tick are available within this scope here!
        */
        updateEntity(entity, 20, function() {

            let entityList = level.getEntitiesWithin(AABB.of(entity.x-10,entity.y-10,entity.z-10,entity.x+10,entity.y+10,entity.z+10))
            let distance
            let closestEntity

            entityList.forEach(e => {
                if(e.type == "minecraft:cow") {
                    let _distance = Math.sqrt(Math.abs((entity.x - e.x) + (entity.y - e.y) + (entity.z - e.z)))
                    if(_distance < distance || distance == undefined) {
                        distance = _distance
                        closestEntity = e
                    }
                }
            })

            if(entity.target != null) {
                if(entity.target.deadOrDying == true || entity.target.removed == true) {
                    entity.setTarget(null)
                }
            } else if(closestEntity != undefined) {
                entity.setTarget(closestEntity)
            }
        })
    }
})

function updateEntity(pEntity, pUpdateSpeed, pUpdateEntity) {
    if (pEntity.removed || (typeof pEntity == LivingEntity && pEntity.deadOrDying)) {
        return;
    } else {
        pUpdateEntity()
        Utils.server.scheduleInTicks(pUpdateSpeed,_ => {
            updateEntity(pEntity, pUpdateSpeed, pUpdateEntity)
        })
    }
}
#

@proven pagoda, in case you were interested

proven pagoda
upbeat jewel
#

works in 1.20; updated tags to reflect this