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.

Featured Replies

Posted

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?

  • Author

Was trying to adapt it from https://github.com/sameer/. I kinda figured I needed a better example. I'll try that and see how it goes. Thanks for your help and very quick replies as usual.

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

  • Author

We all got to start from somewhere.

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

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.