Jump to content

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


TheRPGAdventurer

Recommended Posts

	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 by TheRPGAdventurer
Link to comment
Share on other sites

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 by lehjr
Link to comment
Share on other sites

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. 

 

 

 

 

 

 

 

 

 

 

 

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



×
×
  • Create New...

Important Information

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