Jump to content

EntityJoinWorldEvent - New World vs Old World


the_infamous_1

Recommended Posts

I've been having issues with trying to get the DifficultyInstance in the event's position in the world during EntityJoinWorldEvent. I've recreated the method getDifficultyForLocation below and started debugging it by using it my event handler instead of the original method:

 

public DifficultyInstance getDifficultyAtPosition(World world, BlockPos pos) {
        long inhabitedTime = 0L;
        float moonPhaseFactor = 0.0F;


        HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
        HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);

        if (world.isBlockLoaded(pos)) {
            HardSteel.logger.info("Inside isBlockLoaded check");
            moonPhaseFactor = world.getCurrentMoonPhaseFactor();
            HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);
            
            // world.getChunkAt(pos)
            // returns this.getChunk(pos.getX() >> 4, pos.getZ() >> 4);
            // which means it gets the chunk at (X / 2^4), (Z / 2^4)
            Chunk chunk = world.getChunkAt(pos);
            inhabitedTime = chunk.getInhabitedTime();
            HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
        }

        HardSteel.logger.info("Done with isBlockLoaded check");

        HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
        HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);

        return new DifficultyInstance(world.getDifficulty(), world.getDayTime(), inhabitedTime, moonPhaseFactor);
    }

 

I notice that when opening an old world - meaning, a world that was created before I started ever handling EntityJoinWorldEvent - I can't get past "Chunk chunk = world.getChunkAt(pos)" during world load, causing the world loading process to never finish even though it says "100%" on the screen. Is there a work around I can use, or someway I can account for not being able to get the chunk?

I do notice that isBlockLoaded is a deprecated method, but I don't know how to replace it if it's necessary.

Edited by the_infamous_1
Link to comment
Share on other sites

I want to (re-)arm mobs with my modded armors and weapons instead of vanilla armors and weapons. I'm attempting to achieve this by recreating almost exactly how vanilla arms mobs, by using difficulty instances. I can do this just fine by using LivingEntitySpawn.SpecialSpawn (which also works for old worlds), but then I cannot get the Nether mobs (zombie pigmen and wither skeleton) to naturally spawn with modded stuff, so I am forced to use EntityJoinWorldEvent instead.

Link to comment
Share on other sites

I created a new world just now using LivingSpawnEvent.SpecialSpawn handling instead of EntityJoinWorldEvent. I went into the Nether, and every single Zombie Pigman and Wither Skeleton spawned with the vanilla swords instead of modded ones. The only time they ever spawn with modded swords are when they are spawned with a spawn egg and when they teleport to the Overworld via Nether Portal. Overworld mobs do spawn naturally with modded stuff.

Edited by the_infamous_1
Link to comment
Share on other sites

Okay, found a workaround! (I think)

 

public DifficultyInstance getDifficultyAtPosition(World world, BlockPos pos) {
        long inhabitedTime = 0L;
        float moonPhaseFactor = 0.0F;


        //HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
        //HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);

        if (world.isBlockLoaded(pos)) {
            //HardSteel.logger.info("Inside isBlockLoaded check");
            moonPhaseFactor = world.getCurrentMoonPhaseFactor();
            //HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);

            // world.getChunkAt(pos)
            // returns this.getChunk(pos.getX() >> 4, pos.getZ() >> 4);
            // which means it gets the chunk at (X / 2^4), (Z / 2^4)

            if(world.getChunkProvider().chunkExists(pos.getX() >> 4, pos.getZ() >> 4)){
                HardSteel.logger.info("Chunk Exists");
                Chunk chunk = world.getChunkAt(pos);
                inhabitedTime = chunk.getInhabitedTime();
                //HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
            }
        }

        //HardSteel.logger.info("Done with isBlockLoaded check");

        //HardSteel.logger.info("Inhabited Time is " + inhabitedTime);
        //HardSteel.logger.info("Current Moon Phase Factor is " + moonPhaseFactor);

        return new DifficultyInstance(world.getDifficulty(), world.getDayTime(), inhabitedTime, moonPhaseFactor);
    }

 

As long as I check for the chunk actually existing at the position before I try to get it, everything works fine and I don't get caught in that infinite loop I would get caught in previously.

Link to comment
Share on other sites

15 hours ago, diesieben07 said:

Why is this? I am not sure what made you come to this conclusion.

WorldEntitySpawner::func_226701_a_(still not renamed in the latest mappings) posts the event, but afterwards calls MobEntity::onInitialSpawn which is where Mobs give themselves equipment. I do believe the infamous's idea is to prevent them from having the default equipment. Especially Wither Skeletons and Zombie Pigmen.

Edited by Animefan8888

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

That's correct. I do try to check to see if the mob was already armed by the vanilla onInitialSpawn function and then re-arm them, but I don't know if that's actually working because I can still get mobs spawning in with vanilla equipment.

 

I imagine I can just run an additional check to see if the mob currently has vanilla armor/weapons, and then run them through the re-arming process an additional time, but that seems wasteful.

Edited by the_infamous_1
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
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.



×
×
  • Create New...

Important Information

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