stewbeet.plugins.datapack.sorters
The datapack.sorters plugin generates efficient sorting functions for Minecraft datapacks.
It creates optimized mcfunction files at compile time that can sort lists stored in NBT storage
using configurable algorithms (Quick Sort and Selection Sort), with support for custom comparison
keys, scaling factors, and performance optimizations tailored for Minecraft's execution environment.
Source Code: stewbeet/plugins/datapack/sorters/__init__.py
Source Code: stewbeet/plugins/datapack/sorters/constants.py
Source Code: stewbeet/plugins/datapack/sorters/extend_datapack.py
Source Code: stewbeet/plugins/datapack/sorters/match.py
Source Code: stewbeet/plugins/datapack/sorters/quick_sort.py
Source Code: stewbeet/plugins/datapack/sorters/selection_sort.py
What it does
- Generate high-performance sorting functions for NBT storage lists
- Support multiple sorting algorithms optimized for Minecraft's execution model
- Handle complex data structures with configurable comparison keys
- Provide in-place sorting with minimal memory overhead
- Enable precision control through scaling for decimal values
- Support partial sorting with element limits for performance optimization
Credits
Author: Darukshock - Original implementation of the quick sort algorithm
Dependencies
- β Required: Beet context with datapack namespace support
- π Position: Can run at any point during compilation
- π§ Extension: Requires
stewbeet.plugins.datapack.sorters.extend_datapackto register sorter namespace
Quick Example
Sorters are declared in the sorters registry under data/<namespace>, like so:
data/<namespace>/sorter/<sorter_name>.json
Alternatively, they can be generated with python:
The list can now be sorted:
Configuration
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
algorithm | string | Required | Sorting algorithm to use: "selection_sort" (recommended) or "quick_sort" |
functions_location | string | Required | Namespaced function ID where sorting functions are generated |
to_sort | object | Required | Storage location containing storage and target keys |
to_sort.storage | string | Required | Storage namespace (e.g., "switch:stats") |
to_sort.target | string | Required | NBT path to the list (e.g., "all.modes.sheepwars.played") |
key | string | Required | Key within each list element to compare for sorting |
scale | float | 1.0 | Optional: Scaling factor for numeric values (negative for descending order) |
limit | integer | null | Optional: Maximum elements to sort (selection_sort only) |
β‘ Performance Note: Selection sort significantly outperforms quick sort in Minecraft's storage environment due to quick sort's heavy reliance on macro expansions. Each recursive call in quick sort generates multiple macro invocations for parameter passing and storage manipulation, creating substantial overhead that negates the theoretical O(n log n) advantage. Selection sort's straightforward O(nΒ²) approach with minimal macro usage proves more efficient for typical Minecraft datapack sorting operations.
Basic Example Configuration
require:
- ...
- stewbeet.plugins.datapack.sorters.extend_datapack # Required: register "data/<namespace>/sorter/" as a custom resource
- ...
pipeline:
- ...
- stewbeet.plugins.datapack.sorters # Place this plugin in the pipeline
- ... # Other plugins followExample Sorter Configuration
{
"algorithm": "selection_sort",
"functions_location": "switch:stats/minigame/sort_leaderboard",
"to_sort": {
"storage": "switch:stats",
"target": "all.modes.sheepwars.played"
},
"key": "count",
"scale": 100,
"limit": 10
}Spyglass implementation
Combined with the Spyglass extension for Visual Studio Code, auto-completion and syntax checking can be obtained on sorter files.
Here's how:
- If you haven't already, open your project on Visual Studio Code and install the Spyglass extension.
- Create a Spyglass Config File, and under the
envfield, add the following :
"customResources": {
"sorter": {
"category": "sorter"
}
}- Anywhere in your workspace, create
sorter.mcdoc: stewbeet/plugins/datapack/sorters/mod.mcdoc π
Note: it will work no matter the name as long as it ends in.mcdoc - Restart Visual Studio Code
Features
Algorithm Selection System
- π₯ Selection Sort - Recommended for better Minecraft performance
- β‘ Quick Sort - Alternative using recursive function calls
- π Automatic Routing - Algorithm selection based on configuration
Extended Datapack Support
- ποΈ Custom Resource Type - Sorter objects at
data/<namespace>/sorter/path.json - π Spyglass Support - Syntax checking and auto-completion
- β Multi-Datapack Support - Independent configurations
Advanced Scaling System
- π Precision Control - Scale factor for decimal precision (e.g.,
1000for 3 decimals) - π Reverse Sorting - Negative scale values for descending order
Selection Sort Implementation
- π Minimum Finding - Efficient linear search
- π Partial Sorting - Optional limit parameter for top-N sorting
- πΎ Memory Efficient - In-place sorting with minimal storage overhead
Quick Sort Implementation
- π Pivot Selection - Last element partitioning
- π Recursive Calls - Function-based recursion with macros
- β οΈ Performance Note - Less efficient than selection sort due to macro overhead
Configuration & Generation
- β Input Validation - Type checking and required field validation
- οΏ½ Function Generation - Dynamic mcfunction file creation with macro integration
- οΏ½ Memory Management - Temporary storage with automatic cleanup
Next steps
- Defining items and blocks: where the items this plugin reads are declared.
- All plugins: the rest of the pipeline, in the order it runs.
- Configuring the build: enabling, ordering and configuring plugins.