Jump to content

[1.10.2] Speeding up tile entities


CrashHermit

Recommended Posts

Hello there, I'm trying to speed up the tile entity tick rates in my world by getting a list of all the loaded tile entities and using the World#scheduleBlockUpdate on them several times. The code below shows my attempt to make a block tick 10 times faster. My thinking was if I schedule a block update 10 times each tick then it would be 10 times faster, but in testing it there is no speed increase at all. Am I not understanding how world.scheduleBlockUpdate works or is it something else I am doing wrong?

 

@SubscribeEvent
    public void calendarTime(WorldTickEvent event)
    {
        if(event.phase == TickEvent.Phase.START && event.world.provider.getDimension() == 0)
        {

            List<TileEntity> allTEs = event.world.loadedTileEntityList;
              
            for(int i = 0; i < allTEs.size(); i++)
            {
                Block block = allTEs.get(i).getBlockType();
                BlockPos pos = allTEs.get(i).getPos();
                World world = allTEs.get(i).getWorld();

                for(int j = 0; j < 10; j++)
                {
                    world.scheduleBlockUpdate(pos, block, j, 1);
                }
            }
        }
    }
Link to comment
Share on other sites

Ah I see, thanks for that. I was able to get tile entities to tick by checking to see if they were an instance of ITickable, recasting them as ITickable and calling the update method on them. This does what I was intending. Here is the change in code if anyone needs it.

 

@SubscribeEvent
    public void calendarTime(WorldTickEvent event)
    {
        if(event.phase == TickEvent.Phase.START && event.world.provider.getDimension() == 0)
        {

            List<TileEntity> allTEs = event.world.loadedTileEntityList;
              
            for(int i = 0; i < allTEs.size(); i++)
            {
                Block block = allTEs.get(i).getBlockType();
                BlockPos pos = allTEs.get(i).getPos();
                World world = allTEs.get(i).getWorld();
                TileEntity tile = allTEs.get(i);

                for(int j = 0; j < 10; j++)
                {
                    if(tile != null && !tile.isInvalid())
                    {
                        if(tile instanceof ITickable)
                        {
                            ((ITickable) tile).update();
                        }

                    }
                }
            }
        }
    }

 

I have another question though, now I know there is a way to increase the tick rate of tile entities, is there a way to slow them down?

Edited by CrashHermit
Missing line of code
Link to comment
Share on other sites

2 hours ago, CrashHermit said:

I know there is a way to increase the tick rate of tile entities, is there a way to slow them down?

Try to be efficient about how each one decides that it has nothing to do (yet) and then returns. If that runs smoothly, then you don't need to make more coding for yourself (and risk introducing bugs).

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

@diesieben07 Thanks, that's one step down and thank you for the suggestion @jeffryfisher, optimization of the code will eventually happen. This works almost perfectly for what I am trying to do. I think the plan here is going to be get the list of current TE's by using event#world#tickableTicleEntites, store them in a hashmap and then remove all the tile entities from the event#world#tickableTicleEntites list. The problem I'm running into now is trying to figure out a way to clear the hashmap of TE's that have been removed from the world. Here is what I have so far.

 

private static HashMap<Integer, TileEntity> storageTEs = new HashMap<Integer, TileEntity>();

@SubscribeEvent
    public void calendarTime(WorldTickEvent event)
    {
        if(event.phase == TickEvent.Phase.START && event.world.provider.getDimension() == 0)
        {

            List<TileEntity> allTEs = event.world.tickableTileEntities;
  
            for (int i = 0; i < allTEs.size(); i++)
            {
                TileEntity tile = allTEs.get(i);
                storageTEs.put(i, tile);
                allTEs.remove(i);
            }
            

            for(int i = 0; i < storageTEs.size(); i++)
            {
                TileEntity tile = storageTEs.get(i);

                if(rand.nextDouble() < 0.9)
                {
                    System.out.println("DENY");
                }
                else
                {
                    ((ITickable) tile).update();
                    System.out.println("ALLOW");
                }
            }
        }
    }

 

Link to comment
Share on other sites

As has already been mentioned, Minecraft already lists TEs. Try to avoid reinventing such wheels at the core of the game.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.