Jump to content

How to iterate through my actual chunk?


HenryFoster

Recommended Posts

I would like to find all sign-blocks and their position in the chunk at my player position. My problem is that I dont know how to iterate through the blocks in the chunk.

 

EntityPlayer player = Minecraft.getMinecraft().player;
World world = player.getEntityWorld();
Chunk chunk  = world.getChunkFromBlockCoords(player.getPosition());

Map<BlockPos, TileEntity> myMap = chunk.getTileEntityMap();
for(double x = 0; x <= 16; x++) {
		 System.out.println(chunk.getBlockState(new BlockPos(x+player.posX, player.posY , player.posZ)));  
}  

What I dont understand is how do I get the blocks of exactly the chunk where myPlayerPosition is. I can use loops with offsets but I dont understand how the coordinations work. because for "chunk.getBlockState() I need to set a blockposition but how do I get the position of the first/last block in that actual chunk?

 

I'm looking for something like :

for(double x = 0; x <= 16; x++) {
		 System.out.println(chunk.getBlockState(new BlockPos(x+offset,blablabla)));  
}  

I know its only one dimensional but I want to get it right first before I go on for the other axis. I need to understand how I can turn this relative chunk coordinations into real worldcoordination.

 

I already found this but the topic is more about blockproperties then the iterating.

 

Edited by HenryFoster
Link to comment
Share on other sites

Edit:

 

I got it:

 

@SubscribeEvent
	public void onKeyInput(InputEvent.KeyInputEvent event) 
	{
	  if(KeyBindings.myKey.isPressed())
	  {
		   System.out.println("KEY K");
	   
		   EntityPlayer player = Minecraft.getMinecraft().player;
		   World world = player.getEntityWorld();
		   Chunk chunk  = world.getChunkFromBlockCoords(player.getPosition());
	
		   int a = chunk.x*16;
		   int b = chunk.z*16;
		   System.out.println("Chunk starts at: "+ a*16 + " " + b*16);
		   for(double x = 0; x <= 15; x++) 
		   {  
			   for(double y = player.posY-2; y <= player.posY+10; y++)
			   {
				   for(double z = 0; z <= 15; z++)
				   {
					   //System.out.println(chunk.getBlockState(new BlockPos(a+x,player.posY , b+z)));  
					   BlockPos actualPosition = new BlockPos(a+x,y, b+z);
					   IBlockState state = chunk.getBlockState(actualPosition);
					   if(state.getBlock() instanceof BlockSign) 
					   {
						   for(ITextComponent i: ((TileEntitySign)world.getTileEntity(actualPosition)).signText) 
						   {
							   System.out.println(i.getUnformattedComponentText());
						   }
					   }
				   }
			   }
			   
				   
		   }
		   
	  	}
	}

I use the Chunk coordiantes a and b (yes I know bad names I will change them) and multiply them with 16 to get the start coordinates of the chunk where my player is. Later I define the position of the block "I'm looking at" with the starting position of the chunk + x/z to get the global-coordinates of that block.

Link to comment
Share on other sites

20 hours ago, HenryFoster said:

multiply them

Don’t multiply, use bit shifting. The results are different (integer multiplication rounds down)

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Multiplying works out the same. Its dividing that causes problems.

(You should still bitshift)

  • 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

Theres also a performance gain with bitshifting

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.