Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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

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.

  • Author

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.

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.

  • Author

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.

  • Author

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?

  • Author

Nevermind, I just found the event.getChunk().entityLists[] array.  That makes it easy.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.