Jump to content

How can I disable hostile mob spawns in certain chunks?


BenGBoo

Recommended Posts

Hey all, I'm looking to make a simple mod that disables mobs from spawning in a square around a chunk. I'm new to Java (and therefore Minecraft Modding), so I poked around last night to come up with a simple Event Listener that intercepts new spawns and entity.remove()s the new entity before it can spawn. This seems inefficient because of game mechanics and ongoing spawns, so I was hoping that there was a way to simply disable spawns in a given Chunk. Is this possible?

 

I suppose I'm a bit overwhelmed with the vast amount of classes and methods of Forge's documentation. Anyone have a good recommendation as to where I should start? Or is my code (below) a sufficient place to start?

 

    @SubscribeEvent
    public static void spawnEntity(LivingSpawnEvent event) {
        LivingEntity entity = event.getEntityLiving();
        double deltaX = entity.getPosX() - 50;
        double deltaY = entity.getPosY() - 4;
        double deltaZ = entity.getPosZ() - 50;

        double distance = (Math.sqrt((deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ)));

        if (distance < 100) {
            entity.remove();
            NoSpawn.LOGGER.info("DELETED");
        } else {
            NoSpawn.LOGGER.info(distance);
        }
    }

 

Edited by BenGBoo
Link to comment
Share on other sites

1 hour ago, NOctu said:

Simply get the chunk

 


Chunk chunk = mc.theWorld.getChunkFromChunkCoords(entity.getPosX(), entity.getPosZ());

Oh, perfect! And is there any way to disable certain mobs / hostile mobs from spawning in this specific chunk?

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

This is not correct in so many ways.

  1. mc.theWorld implies two things:
    1. mc is a Minecraft instance, meaning this is the client. Which does not do mob spawning. As such this would be reaching across logical sides.
    2. theWorld has not been the name for this field in forever.
  2. getChunkFromChunkCoords is an ancient name.
  3. getPosX and getPosZ are block coordinates, getChunkFromChunkCoords requires chunk coordinates.

The correct way would be either World#getChunk(int, int) (which requries chunk coordinates) or World#getChunk(BlockPos) (which obviously requires block coordinates.

 

LivingSpawnEvent.CheckSpawn is the best you can do to achieve this. There is nothing built-in.

Awesome, thanks a bunch for the thorough answer, I really appreciate it!

Link to comment
Share on other sites

  • 5 months later...

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.