Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • How to get a squared area around an entity with a for loop
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
TheRPGAdventurer

How to get a squared area around an entity with a for loop

By TheRPGAdventurer, January 30, 2019 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

TheRPGAdventurer    4

TheRPGAdventurer

TheRPGAdventurer    4

  • Dragon Slayer
  • TheRPGAdventurer
  • Members
  • 4
  • 642 posts
Posted January 30, 2019 (edited)
	public BlockPos onGroundAir() {
		BlockPos pos = this.getPosition(); 
	//	for(int width = 1; width <= this.width / 2; width++) {
		    for(int y = 1; y <= 3.4; y++) { 
		      pos = new BlockPos(posX - width + width, posY - y * MathX.clamp(this.getScale(), 0.1, 1), posZ - width + width);
		    }
	//	}
		return pos;
	}
	
	public boolean onSolidGround() {
		IBlockState state = world.getBlockState(onGroundAir());
		return !state.getMaterial().isSolid();
		
	}

I had these two methods, basically they detect if the ground 3.4 blocks below the dragon is Air, if not then the dragon is setFlying to false, basically it detects if the dragon is onGround or not, and I cant use the normal vailla onGround method because they require the dragon to be directly sticking to the block and if at  least one block above is blocking it looks silly flying on the ground. I want to do it again but in a bigger area "square" with more blocks around, because the method above is only one line tall and is the middle of the dragon. Ill use it if the area above dragon is bug enough so it is able to fly around. They get stuck flying below the trees. this question is pretty basic.

Edited January 30, 2019 by TheRPGAdventurer
  • Quote

Share this post


Link to post
Share on other sites

lehjr    6

lehjr

lehjr    6

  • Stone Miner
  • lehjr
  • Members
  • 6
  • 57 posts
Posted January 30, 2019 (edited)

Is that your actual code or is that pseudo code?

 

Edit: 

The current code you have doesn't even need a loop. The way it's written,  it is only checking a single block position using the value at the end of the loop, not a 1 block high box the width of the entity. "y" will never be 3.4 because it is declared as an integer. 

 To do what it looks like your code was intended to work, and to check an entire box around the entity, you'd need roughly the following: 

// check a 1 block high plane below
public boolean onSolidGround()  {
    double y = this.posY - 3.4; // adjust this to the height to check

    for (double z = posZ - width/2; y < posZ + width/2; y++) {
        for(double x = posX - width/2; x < posX + width/2; x++) {
            if (!isSolid(new BlockPos(x, y, z)))
                return false;
        }
    }
    return true;
}

// check an entire box surrounding
public boolean onSolidGround2()  {
    double offsetStart = 0.0; // set this as how far below to start
    double offsetEnd = 0.0; // set this as how far above to stop
    
    for (double y = posY - offsetStart; y < y + height + offsetEnd; y++ ) {
        for (double z = posZ - width / 2; y < posZ + width / 2; y++) {
            for (double x = posX - width / 2; x < posX + width / 2; x++) {
                if (!isSolid(new BlockPos(x, y, z)))
                    return false;
            }
        }
    }
    return true;
}

public boolean isSolid(BlockPos pos) {
    IBlockState state = world.getBlockState(pos);
    return !state.getMaterial().isSolid();
}

 

Edited January 30, 2019 by lehjr
  • Quote

Share this post


Link to post
Share on other sites

TheRPGAdventurer    4

TheRPGAdventurer

TheRPGAdventurer    4

  • Dragon Slayer
  • TheRPGAdventurer
  • Members
  • 4
  • 642 posts
Posted February 1, 2019

ok so this method just checks if the dragon's block in 3.4 solid, it wont stand but fly on top of a 1 block thick area. so how do I check all blocks 3.4 below on the dragon is solid?

  • Quote

Share this post


Link to post
Share on other sites

lehjr    6

lehjr

lehjr    6

  • Stone Miner
  • lehjr
  • Members
  • 6
  • 57 posts
Posted February 1, 2019

In my response, onSolidGround() will actually check if the entire area under the entity as you described what you thought your original methods were doing in your original post.

 

onSolidGround2() (naming? ) will check a box. Set offsetStart  to how far under the entity you want to start. Set offsetEnd to how far above you want to start. 

 

isSolid is just a helper for both. 

 

 

 

 

 

 

 

 

 

 

 

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      Do stuff right after login

      By diesieben07 · Posted 24 minutes ago

      PlayerLoggedInEvent
    • dotifo7120
      Do stuff right after login

      By dotifo7120 · Posted 40 minutes ago

      Is there an event called when a player enters into the world (fully logged)?
    • diesieben07
      Windows installer

      By diesieben07 · Posted 1 hour ago

      Just use the installer, it is cross-platform.
    • T_Jam1
      Windows installer

      By T_Jam1 · Posted 1 hour ago

      I need help!! I can't find the forge windows installer and every time I look something up it never helps, I'm mad, I'm confused, and I need help.
    • diesieben07
      Meine Inkompetenz kennt mal wieder keine Grenzen.(Forge 1.16.1)

      By diesieben07 · Posted 2 hours ago

      Threads merged. Please keep this forum in English.   Make sure your anti virus does not block the jar file. Also ensure you only download it from https://files.minecraftforge.net/.
  • Topics

    • dotifo7120
      1
      Do stuff right after login

      By dotifo7120
      Started 40 minutes ago

    • T_Jam1
      1
      Windows installer

      By T_Jam1
      Started 1 hour ago

    • Mounty
      3
      Meine Inkompetenz kennt mal wieder keine Grenzen.(Forge 1.16.1)

      By Mounty
      Started 3 hours ago

    • Amperka_pro
      5
      Как применить нбт к предмету.

      By Amperka_pro
      Started 4 hours ago

    • MistyMarshes
      9
      [1.16.4] Get IChunk World / get current World for the overworld.

      By MistyMarshes
      Started 15 hours ago

  • Who's Online (See full list)

    • diesieben07
    • CaelTheQuail
    • Lucie
    • Tomzer95
    • Endangered
    • vemerion
    • dotifo7120
    • tf2_mandeokyi
    • Kreepydude
    • Ugdhar
    • Maewolf04
    • MedHelm
    • Cyrus_Blaze
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • How to get a squared area around an entity with a for loop
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community