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

Hey again,

 

So I am trying to get the LivingEventSpawn.CheckSpawn event to run within my block class. I'm still quite new to events, and I'm not even sure of you can get events to run inside a block class... but the reason I need to run it here is so that I can check distances from this block's location. Also, I want to ask how I can check entities within a radius from my block. I've looked all around the web and can't find anything too useful.

@Mod.EventBusSubscriber
public class BlockTorch extends BlockBase {
	protected static final AxisAlignedBB TORCH_AABB = new AxisAlignedBB(0.3125, 0.0D, 0.3125D, 0.6875D, 0.8125D, 0.6875D);
    protected static BlockPos pos = null;

    public BlockTorch(String name, Material materialIn) {
        super(name, materialIn);
        this.setHardness(0.2f);
        this.setHarvestLevel("axe", 0);
        this.setSoundType(SoundType.WOOD);
        this.setLightLevel(1.0f);
    }

	@SubscribeEvent
    public void blockMobSpawning(LivingSpawnEvent.CheckSpawn event){
        float distance = event.getEntity().getDistanceToEntity(event.getEntity());
        Utils.getLogger().info("Distance to spawned Entity: " + distance);
    }

    @Override
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
        this.pos = pos;
        Utils.getLogger().info("Position Initialized: " + pos);
        super.onBlockAdded(worldIn, pos, state);
    }
}

(Not sure why the indents messed up)

1) Blocks are singeltons, as in there is only one instance of the class. So storing the position in the block would not work. What you want is a tile entity, or just a list of all the blocks in some other map somewhere.

2) @Mod.EventBusSubscriber is for static classes/methods. It will only register static handlers.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

  • Author

Oh, I was under the impression that when you placed a block in the world, it would create a new instance of the object, and each one would have separate pos variables. Thanks for clearing that up.

 

As for the checking radius thing? Do You have any clue how to do that from a Tile Entity?

5 minutes ago, MSpace-Dev said:

Oh, I was under the impression that when you placed a block in the world, it would create a new instance of the object.

Oh god no. That would take billions of bytes of RAM to store even the smallest of worlds. No no, Block classes are a model, this is why all of the methods are passed a World and BlockPos value. Also, by making that field static, you forced it to be the same value for all instances of your class anyway, so even if you were right, you still singleton-ized the value.

 

If you need per-position storage, you must use a TileEntity.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

8 hours ago, MSpace-Dev said:

Oh, I was under the impression that when you placed a block in the world, it would create a new instance of the object, and each one would have separate pos variables. Thanks for clearing that up.

 

As for the checking radius thing? Do You have any clue how to do that from a Tile Entity?

Adding tile entities is simple, you just make a class extending TileEntity, then you register it on your server/common proxy, and you override the methods hastileentity and and createtileentity. 

 

registering is like this:

GameRegistry.registerTileEntity(TileBlockBreaker.class, Vars.MOD_ID + ":tile_blockbreaker");

 

  • Author

Oh, I know how to register a Tile Entity. Thanks for trying to help though!

* My actual question was how can I execute events in a Radius from a Tile Entity?

You can't.

What you can do is utilize the position from the event, create an AA-bounding box of desired range and check for presence of your block within that box.

  • Author

Can I create the AABB around the tile entity's location, and then perform actions on entities within that AABB?

 

I noticed a method called getEntitiesWithinAABB()

 

So I'm guessing I can iterate through that list, and perform actions on the mob that I'm looking for within this iteration, correct?

Edited by MSpace-Dev

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.