Tetrachord Lib
Many Minecraft mods implement functional features in 2D and 3D spaces, but brute-force methods often prove inefficient. In this context, we present Tetrachord Lib - a lightweight library designed to optimize various data structures.
Server Optimization
Developers have created several mods to improve server performance. Lithium by jellysquid3_ represents a revolutionary optimization mod that enhances various aspects of Minecraft servers. Using precise algorithms to minimize chunk and block access, this mod improves not only world generation but also mob AI, collision detection, point of interest discovery, and other important aspects to increase computational efficiency and reduce milliseconds per tick on servers.
Starlight by Spottedstar is an innovative modification that redefines Minecraft's lighting system to improve performance during chunk generation, block placement and destruction. Unlike Phosphor, Starlight doesn't alter the vanilla lighting system - instead, it revolutionizes it by introducing a new algorithm.
Data Structures
KD Tree is a data structure used in multidimensional spaces for storing and organizing k-dimensional data points. Functioning as a binary tree, each node in the KD Tree represents a k-dimensional data point and is partitioned according to the feature values (dimensions) of the data point. KD trees are particularly well-suited for performing range queries and nearest neighbor searches in multidimensional datasets, and they can also contribute to optimizing data retrieval in the Minecraft world.

Segment Tree is a data structure used for handling range updates and queries. It divides a segment into smaller subsections and organizes them hierarchically in a tree structure. Segment trees are efficient for updating and querying data ranges. In the context of the Minecraft world represented as a 3D tensor, using a segment tree can significantly improve performance when frequent range updates and queries are involved.

Testing and Evaluation
Spark is a lightweight performance profiler mod that shows detailed time costs of mods, memory and CPU usage, milliseconds per tick, and other performance metrics for clients and servers. We used Spark and selected time costs and mspt as metrics to evaluate Tetrachord Lib's optimization capability.
We implemented the mentioned data structures and conducted experiments using the following benchmarks:
No Hostiles Around Campfire
In a specific scenario, when players light campfires, surrounding areas within a radius of several blocks will no longer spawn monsters. The original mod offers a brute-force method that involves multiple block accesses, leading to a significant increase in mspt on the server and slowing down the game program. We chose to use KD Tree to implement this requirement. We used the Spark profiler to record 1 minute of normal gameplay under different parameter settings, and the experimental results are shown in the following figure:

Increased XP Drop for Ore Blocks Near Beacon
Considering the scenario where players mine blocks close to a beacon, they can receive an additional XP reward based on the number of ore blocks surrounding the mined block. The table below illustrates the response area and "ore score" associated with each type of ore block:
| Type of Ore | Copper | Iron | Gold | Diamond | Netherite |
|---|---|---|---|---|---|
| Response Area | 5x5 | 9x9 | 13x13 | 17x17 | 19x19 |
| Ore Score | 1 | 1 | 1 | 2 | 3 |
We used the Spark profiler to record 32 ore block placements and 32 block mining operations under different parameter settings, and the experimental results are shown in the following figure:

Conclusion
Experiments show that Tetrachord Lib plays an important role in various situations and provides a highly efficient way to implement some requirements for updates and queries in 2D and 3D spaces.
The Tetrachord Lib mod currently functions as a library, so developers can use it as a dependency to improve their mod performance.