Jump to content

[1.11] @SubscribeEvents inside Block class not running


MSpace-Dev

Recommended Posts

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)

Link to comment
Share on other sites

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.

  • Like 1

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

  • Like 1

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.

Link to comment
Share on other sites

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");

 

  • Like 1
Link to comment
Share on other sites

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
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Museumbola merupakan wadah judi online terkhusus untuk sbobet atau judi bola. Dengan daftar dan deposit menggunakan BANK BCA, Anda berkesempatan mendapatkan prediksi jitu pertandingan secara update setiap hari yang bisa anda jadikan acuan untuk bermain judi bola. Hanya dengan 10 ribu rupiah dan anda bermain Mix Parlay menggunakan prediksi yang telah di sediakan oleh kami, anda akan memenangkan jutaan rupiah. Yuk segera klik DAFTAR sekarang juga.
    • I was just trying to play my modded world when i randomly got this crash for no reason. I sorted through like every mod and eventually I realized it was LLibrary but I can't seem to find a solution to fix the crashing. I can't lose the world that I have that uses this mod please help me. Here's the report: https://pastebin.com/0D00B79i If anyone has a solution please let me know.  
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Ligawin88 adalah bocoran slot rekomendasi gacor dari Ligawin88 yang bisa anda temukan di SLOT Ligawin88. Situs SLOT Ligawin88 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Ligawin88 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Ligawin88 merupakan SLOT Ligawin88 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Ligawin88. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Ligawin88 hari ini yang telah disediakan SLOT Ligawin88. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Ligawin88 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Ligawin88 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Ligawin88 di link SLOT Ligawin88.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Asusslot adalah bocoran slot rekomendasi gacor dari Asusslot yang bisa anda temukan di SLOT Asusslot. Situs SLOT Asusslot hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Asusslot terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Asusslot merupakan SLOT Asusslot hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Asusslot. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Asusslot hari ini yang telah disediakan SLOT Asusslot. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Asusslot terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Asusslot terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Asusslot di link SLOT Asusslot.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Galeri555 adalah bocoran slot rekomendasi gacor dari Galeri555 yang bisa anda temukan di SLOT Galeri555. Situs SLOT Galeri555 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Galeri555 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Galeri555 merupakan SLOT Galeri555 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Galeri555. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Galeri555 hari ini yang telah disediakan SLOT Galeri555. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Galeri555 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Galeri555 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Galeri555 di link SLOT Galeri555.
  • Topics

×
×
  • Create New...

Important Information

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