Jump to content

Distance from block to spawn event[solved]


Stroam
 Share

Recommended Posts

I'm trying to make a mob ward that prevents mobs from spawning within a set area for 1.12. I was hoping someone could point me to example code that checks to see if mob is within x distances of block. So far this is what I've been able to adapt from old 1.7 mods.

Spoiler

EventHandler

Spoiler

public class EventHandlerServer
{
    public static String debug;
    public static List<TileEntity> wardPositions;

    
    public static boolean isInRangeOfWard(final Entity entity) {
        for (final TileEntity coord : EventHandlerServer.wardPositions) {
            if (coord.getWorld().provider.getDimension() == entity.world.provider.getDimension() && entity.world.getTileEntity(coord.getPos()) instanceof IMobWard) {
                final TileEntity tile = entity.world.getTileEntity(coord.getPos());
                final double distanceSq = tile.getDistanceSq(entity.posX, entity.posY,entity.posZ);
                if (distanceSq <= ((IMobWard)tile).getMobWardRangeSquared()) {
                    return true;
                }
                continue;
            }
        }
        return false;
    }

    @SubscribeEvent
    public void mobWardDenySpawn(final LivingSpawnEvent.CheckSpawn event) {
        if (event.getResult() == Event.Result.ALLOW) {
            return;
        }
        if (event.getEntityLiving().isCreatureType(EnumCreatureType.MONSTER, false) && isInRangeOfWard(event.getEntity())) {
            event.setResult(Event.Result.DENY);
        }
    }
}

TileEntity

Spoiler

public class TileEntityMobWardTalisman extends TileEntity implements IMobWard
{
    @Override
    public double getMobWardRangeSquared() {
        if (this.getBlockType() instanceof BlockMobWardTalisman) {
            return 4096;
        }
        return 0;
    }
    
    public void invalidate() {
        EventHandlerServer.wardPositions.remove(this);
        super.invalidate();
    }
    
    public void onChunkUnload() {
        super.onChunkUnload();
        EventHandlerServer.wardPositions.remove(this);
    }
    
    public void validate() {
        final TileEntity myCoord = this;
        for (int i = 0; i < EventHandlerServer.wardPositions.size(); ++i) {
            final TileEntity coord = EventHandlerServer.wardPositions.get(i);
            if (myCoord == coord) {
                return;
            }
        }
        EventHandlerServer.wardPositions.add(myCoord);
    }

}

 

 

 

 

Edited by Stroam

You don't need to point out what's wrong with the programmer. I already know. Now what's wrong with the code?

Link to comment
Share on other sites

Oh god. Please stop using that code, immediately. It is broken in many ways and very inefficient on top of that.

You only need to search the chunks that are within the range of your entity and then you can search Chunk::getTileEntityMap().values() for your TE. Even if you don't do that, you can't just stuff tile entities into a global list like that.

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 Terms of Use.