Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16] how to get loaded chunks?


kiou.23
 Share

Recommended Posts

So I have a class attached to the chunks through capabilities, and I want to update the values in world tick. I have an event subscriber for the TickEvent.WorldTickEvent event, but I don't know how to get the loaded chunks. I would also need access to multiple chunks at once, as the value for a chunks next state would depend on it's neighbours

 

Edit: I solved it myself, took me a few hours...

If anyone croses this topic and is also looking how to do it (I stripped the code to only contain the relevant snippets):

 

@Mod(TutorialMod.MOD_ID)
public class TutorialMod
{
    public static final String MOD_ID = "tutorial";

    public static final Logger LOGGER = LogManager.getLogger();
    public static final Random RANDOM = new Random();

    public static final Method getLoadedChunksMethod;

    public TutorialMod() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        modEventBus.addListener(this::setup);

        MinecraftForge.EVENT_BUS.register(this);
    }


    public void setup(final FMLCommonSetupEvent event) {
        if (getLoadedChunksMethod == null) {
            LOGGER.error("getLoadedChunksMethod: Chunk Reflection didn't work!");
            return;
        }
        getLoadedChunksMethod.setAccessible(true);
    }

    @SubscribeEvent
    public void onTick(final TickEvent.WorldTickEvent event) {
        World world = event.world;

        ServerChunkProvider serverChunkProvider = (ServerChunkProvider) world.getChunkProvider();

        try {
            Iterable<ChunkHolder> chunks = (Iterable<ChunkHolder>)getLoadedChunksMethod.invoke(serverChunkProvider.chunkManager);

            chunks.forEach(chunkHolder -> {
                Chunk chunk = chunkHolder.getChunkIfComplete();
                if (chunk == null) return;
                ChunkValue chunkValue = chunk.getCapability(ModCapabilities.CHUNK_VALUE_CAP, null).orElse(null);
                if (chunkValue == null) {
                    LOGGER.error("chunkValue is null");
                    return;
                }
                chunkValue.incrementValue(RANDOM.nextInt(200) == 0 ? 1 : 0);
            });

        } catch (Exception e) {
            LOGGER.error("Error on TickEvent Handler: " + e.getMessage());
            return;
        }
    }

    private static Method getGetLoadedChunksMethod() {
        try {
            return ChunkManager.class.getDeclaredMethod("getLoadedChunksIterable");
        } catch (Exception e) {
            LOGGER.error("Exception in getGetLoadedChunksMethod: " + e.getMessage());
            return null;
        }
    }

    static {
        getLoadedChunksMethod = getGetLoadedChunksMethod();
    }
}

 

Edited by kiou.23
Link to comment
Share on other sites

Okay, I thought I had solved it

but the the way I did it is very innefective

I tried getting the loadedPositions from ChunkManager, and pass it into a method that returns the Chunk from the pos as a long:

public void onTick(final TickEvent.WorldTickEvent event) {
        ChunkManager chunkManager = ((ServerChunkProvider) event.world.getChunkProvider()).chunkManager;

        LongSet loadedPositions;

        try {
            loadedPositions = (LongSet)loadedPositionsField.get(chunkManager);
        } catch (Exception e) {
            LOGGER.error("Error on TickEvent Handler (loadedPositions): " + e.getMessage());
            return;
        }
  
        for (Long pos : loadedPositions) {

            ChunkHolder chunkHolder;
            try {
                chunkHolder = (ChunkHolder)getLoadedChunkMethod.invoke(chunkManager, pos);
            } catch (Exception e) {
                LOGGER.error("Error on TickEvent Handler (getLoadedChunk): " + e.getMessage());
                continue;
            }
            Chunk chunk = chunkHolder.getChunkIfComplete();
            if (chunk == null) continue;

            ChunkValue chunkValue = chunk.getCapability(ModCapabilities.CHUNK_VALUE_CAP, null).orElse(null);
            if (chunkValue == null) {
                LOGGER.error("chunkValue is null");
                continue;
            }

            chunkValue.incrementValue(RANDOM.nextInt(200) == 0 ? 1 : 0);
        }
    }

But it erroed when trying to get the loadedPositions

 

can anyone help with this?

or additionally, is there a better way to do this? that doesn't require getting the loaded chunks at all?

Link to comment
Share on other sites

Instead of getting all chunks and checking for each if you want to increment their value, instead pick a percentage of the loaded chunks and increment just their values. That way you have to loop through a lot less to achieve the same effect.

 

To safely get all loaded chunks you need to use ChunkManager#getLoadedChunksIterable. It also requires reflection, but accessing the fields directly is not threadsafe, as they are also used from the chunk loading thread.

Link to comment
Share on other sites

11 hours ago, diesieben07 said:

To safely get all loaded chunks you need to use ChunkManager#getLoadedChunksIterable. It also requires reflection, but accessing the fields directly is not threadsafe, as they are also used from the chunk loading thread.

Yeah, that's what I was using at first, but if I looped through all valeus of the Iterable it would have about 2000 to 3000 ChunkHolders, and more than half of them would be null (I think the ChunkHolder wasn't null, but returned null when I tried to get the chunk using getChunkIfComplete).

Then after looking at the Chunk Manager class I found the loadedPositions field, which seemed to only contain the position of the ChunkHolders that were completed.

But if I should use the getLoadedChunksIterable, then what should I do to not waste loop iterations on ChunkHolders that would return null? or is ChunkHolder#getChunkIfComplete not the correct method to use?

 

11 hours ago, diesieben07 said:

Instead of getting all chunks and checking for each if you want to increment their value, instead pick a percentage of the loaded chunks and increment just their values. That way you have to loop through a lot less to achieve the same effect.

Wouldn't I have to iterate trough alll chunks in the same way tho?

Link to comment
Share on other sites

18 minutes ago, kiou.23 said:

Yeah, that's what I was using at first, but if I looped through all valeus of the Iterable it would have about 2000 to 3000 ChunkHolders, and more than half of them would be null (I think the ChunkHolder wasn't null, but returned null when I tried to get the chunk using getChunkIfComplete).

Vanilla uses ChunkHolder#getTickingFuture().getNow(ChunkHolder.UNLOADED_CHUNK).left() and then checks Optional#isPresent on the result before ticking chunks. Using getChunkIfComplete and using a null check comes down to the same thing.

 

20 minutes ago, kiou.23 said:

Wouldn't I have to iterate trough alll chunks in the same way tho?

Not really, no. You would only pick a few values out of a list and work on those, instead of handling all of them.

Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

Vanilla uses ChunkHolder#getTickingFuture().getNow(ChunkHolder.UNLOADED_CHUNK).left() and then checks Optional#isPresent on the result before ticking chunks. Using getChunkIfComplete and using a null check comes down to the same thing.

Hold on, can I subscribe to a Chunk Tick Event? That would really simplify things, but There is no ChunkTickEvent in the TickEvent

actually, is the WorldTickEvent even what I should be using?

(Also, I heard that the tick event is called twice, one for each phase, so I should check the phase before running right? for what phase should I check, does it make a difference?)

 

8 minutes ago, diesieben07 said:

Not really, no. You would only pick a few values out of a list and work on those, instead of handling all of them.

This isn't realted to Forge, but I don't see how I would do this? would if I iterate, say, through only 20% of the chunks wouldn't the other 80% never be updated? or is the Iterable in a random order every time?

 

Edit: There is a ChunkEvent, does that help me?

Edited by kiou.23
Link to comment
Share on other sites

1 hour ago, kiou.23 said:

Hold on, can I subscribe to a Chunk Tick Event? That would really simplify things, but There is no ChunkTickEvent in the TickEvent

actually, is the WorldTickEvent even what I should be using?

There is no chunk tick event that I am aware of, no.

 

1 hour ago, kiou.23 said:

(Also, I heard that the tick event is called twice, one for each phase, so I should check the phase before running right? for what phase should I check, does it make a difference?)

One runs before the vanilla tick (START) and one after (END). For most things it does not make a difference which one you pick.

 

1 hour ago, kiou.23 said:

This isn't realted to Forge, but I don't see how I would do this? would if I iterate, say, through only 20% of the chunks wouldn't the other 80% never be updated? or is the Iterable in a random order every time?

There are algorithms which allow you to pick N random elements from a list, without traversing the whole list. See here for example: https://stackoverflow.com/a/35278327/3638966.

 

1 hour ago, kiou.23 said:

Edit: There is a ChunkEvent, does that help me?

It doesn't. it is a base class for several chunk related events, subscribing to ChunkEvent itself does not produce any meaningful results.

  • Like 1
Link to comment
Share on other sites

On 12/5/2020 at 8:19 PM, diesieben07 said:

There are algorithms which allow you to pick N random elements from a list, without traversing the whole list. See here for example: https://stackoverflow.com/a/35278327/3638966.

Alright, something like this?

	@SubscribeEvent
    public void onWorldTick(final TickEvent.WorldTickEvent event) {
        if (event.side.isClient()) return;
        if (event.phase == TickEvent.Phase.END) return;

        ServerChunkProvider serverChunkProvider = (ServerChunkProvider) event.world.getChunkProvider();

        Iterable<ChunkHolder> iterable;

        try {
            iterable = (Iterable<ChunkHolder>)getLoadedChunksIterable.invoke(serverChunkProvider.chunkManager);
        } catch(Exception e) {
            return;
        }

        List<ChunkHolder> chunkHolders =
                Arrays.stream(Iterables.toArray(iterable, ChunkHolder.class)).collect(Collectors.toList());

        int length = Iterables.size(chunkHolders);
        int n = 500;
        if (length < n) n = length;

        for (int i = length - 1; i >= length - n; i-- ) {
            Collections.swap(chunkHolders, i, RANDOM.nextInt(i + 1));
        }

        for(ChunkHolder chunkHolder : chunkHolders.subList(length - n, length);) {
            Chunk chunk = chunkHolder.getChunkIfComplete();
            if (chunk == null) continue;

            ChunkValue chunkValue = chunk.getCapability(ModCapabilities.CHUNK_VALUE_CAP, null).orElse(null);
            if (chunkValue == null) continue;

            chunkValue.incrementValue(RANDOM.nextInt(40) == 0 ? 1 : 0);
        }
    }

 

Having to iterate over the incomplete Chunk Holders just feels inefficient. But it does work

 

Link to comment
Share on other sites

  • That is not how you handle exceptions, ever.
  • Because this is a tick event and happens often, you might want to use MethodHandles or an access transformer instead of pure reflection, for performance reasons.
  • That is a very inefficient way way to turn an iterable into a List. You should use Lists.newArrayList instead.
  • Why are you using Iterables.size?
  • 3 hours ago, kiou.23 said:

    Having to iterate over the incomplete Chunk Holders just feels inefficient.

    It's what vanilla does, too. There is no list of just the complete holders.

Link to comment
Share on other sites

1 hour ago, diesieben07 said:
  • That is not how you handle exceptions, ever.

I'm really bad at handling with exceptions, mostly because I have a limited experience with Java. What should I be doing instead?

 

1 hour ago, diesieben07 said:
  • Because this is a tick event and happens often, you might want to use MethodHandles or an access transformer instead of pure reflection, for performance reasons.

Makes sense, I don't know what those thing are but I'll look into that

1 hour ago, diesieben07 said:
  • That is a very inefficient way way to turn an iterable into a List. You should use Lists.newArrayList instead.

I was looking for a way to convert a Iterable into a List, and this was the first I found, I'll update it then

1 hour ago, diesieben07 said:
  • Why are you using Iterables.size?

My bad, I can use the length of the list, just didn't see it amidst the hundreds of times I rewrote this method

Link to comment
Share on other sites

49 minutes ago, kiou.23 said:

I'm really bad at handling with exceptions, mostly because I have a limited experience with Java. What should I be doing instead?

Think about when this exception occurs. In this case this can happen if the field does not exist or you don't get access to it. Either of these cases is an error condition (because the field should be there and it should be accessible to you because you are using ObfuscationReflectionHelper). So, don't just "carry on" - throw an exception yourself, with an error message that explains what's wrong and the original exception as the cause. This will cause the game to crash, show the problem.

  • Thanks 1
Link to comment
Share on other sites

  • 10 months later...
15 minutes ago, Gr3atsaga said:

Is there a different way to get the loaded chunks in 1.16.5?  When I run the below line, it does not find the method.
 

ChunkManager.class.getDeclaredMethod("getLoadedChunksIterable");

 

What are you trying to achieve? Why do you need the loaded chunks?

Link to comment
Share on other sites

I hardly think the reason I want them is relevant, but if you really must know I want to know if I can force unload all chunks when all players go offline on a server.  This would included chunks being chunk loaded by a chunk loader.  Though, I'm not even sure that it would work since I am not familiar with how chunk loaders work.  If they stop the unload event from happening, then I guess it's just not possible to do.

Edited by Gr3atsaga
Link to comment
Share on other sites

Yeah that is not going to work. You'll have to look into how the Ticket system works, which is what prevents chunks from unloading.

As long as a chunk has a ticket attached to it (be it from a player or some other means) it won't unload and there isn't really a way to bypass that easily, even if you were to "get all loaded chunks".

This is why I ask.

Link to comment
Share on other sites

58 minutes ago, diesieben07 said:

Yeah that is not going to work. You'll have to look into how the Ticket system works, which is what prevents chunks from unloading.

As long as a chunk has a ticket attached to it (be it from a player or some other means) it won't unload and there isn't really a way to bypass that easily, even if you were to "get all loaded chunks".

This is why I ask.

That's fair.  I still would like to know how to get the loaded chunks, just in case I ever need to.

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.