Minecraft Blogs / Tutorial

⚙️ Datapacking In-Depth: Help, I'm lagging! | Lag | DID 4

  • 1,314 views, 6 today
  • 8
  • 2
  • 2
Kefaku's Avatar Kefaku
Level 42 : Master Nerd
73
Lag


Contents

0. Prerequisites
1. Introduction
2. Types of Lag
  2.1. Client Lag (FPS)
  2.2. Server Lag (TPS)
  2.3. Latency
3. Improving FPS
  3.1. Forge
  3.2. Fabric
4. Optimizing Datapacks
  4.1. Avoid the Unnecessary
  4.2. Predicates/Advancements
  4.3. Function Trees
5. Conclusion and Further Reading
⚙️ Datapacking In-Depth: Help, I'm lagging! | Lag | DID 4
⚙️ Datapacking In-Depth: Help, I'm lagging! | Lag | DID 4
⚙️ Datapacking In-Depth: Help, I'm lagging! | Lag | DID 4



0. Prerequisites

Knowledge of the following topics is required for this explanation. If you don't have that knowledge, I suggest you visit the linked resource and learn about that topic first.

Topic
Suggested Resource
Creating a basic function datapack
Function Datapacks for Dummies by Bertiecrafter, at least quickstart
Scoreboards
Function Datapacks for Dummie by Bertiecrafter, section 7.1



1. Introduction

While thinking about new topics to cover in DID, lag came to my mind pretty soon, simply because it is a topic we all have experiences with. Everyone has had some lag in one of their worlds or while playing on a server before (if not, what kind of beast is your computer 🤔), therefore it only seemed logical to address this topic in an article - and here we are!
In this episode of DID you're going to learn about different types of lag and what you can do to prevent them. Of course, not all types of lag are related to datapacks, but I still decided to include them, since this might also be helpful or at least interesting for some of you.
Let the optimizing begin!


2. Types of Lag

2.1. Client Lag (FPS)

The first type of lag I want to explain: client lag. You've all experienced this type of lag before: You're casually playing minecraft, chitchatting with your friends, and suddenly, your game turns into a slideshow. Your frames per second (fps) drop and you can tell from looking at your screen: this game isn't running smooth. This type of lag occurs if you're device (or to be more specific, your graphics card) can't keep up with whatever is happening. Usually, stuff happens in your minecraft world and your graphics card generates images (frames) displaying your world. If those frames are generated less frequently, for example because your graphics card is busy doing other things, your fps drop and the game doesn't look smooth anymore. Of course, smooth is a pretty subjective term, but most of the time anything above 20 to 30 fps is considered smooth.
If you want to know how many fps you currently have, you can see that in the debug screen (press alt+F3; graph at the bottom left):



That was, how the graph looks like with lag and below is an example for a graph without lag:



Important side note: You can't have unlimited fps, they are limited to your monitors refresh rate. For example, if your monitor has a refresh rate of 60Hz, minecraft could be running at a million fps, you wouldn't even notice it, because your monitor can only display 60 fps.

2.2. Server Lag (TPS)

Server lag is very similar to client lag, except it's not your graphics card beeing unable to keep up, but the server. Now you might say: Great, I'm playing single player, I don't have a server, I'm immune to server lag! WRONG! Even if you're playing single player, minecraft uses an internal server for calculations. The only difference to multiplayer is, the server is running locally on your device.
Now that we've cleared that up, let's move on to tps. The server uses a loop to calculate all important stuff in your world, like for example where entities are supposed to move to next. Doing all those calculations 1 time means 1 tick. The optimum and simultaneously the maximum is 20 ticks per second (tps). If minecraft runs at 20 tps, everything is as it should be. The second important thing for understanding sever lag are mspt (milliseconds per tick). This tells you, how many milliseconds the server needs to calculate everything that happens in one tick. Since there are 1000 milliseconds in a second and the goal is to calculate 20 ticks per second, mspt need to be below 1000/20 = 50, in order to keep up with the speed minecraft wants to run at. When too much happens and the server needs more than 50 milliseconds to calculate a single tick, obviously minecraft can't run at 20 tps anymore. Now, there are two options for what could happen:
  1. If the lag only lasts for a short amount of time (e.g. someone ignites a lot of tnt, it causes lag, but the situation is soon over as all tnts have exploded), the server can decide to briefly run faster when it's over make up for the slower speed it had. You can witness this kind of behaviour, when you had some lag in your world and shortly after it stops, all entities in your world suddenly move way faster for a moment.
  2. If the lag lasts longer, the server can skip some ticks entirely. It simply forgets about the ticks it didn't have time to calculate. If this happens, you can see it in the server log (multiplayer) or output log (singleplayer), which will look like this:

    This message tells you, how many ticks have been skipped (40 in this example).
Tps and mspt are also displayed on the debug screen (press alt+F3; graph at the bottom right):



2.3. Latency

Imagine this situation: You're playing on a minecraft server with your friends, when something astonishing happens - you decide do mine a block of dirt. ⛏️ You hold your left mouse button pressed, the dirt cracks and finally, it vanishes, just to reappear a moment later. After another moment of you doing nothing, the block of dirt breaks again, but this time for good and while dropping the item.

The situation you just witnessed is an example of the last type of lag I wanted to cover here. It is caused by high latency, network latency to be more precise. In simple terms: your internet connection is to slow. You break the block and your client recognizes that. It sends this information to the server. Meanwhile the server sends your client a message telling it, how the world looks. Since your internet connection is to slow (your ping is to high) the server hasn't received the information about the broken block when doing so and therefore it sends a version of the world to your client, in which the block is still there. The client then renders the world it got sent - with the block you had broken. And when finally, the servers gets notified about the broken block, the block truly breaks (again).

Since this type of lag is linked to the speed of your internet connection, you can't really do anything to easily fix it. Sad 😔
It also depends on the location of the server, for example playing on a british or american server can be very different.


3. Improving FPS

There are many mods that try to optimize the way minecraft functions, in order to improve your FPS (and sometimes even TPS), I just picked my favourites for the most popular modloaders forge and fabric. Which one should I choose, you might ask? Well, it doesn't really matter. If you already use forge, choose the one compatible with your forge, if you use fabric, use the one compatible with fabric. If you haven't used any mods so far, just pick one at random.

3.1. Forge

This is my suggestion for forge.


OptiFine


OptiFine is probably the most popular mod for optimizing FPS in minecraft. It comes with various graphical and performance improvements and also supports shaders.


Get started with Sodium:
🔗 r/OptiFine
🔗 optifine.net


3.2. Fabric

These are my suggestions for fabric. These three mods are compatible and can be used together. They all optimize different parts of the game.


Sodium


Sodium changes the rendering engine, resulting in major FPS improvements on most devices.


Get started with Sodium:
🔗 Download on Modrinth
🔗 Download on CurseForge
🔗 Sourcecode on Github




Lithium


Lithium is a mod that can be used both server-side and client-side. It can optimize the server performance, resulting in improved FPS and TPS.


Get started with Lithium:
🔗 Download on Modrinth
🔗 Download on CurseForge
🔗 Sourcecode on Github




Phosphor


Phosphor updates the lighting engine, something that is often very laggy, and therefore can also improve your FPS.


Get started with Phosphor:
🔗 Download on Modrinth
🔗 Download on CurseForge
🔗 Sourcecode on Github




4. Optimizing Datapacks

4.1. Avoid the Unnecessary

The first tip for optimizing datapacks is as simple as it gets: only use the functions you really need. Think about everything you do twice.
Do you really need to check for a player having used that item every? Checking every 2 ticks would already mean half as many calculations minecraft needs to handle and players probably wont even notice the difference between a 0.05 second delay and a delay of 0.1 second. (see DID 2, if you don't know how to make slower loops)
Do you really need those fancy particles and animations if there isn't even a player nearby?
Overall, little things like these can make a big difference, so be sure to always pay attention to detail when developing datapacks.


4.2. Predicates/Advancements

Predicates and advancements can be used to check certain conditions. In most cases they are more efficient to use, than a simple execute (if|unless) <condition>.
Predicates are simple json files which can list 1 or more conditions and you can check for those conditions using execute (if|unless) predicate. Advancements can check the conditions for being awarded to a player. This is useful if you want a particular check to always happen, and run some commands in any situation where a player fulfils the conditions. To run commands using an advancement, simply add a function command as reward function. Also, make sure to revoke the advancement, so the check works multiple times (because the checking for a specific player stops, when the player already has that advancement; just use advancement revoke <player> only <advancement>)
Since predicates and advancements have a lot of functionality and I can't explain it all in this article, here are some resources you can use instead:

Predicates
Advancements



4.3. Function Trees

Function trees are another way to reduce the number of commands minecraft needs to run. The most common type, the one I'm going to explain here, is a binary tree.
Imagine a situation like this: You have a scoreboard named value and want to run a command based on the value of #x on that scoreboard. For example, a custom boss mob you coded has different attacks, you generate a random number (as you have learned in DID 3) to choose which one the boss uses and the want to run the commands for that attack based on the scoreboard value. Of course you could do something like:

execute if score #x value matches 1 run <some atack>
execute if score #x value matches 2 run <some other atack>
execute if score #x value matches 3 run <another atack>
execute if score #x value matches 4 run <powerful atack>
...

The problem with this is that if you have 16 possible options, Minecraft will always have to run 16 commands to check for each possible value to make sure you got the one you wanted. Using a function tree you can reduce that number and of course, less checks mean better performance. Awesome, right? But I haven't even told you the coolest thing yet: This method gets more efficient, the more possible values you have. For example, if there are 8 possible values, you need 8 checks using the method above, but only 6 checks using a binary function tree. That doesn't seem much better, does it? It only starts getting impressive with bigger examples: For 32 possible values you still need 32 checks using the method above, but you'd only need 10 checks using a binary function tree.
Now, how does this magic work? It's actually quite simple. You check in multiple levels, but you only do 2 checks per level. For the example, we'll try to check for the value of #x on the scoreboard value, with #x beeing a value between 1 and 8. In the first level of our function tree, well check wether the value is in the upper or lower half of all possible values:

# File Location: ./data/did/functions/lag/function_tree.mcfunction
# as/at server

execute if score #x value matches 1..4 run function did:lag/function_tree/1_4
execute if score #x value matches 5..8 run function did:lag/function_tree/5_8

This simple check for upper or lower half will now be continued for all levels.
Let's pretend the value we are looking for is 3. That means value is in the lower half (1..4) and the function did:lag/function_tree/1_4 will be run in the above example code. Now we know for sure that the value is between 1 and 4, so we check for upper or lower half again:

# File Location: ./data/did/functions/lag/function_tree/1_4.mcfunction
# as/at server

execute if score #x value matches 1..2 run function did:lag/function_tree/1_4/1_2
execute if score #x value matches 3..4 run function did:lag/function_tree/1_4/3_4


Now, the value is in the upper half (3..4), therfore the function did:lag/function_tree/1_4/3_4 will run in the example. In the next level, upper and lower half are only one value each (3 or 4), therefore the next level of the tree will be the last level and identify the value of 3. You can then run wathever command you want, I'll just output the value to chat using /say.

# File Location: ./data/did/functions/lag/function_tree/1_4/3_4.mcfunction
# as/at server

execute if score #x value matches 3 run say value = 3
execute if score #x value matches 4 run say value = 4

Now, you'd only need to add functions to check all other possible value following the same scheme.
I don't think it's necessary to list all of them in this article, if you want to take a look at them, simply download the example datapack, which is linked in the conclusion of this article. In that datapack, I included the full function tree example.
One last side note, which might be obvious: The more possible options you have, the more levels you'll need, for example you'd need 4 levels for 16 possible values, 5 levels for 32 possible values and so on.


5. Conclusion and Further Reading

Congratulations! You know (hopefully) know a lot more about randomizing in minecraft and will use your knowledge to create awesome datapacks in the future.You know how to get random numbers, functions, entities and locations. Your worlds are going to be so much more random after this, enjoy :P
If you have any further questions, feel free to ask in the commands.

assignment Challenge

There was a lot of general information and tips in this episode, but not much specific information about datapacks. That's why I couldn't find a specific challenge this time. So I'm going to give you an impossible challenge:

Always keep your datapacks clean and optimised, with as little lag as possible.

I know that no one will ever be able to do this, because sometimes we just throw together a quick idea and it will never be the most efficient thing in the world. Overall, that's good. If you feel like doing something, do it without thinking too much about it, but at least keep optimising in the back of your mind, while doing so.


If you want to check out the function tree I created for this tutorial, change some things around and test it for yourself, you can download the tutorial datapack here:

🔗 DID Tutorial Datapack: Lag

If you want to read more about optimizing, I recommend these resources:

🔗Optimising (r/MinecraftCommads)
🔗How to Optimize Your Datapack (Cloud Wolf)


👍👎
How did you like it?
Please leave your feedback in the comments.

If you want to learn even more about datapacks, you can find other episodes of DID and more in the Datapack Knowledge Book.
Tags

1 Update Logs

Update #1 : by Kefaku 04/12/2023 4:19:46 pmApr 12th, 2023

Create an account or sign in to comment.

theblackswitch
01/13/2024 2:30 am
Level 24 : Expert Nerd
theblackswitch's Avatar
Thanks This Helped me a LOT!
2
Kefaku
01/13/2024 3:51 pm
Level 42 : Master Nerd
Kefaku's Avatar
glad I could help :D
2
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome