#get all the player's pets

23 messages · Page 1 of 1 (latest)

pseudo sail
#

i need a method that would let me iterate over a player's pets on their death and turn their owner's id to dead

rough vigilBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

pseudo sail
#

essentially making pets forget their owners when they die. a better solution would be to untame them, but i'm not sure it's possible

marble wharf
pseudo sail
#

also modded ones

#

so i guess the list could be pulled from the 'best friends forever' achievement?

#

do I just iterate all the entities and find those with the matching Owner tag?

#

but how do I handle those that aren't loaded

marble wharf
#

The only way I can imagine this being possible (in a clean way) is using forge's AnimalTameEvent
Storing the uuid in player's persistent data and then when killed check if the entities are loaded and change thier tag

As you suggested above you could also loop over all entities, but then idk how to get the list of tamable entities

the above solution would also support unloaded ones, technically, by doing some modifications, but it's horribly slow

#

allthough the AnimalTameEvent might also not work for modded tamable animals

pseudo sail
#

i could also store a death counter on the player and in the tamed animals, untaming them on load if there's a mismatch

#

but there doesn't seem to be a load event

#

entity tick might work but seems awfully slow

#

given the amount of entities there are loaded at once

marble wharf
#

yeah that would probably be the best way, but yeah slow, you would probably need to distribute the all entity scanning across multiple ticks

#

maybe like scan 100 entities, if there are more, do them in the next

proud topaz
#

I've used a method before to get all tamed entities of a player or if the entity is tamed in general:

//You can use this to check if an entity is tamed to an owner - including modded entities that use things like ownerUUID or even isPlayerTrusted
function isTamedBy(entity, player) {
    let hasOwner = entity?.owner === player;
    let hasOwnerUUID = !!entity?.ownerUUID;
    let isTrustedByPlayer = typeof entity?.isPlayerTrusted === "function" 
        ? !!entity.isPlayerTrusted(player.uuid) 
        : false;
    return {
        tamed: hasOwner || hasOwnerUUID || isTrustedByPlayer,
        hasOwner: hasOwner,
        hasOwnerUUID: hasOwnerUUID,
        isTrustedByPlayer: isTrustedByPlayer
    };
}

//Checks if the entity is tamed in general without needing to provide player data
function isTamed(entity) {
    return (!!entity?.owner || !!entity?.ownerUUID || !!entity?.nbt?.TrustedPlayers);
}
pseudo sail
#

thanks

pseudo sail
#
//record owner death count on taming attempt
ItemEvents.entityInteracted((event)=>{
    let {target,player} = event
    if (isTamed(target)) return;
    target.persistentData.ownerDeaths = player.persistentData.deaths || 0
})

//record player death count
PlayerEvents.respawned((event)=>{
    event.player.persistentData.deaths = event.player.persistentData.deaths ? event.player.persistentData.deaths + 1 : 1
})

ServerEvents.tick((event)=>{
    event.entities.forEach((entity)=>{
        if (!isTamed(entity)) return;
        event.players.forEach((player)=>{
            if (isTamedBy(entity, player) && player.persistentData.deaths != entity.persistentData.ownerDeaths) {
                untame(entity)
                return;
            }
        })
    })
})

//You can use this to check if an entity is tamed to an owner - including modded entities that use things like ownerUUID or even isPlayerTrusted
function isTamedBy(entity, player) {
    let hasOwner = entity?.owner === player;
    let hasOwnerUUID = !!entity?.ownerUUID;
    let isTrustedByPlayer = typeof entity?.isPlayerTrusted === "function" 
        ? !!entity.isPlayerTrusted(player.uuid) 
        : false;
    return {
        tamed: hasOwner || hasOwnerUUID || isTrustedByPlayer,
        hasOwner: hasOwner,
        hasOwnerUUID: hasOwnerUUID,
        isTrustedByPlayer: isTrustedByPlayer
    };
}

//Checks if the entity is tamed in general without needing to provide player data
function isTamed(entity) {
    return (!!entity?.owner || !!entity?.ownerUUID || !!entity?.nbt?.TrustedPlayers);
}

function untame(entity) {
    delete entity.owner;
    delete entity.ownerUUID;
    delete entity.isPlayerTrusted;
}

i hope this works

#

it does not

#

seems like even running /data remove entity UUID Owner doesn't work

#

i guess i'll have to kill it

#

or set owner to dead