Jump to content

[1.12.2] Acquiring a list (or other structure) of Blocks around the player


DeadPix

Recommended Posts

Hello again!

I need to get an information about the blocks that are around the player (or a certain location) - probably for few chunks around the player. I understand that I can loop through coordinates around the player and create a BlockPos structure for each block around the player and then query them (pretty much check them in condition) on what they are, but it seems to me, that this functionality is something that should be already implemented in the game/forge itself. I looked through the documentation and there is nothing except for BlockStates, I also tried looking into the code, but am pretty much clueless at the moment.

 

What is the best way to do the following.

  • Get information about 'any' block? Is it to create a BlockPos object for it and then somehow convert it to Block and find info from there? And how would I convert BlockPos to Block correctly, please?
  • Get information about chunk and its composure (e.g. what blocks are in there at specific positions, or to specified 3D-depth from some position?

 

Are there dedicated structures that can be loaded easily, or do I have to manually specify every block  I am interested in inspecting? 

 

Thank you!

Link to comment
Share on other sites

4 minutes ago, DeadPix said:

but it seems to me, that this functionality is something that should be already implemented in the game/forge itself

Yeah, its called BlockPos.getAllInBox(...)

5 minutes ago, DeadPix said:

Is it to create a BlockPos object for it and then somehow convert it to Block and find info from there?

...Yes, by using World#getBlockState(BlockPos)

Unless you know what block you want info about and aren't interested in "what block is over there?" In which case you can do something like Blocks.STONE.getHardness(...)

6 minutes ago, DeadPix said:

Get information about chunk and its composure (e.g. what blocks are in there at specific positions, or to specified 3D-depth from some position?

BlockPos.getAllInBox(...)

6 minutes ago, DeadPix said:

or do I have to manually specify every block  I am interested in

Yes

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

One thing to keep in mind is that iterating through a large area will likely cause performance issues. You have to realize that the number of blocks grows rapidly. For example, in one chunk there is 65k block positions to scan! If you scan for two chunks in each direction you'll be at over 1 million block positions.

 

Due to this performance hit, there are not really any vanilla mechanisms that scan blocks for a distance. There are a lot of "neighbor" checks (like redstone connections) but they only check the next block. Thinks like bookshelves have a dedicated scan when the enchanting table is opened and it only checks a short distance for specific placement.

 

What exactly are you trying to achieve?

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

5 minutes ago, jabelar said:

For example, in one chunk there is 65k block positions to scan!

Good news, looking at a block and doing nothing takes almost no time. I've benchmarked this. Scanning an entire chunk (in my case, 16384 blocks as my code stopped when it reached the surface starting at bedrock) takes about 400,000 nanoseconds (0.4 miliseconds).

 

That still limits how many chunks you can scan (if you're doing something every tick, you can probably look at 10 chunks and still be safe). If you're doing it less often (e.g. once when the chunk is generated) you can look at a lot more or schedule it (e.g. only scan one chunk every tick, but change what chunk is being scanned).

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

Alright guys, thank you all for wonderful replies.

 

As @jabelar asked, what I am trying to achieve - I am trying to write an 'AI' that will be walking through the lands and completing some goals (the AI takes over the player). At the very moment I am trying to come up with some reasonable path finding algo that I could further extend. I pretty much want to set target destination to which from player.posX/posY/posZ the AI will travel, and so I need to probably start with creating a structure that will represent visible surface. Is it correct?

 

And would something like this be a good approach?

  • Start with figuring out on what block I am currently standing. Check block bellow and the block above on whether it is an air block to determine that I am on 'some sort' of a surface.
  • Continue with all blocks around (all eight of them) the same way as above, storing only those that have 2 (or 3 - possible to jump) air blocks above them.
  • Extend the 'circle' and so on and so forth, up until some distance. 
Output of this function would then be sort of a map of my surroundings on which I could then start calculating the best path. Also when I would need some specific item or tool or ore to mine, I would take it into account in the algorithm I mentioned and store these values there too so I could feed my path finding with that. 
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

I hope you benchmarked this correctly. The JVM is stupidly smart and will figure out that your loop does nothing and just not do the loop at all.

I did.

I specifically benchmarked an code I was doing things with. That is, I specifically wanted to know how long it was taking my code to do its processing.

That is, I have code that says "is this an block ore? no? continue. yes? do this other thing with it."

In chunks that contained no ore, it took about 400,000 nanos to process. In chunks with ore, it would take as much as 800,000 nanos (usually closer to 600,000; it depended on how many blocks of ore there were and it was usually "less" than "more" due to the distributions I was using).

 

If you're interested, here's the code. The timing was done in the event handler class around the call to this method.

(The same method in 1.12; might be cleaner)

Edited by Draco18s

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

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

    • I have done this now but have got the error:   'food(net.minecraft.world.food.FoodProperties)' in 'net.minecraft.world.item.Item.Properties' cannot be applied to                '(net.minecraftforge.registries.RegistryObject<net.minecraft.world.item.Item>)' public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register( "lemon_juice", () -> new Item( new HoneyBottleItem.Properties().stacksTo(1).food( (new FoodProperties.Builder()) .nutrition(3) .saturationMod(0.25F) .effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.01f ) .build() ) )); The code above is from the ModFoods class, the one below from the ModItems class. public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice", () -> new Item(new Item.Properties().food(ModFoods.LEMON_JUICE)));   I shall keep going between them to try and figure out the cause. I am sorry if this is too much for you to help with, though I thank you greatly for your patience and all the effort you have put in to help me.
    • I have been following these exact tutorials for quite a while, I must agree that they are amazing and easy to follow. I have registered the item in the ModFoods class, I tried to do it in ModItems (Where all the items should be registered) but got errors, I think I may need to revert this and figure it out from there. Once again, thank you for your help! 👍 Just looking back, I have noticed in your code you added ITEMS.register, which I am guessing means that they are being registered in ModFoods, I shall go through the process of trial and error to figure this out.
    • ♈+2349027025197ஜ Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join a brotherhood for protection and wealth here’s is your opportunity, but you should know there’s no ritual without repercussions but with the right guidance and support from this great temple your destiny is certain to be changed for the better and equally protected depending if you’re destined for greatness Call now for enquiry +2349027025197☎+2349027025197₩™ I want to join ILLUMINATI occult without human sacrificeGREATORLDRADO BROTHERHOOD OCCULT , Is The Club of the Riches and Famous; is the world oldest and largest fraternity made up of 3 Millions Members. We are one Family under one father who is the Supreme Being. In Greatorldrado BROTHERHOOD we believe that we were born in paradise and no member should struggle in this world. Hence all our new members are given Money Rewards once they join in order to upgrade their lifestyle.; interested viewers should contact us; on. +2349027025197 ۝ஐℰ+2349027025197 ₩Greatorldrado BROTHERHOOD OCCULT IS A SACRED FRATERNITY WITH A GRAND LODGE TEMPLE SITUATED IN G.R.A PHASE 1 PORT HARCOURT NIGERIA, OUR NUMBER ONE OBLIGATION IS TO MAKE EVERY INITIATE MEMBER HERE RICH AND FAMOUS IN OTHER RISE THE POWERS OF GUARDIANS OF AGE+. +2349027025197   SEARCHING ON HOW TO JOIN THE Greatorldrado BROTHERHOOD MONEY RITUAL OCCULT IS NOT THE PROBLEM BUT MAKE SURE YOU'VE THOUGHT ABOUT IT VERY WELL BEFORE REACHING US HERE BECAUSE NOT EVERYONE HAS THE HEART TO DO WHAT IT TAKES TO BECOME ONE OF US HERE, BUT IF YOU THINK YOU'RE SERIOUS MINDED AND READY TO RUN THE SPIRITUAL RACE OF LIFE IN OTHER TO ACQUIRE ALL YOU NEED HERE ON EARTH CONTACT SPIRITUAL GRANDMASTER NOW FOR INQUIRY +2349027025197   +2349027025197 Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join
    • Hi, I'm trying to use datagen to create json files in my own mod. This is my ModRecipeProvider class. public class ModRecipeProvider extends RecipeProvider implements IConditionBuilder { public ModRecipeProvider(PackOutput pOutput) { super(pOutput); } @Override protected void buildRecipes(Consumer<FinishedRecipe> pWriter) { ShapedRecipeBuilder.shaped(RecipeCategory.MISC, ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()) .pattern("SSS") .pattern("SSS") .pattern("SSS") .define('S', ModItems.COMPRESSED_DIAMOND.get()) .unlockedBy(getHasName(ModItems.COMPRESSED_DIAMOND.get()), has(ModItems.COMPRESSED_DIAMOND.get())) .save(pWriter); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, ModItems.COMPRESSED_DIAMOND.get(),9) .requires(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()) .unlockedBy(getHasName(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()), has(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get())) .save(pWriter); ShapedRecipeBuilder.shaped(RecipeCategory.MISC, ModItems.COMPRESSED_DIAMOND.get()) .pattern("SSS") .pattern("SSS") .pattern("SSS") .define('S', Blocks.DIAMOND_BLOCK) .unlockedBy(getHasName(ModItems.COMPRESSED_DIAMOND.get()), has(ModItems.COMPRESSED_DIAMOND.get())) .save(pWriter); } } When I try to run the runData client, it shows an error:  Caused by: java.lang.IllegalStateException: Duplicate recipe compressed:compressed_diamond I know that it's caused by the fact that there are two recipes for the ModItems.COMPRESSED_DIAMOND. But I need both of these recipes, because I need a way to craft ModItems.COMPRESSED_DIAMOND_BLOCK and restore 9 diamond blocks from ModItems.COMPRESSED_DIAMOND. Is there a way to solve this?
  • Topics

×
×
  • Create New...

Important Information

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