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

1.14.4 Creating falling blocks in tick event causes extreme lag


DFined
 Share

Recommended Posts

Hi!

I have been working on a mod, that creates an ash-rain event, where ash blocks start falling from the sky. Currently I use the player tick event to spawn the blocks around the player. The only problem is, this seems to cause extreme lag, even when the block creation rate is such that only 10-20 blocks are falling at any time. I have tested much larger amounts of manually placed blocks falling at once and not causing lag. I have also tried creating sand blocks instead of the custom ash block to no difference, so the problem is not in the block itself either. Here is the code. If anyone knows what the problem might be, I would be very grateful. I assume its probably some mechanic I dont know about, and will go dig in it some more, but I'm stumped.

@Mod.EventBusSubscriber(bus= Mod.EventBusSubscriber.Bus.FORGE)
    public static class WorldEvents{
        private static final int ASHFALLRADIUS = 7*16;
        private static final float ASHFALLINTERVAL = 1;
        private static float interval = 0.f;
        @SubscribeEvent
        public static void onTick(TickEvent.PlayerTickEvent tickEvent) {
            PlayerEntity player = tickEvent.player;
            World world = tickEvent.player.world;

            if(world.isRemote()) {
                Random rand = world.getRandom();
                interval += rand.nextFloat();
                if(interval > 5.f) {
                    BlockPos dropPos = new BlockPos(
                            player.posX + rand.nextInt(ASHFALLRADIUS * 2) - ASHFALLRADIUS,
                            150,
                            player.posZ + rand.nextInt(ASHFALLRADIUS * 2) - ASHFALLRADIUS);
                    if (world.getChunkProvider().isChunkLoaded(new ChunkPos(dropPos))) {
                        BlockState state = ashBlock.getDefaultState().with(ashBlock.LAYERS, rand.nextInt(8) + 1);
                        world.setBlockState(dropPos, state, 6);
                    }
                    interval = 0;
                }
            }
        }
    }

 

Link to comment
Share on other sites

26 minutes ago, diesieben07 said:

You must modify the world on the server, not the client.

And that interval field cannot just be a static field somewhere, you need to actually bind this to the player, the world, etc.

Oh, my bad, I do actually have if(!world.isRemote()) in the code, I was just checking that I am not misunderstanding isRemote() when writing this post and forgot to change it back. So the lag isnt due to it being clientside (if it actually was done on the client the blocks wouldn't fall at all). As for the static field I know Ill have to change it. For now it works and this is just a quick and dirty way to check if the concept as a whole has any problems. Such as this weird lag.

Link to comment
Share on other sites

Your code is on average creating a new block in the world every tick (PlayerTickEvent fires twice per tick, you need to check the phase). This will cause lag, no matter what you do, because you are causing lighting and chunk updates every single tick. Placing the blocks at once does not cause lag, because it causes one big update, so you will have one slow tick. But with your method all ticks are slow.

  • Like 1
Link to comment
Share on other sites

27 minutes ago, diesieben07 said:

Your code is on average creating a new block in the world every tick (PlayerTickEvent fires twice per tick, you need to check the phase). This will cause lag, no matter what you do, because you are causing lighting and chunk updates every single tick. Placing the blocks at once does not cause lag, because it causes one big update, so you will have one slow tick. But with your method all ticks are slow.

So its the block updates... Thanks! And I have found a solution as well. If anyone else is looking for how to do this: instead of creating the blocks, its better to just spawn the FallingBlockEntity. That way the lag vanishes completely.

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.