Jump to content

Checking if chunk is loaded?


jredfox

Recommended Posts

I tried 

System.out.println("First Check:" + e.world.isChunkGeneratedAt(chunkX,chunkZ));

but, it said true when I was checking a chunk 100 blocks away. It returned true since the chunk generated that far away was already stored on the disk how should I test for actual loaded chunks?

Edited by jredfox
Link to comment
Share on other sites

18 minutes ago, jabelar said:

Well the Chunk class has an isLoaded() method. So what about world.getChunkFromChunkCoords(chunkX, chunkY).isLoaded()?

calling world get chunk will provide the chunk if not loaded and will always return true I believe. In 1.7.10 there was a world.chunkExists(chunkX,chunkZ) where it sounded like it checked only loaded?

Edited by jredfox
Link to comment
Share on other sites

7 minutes ago, jabelar said:

There is a chunkExists() method in the ChunkProvider class. So world.getChunkProvider().chunkExists(chunkX, chunkY) looks promising..

it says chunkExists I believe it returns whether or not it's been generated not if it's currently loaded into memory helpful but, wrong boolean

Edited by jredfox
Link to comment
Share on other sites

If you look at the ChunkProviderServer code I think the chunkExists() is actually just the loaded chunks. There is another method called isChunkGeneratedAt(). Also, if you look at the implementation of the chunkExists() it looks at the id2ChunkMap which it also uses for methods like getLoadedChunkCount(). So it seems to be loaded chunks.

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

Link to comment
Share on other sites

7 minutes ago, jabelar said:

If you look at the ChunkProviderServer code I think the chunkExists() is actually just the loaded chunks. There is another method called isChunkGeneratedAt(). Also, if you look at the implementation of the chunkExists() it looks at the id2ChunkMap which it also uses for methods like getLoadedChunkCount(). So it seems to be loaded chunks.

maybe I could do both? Either way it seems to be protected weird
 

return world.isChunkLoaded() && world.chunkProivder.chunkExists();

 

ChunkProvider.chunkExists()doesn't seem to be a method for the interface :( I could check for chunk provider loaded  chunk method

I think I should always use the world's implementation instead of chunk providers since that should always work but, the method is protected I don't want to use reflection why is it protected?

Edited by jredfox
Link to comment
Share on other sites

4 minutes ago, jabelar said:

The world.isChunkLoaded() just calls the chunkExists() method. You don't need both.

WorldServer is protected why? I feel this should always be public especially with checking stuff like chunks are loaded
 

    protected boolean isChunkLoaded(int x, int z, boolean allowEmpty)
    {
        return this.getChunkProvider().chunkExists(x, z);
    }

 

Edited by jredfox
Link to comment
Share on other sites

not sure if this is proper but, I got it working would prefer no reflection this is something that should always be visable:
 

   /**
    * uses reflection since the world method is protected
    */
   public static boolean isChunkLoaded(World w,int x,int z,boolean allowEmpty)
   {
	   try
	   {
		   Method m = FieldAcess.chunkLoaded;
		   m.setAccessible(true);
		   return (Boolean) m.invoke(w, x,z,allowEmpty);
	   }
	   catch(Throwable t)
	   {
		   t.printStackTrace();
	   }
	   return false;
   }

 

Link to comment
Share on other sites

37 minutes ago, diesieben07 said:
  • Don't call setAccessible every time.
  • You almost never want to catch Throwable.
  • That is not how you handle an exception.

 

Because in vanilla Minecraft it is not needed outside the world class. So it's protected. You need to stop expecting Minecraft to have a nice API.

If I don't the world throws an Illegal Security exception I tried just setting it accessible only once on startup it sometimes fails if the world becomes a new instance. And it is needed outside the world class for modded terrain generation if they are optimized  that is

Edited by jredfox
Link to comment
Share on other sites

4 hours ago, diesieben07 said:

That is not true and not how reflection works.

 

Minecraft is not written with mods in mind.

well I got the crash report as stated above when the object of the world was created as a new instance rarely but, it did happen as well as it occurring when I was trying to access player methods because I didn't set it accessible every time just once in pre init.

Link to comment
Share on other sites

I'm still really confused why you're using the reflection and stuff. Are you sure that the chunk provider chunkExists() doesn't work? That is public method and is exactly what the protected method WorldServer.isChunkLoaded() calls. That method is used by all the vanilla code such as isAreaLoaded(), isBlockLoaded() and so forth.

 

You should only need the public method world.getChunkProvider().chunkExists().

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

Link to comment
Share on other sites

On 5/24/2018 at 10:36 AM, jabelar said:

I'm still really confused why you're using the reflection and stuff. Are you sure that the chunk provider chunkExists() doesn't work? That is public method and is exactly what the protected method WorldServer.isChunkLoaded() calls. That method is used by all the vanilla code such as isAreaLoaded(), isBlockLoaded() and so forth.

 

You should only need the public method world.getChunkProvider().chunkExists().

it won't always work with modded worlds and that's the issue.

Link to comment
Share on other sites

3 hours ago, jredfox said:

it won't always work with modded worlds and that's the issue.

Why? Why would a mod change that part of the ChunkProvider? Most modded dimensions change the ChunkGenerator but don't fool around with ChunkProvider. But if they did the custom ChunkProvider would need to implement the IChunkProvider interface. That interface requires an implementation of the getLoadedChunk() method which only returns loaded chunk (null if not loaded).

 

The getLoadedChunk() for the vanilla ChunkProviderServer accesses the same map I already mentioned, so the chunkExists() is legit at least when ChunkProviderServer is used.

 

In any case, what mods change this functionality? They all need a sense of what is loaded even if they change how things load, and the interface requires an implementation of the getLoadedChunk() method. 

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

Link to comment
Share on other sites

So I looked through the source of a number of mods that create major dimension stuff. Like Twilight Forest, Advanced Rocketry, AbysmalCraft etc. and none of them create any custom chunk providers. Advanced Rocketry looks like it does but they actually misnamed their classes -- the ones they call "ChunkProvider" actually implement IChunkGenerator not IChunkProvider.

 

The main mod I can find quickly that does create custom IChunkProvider implementations is GalactiCraft. They always return true for the chunkExists(). Now, you might think that is a problem but I'm thinking that it might actually be correct -- you'd have to figure out what they're doing but they probably had a good reason for this.  Remember that the chunkExists() is checked by things like the World classes when they are doing all sorts of things like looking for entities and such. So if they're reporting true for this then I guess it means that for those specific custom providers that the chunk exists ("loaded") at all times. I don't know much about GalactiCraft's loading but they do explain that there is generally loading and such going on: https://wiki.micdoodle8.com/wiki/Sealed_space#Chunk_loading

 

Any, generally the point of an interface is that it is supposed to be honored. So I doubt if any quality mod would report that chunks are loaded when they are not.

Edited by jabelar

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

Link to comment
Share on other sites

On 5/26/2018 at 5:54 PM, jabelar said:

Why? Why would a mod change that part of the ChunkProvider? Most modded dimensions change the ChunkGenerator but don't fool around with ChunkProvider. But if they did the custom ChunkProvider would need to implement the IChunkProvider interface. That interface requires an implementation of the getLoadedChunk() method which only returns loaded chunk (null if not loaded).

 

The getLoadedChunk() for the vanilla ChunkProviderServer accesses the same map I already mentioned, so the chunkExists() is legit at least when ChunkProviderServer is used.

 

In any case, what mods change this functionality? They all need a sense of what is loaded even if they change how things load, and the interface requires an implementation of the getLoadedChunk() method. 

you think of it wrongly the world loaded chunks is what I am worried about not necessarily the chunk provider might have an if else statement for modded worlds or just plain not using it always for chunk provider.


If you looked at another method checking for chunk generated at checks both the world and chunk provider

Anyways this thread is solved

Edited by jredfox
Link to comment
Share on other sites

21 minutes ago, jredfox said:

you think of it wrongly the world loaded chunks is what I am worried about not necessarily the chunk provider might have an if else statement for modded worlds or just plain not using it always for chunk provider.


If you looked at another method checking for chunk generated at checks both the world and chunk provider

 

While there is no harm in doing a useless check, it makes no sense really. There are no examples of vanilla or popular mods that do that. And why would a mod return a wrong value for an important interface? And lastly, if any mod did change the whole chunk loading system so much that you can't trust the loading interface then your world check cannot be trusted either!

 

To really make the point: WorldServer class (which no one ever modifies) has method isChunkLoaded() which has the following source:

    protected boolean isChunkLoaded(int x, int z, boolean allowEmpty)
    {
        return this.getChunkProvider().chunkExists(x, z);
    }

 

This isChunkLoaded() method is the ONLY thing used to check chunk loading in all the following methods (which no one ever change):

  • World#isAreaLoaded()
  • World#isBlockLoaded()
  • World#updateEntities()
  • WorldServer#tickPlayers()
  • World#getEntitiesInAABB()
  • etc.

And these methods are relied on by almost everything else. Like World#getBlockState() calls isBlockLoaded() which calls isChunkLoaded().

 

If all the important functionality in the game relies on the chunk providers chunkExists() method. I really don't get why your mod would require anything else. 

 

But no harm if you want to do extra checks I guess.

 

 

 

Edited by jabelar

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

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

    • So i have a custom ore and, arround the ore, a bunch of randomly placed custom stone blocks should be placed. After applying it, i've found that it causes moderate to extreme world generation lag (new chunks refusing to load after moving for a while, height slices of the same chunk appearing and disappearing as I get into them instead of the usual long continous chunk, new chunks generating extremely close to me instead of to the set render distance...) I've been debugging for a while and I know for a fact this is causing the lag (and sometimes freeze of the world loading screen on a new world and/or the saving world screen when quitting), since comenting it just makes the worldgen work as usual and I want to see if its really that computationally expensive, if there are other ways of doing it or if the process can be simplfied or optimized. I've tried a lot of combinations for the same code but I am just stuck. Is it some kind of generation cascading im missing?   Here is the code for the class. The code inside the if (placed) is the one causing this mess. I can see that the code might not be the most optimized thing, but it does what's supposed to... but at the cost of causing all this. Any tips? package es.nullbyte.relativedimensions.worldgen.oregen.oreplacements; import es.nullbyte.relativedimensions.blocks.BlockInit; import es.nullbyte.relativedimensions.blocks.ModBlockTags; import net.minecraft.core.BlockPos; import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; import net.minecraft.world.level.levelgen.feature.OreFeature; import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration; import java.util.Optional; public class AberrantOreFeature extends OreFeature { public AberrantOreFeature() { super(OreConfiguration.CODEC); } @Override public boolean place(FeaturePlaceContext<OreConfiguration> ctx) { // Get the world and the position from the context WorldGenLevel world = ctx.level(); BlockPos origin = ctx.origin(); // Offset the origin by 8 in the x and z directions to avoid cascading chunk generation BlockPos offsetOrigin = origin.offset(8, 0, 8); // Create a new context with the offset origin FeaturePlaceContext<OreConfiguration> offsetCtx = new FeaturePlaceContext<>( Optional.empty(), world, ctx.chunkGenerator(), ctx.random(), offsetOrigin, ctx.config() ); // Generate the entire vein of ore at the offset origin boolean placed = super.place(offsetCtx); // If the vein was generated successfully if (placed) { // Define the block to replace surrounding blocks with BlockState surroundingBlockState = BlockInit.ABERRANT_MINERALOID.get().defaultBlockState(); // Generate a random size for the area of corruption int areaSizeX = ctx.random().nextInt(3) + 1; // between 1 and 4 int areaSizeY = ctx.random().nextInt(3) + 1; // between 1 and 4 int areaSizeZ = ctx.random().nextInt(3) + 1; // between 1 and 4 // Calculate the number of blocks to be corrupted based on the area size double numBlocksToCorrupt = (areaSizeX + areaSizeY + areaSizeZ / 2.0) ; // Counter for the number of blocks corrupted int numBlocksCorrupted = 0; // Loop for each block to be corrupted while (numBlocksCorrupted < numBlocksToCorrupt) { // Generate a random position within the area, using the offset origin BlockPos randomPos = offsetOrigin.offset( ctx.random().nextInt(2 * areaSizeX + 1) - areaSizeX, // between -areaSize and areaSize ctx.random().nextInt(2 * areaSizeY + 1) - areaSizeY, ctx.random().nextInt(2 * areaSizeZ + 1) - areaSizeZ ); // If the block at the random position is in the IS_ORE_ABERRANTABLE tag, replace it if (world.getBlockState(randomPos).is(ModBlockTags.STONE_ABERRANTABLE)) { world.setBlock(randomPos, surroundingBlockState, 2); numBlocksCorrupted++; } } } return placed; } }  
    • Here is a tutorial from this same forum that I tried and kinda made work. Take into account that you will have to manage the offset (like rotation, and the offset relative to things like the main hand, offhand etc) by yourself and that can get very troublesome at times.  
    • 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
  • Topics

×
×
  • Create New...

Important Information

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