Jump to content

[SOLVED] [1.10] Find a tile entity within a AABB bounding box placed when an event happens


MSpace-Dev

Recommended Posts

Hello everyone,

 

So, I am making a block that needs to check when any mobs spawn around it. What I want to do is when this LivingSpawnEvent happens, is create an AABB at this event's position, and CHECK if my tile entity is within this box. I have no idea how to do this, and I've tried scouring the web, with no success. I'm also not sure how to create my AABB at this specific event's coords. This is what I have so far.

 

EVENT HANDLER:

@Mod.EventBusSubscriber
public class MonsterTotemsEventHandler {
    @SubscribeEvent
    public void mobSpawn(LivingSpawnEvent e) {
        BlockPos eventPos = e.getEntity().getPosition();
        AxisAlignedBB eventAABB = new AxisAlignedBB(10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f);
      	// if (tile entity is in AABB) {
    	//	execute code here
    	//}
    }
}

 

Thanks!

Edited by MSpace-Dev
Topic Solved
Link to comment
Share on other sites

You can call BlockPos#getAllInBox which returns a collection of BlockPos. You need to give the method 2 opposite corners. You can get those by using BlockPos::add(+-x, +-y, +-z)

You can then do a normal for-loop or lambda-for-each to iterate over each BlockPos.

  • Like 1

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

Awesome, that has helped a lot! Just one thing, I am new to Tile Entities. So, I am not sure how to reference the Tile Entity that I'm looking for. I have "bound" the Tile Entity to the block, using the createTileEntity() method. From here, do I just check for the blockstate, or do I search for the tile entity? Here is my code so far

@Mod.EventBusSubscriber
public class MonsterTotemsEventHandler {
    @SubscribeEvent
    public void mobSpawn(LivingSpawnEvent e) {
        BlockPos eventPos = e.getEntity().getPosition();
        Iterable<BlockPos> blocks = BlockPos.getAllInBox(eventPos.add(-10.0f, -10.0f, -10.0f), eventPos.add(10.0f, 10.0f, 10.0f));

        for (BlockPos pos : blocks) {
            World world = e.getWorld();
            if(world.getTileEntity(pos) == ){ // Not sure what to put here <<
				// execute code
            }
        }
    }
}

 

Link to comment
Share on other sites

Well, not all blocks have tileentities. In those cases, World::getTileEntity will return null, so you'll need a null-check first, or you'll very likely crash. After that, you can use an instanceof instead of equals-check, and just reference your TileEntity's type eg if(world.getTileEntity(pos) instanceof TileEntityFurnace)

Edited by Matryoshika
  • Like 1

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

So, I'm getting this error when in-game.

A TileEntity type io.github.mspacedev.tiles.TileEntityTotemBase has throw an exception trying to write state. It will not persist. Report this to the mod author
java.lang.RuntimeException: class io.github.mspacedev.tiles.TileEntityTotemBase is missing a mapping!

Even though, I have reason to believe, that I have mapped the Tile Entity. (Again, new to this)

 

BLOCK CLASS:

public class BlockTotemBase extends BlockBase implements ITileEntityProvider {

    public BlockTotemBase(String name, Material materialIn) {
        super(name, materialIn);
    }

    @Nullable
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new TileEntityTotemBase();
    }
}

 

TILE ENTITY TOTEM BASE:

public class TileEntityTotemBase extends TileEntity {
	// Empty
}

 

MAIN CLASS INIT: (removed a bunch of unnecessary code, that does not need to be seen)

@Mod(modid = Reference.MODID, name = Reference.NAME, version = Reference.VERSION )
public class MonsterTotems {
    public static void init(FMLInitializationEvent event){
        proxy.init(event);
        ModTileEntities.init();
    }
}

 

MOD TILE ENTITIES:

public class ModTileEntities {

    public static void init(){
        GameRegistry.registerTileEntity(TileEntityTotemBase.class, "totem_base_i");
    }
}

 

Btw, I register the BlockTotemBase with the same id as the Tile Entity.

Link to comment
Share on other sites

6 minutes ago, MSpace-Dev said:

MAIN CLASS INIT: (removed a bunch of unnecessary code, that does not need to be seen)


@Mod(modid = Reference.MODID, name = Reference.NAME, version = Reference.VERSION )
public class MonsterTotems {
    public static void init(FMLInitializationEvent event){
        proxy.init(event);
        ModTileEntities.init();
    }
}

 

You need to make your init method non-static and you need to add @Mod.EventHandler on top of the method.

Edited by larsgerrits
  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

I've done that. Still get the same error.

 

MAIN CLASS:

public void init(FMLInitializationEvent event){
        proxy.init(event);
        ModTileEntities.init();
    }

 

MOD TILE ENTITIES:

public class ModTileEntities {

    @Mod.EventHandler
    public static void init(){
        GameRegistry.registerTileEntity(TileEntityTotemBase.class, "totem_base_i_te");
    }
}

 

Link to comment
Share on other sites

3 minutes ago, MSpace-Dev said:

I've done that. Still get the same error.

 

MAIN CLASS:


public void init(FMLInitializationEvent event){
        proxy.init(event);
        ModTileEntities.init();
    }

 

MOD TILE ENTITIES:


public class ModTileEntities {

    @Mod.EventHandler
    public static void init(){
        GameRegistry.registerTileEntity(TileEntityTotemBase.class, "totem_base_i_te");
    }
}

 

No, add @Mod.EventHandler on top of the init method in your @Mod class.

  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Awesome, finally got that working. Geez, sorry about that... 

Now that that is out of the way, back to my main question. I have got this so far:

 

MONSTER TOTEMS EVENT HANDLER:

@Mod.EventBusSubscriber
public class MonsterTotemsEventHandler {
    @SubscribeEvent
    public void mobSpawn(LivingSpawnEvent e) {
      	Utils.getLogger().info("MOB SPAWNED!!!");

        BlockPos eventPos = e.getEntity().getPosition();
        Iterable<BlockPos> blocks = BlockPos.getAllInBox(eventPos.add(-100.0f, -100.0f, -100.0f), eventPos.add(100.0f, 100.0f, 100.0f));

        for (BlockPos pos : blocks) {
            World world = e.getWorld();
            if(world.getTileEntity(pos) != null) {
                if (world.getTileEntity(pos) instanceof TileEntityTotemBase) {
                    e.getEntity().setFire(100);
                  	Utils.getLogger().info("MOB CLOSE TO TILE ENTITY!!!");
                }
            }
        }
    }
}

 

Anyways, long-story-short, none of the logger messages get outputted to the console, when a mob spawns during night time. Which means this event handler isn't working.

 

MAIN CLASS: (postInit())

public void postInit(FMLPostInitializationEvent event){
        proxy.postInit(event);
        MinecraftForge.EVENT_BUS.register(new MonsterTotemsEventHandler());
    }

 

EDIT: I need @Mod.EventHandler on top... don't I...?

Edited by MSpace-Dev
Link to comment
Share on other sites

4 minutes ago, MSpace-Dev said:

EDIT: I need @Mod.EventHandler on top... don't I...?

Yes, you need it on top of all the FML lifecycle events.

  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

  • 2 months later...
On 17.12.2017 at 0:12 AM, diesieben07 said:
  • Problematic code issue 4 and 10.
  • instanceof includes a null check already, you do not need a separate one.
  • There is a much more efficient way to check for TileEntities in an area, since every Chunk has a list of it's TileEntities (Chunk::getTileEntityMap). You can just get all Chunks that fall within your area and only check the TileEntities within them, instead of every single block position. This is particularly important since you are doing this on every mob spawn (this happens a lot!). For something even more efficient you could add a chunk capability and use it to store only your TileEntities within that chunk, which would allow you to only check that map.

 

I've read through the docs but it's still kinda hazy for me, wouldn't it require a fair amount of workaround to add a capability to such an important part of vanilla code?

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

    • Yes... You're right, this mod conflicts with very many other mods causing this error.
    • Hi, the microphone mod is not working on my Mac. It says “launcher does not support MacOS microphone permissions” Thank you in advance for answering.
    • Make sure you have Optifine installed as a mod. Go into Options > Video Settings > Shaders > and then click the shader you want Make sure the shader.zip files are in the shaderpacks folder inside the minecraft folder
    • It sounds like you're probably registering the item in the wrong place, try looking at this tutorial for how to register items:  Forge Modding Tutorial - Minecraft 1.20: Custom Items & Creative Mode Tab | #2 This (free) tutorial series is excellent, by the way, and I'd highly recommend watching through some or all of the videos. There may also be an error in the code I showed above since I was in a hurry, but it should be enough for the general idea. I can't be more specific since I don't know exactly what you plan to do.
    • Realizing I was a victim of a scam was a devastating blow. My initial investment of $89,000, driven by dreams of financial success and the buzz surrounding a new cryptocurrency project, turned into a nightmare. The project promised high returns and rapid gains, attracting many eager investors like myself. However, as time passed and inconsistencies began to surface, it became evident that I had made a grave mistake by not thoroughly vetting the brokerage company handling the investment. Feeling anxious and betrayed, I desperately searched for a way to recover my funds. It was during this frantic search that I stumbled upon the Lee Ultimate Hacker tool through a Facebook post. With little left to lose, I decided to reach out to their team for help. To my relief, they were quick to respond and immediately started recovering my compromised email and regaining access to my cryptocurrency wallets. The team at Lee Ultimate Hacker was incredibly professional and transparent throughout the process. They meticulously traced the digital footprints left by the scammers, employing advanced technological methods to unravel the complex network that had ensnared my funds. Their expertise in cybersecurity and recovery strategies gradually began to turn the tide in my favor. Although the scammers had already siphoned off $30,000 worth of Bitcoin, Lee Ultimate Hacker was relentless in their pursuit. They managed to expose the fraudulent activities of the scam operators, revealing their identities and the mechanisms they used to lure investors. This exposure was crucial not only for my case but also as a warning to the wider community about the perils of unverified investment schemes. As we progressed, it became a race against time to retrieve the remaining $59,000 before the scammers could vanish completely. Each step forward was met with new challenges, as these criminals constantly shifted tactics and moved their digital assets to evade capture. Nonetheless, the determination and skill of the recovery team kept us hopeful. Throughout this ordeal, I learned the hard value of caution and due diligence in investment, especially within the volatile world of cryptocurrency. The experience has been incredibly taxing, both emotionally and financially, but the support and results provided by Lee Ultimate Hacker have been indispensable. The recovery process is ongoing, and while the final outcome remains uncertain, the progress made so far gives me hope. The battle to recover the full amount of my investment continues, and with the expertise of Lee Ultimate Hacker, I remain optimistic about the eventual recovery of my funds. Their commitment to their clients and proficiency in handling such complex cases truly sets them apart in the field of cyber recovery. LEEULTIMATEHACKER@ AOL. COM   Support @ leeultimatehacker . com.  telegram:LEEULTIMATE   wh@tsapp +1  (715) 314  -  9248     
  • Topics

×
×
  • Create New...

Important Information

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