Jump to content

[SOLVED] [1.7.10] Determining when an entity is loaded and saved off/removed


Recommended Posts

Posted

I was wondering if there is a way to know when an entity (mob or animal) is loaded (not necessarily spawned) and when they are removed (not necessarily killed or despawned) from active memory?

 

I was thinking of using the EntityJoinWorldEvent, but I didn't know if that was when the entity was first spawned or loaded back into the game.  As well, I didn't find an event that might be fired when the entity was saved off.

 

Thanks

Posted

If you are looking for straight load into memory then EntityEvent.EntityConstructing

 

This event should be used if you want to assign some extra values (ExtendedProps) to entity when it's loaded to game. Then the entity will join the world and you can use anything added in constructing event.

 

EDIT: About "removing from memory"

Inside Entity there is:

    /**
     * Will get destroyed next tick.
     */
    public void setDead()
    {
        this.isDead = true;
    }
    /**
     * Checks whether target entity is alive.
     */
    public boolean isEntityAlive()
    {
        return !this.isDead;
    }

This is probably what you are looking for. You can check every tick (using events ofc.) if given isEntityAlive() and once it returns it's not you could run one last living-code, next tick poof, everything is removed (object is nulled).

 

Note: Second part (removing) has never been tested personally, but LOOKS like it's what you want.

Note 2: If you are trying to attempt any data-saving stuff before entity is removed, there are special events for this, just sayin.

1.7.10 is no longer supported by forge, you are on your own.

Posted

If you are looking for straight load into memory then EntityEvent.EntityConstructing

 

This event should be used if you want to assign some extra values (ExtendedProps) to entity when it's loaded to game. Then the entity will join the world and you can use anything added in constructing event.

 

Thanks, that will probably do the trick for the loading entity portion.  (Crud, I forgot to ask, but I'm assuming there would something similar for a TileEntity).

 

EDIT: About "removing from memory"

Inside Entity there is:

    /**
     * Will get destroyed next tick.
     */
    public void setDead()
    {
        this.isDead = true;
    }
    /**
     * Checks whether target entity is alive.
     */
    public boolean isEntityAlive()
    {
        return !this.isDead;
    }

This is probably what you are looking for. You can check every tick (using events ofc.) if given isEntityAlive() and once it returns it's not you could run one last living-code, next tick poof, everything is removed (object is nulled).

 

Note: Second part (removing) has never been tested personally, but LOOKS like it's what you want.

Note 2: If you are trying to attempt any data-saving stuff before entity is removed, there are special events for this, just sayin.

I'm looking for something more towards Note2, I want to do some data-saving and processing before entity is removed - again, doesn't have to mean the entity is killed, just unloaded.

 

My overall goal is to track the moment a certain entity joins the world, and the moment it leaves the world, whether that be by spawn, load, death or save.

Posted

Could you please tell what EXACLY this tracking will be used for (what you want to archieve)? There are few ways to do few things that come to my mind, you pointed out "Note 2" so i assume you want to store some data, but what exacly? Maybe you don't even need to track that stuff, but just use save methods present in ExtendedProps writeNBT().

I have literally no idea what else than saving you could do "right before entity is nulled" because everything that happens next is literally non-existent for this entity (you know... it doesn't exist). Some stuff could be easily done with simple tricks instead of making very generalized handling (wide-reach events).

1.7.10 is no longer supported by forge, you are on your own.

Posted

I'm doing very little saving of data really.

 

What I am attempting to do is create a virtual entity that exists when the actual entity is removed - transferring the actual entity state to the virtual entity. Then when the actual entity is reloaded, I can transfer state back from virtual entity to actual entity.

 

I'm attempting to perform entity processing even when the entity would normally saved off because it exceeds the maximum chunk distance from the player.

Posted

Ok cool.  However, since it is a chunk unloading event, there isn't an entity or entity list associated with the chunk.  Do you think the best method of getting all the entities within that chunk is to use world.getEntitiesWithinAABB(...)? or is there a better method?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.