Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Code freezing server during world load, really cannot understand why


 Share

Recommended Posts

I have added an option to replace all skeletons in overworld with zombies, but it may (exactly, MAY and not WILL) crash server during world load.
Here is a problemmatic piece of code:
 

/* This code is inside EntityJoinWorldEvent event handler */
/* Replace skeletons with zombies? */


        if (ConfigHandler.GENERAL.noSkeletons.get()) {
                //Zombienation.LOGGER.debug("Attempting to replace skeleton with zombie...");
                if (event.getEntity() instanceof SkeletonEntity || event.getEntity() instanceof StrayEntity) {
                    if (event.getEntity().level.dimension().location().toString().equals("minecraft:overworld")) {
                        /* Random zombie randomly turns into any other zombie from vanilla or my mod on spawn */
                        /* Zombie itself isn't a problem - I have tried vanilla zombies or even pigs */
                        RandomZombie zombie = new RandomZombie(ZombiesRegistry.RANDOM_ZOMBIE.get(), event.getEntity().level);
                        zombie.setPos(event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ());
                        event.getEntity().level.addFreshEntity(zombie); //Server freezes at this line
                        /* Line below is never reached if server freezes, so it can't be an issue? */
                        event.setCanceled(true);
                        //Zombienation.LOGGER.debug("Done!");
                    }
                }
        }

 

Edited by Mad Alchemist
Fixed a typo in code piece
Link to comment
Share on other sites

You cannot spawn entities during EntityJoinWorldEvent safely, as it may fire during chunk loading (and you cannot spawn entities in a chunk that is currently loading). If you try to do this, the server tries to wait for the chunk to load fully so it can spawn the entity, but the chunk cannot continue loading until your event handler has completed. But your event handler cannot complete until the chunk is fully loaded so it can spawn the entity. Aka: You have a deadlock.

Look at ForgeInternalHandler#onEntityJoinWorld for how to do it properly.

14 hours ago, Mad Alchemist said:
event.getEntity() instanceof SkeletonEntity || event.getEntity() instanceof StrayEntity

This should really check the entity type instead, not the class.

 

14 hours ago, Mad Alchemist said:
event.getEntity().level.dimension().location().toString().equals("minecraft:overworld")

Compare the ResourceKey (result of dimension()) to Level.OVERWORLD instead of hardcoding the string value like this.

  • Thanks 1
Link to comment
Share on other sites

23 hours ago, diesieben07 said:

This should really check the entity type instead, not the class.

Leaving it "as is", it will also replace everything that extends SkeletonEntity or StrayEntity classes?
Not sure if I want it... It may be desired behavior if some mod adds skeletons by extending vanilla ones...

And thanks for help, I probably would never find out it myself...

 

Link to comment
Share on other sites

6 minutes ago, Mad Alchemist said:

Where is this "ForgeInternalHandler" located?

In Forge's code. Use your IDE's "search for class" functionality.

6 minutes ago, Mad Alchemist said:

And, can't I just do it in entity tick and check if chunk entity is in is fully loaded?

That would be way less efficient.

Link to comment
Share on other sites

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.