Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] how to get loaded chunks?
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
kiou.23

[1.16] how to get loaded chunks?

By kiou.23, December 4, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 4, 2020 (edited)

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 December 5, 2020 by kiou.23
  • Quote

Share this post


Link to post
Share on other sites

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 5, 2020

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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7690

diesieben07

diesieben07    7690

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7690
  • 56306 posts
Posted December 5, 2020

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.

  • Quote

Share this post


Link to post
Share on other sites

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 5, 2020
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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7690

diesieben07

diesieben07    7690

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7690
  • 56306 posts
Posted December 5, 2020
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.

  • Quote

Share this post


Link to post
Share on other sites

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 5, 2020 (edited)
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 December 5, 2020 by kiou.23
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7690

diesieben07

diesieben07    7690

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7690
  • 56306 posts
Posted December 5, 2020
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
  • Quote

Share this post


Link to post
Share on other sites

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 7, 2020
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

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7690

diesieben07

diesieben07    7690

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7690
  • 56306 posts
Posted December 7, 2020
  • 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.

  • Quote

Share this post


Link to post
Share on other sites

kiou.23    10

kiou.23

kiou.23    10

  • Creeper Killer
  • kiou.23
  • Members
  • 10
  • 190 posts
Posted December 7, 2020
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

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7690

diesieben07

diesieben07    7690

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7690
  • 56306 posts
Posted December 7, 2020
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
  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      The game crashed whilst rendering overlay

      By diesieben07 · Posted 1 hour ago

      In the future please make your own thread instead of posting in an old, unrelated thread.   Delete this file.
    • FactionsFire
      Exit code 0 Sorry for inconvenience

      By FactionsFire · Posted 1 hour ago

      I am trying to lanuch 1.8.9 forge and i get "An unexpected issue happened and the game has crashed exit sorry for inconvenience exit code 0"" And this has happened for about a week. 
    • Merken
      The game crashed whilst rendering overlay

      By Merken · Posted 1 hour ago

      The game crashed whilst rendering overlay Error: net.minecraftforge.fml.config.ConfigFileTypeHandler$ConfigLoadingException: Failed loading config file forge-client.toml of type CLIENT for modid forge Exit Code: -1
    • Merken
      The game crashed whilst rendering overlay

      By Merken · Posted 1 hour ago

      The game crashed whilst rendering overlay Error: net.minecraftforge.fml.config.ConfigFileTypeHandler$ConfigLoadingException: Failed loading config file forge-client.toml of type CLIENT for modid forge Exit Code: -1
    • diesieben07
      My game keeps crashing

      By diesieben07 · Posted 2 hours ago

      You do not. In the future please make your own thread instead of posting in some random thread that looks vaguely similar.   Your Optifine version is outdated and not listed as compatible with your version of Forge. Refer to the Optifine downloads page regarding compatibility with Forge.
  • Topics

    • Merken
      2
      The game crashed whilst rendering overlay

      By Merken
      Started 1 hour ago

    • FactionsFire
      0
      Exit code 0 Sorry for inconvenience

      By FactionsFire
      Started 1 hour ago

    • seekR4621
      1
      My game keeps crashing

      By seekR4621
      Started 3 hours ago

    • NorthWestWind
      2
      [1.16.x] Stop Held Item blobbing in Hand

      By NorthWestWind
      Started 3 hours ago

    • ehbean
      1
      1.16.x Custom Furnace/Brewing Stand

      By ehbean
      Started 12 hours ago

  • Who's Online (See full list)

    • diesieben07
    • Nanook
    • NorthWestWind
    • Ginge
    • Differentiation
    • Luis_ST
    • Ghost0nLinéaire
    • charlie121kkt
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] how to get loaded chunks?
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community