Skip to main content
Anti-lag Sheep

Anti-lag Sheep #

Updated 2023-02-28

Back in the day, I built some really big wool farms in Minecraft. Wool blocks were the only way to build colorful structures. As other options like terracotta (originally stained clay) and concrete have become available, I’m less inclined to build large buildings or statues out of wool, but I still occasionally have reason for large quantities of wool.

Lag is less of a problem now that I’m primarily playing single-player, but I remember feeling guilty that the SMP server I used to play on would lag noticeably whenever I was near my wool farm. For that reason, I built the farm far from anything else so lag would only happen while I was actually using the farm. I’m still weary of any farm design that requires large numbers of entities and it occured to me that I can fix that with a simple data pack.

The effect of this data pack is simple. If there are one or two sheep of the same color on a farm, they will slowly level up, giving more wool until they give about a stack with each shearing. If there are three or more sheep of the same color, the effect won’t happen. So the maximum number of sheep that can efficiently be on the same farm is 32, two of each color.

Download #

If you want to use the data pack in your own world, you can find specifics on how to install and use it along with downloads on the github page. Remember that data packs are only compatible with the Java Edition of Minecraft.

Behind the Scenes #

There’s not much to say about this one. My first plan was to modify the loot table of sheep using predicates to have them drop increasing amounts of wool over time. All I’d need was some code to adjust a scoreboard value each time a sheep is sheared and it would be good.

Unfortunately, it turned out that the wool drops for differently colored sheep was hard-coded, so I couldn’t override it with a data pack. I don’t know if that was ever changed, but it was true at the time I was making this data pack.

The data pack tracks every sheep and detects when its sheared state changes from false to true. It then spawns a placeholder wool item tagged as “temp” so it can find the wool again later.

tag @e remove temp
summon minecraft:item ~ ~0.8 ~ {Tags:["temp"], PickupDelay:14, Item:{Count:1, id:"minecraft:white_wool"}}

The data pack then changes the wool’s color with a set of 15 conditional execute as commands for the other colors of sheep.

execute as @s[nbt={Color:1b}] run execute as @e[tag=temp] run data modify entity @s Item.id set value "minecraft:orange_wool"

It would have been more efficient to spawn the wool at this step, but keeping a single copy of the summon command was probably easier to update or debug.

The wool’s quantity needs to be set separately with a series of conditionals.

execute as @s[scores={sheepLevel=10..14}] run execute as @e[tag=temp] run data modify entity @s Item.Count set value 4b

If I was making this data pack again, I’d probably choose to write all this data into storage and only copy it to the item at the end. That would save at least one @e selector. The @e selector has to search through every loaded entity, so I generally try to avoid it as much as possible. But this was one of the earlier data packs I made and I don’t think I’d ever used data storage yet.

The last thing to do is update the levels for all sheep of the same color. The quickest way to do this, unfortunately, is with another block of conditional executes. First run scoreboard players add @s sheepLevel 3 to increase the sheared sheep’s level by 3, then use execute as @s[nbt={Color:1b}] run scoreboard players remove @e[scores={sheepLevel=1..}, distance=..500, nbt={Color:1b}] sheepLevel 1 for each possible sheep color to decrement every sheep’s level by 1. That includes the sheep running the command, so the net effect is that its level goes up by 2 and all other sheep of the same level go down by 1.

If there are three sheep of the same color, that will result in them all staying at the same level on average. Any more and their levels will reduce over time. Any less and their levels will increase over time.

And that’s it. There isn’t even anything to show the sheep’s levels. You just know it’s working when you start getting more wool with each shearing.