aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/1057-Replace-all-RegionFile-operations-with-SectorFile.patch
blob: 351854051fd3e534318397adc0e3158d9bebb48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Wed, 7 Feb 2024 18:21:14 -0800
Subject: [PATCH] Replace all RegionFile operations with SectorFile

Please see https://github.com/PaperMC/SectorTool
for details on the new format and how to use the tool to
convert the world or how to revert the conversion.

This patch includes conversion logic to convert RegionFiles to
SectorFile on demand. If a SectorFile does not exist, it will
attempt to copy chunk/entity/poi data from existing RegionFiles.

Included in this test patch is logic to dump SectorFile operation
tracing to file `sectorfile.tracer` in the root dir of a world. The
file is not compressed, and it is appended to only. As a result of
the lack of compression, when sending the file back for analysis
please compress it to reduce size usage.

This tracing will be useful for later tests to perform parameter
scanning on some of the parameters of SectorFile:
1. The section shift
2. The sector size
3. SectorFile cache size

diff --git a/build.gradle.kts b/build.gradle.kts
index 4998aff0b7cb084dcda15c6a18bbe45e99b6000a..4649e14d279ab63e07658addf840e6e5da25d387 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -30,6 +30,8 @@ dependencies {
     alsoShade(log4jPlugins.output)
     implementation("io.netty:netty-codec-haproxy:4.1.97.Final") // Paper - Add support for proxy protocol
     // Paper end
+    implementation("com.github.luben:zstd-jni:1.5.5-11")
+    implementation("org.lz4:lz4-java:1.8.0")
     implementation("org.apache.logging.log4j:log4j-iostreams:2.22.1") // Paper - remove exclusion
     implementation("org.ow2.asm:asm-commons:9.7")
     implementation("org.spongepowered:configurate-yaml:4.2.0-SNAPSHOT") // Paper - config files
diff --git a/src/main/java/ca/spottedleaf/io/buffer/BufferChoices.java b/src/main/java/ca/spottedleaf/io/buffer/BufferChoices.java
new file mode 100644
index 0000000000000000000000000000000000000000..01c4dd5a547bdf68a58a03ee76783425abd88b23
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/buffer/BufferChoices.java
@@ -0,0 +1,34 @@
+package ca.spottedleaf.io.buffer;
+
+import java.io.Closeable;
+
+public record BufferChoices(
+        /* 16kb sized buffers */
+        BufferTracker t16k,
+        /* 1mb sized buffers */
+        BufferTracker t1m,
+
+        ZstdTracker zstdCtxs
+) implements Closeable {
+
+    public static BufferChoices createNew(final int maxPer) {
+        return new BufferChoices(
+                new SimpleBufferManager(maxPer, 16 * 1024).tracker(),
+                new SimpleBufferManager(maxPer, 1 * 1024 * 1024).tracker(),
+                new ZstdCtxManager(maxPer).tracker()
+        );
+    }
+
+    public BufferChoices scope() {
+        return new BufferChoices(
+                this.t16k.scope(), this.t1m.scope(), this.zstdCtxs.scope()
+        );
+    }
+
+    @Override
+    public void close() {
+        this.t16k.close();
+        this.t1m.close();
+        this.zstdCtxs.close();
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/buffer/BufferTracker.java b/src/main/java/ca/spottedleaf/io/buffer/BufferTracker.java
new file mode 100644
index 0000000000000000000000000000000000000000..ce5ea4eb4217aed766438564cf9ef127696695f4
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/buffer/BufferTracker.java
@@ -0,0 +1,58 @@
+package ca.spottedleaf.io.buffer;
+
+import java.io.Closeable;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class BufferTracker implements Closeable {
+
+    private static final ByteBuffer[] EMPTY_BYTE_BUFFERS = new ByteBuffer[0];
+    private static final byte[][] EMPTY_BYTE_ARRAYS = new byte[0][];
+
+    public final SimpleBufferManager bufferManager;
+    private final List<ByteBuffer> directBuffers = new ArrayList<>();
+    private final List<byte[]> javaBuffers = new ArrayList<>();
+
+    private boolean released;
+
+    public BufferTracker(final SimpleBufferManager bufferManager) {
+        this.bufferManager = bufferManager;
+    }
+
+    public BufferTracker scope() {
+        return new BufferTracker(this.bufferManager);
+    }
+
+    public ByteBuffer acquireDirectBuffer() {
+        final ByteBuffer ret = this.bufferManager.acquireDirectBuffer();
+        this.directBuffers.add(ret);
+        return ret;
+    }
+
+    public byte[] acquireJavaBuffer() {
+        final byte[] ret = this.bufferManager.acquireJavaBuffer();
+        this.javaBuffers.add(ret);
+        return ret;
+    }
+
+    @Override
+    public void close() {
+        if (this.released) {
+            throw new IllegalStateException("Double-releasing buffers (incorrect class usage?)");
+        }
+        this.released = true;
+
+        final ByteBuffer[] directBuffers = this.directBuffers.toArray(EMPTY_BYTE_BUFFERS);
+        this.directBuffers.clear();
+        for (final ByteBuffer buffer : directBuffers) {
+            this.bufferManager.returnDirectBuffer(buffer);
+        }
+
+        final byte[][] javaBuffers = this.javaBuffers.toArray(EMPTY_BYTE_ARRAYS);
+        this.javaBuffers.clear();
+        for (final byte[] buffer : javaBuffers) {
+            this.bufferManager.returnJavaBuffer(buffer);
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/buffer/SimpleBufferManager.java b/src/main/java/ca/spottedleaf/io/buffer/SimpleBufferManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..0b5d59c355582250ec0e2ce112ab504c74d346fe
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/buffer/SimpleBufferManager.java
@@ -0,0 +1,124 @@
+package ca.spottedleaf.io.buffer;
+
+import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayDeque;
+
+public final class SimpleBufferManager {
+
+    private final int max;
+    private final int size;
+
+    private final ReferenceOpenHashSet<ByteBuffer> allocatedNativeBuffers;
+    private final ReferenceOpenHashSet<byte[]> allocatedJavaBuffers;
+
+    private final ArrayDeque<ByteBuffer> nativeBuffers;
+    // ByteBuffer.equals is not reference-based...
+    private final ReferenceOpenHashSet<ByteBuffer> storedNativeBuffers;
+    private final ArrayDeque<byte[]> javaBuffers;
+
+    public SimpleBufferManager(final int maxPer, final int size) {
+        this.max = maxPer;
+        this.size = size;
+
+        if (maxPer < 0) {
+            throw new IllegalArgumentException("'Max per' is negative");
+        }
+
+        if (size < 0) {
+            throw new IllegalArgumentException("Size is negative");
+        }
+
+        final int alloc = Math.min(10, maxPer);
+
+        this.allocatedNativeBuffers = new ReferenceOpenHashSet<>(alloc);
+        this.allocatedJavaBuffers = new ReferenceOpenHashSet<>(alloc);
+
+        this.nativeBuffers = new ArrayDeque<>(alloc);
+        this.storedNativeBuffers = new ReferenceOpenHashSet<>(alloc);
+        this.javaBuffers = new ArrayDeque<>(alloc);
+    }
+
+    public BufferTracker tracker() {
+        return new BufferTracker(this);
+    }
+
+    public ByteBuffer acquireDirectBuffer() {
+        ByteBuffer ret;
+        synchronized (this) {
+            ret = this.nativeBuffers.poll();
+            if (ret != null) {
+                this.storedNativeBuffers.remove(ret);
+            }
+        }
+        if (ret == null) {
+            ret = ByteBuffer.allocateDirect(this.size);
+            synchronized (this) {
+                this.allocatedNativeBuffers.add(ret);
+            }
+        }
+
+        ret.order(ByteOrder.BIG_ENDIAN);
+        ret.limit(ret.capacity());
+        ret.position(0);
+
+        return ret;
+    }
+
+    public synchronized void returnDirectBuffer(final ByteBuffer buffer) {
+        if (!this.allocatedNativeBuffers.contains(buffer)) {
+            throw new IllegalArgumentException("Buffer is not allocated from here");
+        }
+        if (this.storedNativeBuffers.contains(buffer)) {
+            throw new IllegalArgumentException("Buffer is already returned");
+        }
+        if (this.nativeBuffers.size() < this.max) {
+            this.nativeBuffers.addFirst(buffer);
+            this.storedNativeBuffers.add(buffer);
+        } else {
+            this.allocatedNativeBuffers.remove(buffer);
+        }
+    }
+
+    public byte[] acquireJavaBuffer() {
+        byte[] ret;
+        synchronized (this) {
+            ret = this.javaBuffers.poll();
+        }
+        if (ret == null) {
+            ret = new byte[this.size];
+            synchronized (this) {
+                this.allocatedJavaBuffers.add(ret);
+            }
+        }
+        return ret;
+    }
+
+    public synchronized void returnJavaBuffer(final byte[] buffer) {
+        if (!this.allocatedJavaBuffers.contains(buffer)) {
+            throw new IllegalArgumentException("Buffer is not allocated from here");
+        }
+        if (this.javaBuffers.contains(buffer)) {
+            throw new IllegalArgumentException("Buffer is already returned");
+        }
+        if (this.javaBuffers.size() < this.max) {
+            this.javaBuffers.addFirst(buffer);
+        } else {
+            this.allocatedJavaBuffers.remove(buffer);
+        }
+    }
+
+    public synchronized void clearReturnedBuffers() {
+        this.allocatedNativeBuffers.removeAll(this.nativeBuffers);
+        this.storedNativeBuffers.removeAll(this.nativeBuffers);
+        this.nativeBuffers.clear();
+
+        this.allocatedJavaBuffers.removeAll(this.javaBuffers);
+        this.javaBuffers.clear();
+    }
+
+    public int getSize() {
+        return this.size;
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/buffer/ZstdCtxManager.java b/src/main/java/ca/spottedleaf/io/buffer/ZstdCtxManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..4bf3b899039a0f65229e517d79ece080a17cf9f7
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/buffer/ZstdCtxManager.java
@@ -0,0 +1,114 @@
+package ca.spottedleaf.io.buffer;
+
+import com.github.luben.zstd.ZstdCompressCtx;
+import com.github.luben.zstd.ZstdDecompressCtx;
+import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
+import java.util.ArrayDeque;
+import java.util.function.Supplier;
+
+public final class ZstdCtxManager {
+
+    private final int max;
+
+    private final ReferenceOpenHashSet<ZstdCompressCtx> allocatedCompress;
+    private final ReferenceOpenHashSet<ZstdDecompressCtx> allocatedDecompress;
+
+    private final ArrayDeque<ZstdCompressCtx> compressors;
+    private final ArrayDeque<ZstdDecompressCtx> decompressors;
+
+    public ZstdCtxManager(final int maxPer) {
+        this.max = maxPer;
+
+        if (maxPer < 0) {
+            throw new IllegalArgumentException("'Max per' is negative");
+        }
+
+        final int alloc = Math.min(10, maxPer);
+
+        this.allocatedCompress = new ReferenceOpenHashSet<>(alloc);
+        this.allocatedDecompress = new ReferenceOpenHashSet<>(alloc);
+
+        this.compressors = new ArrayDeque<>(alloc);
+        this.decompressors = new ArrayDeque<>(alloc);
+    }
+
+    public ZstdTracker tracker() {
+        return new ZstdTracker(this);
+    }
+
+    public ZstdCompressCtx acquireCompress() {
+        ZstdCompressCtx ret;
+        synchronized (this) {
+            ret = this.compressors.poll();
+        }
+        if (ret == null) {
+            ret = new ZstdCompressCtx();
+            synchronized (this) {
+                this.allocatedCompress.add(ret);
+            }
+        }
+
+        ret.reset();
+
+        return ret;
+    }
+
+    public synchronized void returnCompress(final ZstdCompressCtx compressor) {
+        if (!this.allocatedCompress.contains(compressor)) {
+            throw new IllegalArgumentException("Compressor is not allocated from here");
+        }
+        if (this.compressors.contains(compressor)) {
+            throw new IllegalArgumentException("Compressor is already returned");
+        }
+        if (this.compressors.size() < this.max) {
+            this.compressors.addFirst(compressor);
+        } else {
+            this.allocatedCompress.remove(compressor);
+        }
+    }
+
+    public ZstdDecompressCtx acquireDecompress() {
+        ZstdDecompressCtx ret;
+        synchronized (this) {
+            ret = this.decompressors.poll();
+        }
+        if (ret == null) {
+            ret = new ZstdDecompressCtx();
+            synchronized (this) {
+                this.allocatedDecompress.add(ret);
+            }
+        }
+
+        ret.reset();
+
+        return ret;
+    }
+
+    public synchronized void returnDecompress(final ZstdDecompressCtx decompressor) {
+        if (!this.allocatedDecompress.contains(decompressor)) {
+            throw new IllegalArgumentException("Decompressor is not allocated from here");
+        }
+        if (this.decompressors.contains(decompressor)) {
+            throw new IllegalArgumentException("Decompressor is already returned");
+        }
+        if (this.decompressors.size() < this.max) {
+            this.decompressors.addFirst(decompressor);
+        } else {
+            this.allocatedDecompress.remove(decompressor);
+        }
+    }
+
+    public synchronized void clearReturnedBuffers() {
+        this.allocatedCompress.removeAll(this.compressors);
+        ZstdCompressCtx compress;
+        while ((compress = this.compressors.poll()) != null) {
+            compress.close();
+        }
+
+        this.allocatedDecompress.removeAll(this.decompressors);
+        ZstdDecompressCtx decompress;
+        while ((decompress = this.decompressors.poll()) != null) {
+            decompress.close();
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/buffer/ZstdTracker.java b/src/main/java/ca/spottedleaf/io/buffer/ZstdTracker.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad6d4e69fea8bb9dea42c2cc3389a1bdb86e25f7
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/buffer/ZstdTracker.java
@@ -0,0 +1,60 @@
+package ca.spottedleaf.io.buffer;
+
+import com.github.luben.zstd.ZstdCompressCtx;
+import com.github.luben.zstd.ZstdDecompressCtx;
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class ZstdTracker implements Closeable {
+
+    private static final ZstdCompressCtx[] EMPTY_COMPRESSORS = new ZstdCompressCtx[0];
+    private static final ZstdDecompressCtx[] EMPTY_DECOMPRSSORS = new ZstdDecompressCtx[0];
+
+    public final ZstdCtxManager zstdCtxManager;
+    private final List<ZstdCompressCtx> compressors = new ArrayList<>();
+    private final List<ZstdDecompressCtx> decompressors = new ArrayList<>();
+
+    private boolean released;
+
+    public ZstdTracker(final ZstdCtxManager zstdCtxManager) {
+        this.zstdCtxManager = zstdCtxManager;
+    }
+
+    public ZstdTracker scope() {
+        return new ZstdTracker(this.zstdCtxManager);
+    }
+
+    public ZstdCompressCtx acquireCompressor() {
+        final ZstdCompressCtx ret = this.zstdCtxManager.acquireCompress();
+        this.compressors.add(ret);
+        return ret;
+    }
+
+    public ZstdDecompressCtx acquireDecompressor() {
+        final ZstdDecompressCtx ret = this.zstdCtxManager.acquireDecompress();
+        this.decompressors.add(ret);
+        return ret;
+    }
+
+    @Override
+    public void close() {
+        if (this.released) {
+            throw new IllegalStateException("Double-releasing buffers (incorrect class usage?)");
+        }
+        this.released = true;
+
+        final ZstdCompressCtx[] compressors = this.compressors.toArray(EMPTY_COMPRESSORS);
+        this.compressors.clear();
+        for (final ZstdCompressCtx compressor : compressors) {
+            this.zstdCtxManager.returnCompress(compressor);
+        }
+
+        final ZstdDecompressCtx[] decompressors = this.decompressors.toArray(EMPTY_DECOMPRSSORS);
+        this.decompressors.clear();
+        for (final ZstdDecompressCtx decompressor : decompressors) {
+            this.zstdCtxManager.returnDecompress(decompressor);
+        }
+    }
+
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/ConversionRegionFile.java b/src/main/java/ca/spottedleaf/io/region/ConversionRegionFile.java
new file mode 100644
index 0000000000000000000000000000000000000000..e1fa28ab159360b3886e7b8452ecd7101e772b98
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/ConversionRegionFile.java
@@ -0,0 +1,567 @@
+package ca.spottedleaf.io.region;
+
+import ca.spottedleaf.io.region.SectorFile;
+import ca.spottedleaf.io.region.io.bytebuffer.BufferedFileChannelInputStream;
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferInputStream;
+import ca.spottedleaf.io.buffer.BufferChoices;
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import net.jpountz.lz4.LZ4BlockInputStream;
+import net.jpountz.lz4.LZ4BlockOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.EOFException;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.IntBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.AtomicMoveNotSupportedException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
+
+/**
+ * Read/write support of the region file format used by Minecraft
+ */
+public final class ConversionRegionFile implements Closeable {
+
+    /* Based on https://github.com/JosuaKrause/NBTEditor/blob/master/src/net/minecraft/world/level/chunk/storage/RegionFile.java */
+
+    public static final String ANVIL_EXTENSION = ".mca";
+    public static final String MCREGION_EXTENSION = ".mcr";
+
+    private static final int SECTOR_SHIFT = 12;
+    private static final int SECTOR_SIZE = 1 << SECTOR_SHIFT;
+
+    private static final int BYTE_SIZE   = Byte.BYTES;
+    private static final int SHORT_SIZE  = Short.BYTES;
+    private static final int INT_SIZE    = Integer.BYTES;
+    private static final int LONG_SIZE   = Long.BYTES;
+    private static final int FLOAT_SIZE  = Float.BYTES;
+    private static final int DOUBLE_SIZE = Double.BYTES;
+
+    private static final int DATA_METADATA_SIZE = BYTE_SIZE + INT_SIZE;
+
+    private final int[] header = new int[SECTOR_SIZE / INT_SIZE];
+    private final int[] timestamps = new int[SECTOR_SIZE / INT_SIZE];
+
+    public final File file;
+    public final int sectionX;
+    public final int sectionZ;
+
+    private FileChannel channel;
+    private final boolean readOnly;
+    private final SectorFile.SectorAllocator sectorAllocator = new SectorFile.SectorAllocator(getLocationOffset(-1) - 1, getLocationSize(-1));
+    {
+        this.sectorAllocator.allocate(2, false);
+    }
+
+    private ByteBuffer raw;
+
+    public ConversionRegionFile(final File file, final int sectionX, final int sectionZ, final BufferChoices unscopedBufferChoices,
+                                final boolean readOnly) throws IOException {
+        this.file = file;
+        this.sectionX = sectionX;
+        this.sectionZ = sectionZ;
+        this.readOnly = readOnly;
+
+        if (readOnly) {
+            this.channel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
+        } else {
+            file.getParentFile().mkdirs();
+            this.channel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
+        }
+
+        final long size = this.channel.size();
+
+        if (size != 0L) {
+            if (size < (2L * (long)SECTOR_SIZE)) {
+                System.err.println("Truncated header in file: " + file.getAbsolutePath());
+                return;
+            }
+
+            try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+                final ByteBuffer headerBuffer = scopedBufferChoices.t16k().acquireDirectBuffer();
+
+                headerBuffer.order(ByteOrder.BIG_ENDIAN);
+                headerBuffer.limit(2 * SECTOR_SIZE);
+                headerBuffer.position(0);
+                this.channel.read(headerBuffer, 0L);
+                headerBuffer.flip();
+
+                final IntBuffer headerIntBuffer = headerBuffer.asIntBuffer();
+
+                headerIntBuffer.get(0, this.header, 0, SECTOR_SIZE / INT_SIZE);
+                headerIntBuffer.get(0 + SECTOR_SIZE / INT_SIZE, this.timestamps, 0, SECTOR_SIZE / INT_SIZE);
+
+                for (int i = 0; i < this.header.length; ++i) {
+                    final int location = this.header[i];
+
+                    if (location == 0) {
+                        continue;
+                    }
+
+                    if (!this.sectorAllocator.tryAllocateDirect(getLocationOffset(location), getLocationSize(location), false)) {
+                        this.header[i] = 0;
+                        System.err.println("Invalid sector allocation in regionfile '" + this.file.getAbsolutePath() + "': (" + i + "," + location + ")");
+                    }
+                }
+            }
+        }
+    }
+
+    public void fillRaw(final BufferChoices unscopedBufferChoices) throws IOException {
+        if (this.raw != null) {
+            throw new IllegalStateException("Already filled raw");
+        }
+        if (!this.readOnly) {
+            throw new IllegalStateException("Cannot fill raw in write mode");
+        }
+
+        final long length = Math.max(0L, this.channel.size());
+
+        final ByteBuffer raw = ByteBuffer.allocate((int)length);
+
+        try (final BufferChoices scopedChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer buffer = scopedChoices.t1m().acquireDirectBuffer();
+
+            long offset = 0L;
+
+            while (this.channel.read(buffer, offset) >= 0) {
+                final int read = buffer.position();
+
+                buffer.flip();
+
+                raw.put(buffer);
+
+                buffer.limit(buffer.capacity());
+                buffer.position(0);
+
+                offset += read;
+            }
+        }
+
+        raw.flip();
+
+        this.raw = raw;
+    }
+
+    public int readRaw(final ByteBuffer dst, final long position) {
+        if (position < 0) {
+            throw new IllegalArgumentException();
+        }
+        if (position >= (long)Integer.MAX_VALUE) {
+            return -1;
+        }
+
+        final int tryRead = dst.remaining();
+        final int remainingRaw = this.raw.limit() - (int)position;
+
+        if (tryRead <= 0) {
+            return 0;
+        }
+
+        if (remainingRaw <= 0) {
+            return -1;
+        }
+
+        final int toRead = Math.min(tryRead, remainingRaw);
+
+        dst.put(dst.position(), this.raw, (int)position, toRead);
+        dst.position(dst.position() + toRead);
+
+        return toRead;
+    }
+
+    private static int getLocationSize(final int location) {
+        return location & 255;
+    }
+
+    private static int getLocationOffset(final int location) {
+        return location >>> 8;
+    }
+
+    private static int makeLocation(final int offset, final int size) {
+        return (offset << 8) | (size & 255);
+    }
+
+    public boolean has(final int x, final int z) {
+        return this.getLocation(x, z) != 0;
+    }
+
+    private File getExternalFile(final int x, final int z) {
+        final int cx = (x & 31) | (this.sectionX << 5);
+        final int cz = (z & 31) | (this.sectionZ << 5);
+
+        return new File(this.file.getParentFile(), "c." + cx + "." + cz + ".mcc");
+    }
+
+    public static record AllocationStats(long fileSectors, long allocatedSectors, long alternateAllocatedSectors, long alternateAllocatedSectorsPadded, long dataSizeBytes, int errors) {}
+
+    private int[] getHeaderSorted() {
+        final IntArrayList list = new IntArrayList(this.header.length);
+        for (final int location : this.header) {
+            list.add(location);
+        }
+
+        list.sort((a, b) -> Integer.compare(getLocationOffset(a), getLocationOffset(b)));
+
+        return list.toArray(new int[this.header.length]);
+    }
+
+    public AllocationStats computeStats(final BufferChoices unscopedBufferChoices, final int alternateSectorSize,
+                                        final int alternateHeaderSectors,
+                                        final int alternateOverhead) throws IOException {
+        final long fileSectors = (this.file.length() + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+
+        long allocatedSectors = Math.min(fileSectors, 2L);
+        long alternateAllocatedSectors = (long)alternateHeaderSectors;
+        int errors = 0;
+        long dataSize = 0L;
+
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer ioBuffer = scopedBufferChoices.t16k().acquireDirectBuffer();
+
+            for (final int location : this.getHeaderSorted()) {
+                if (location == 0) {
+                    continue;
+                }
+
+                final int offset = getLocationOffset(location);
+                final int size = getLocationSize(location);
+
+                if (offset <= 1 || size <= 0 || (offset + size) > fileSectors) {
+                    // invalid
+                    ++errors;
+                    continue;
+                }
+
+                ioBuffer.limit(INT_SIZE);
+                ioBuffer.position(0);
+
+                this.channel.read(ioBuffer, (long)offset << SECTOR_SHIFT);
+
+                if (ioBuffer.hasRemaining()) {
+                    ++errors;
+                    continue;
+                }
+
+                final int rawSize = ioBuffer.getInt(0) + INT_SIZE;
+
+                final int rawEnd = rawSize + (offset << SECTOR_SHIFT);
+                if (rawSize <= 0 || rawEnd <= 0 || ((rawSize + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT) > fileSectors) {
+                    ++errors;
+                    continue;
+                }
+
+                final int compressedSize = rawSize - (INT_SIZE + BYTE_SIZE);
+
+                final int alternateRawSize = (compressedSize + alternateOverhead);
+
+                // support forge oversized by using data size
+                allocatedSectors += (long)((rawSize + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT);
+                dataSize += (long)compressedSize;
+                alternateAllocatedSectors += (long)((alternateRawSize + (alternateSectorSize - 1)) / alternateSectorSize);
+            }
+        }
+
+        final long diff = SECTOR_SIZE / alternateSectorSize;
+        final long alternateAllocatedSectorsPadded = diff <= 1L ? alternateAllocatedSectors : ((alternateAllocatedSectors + (diff - 1L)) / diff) * diff;
+
+        return new AllocationStats(fileSectors, allocatedSectors, alternateAllocatedSectors, alternateAllocatedSectorsPadded, dataSize, errors);
+    }
+
+    public boolean read(final int x, final int z, final BufferChoices unscopedBufferChoices, final ConversionRegionFile.CustomByteArrayOutputStream decompressed) throws IOException {
+        return this.read(x, z, unscopedBufferChoices, decompressed, false) >= 0;
+    }
+
+    public int read(final int x, final int z, final BufferChoices unscopedBufferChoices, final ConversionRegionFile.CustomByteArrayOutputStream decompressed,
+                    final boolean raw) throws IOException {
+        final int location = this.getLocation(x, z);
+
+        if (location == 0) {
+            // absent
+            return -1;
+        }
+
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            ByteBuffer compressedData = scopedBufferChoices.t1m().acquireDirectBuffer();
+
+            final long foff = ((long)getLocationOffset(location) * (long)SECTOR_SIZE);
+            int fsize = (getLocationSize(location) * SECTOR_SIZE);
+
+            if (fsize == (255 * SECTOR_SIZE)) {
+                // support for forge/spigot style oversized chunk format (pre 1.15)
+                final ByteBuffer extendedLen = ByteBuffer.allocate(INT_SIZE);
+                if (this.raw == null) {
+                    this.channel.read(extendedLen, foff);
+                } else {
+                    this.readRaw(extendedLen, foff);
+                }
+                fsize = extendedLen.getInt(0);
+                if (fsize > compressedData.capacity()) {
+                    // do not use direct here, the read() will allocate one and free it immediately - which is something
+                    // we cannot do with standard API
+                    compressedData = ByteBuffer.allocate(fsize);
+                }
+            }
+
+            compressedData.order(ByteOrder.BIG_ENDIAN);
+            compressedData.limit(fsize);
+            compressedData.position(0);
+
+            int r = this.raw != null ? this.readRaw(compressedData, foff) : this.channel.read(compressedData, foff);
+            if (r < DATA_METADATA_SIZE) {
+                throw new IOException("Truncated data");
+            }
+            compressedData.flip();
+
+
+            final int length = compressedData.getInt(0) - BYTE_SIZE;
+            byte type = compressedData.get(0 + INT_SIZE);
+            compressedData.position(0 + INT_SIZE + BYTE_SIZE);
+            compressedData.limit(compressedData.getInt(0) + INT_SIZE);
+
+            if (compressedData.remaining() < length) {
+                throw new EOFException("Truncated data");
+            }
+
+            final InputStream rawIn;
+            if ((type & 128) != 0) {
+                // stored externally
+                type = (byte)((int)type & 127);
+
+                final File external = this.getExternalFile(x, z);
+                if (!external.isFile()) {
+                    System.err.println("Externally stored chunk data '" + external.getAbsolutePath() + "' does not exist");
+                    return -1;
+                }
+
+                rawIn = new BufferedFileChannelInputStream(scopedBufferChoices.t16k().acquireDirectBuffer(), this.getExternalFile(x, z));
+            } else {
+                rawIn = new ByteBufferInputStream(compressedData);
+            }
+
+            InputStream decompress = null;
+
+            try {
+                if (!raw) {
+                    switch (type) {
+                        case 1: { // GZIP
+                            decompress = new GZIPInputStream(rawIn);
+                            break;
+                        }
+                        case 2: { // DEFLATE
+                            decompress = new InflaterInputStream(rawIn);
+                            break;
+                        }
+                        case 3: { // NONE
+                            decompress = rawIn;
+                            break;
+                        }
+                        case 4: { // LZ4
+                            decompress = new LZ4BlockInputStream(rawIn);
+                            break;
+                        }
+                        default: {
+                            throw new IOException("Unknown type: " + type);
+                        }
+                    }
+                } else {
+                    if (type <= 0 || type > 4) {
+                        throw new IOException("Unknown type: " + type);
+                    }
+                    decompress = rawIn;
+                }
+
+                final byte[] tmp = scopedBufferChoices.t16k().acquireJavaBuffer();
+
+                while ((r = decompress.read(tmp)) >= 0) {
+                    decompressed.write(tmp, 0, r);
+                }
+
+                return type;
+            } finally {
+                if (decompress != null) {
+                    decompress.close();
+                }
+            }
+        }
+    }
+
+    private void writeHeader(final BufferChoices unscopedBufferChoices) throws IOException {
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer buffer = scopedBufferChoices.t16k().acquireDirectBuffer();
+
+            buffer.limit((this.header.length + this.timestamps.length) * INT_SIZE);
+            buffer.position(0);
+
+            buffer.asIntBuffer().put(this.header).put(this.timestamps);
+
+            this.channel.write(buffer, 0L << SECTOR_SHIFT);
+        }
+    }
+
+    public void delete(final int x, final int z, final BufferChoices unscopedBufferChoices) throws IOException {
+        final int location = this.getLocation(x, z);
+        if (location == 0) {
+            return;
+        }
+
+        this.setLocation(x, z, 0);
+        this.setTimestamp(x, z, 0);
+        this.getExternalFile(x, z).delete();
+
+        this.writeHeader(unscopedBufferChoices);
+        this.sectorAllocator.freeAllocation(getLocationOffset(location), getLocationSize(location));
+    }
+
+    public void write(final int x, final int z, final BufferChoices unscopedBufferChoices, final int compressType,
+                      final byte[] data, final int off, final int len) throws IOException {
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            final CustomByteArrayOutputStream rawStream = new CustomByteArrayOutputStream(scopedBufferChoices.t1m().acquireJavaBuffer());
+
+            for (int i = 0; i < (INT_SIZE + BYTE_SIZE); ++i) {
+                rawStream.write(0);
+            }
+
+            final OutputStream out;
+            switch (compressType) {
+                case 1: { // GZIP
+                    out = new GZIPOutputStream(rawStream);
+                    break;
+                }
+                case 2: { // DEFLATE
+                    out = new DeflaterOutputStream(rawStream);
+                    break;
+                }
+                case 3: { // NONE
+                    out = rawStream;
+                    break;
+                }
+                case 4: { // LZ4
+                    out = new LZ4BlockOutputStream(rawStream);
+                    break;
+                }
+                default: {
+                    throw new IOException("Unknown type: " + compressType);
+                }
+            }
+
+            out.write(data, off, len);
+            out.close();
+
+            ByteBuffer.wrap(rawStream.getBuffer(), 0, rawStream.size())
+                    .putInt(0, rawStream.size() - INT_SIZE)
+                    .put(INT_SIZE, (byte)compressType);
+
+            final int requiredSectors = (rawStream.size() + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+            final ByteBuffer write;
+
+            if (requiredSectors <= 255) {
+                write = ByteBuffer.wrap(rawStream.getBuffer(), 0, rawStream.size());
+            } else {
+                write = ByteBuffer.allocate(INT_SIZE + BYTE_SIZE);
+                write.putInt(0, 1);
+                write.put(INT_SIZE, (byte)(128 | compressType));
+            }
+
+            final int internalSectors = (write.remaining() + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+            final int internalOffset = this.sectorAllocator.allocate(internalSectors, true);
+            if (internalOffset <= 0) {
+                throw new IllegalStateException("Failed to allocate internally");
+            }
+
+            this.channel.write(write, (long)internalOffset << SECTOR_SHIFT);
+
+            if (requiredSectors > 255) {
+                final File external = this.getExternalFile(x, z);
+
+                System.out.println("Storing " + rawStream.size() + " bytes to " + external.getAbsolutePath());
+
+                final File externalTmp = new File(external.getParentFile(), external.getName() + ".tmp");
+                externalTmp.delete();
+                externalTmp.createNewFile();
+                try (final FileOutputStream fout = new FileOutputStream(externalTmp)) {
+                    fout.write(rawStream.getBuffer(), INT_SIZE + BYTE_SIZE, rawStream.size() - (INT_SIZE + BYTE_SIZE));
+                }
+
+                try {
+                    Files.move(externalTmp.toPath(), external.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
+                } catch (final AtomicMoveNotSupportedException ex) {
+                    Files.move(externalTmp.toPath(), external.toPath(), StandardCopyOption.REPLACE_EXISTING);
+                }
+            } else {
+                this.getExternalFile(x, z).delete();
+            }
+
+            final int oldLocation = this.getLocation(x, z);
+
+            this.setLocation(x, z, makeLocation(internalOffset, internalSectors));
+            this.setTimestamp(x, z, (int)(System.currentTimeMillis() / 1000L));
+
+            this.writeHeader(unscopedBufferChoices);
+
+            if (oldLocation != 0) {
+                this.sectorAllocator.freeAllocation(getLocationOffset(oldLocation), getLocationSize(oldLocation));
+            }
+        }
+    }
+
+    private static int makeIndex(final int x, final int z) {
+        return (x & 31) | ((z & 31) << 5);
+    }
+
+    public int getLocation(final int x, final int z) {
+        return this.header[makeIndex(x, z)];
+    }
+
+    private void setLocation(final int x, final int z, final int location) {
+        this.header[makeIndex(x, z)] = location;
+    }
+
+    public int getTimestamp(final int x, final int z) {
+        return this.timestamps[makeIndex(x, z)];
+    }
+
+    private void setTimestamp(final int x, final int z, final int time) {
+        this.timestamps[makeIndex(x, z)] = time;
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        final FileChannel channel = this.channel;
+        if (channel != null) {
+            this.channel = null;
+            if (this.readOnly) {
+                channel.close();
+            } else {
+                try {
+                    channel.force(true);
+                } finally {
+                    channel.close();
+                }
+            }
+        }
+    }
+
+    public static final class CustomByteArrayOutputStream extends ByteArrayOutputStream {
+
+        public CustomByteArrayOutputStream(final byte[] bytes) {
+            super(0);
+            this.buf = bytes;
+        }
+
+        public byte[] getBuffer() {
+            return this.buf;
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/MinecraftRegionFileType.java b/src/main/java/ca/spottedleaf/io/region/MinecraftRegionFileType.java
new file mode 100644
index 0000000000000000000000000000000000000000..19fae8b8e76d0f1b4b0583ee5f496b70976452ac
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/MinecraftRegionFileType.java
@@ -0,0 +1,61 @@
+package ca.spottedleaf.io.region;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
+import java.util.Collections;
+
+public final class MinecraftRegionFileType {
+
+    private static final Int2ObjectLinkedOpenHashMap<MinecraftRegionFileType> BY_ID = new Int2ObjectLinkedOpenHashMap<>();
+    private static final Int2ObjectLinkedOpenHashMap<String> NAME_TRANSLATION = new Int2ObjectLinkedOpenHashMap<>();
+    private static final Int2ObjectMap<String> TRANSLATION_TABLE = Int2ObjectMaps.unmodifiable(NAME_TRANSLATION);
+
+    public static final MinecraftRegionFileType CHUNK = new MinecraftRegionFileType("region", 0, "chunk_data");
+    public static final MinecraftRegionFileType POI = new MinecraftRegionFileType("poi", 1, "poi_chunk");
+    public static final MinecraftRegionFileType ENTITY = new MinecraftRegionFileType("entities", 2, "entity_chunk");
+
+    private final String folder;
+    private final int id;
+    private final String name;
+
+    public MinecraftRegionFileType(final String folder, final int id, final String name) {
+        if (BY_ID.putIfAbsent(id, this) != null) {
+            throw new IllegalStateException("Duplicate ids");
+        }
+        NAME_TRANSLATION.put(id, name);
+
+        this.folder = folder;
+        this.id = id;
+        this.name = name;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public String getFolder() {
+        return this.folder;
+    }
+
+    public int getNewId() {
+        return this.id;
+    }
+
+    public static MinecraftRegionFileType byId(final int id) {
+        return BY_ID.get(id);
+    }
+
+    public static String getName(final int id) {
+        final MinecraftRegionFileType type = byId(id);
+        return type == null ? null : type.getName();
+    }
+
+    public static Iterable<MinecraftRegionFileType> getAll() {
+        return Collections.unmodifiableCollection(BY_ID.values());
+    }
+
+    public static Int2ObjectMap<String> getTranslationTable() {
+        return TRANSLATION_TABLE;
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/SectorFile.java b/src/main/java/ca/spottedleaf/io/region/SectorFile.java
new file mode 100644
index 0000000000000000000000000000000000000000..e94f927094c8d5a09bfaf491d623bfa7add0b435
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/SectorFile.java
@@ -0,0 +1,1925 @@
+package ca.spottedleaf.io.region;
+
+import ca.spottedleaf.io.region.io.bytebuffer.BufferedFileChannelInputStream;
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferInputStream;
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferOutputStream;
+import ca.spottedleaf.io.buffer.BufferChoices;
+import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.IntIterator;
+import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
+import it.unimi.dsi.fastutil.longs.LongBidirectionalIterator;
+import it.unimi.dsi.fastutil.longs.LongComparator;
+import it.unimi.dsi.fastutil.longs.LongRBTreeSet;
+import net.jpountz.xxhash.StreamingXXHash64;
+import net.jpountz.xxhash.XXHash64;
+import net.jpountz.xxhash.XXHashFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.io.Closeable;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.AtomicMoveNotSupportedException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Random;
+
+public final class SectorFile implements Closeable {
+
+    private static final XXHashFactory XXHASH_FACTORY = XXHashFactory.fastestInstance();
+    // Java instance is used for streaming hash instances, as streaming hash instances do not provide bytebuffer API
+    // Native instances would use GetPrimitiveArrayCritical and prevent GC on G1
+    private static final XXHashFactory XXHASH_JAVA_FACTORY = XXHashFactory.fastestJavaInstance();
+    private static final XXHash64 XXHASH64 = XXHASH_FACTORY.hash64();
+    // did not find a use to change this from default, but just in case
+    private static final long XXHASH_SEED = 0L;
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SectorFile.class);
+
+    private static final int BYTE_SIZE   = Byte.BYTES;
+    private static final int SHORT_SIZE  = Short.BYTES;
+    private static final int INT_SIZE    = Integer.BYTES;
+    private static final int LONG_SIZE   = Long.BYTES;
+    private static final int FLOAT_SIZE  = Float.BYTES;
+    private static final int DOUBLE_SIZE = Double.BYTES;
+
+    public static final String FILE_EXTENSION              = ".sf";
+    public static final String FILE_EXTERNAL_EXTENSION     = ".sfe";
+    public static final String FILE_EXTERNAL_TMP_EXTENSION = FILE_EXTERNAL_EXTENSION + ".tmp";
+
+    public static String getFileName(final int sectionX, final int sectionZ) {
+        return sectionX + "." + sectionZ + FILE_EXTENSION;
+    }
+
+    private static String getExternalBase(final int sectionX, final int sectionZ,
+                                          final int localX, final int localZ,
+                                          final int type) {
+        final int absoluteX = (sectionX << SECTION_SHIFT) | (localX & SECTION_MASK);
+        final int absoluteZ = (sectionZ << SECTION_SHIFT) | (localZ & SECTION_MASK);
+
+        return absoluteX + "." + absoluteZ + "-" + type;
+    }
+
+    public static String getExternalFileName(final int sectionX, final int sectionZ,
+                                             final int localX, final int localZ,
+                                             final int type) {
+        return getExternalBase(sectionX, sectionZ, localX, localZ, type) + FILE_EXTERNAL_EXTENSION;
+    }
+
+    public static String getExternalTempFileName(final int sectionX, final int sectionZ,
+                                                  final int localX, final int localZ, final int type) {
+        return getExternalBase(sectionX, sectionZ, localX, localZ, type) + FILE_EXTERNAL_TMP_EXTENSION;
+    }
+
+    public static final int SECTOR_SHIFT = 9;
+    public static final int SECTOR_SIZE = 1 << SECTOR_SHIFT;
+
+    public static final int SECTION_SHIFT = 5;
+    public static final int SECTION_SIZE = 1 << SECTION_SHIFT;
+    public static final int SECTION_MASK = SECTION_SIZE - 1;
+
+    // General assumptions: Type header offsets are at least one sector in size
+
+    /*
+     * File Header:
+     * First 8-bytes: XXHash64 of entire header data, excluding hash value
+     * Next 42x8 bytes: XXHash64 values for each type header
+     * Next 42x4 bytes: sector offsets of type headers
+     */
+    private static final int FILE_HEADER_SECTOR = 0;
+    public static final int MAX_TYPES = 42;
+
+    public static final class FileHeader {
+
+        public static final int FILE_HEADER_SIZE_BYTES = LONG_SIZE + MAX_TYPES*(LONG_SIZE + INT_SIZE);
+        public static final int FILE_HEADER_TOTAL_SECTORS = (FILE_HEADER_SIZE_BYTES + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+
+        public final long[] xxHash64TypeHeader = new long[MAX_TYPES];
+        public final int[] typeHeaderOffsets = new int[MAX_TYPES];
+
+        public FileHeader() {
+            if (ABSENT_HEADER_XXHASH64 != 0L || ABSENT_TYPE_HEADER_OFFSET != 0) { // arrays default initialise to 0
+                this.reset();
+            }
+        }
+
+        public void reset() {
+            Arrays.fill(this.xxHash64TypeHeader, ABSENT_HEADER_XXHASH64);
+            Arrays.fill(this.typeHeaderOffsets, ABSENT_TYPE_HEADER_OFFSET);
+        }
+
+        public void write(final ByteBuffer buffer) {
+            final int pos = buffer.position();
+
+            // reserve XXHash64 space
+            buffer.putLong(0L);
+
+            buffer.asLongBuffer().put(0, this.xxHash64TypeHeader);
+            buffer.position(buffer.position() + MAX_TYPES * LONG_SIZE);
+
+            buffer.asIntBuffer().put(0, this.typeHeaderOffsets);
+            buffer.position(buffer.position() + MAX_TYPES * INT_SIZE);
+
+            final long hash = computeHash(buffer, pos);
+
+            buffer.putLong(pos, hash);
+        }
+
+        public static void read(final ByteBuffer buffer, final FileHeader fileHeader) {
+            buffer.duplicate().position(buffer.position() + LONG_SIZE).asLongBuffer().get(0, fileHeader.xxHash64TypeHeader);
+
+            buffer.duplicate().position(buffer.position() + LONG_SIZE + LONG_SIZE * MAX_TYPES)
+                .asIntBuffer().get(0, fileHeader.typeHeaderOffsets);
+
+            buffer.position(buffer.position() + FILE_HEADER_SIZE_BYTES);
+        }
+
+        public static long computeHash(final ByteBuffer buffer, final int offset) {
+            return XXHASH64.hash(buffer, offset + LONG_SIZE, FILE_HEADER_SIZE_BYTES - LONG_SIZE, XXHASH_SEED);
+        }
+
+        public static boolean validate(final ByteBuffer buffer, final int offset) {
+            final long expected = buffer.getLong(offset);
+
+            return expected == computeHash(buffer, offset);
+        }
+
+        public void copyFrom(final FileHeader src) {
+            System.arraycopy(src.xxHash64TypeHeader, 0, this.xxHash64TypeHeader, 0, MAX_TYPES);
+            System.arraycopy(src.typeHeaderOffsets, 0, this.typeHeaderOffsets, 0, MAX_TYPES);
+        }
+    }
+
+    public static record DataHeader(
+            long xxhash64Header,
+            long xxhash64Data,
+            long timeWritten,
+            int compressedSize,
+            short index,
+            byte typeId,
+            byte compressionType
+    ) {
+
+        public static void storeHeader(final ByteBuffer buffer, final XXHash64 xxHash64,
+                                       final long dataHash, final long timeWritten,
+                                       final int compressedSize, final short index, final byte typeId,
+                                       final byte compressionType) {
+            final int pos = buffer.position();
+
+            buffer.putLong(0L); // placeholder for header hash
+            buffer.putLong(dataHash);
+            buffer.putLong(timeWritten);
+            buffer.putInt(compressedSize);
+            buffer.putShort(index);
+            buffer.put(typeId);
+            buffer.put(compressionType);
+
+            // replace placeholder for header hash with real hash
+            buffer.putLong(pos, computeHash(xxHash64, buffer, pos));
+        }
+
+        public static final int DATA_HEADER_LENGTH = LONG_SIZE + LONG_SIZE + LONG_SIZE + INT_SIZE + SHORT_SIZE + BYTE_SIZE + BYTE_SIZE;
+
+        public static DataHeader read(final ByteBuffer buffer) {
+            if (buffer.remaining() < DATA_HEADER_LENGTH) {
+                return null;
+            }
+
+            return new DataHeader(
+                    buffer.getLong(), buffer.getLong(), buffer.getLong(),
+                    buffer.getInt(), buffer.getShort(), buffer.get(), buffer.get()
+            );
+        }
+
+        public static DataHeader read(final ByteBufferInputStream input) throws IOException {
+            final ByteBuffer buffer = ByteBuffer.allocate(DATA_HEADER_LENGTH);
+
+            // read = 0 when buffer is full
+            while (input.read(buffer) > 0);
+
+            buffer.flip();
+            return read(buffer);
+        }
+
+        public static long computeHash(final XXHash64 xxHash64, final ByteBuffer header, final int offset) {
+            return xxHash64.hash(header, offset + LONG_SIZE, DATA_HEADER_LENGTH - LONG_SIZE, XXHASH_SEED);
+        }
+
+        public static boolean validate(final XXHash64 xxHash64, final ByteBuffer header, final int offset) {
+            final long expectedSeed = header.getLong(offset);
+            final long computedSeed = computeHash(xxHash64, header, offset);
+
+            return expectedSeed == computedSeed;
+        }
+    }
+
+    private static final int SECTOR_LENGTH_BITS = 10;
+    private static final int SECTOR_OFFSET_BITS = 22;
+    static {
+        if ((SECTOR_OFFSET_BITS + SECTOR_LENGTH_BITS) != 32) {
+            throw new IllegalStateException();
+        }
+    }
+
+    private static final int MAX_NORMAL_SECTOR_OFFSET = (1 << SECTOR_OFFSET_BITS) - 2; // inclusive
+    private static final int MAX_NORMAL_SECTOR_LENGTH = (1 << SECTOR_LENGTH_BITS) - 1;
+
+    private static final int MAX_INTERNAL_ALLOCATION_BYTES = SECTOR_SIZE * (1 << SECTOR_LENGTH_BITS);
+
+    public static final int TYPE_HEADER_OFFSET_COUNT = SECTION_SIZE * SECTION_SIZE; // total number of offsets per type header
+    public static final int TYPE_HEADER_SECTORS = (TYPE_HEADER_OFFSET_COUNT * INT_SIZE) / SECTOR_SIZE; // total number of sectors used per type header
+
+    // header location is just raw sector number
+    // so, we point to the header itself to indicate absence
+    private static final int ABSENT_TYPE_HEADER_OFFSET = FILE_HEADER_SECTOR;
+    private static final long ABSENT_HEADER_XXHASH64 = 0L;
+
+    private static int makeLocation(final int sectorOffset, final int sectorLength) {
+        return (sectorOffset << SECTOR_LENGTH_BITS) | (sectorLength & ((1 << SECTOR_LENGTH_BITS) - 1));
+    }
+
+    // point to file header sector when absent, as we know that sector is allocated and will not conflict with any real allocation
+    private static final int ABSENT_LOCATION              = makeLocation(FILE_HEADER_SECTOR, 0);
+    // point to outside the maximum allocatable range for external allocations, which will not conflict with any other
+    // data allocation (although, it may conflict with a type header allocation)
+    private static final int EXTERNAL_ALLOCATION_LOCATION = makeLocation(MAX_NORMAL_SECTOR_OFFSET + 1, 0);
+
+    private static int getLocationOffset(final int location) {
+        return location >>> SECTOR_LENGTH_BITS;
+    }
+
+    private static int getLocationLength(final int location) {
+        return location & ((1 << SECTOR_LENGTH_BITS) - 1);
+    }
+
+    private static int getIndex(final int localX, final int localZ) {
+        return (localX & SECTION_MASK) | ((localZ & SECTION_MASK) << SECTION_SHIFT);
+    }
+
+    private static int getLocalX(final int index) {
+        return index & SECTION_MASK;
+    }
+
+    private static int getLocalZ(final int index) {
+        return (index >>> SECTION_SHIFT) & SECTION_MASK;
+    }
+
+    public final File file;
+    public final int sectionX;
+    public final int sectionZ;
+    private final FileChannel channel;
+    private final boolean sync;
+    private final boolean readOnly;
+    private final SectorAllocator sectorAllocator = newSectorAllocator();
+    private final SectorFileCompressionType compressionType;
+
+    private static final class TypeHeader {
+
+        public static final int TYPE_HEADER_SIZE_BYTES = TYPE_HEADER_OFFSET_COUNT * INT_SIZE;
+
+        public final int[] locations;
+
+        private TypeHeader() {
+            this.locations = new int[TYPE_HEADER_OFFSET_COUNT];
+            if (ABSENT_LOCATION != 0) {
+                this.reset();
+            }
+        }
+
+        private TypeHeader(final int[] locations) {
+            this.locations = locations;
+            if (locations.length != TYPE_HEADER_OFFSET_COUNT) {
+                throw new IllegalArgumentException();
+            }
+        }
+
+        public void reset() {
+            Arrays.fill(this.locations, ABSENT_LOCATION);
+        }
+
+        public static TypeHeader read(final ByteBuffer buffer) {
+            final int[] locations = new int[TYPE_HEADER_OFFSET_COUNT];
+            buffer.asIntBuffer().get(0, locations, 0, TYPE_HEADER_OFFSET_COUNT);
+
+            return new TypeHeader(locations);
+        }
+
+        public void write(final ByteBuffer buffer) {
+            buffer.asIntBuffer().put(0, this.locations);
+
+            buffer.position(buffer.position() + TYPE_HEADER_SIZE_BYTES);
+        }
+
+        public static long computeHash(final ByteBuffer buffer, final int offset) {
+            return XXHASH64.hash(buffer, offset, TYPE_HEADER_SIZE_BYTES, XXHASH_SEED);
+        }
+    }
+
+    private final Int2ObjectLinkedOpenHashMap<TypeHeader> typeHeaders = new Int2ObjectLinkedOpenHashMap<>();
+    private final Int2ObjectMap<String> typeTranslationTable;
+    private final FileHeader fileHeader = new FileHeader();
+
+    private void checkHeaderExists(final int type) {
+        // we want to error when a type is used which is not mapped
+        if (this.typeTranslationTable.get(type) == null) {
+            throw new IllegalArgumentException("Unknown type " + type);
+        }
+    }
+
+    static {
+        final int smallBufferSize = 16 * 1024; // 16kb
+        if (FileHeader.FILE_HEADER_SIZE_BYTES > smallBufferSize) {
+            throw new IllegalStateException("Cannot read file header using single small buffer");
+        }
+        if (TypeHeader.TYPE_HEADER_SIZE_BYTES > smallBufferSize) {
+            throw new IllegalStateException("Cannot read type header using single small buffer");
+        }
+    }
+
+    public static final int OPEN_FLAGS_READ_ONLY   = 1 << 0;
+    public static final int OPEN_FLAGS_SYNC_WRITES = 1 << 1;
+
+    public SectorFile(final File file, final int sectionX, final int sectionZ,
+                      final SectorFileCompressionType defaultCompressionType,
+                      final BufferChoices unscopedBufferChoices, final Int2ObjectMap<String> typeTranslationTable,
+                      final int openFlags) throws IOException {
+        final boolean readOnly = (openFlags & OPEN_FLAGS_READ_ONLY) != 0;
+        final boolean sync = (openFlags & OPEN_FLAGS_SYNC_WRITES) != 0;
+
+        if (readOnly & sync) {
+            throw new IllegalArgumentException("Cannot set read-only and sync");
+        }
+        this.file = file;
+        this.sectionX = sectionX;
+        this.sectionZ = sectionZ;
+        if (readOnly) {
+            this.channel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
+        } else {
+            if (sync) {
+                this.channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.DSYNC);
+            } else {
+                this.channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
+            }
+        }
+        this.sync = sync;
+        this.readOnly = readOnly;
+        this.typeTranslationTable = typeTranslationTable;
+        this.compressionType = defaultCompressionType;
+
+        if (this.channel.size() != 0L) {
+            this.readFileHeader(unscopedBufferChoices);
+        }
+
+        // validate types
+        for (final IntIterator iterator = typeTranslationTable.keySet().iterator(); iterator.hasNext(); ) {
+            final int type = iterator.nextInt();
+
+            if (type < 0 || type >= MAX_TYPES) {
+                throw new IllegalStateException("Type translation table contains illegal type: " + type);
+            }
+        }
+    }
+
+    private TypeHeader createTypeHeader(final int type, final BufferChoices unscopedBufferChoices) throws IOException {
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer ioBuffer = scopedBufferChoices.t16k().acquireDirectBuffer();
+            final int offset = this.sectorAllocator.allocate(TYPE_HEADER_SECTORS, false); // in sectors
+            if (offset <= 0) {
+                throw new IllegalStateException("Cannot allocate space for header " + this.debugType(type) + ":" + offset);
+            }
+
+            final TypeHeader ret = new TypeHeader();
+
+            this.fileHeader.typeHeaderOffsets[type] = offset;
+            // hash will be computed by writeTypeHeader
+            this.typeHeaders.put(type, ret);
+
+            this.writeTypeHeader(ioBuffer, type, true, true);
+
+            return ret;
+        }
+    }
+
+    public int forTestingAllocateSector(final int sectors) {
+        return this.sectorAllocator.allocate(sectors, true);
+    }
+
+    private String debugType(final int type) {
+        final String name = this.typeTranslationTable.get(type);
+        return "{id=" + type + ",name=" + (name == null ? "unknown" : name) + "}";
+    }
+
+    private static SectorAllocator newSectorAllocator() {
+        final SectorAllocator newSectorAllocation = new SectorAllocator(MAX_NORMAL_SECTOR_OFFSET, MAX_NORMAL_SECTOR_LENGTH);
+        if (!newSectorAllocation.tryAllocateDirect(FILE_HEADER_SECTOR, FileHeader.FILE_HEADER_TOTAL_SECTORS, false)) {
+            throw new IllegalStateException("Cannot allocate initial header");
+        }
+        return newSectorAllocation;
+    }
+
+    private void makeBackup(final File target) throws IOException {
+        this.channel.force(true);
+        Files.copy(this.file.toPath(), target.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
+    }
+
+    public static final int RECALCULATE_FLAGS_NO_BACKUP = 1 << 0;
+    public static final int RECALCULATE_FLAGS_NO_LOG    = 1 << 1;
+
+    // returns whether any changes were made, useful for testing
+    public boolean recalculateFile(final BufferChoices unscopedBufferChoices, final int flags) throws IOException {
+        if (this.readOnly) {
+            return false;
+        }
+        if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+            LOGGER.error("An inconsistency has been detected in the headers for file '" + this.file.getAbsolutePath() +
+                "', recalculating the headers", new Throwable());
+        }
+        // The headers are determined as incorrect, so we are going to rebuild it from the file
+        final SectorAllocator newSectorAllocation = newSectorAllocator();
+
+        if ((flags & RECALCULATE_FLAGS_NO_BACKUP) == 0) {
+            final File backup = new File(this.file.getParentFile(), this.file.getName() + "." + new Random().nextLong() + ".backup");
+            if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                LOGGER.info("Making backup of '" + this.file.getAbsolutePath() + "' to '" + backup.getAbsolutePath() + "'");
+            }
+            this.makeBackup(backup);
+        }
+
+        class TentativeTypeHeader {
+            final TypeHeader typeHeader = new TypeHeader();
+            final long[] timestamps = new long[TYPE_HEADER_OFFSET_COUNT];
+        }
+
+        final Int2ObjectLinkedOpenHashMap<TentativeTypeHeader> newTypeHeaders = new Int2ObjectLinkedOpenHashMap<>();
+
+        // order of precedence of data found:
+        // newest timestamp,
+        // located internally,
+        // located closest to start internally
+
+        // force creation tentative type headers for required headers, as we will later replace the current ones
+        for (final IntIterator iterator = this.typeTranslationTable.keySet().iterator(); iterator.hasNext();) {
+            newTypeHeaders.put(iterator.nextInt(), new TentativeTypeHeader());
+        }
+
+        // search for internal data
+
+        try (final BufferChoices scopedChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer buffer = scopedChoices.t1m().acquireDirectBuffer();
+
+            final long fileSectors = (this.channel.size() + (long)(SECTOR_SIZE - 1)) >>> SECTOR_SHIFT;
+            for (long i = (long)(FILE_HEADER_SECTOR + FileHeader.FILE_HEADER_TOTAL_SECTORS); i <= Math.min(fileSectors, (long)MAX_NORMAL_SECTOR_OFFSET); ++i) {
+                buffer.limit(DataHeader.DATA_HEADER_LENGTH);
+                buffer.position(0);
+
+                this.channel.read(buffer, i << SECTOR_SHIFT);
+
+                if (buffer.hasRemaining()) {
+                    // last sector, which is truncated
+                    continue;
+                }
+
+                buffer.flip();
+
+                if (!DataHeader.validate(XXHASH64, buffer, 0)) {
+                    // no valid data allocated on this sector
+                    continue;
+                }
+
+                final DataHeader dataHeader = DataHeader.read(buffer);
+                // sector size = (compressed size + header size + SECTOR_SIZE-1) >> SECTOR_SHIFT
+                final int maxCompressedSize = (MAX_NORMAL_SECTOR_LENGTH << SECTOR_SHIFT) - DataHeader.DATA_HEADER_LENGTH;
+
+                if (dataHeader.compressedSize > maxCompressedSize || dataHeader.compressedSize < 0) {
+                    // invalid size
+                    continue;
+                }
+
+                final int typeId = (int)(dataHeader.typeId & 0xFF);
+                final int index = (int)(dataHeader.index & 0xFFFF);
+
+                if (typeId < 0 || typeId >= MAX_TYPES) {
+                    // type id is too large or small
+                    continue;
+                }
+
+                final TentativeTypeHeader typeHeader = newTypeHeaders.computeIfAbsent(typeId, (final int key) -> {
+                    return new TentativeTypeHeader();
+                });
+
+                final int prevLocation = typeHeader.typeHeader.locations[index];
+                if (prevLocation != ABSENT_LOCATION) {
+                    // try to skip data if the data is older
+                    final long prevTimestamp = typeHeader.timestamps[index];
+
+                    if ((dataHeader.timeWritten - prevTimestamp) <= 0L) {
+                        // this data is older, skip it
+                        // since we did not validate the data, we cannot skip over the sectors it says it has allocated
+                        continue;
+                    }
+                }
+
+                // read remaining data
+                buffer.limit(dataHeader.compressedSize);
+                buffer.position(0);
+                this.channel.read(buffer, (i << SECTOR_SHIFT) + (long)DataHeader.DATA_HEADER_LENGTH);
+
+                if (buffer.hasRemaining()) {
+                    // data is truncated, skip
+                    continue;
+                }
+
+                buffer.flip();
+
+                // validate data against hash
+                final long gotHash = XXHASH64.hash(buffer, 0, dataHeader.compressedSize, XXHASH_SEED);
+
+                if (gotHash != dataHeader.xxhash64Data) {
+                    // not the data we expect
+                    continue;
+                }
+
+                // since we are a newer timestamp than prev, replace it
+
+                final int sectorOffset = (int)i; // i <= MAX_NORMAL_SECTOR_OFFSET
+                final int sectorLength = (dataHeader.compressedSize + DataHeader.DATA_HEADER_LENGTH + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+                final int newLocation = makeLocation(sectorOffset, sectorLength);
+
+                if (!newSectorAllocation.tryAllocateDirect(sectorOffset, sectorLength, false)) {
+                    throw new IllegalStateException("Unable to allocate sectors");
+                }
+
+                if (prevLocation != ABSENT_LOCATION && prevLocation != EXTERNAL_ALLOCATION_LOCATION) {
+                    newSectorAllocation.freeAllocation(getLocationOffset(prevLocation), getLocationLength(prevLocation));
+                }
+
+                typeHeader.typeHeader.locations[index] = newLocation;
+                typeHeader.timestamps[index] = dataHeader.timeWritten;
+
+                // skip over the sectors, we know they're good
+                i += (long)sectorLength;
+                --i;
+                continue;
+            }
+        }
+
+        final IntOpenHashSet possibleTypes = new IntOpenHashSet(128);
+        possibleTypes.addAll(this.typeTranslationTable.keySet());
+        possibleTypes.addAll(this.typeHeaders.keySet());
+        possibleTypes.addAll(newTypeHeaders.keySet());
+
+        // search for external files
+        for (final IntIterator iterator = possibleTypes.iterator(); iterator.hasNext();) {
+            final int type = iterator.nextInt();
+            for (int localZ = 0; localZ < SECTION_SIZE; ++localZ) {
+                for (int localX = 0; localX < SECTION_SIZE; ++localX) {
+                    final File external = this.getExternalFile(localX, localZ, type);
+                    if (!external.isFile()) {
+                        continue;
+                    }
+
+                    final int index = getIndex(localX, localZ);
+
+                    // read header
+                    final DataHeader header;
+                    try (final BufferChoices scopedChoices = unscopedBufferChoices.scope();
+                        final FileChannel input = FileChannel.open(external.toPath(), StandardOpenOption.READ)) {
+                        final ByteBuffer buffer = scopedChoices.t16k().acquireDirectBuffer();
+
+                        buffer.limit(DataHeader.DATA_HEADER_LENGTH);
+                        buffer.position(0);
+
+                        input.read(buffer);
+
+                        buffer.flip();
+
+                        header = DataHeader.read(buffer);
+
+                        if (header == null) {
+                            // truncated
+                            LOGGER.warn("Deleting truncated external file '" + external.getAbsolutePath() + "'");
+                            external.delete();
+                            continue;
+                        }
+
+                        if (!DataHeader.validate(XXHASH64, buffer, 0)) {
+                            LOGGER.warn("Failed to verify header hash for external file '" + external.getAbsolutePath() + "'");
+                            continue;
+                        }
+                    } catch (final IOException ex) {
+                        LOGGER.warn("Failed to read header from external file '" + external.getAbsolutePath() + "'", ex);
+                        continue;
+                    }
+
+                    // verify the rest of the header
+
+                    if (type != ((int)header.typeId & 0xFF)) {
+                        LOGGER.warn("Mismatch of type and expected type for external file '" + external.getAbsolutePath() + "'");
+                        continue;
+                    }
+
+                    if (index != ((int)header.index & 0xFFFF)) {
+                        LOGGER.warn("Mismatch of index and expected index for external file '" + external.getAbsolutePath() + "'");
+                        continue;
+                    }
+
+                    if (external.length() != ((long)DataHeader.DATA_HEADER_LENGTH + (long)header.compressedSize)) {
+                        LOGGER.warn("Mismatch of filesize and compressed size for external file '" + external.getAbsolutePath() + "'");
+                        continue;
+                    }
+
+                    // we are mostly certain the data is valid, but need still to check the data hash
+                    // we can test the timestamp against current data before the expensive data hash operation though
+
+                    final TentativeTypeHeader typeHeader = newTypeHeaders.computeIfAbsent(type, (final int key) -> {
+                        return new TentativeTypeHeader();
+                    });
+
+                    final int prevLocation = typeHeader.typeHeader.locations[index];
+                    final long prevTimestamp = typeHeader.timestamps[index];
+
+                    if (prevLocation != ABSENT_LOCATION) {
+                        if ((header.timeWritten - prevTimestamp) <= 0L) {
+                            // this data is older, skip
+                            continue;
+                        }
+                    }
+
+                    // now we can test the hash, after verifying everything else is correct
+
+                    try {
+                        final Long externalHash = computeExternalHash(unscopedBufferChoices, external);
+                        if (externalHash == null || externalHash.longValue() != header.xxhash64Data) {
+                            LOGGER.warn("Failed to verify hash for external file '" + external.getAbsolutePath() + "'");
+                            continue;
+                        }
+                    } catch (final IOException ex) {
+                        LOGGER.warn("Failed to compute hash for external file '" + external.getAbsolutePath() + "'", ex);
+                        continue;
+                    }
+
+                    if (prevLocation != ABSENT_LOCATION && prevLocation != EXTERNAL_ALLOCATION_LOCATION) {
+                        newSectorAllocation.freeAllocation(getLocationOffset(prevLocation), getLocationLength(prevLocation));
+                    }
+
+                    typeHeader.typeHeader.locations[index] = EXTERNAL_ALLOCATION_LOCATION;
+                    typeHeader.timestamps[index] = header.timeWritten;
+                }
+            }
+        }
+
+        // now we can build the new headers
+        final Int2ObjectLinkedOpenHashMap<TypeHeader> newHeaders = new Int2ObjectLinkedOpenHashMap<>(newTypeHeaders.size());
+        final FileHeader newFileHeader = new FileHeader();
+
+        for (final Iterator<Int2ObjectMap.Entry<TentativeTypeHeader>> iterator = newTypeHeaders.int2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
+            final Int2ObjectMap.Entry<TentativeTypeHeader> entry = iterator.next();
+
+            final int type = entry.getIntKey();
+            final TentativeTypeHeader tentativeTypeHeader = entry.getValue();
+
+            boolean hasData = false;
+            for (final int location : tentativeTypeHeader.typeHeader.locations) {
+                if (location != ABSENT_LOCATION) {
+                    hasData = true;
+                    break;
+                }
+            }
+
+            if (!hasData) {
+                continue;
+            }
+
+            final int sectorOffset = newSectorAllocation.allocate(TYPE_HEADER_SECTORS, false);
+            if (sectorOffset < 0) {
+                throw new IllegalStateException("Failed to allocate type header");
+            }
+
+            newHeaders.put(type, tentativeTypeHeader.typeHeader);
+            newFileHeader.typeHeaderOffsets[type] = sectorOffset;
+            // hash will be computed later by writeTypeHeader
+        }
+
+        // now print the changes we're about to make
+        if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+            LOGGER.info("Summarizing header changes for sectorfile " + this.file.getAbsolutePath());
+        }
+
+        boolean changes = false;
+
+        // make sure to use the tentative type headers, in case the tentative header was not allocated due to being empty
+        for (final Iterator<Int2ObjectMap.Entry<TentativeTypeHeader>> iterator = newTypeHeaders.int2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
+            final Int2ObjectMap.Entry<TentativeTypeHeader> entry = iterator.next();
+            final int type = entry.getIntKey();
+            final TypeHeader newTypeHeader = entry.getValue().typeHeader;
+            final TypeHeader oldTypeHeader = this.typeHeaders.get(type);
+
+            boolean hasChanges;
+            if (oldTypeHeader == null) {
+                hasChanges = false;
+                final int[] test = newTypeHeader.locations;
+                for (int i = 0; i < test.length; ++i) {
+                    if (test[i] != ABSENT_LOCATION) {
+                        hasChanges = true;
+                        break;
+                    }
+                }
+            } else {
+                hasChanges = !Arrays.equals(oldTypeHeader.locations, newTypeHeader.locations);
+            }
+
+            if (!hasChanges) {
+                // make logs easier to read by only logging one line if there are no changes
+                if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                    LOGGER.info("No changes for type " + this.debugType(type) + " in sectorfile " + this.file.getAbsolutePath());
+                }
+                continue;
+            }
+
+            if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                LOGGER.info("Changes for type " + this.debugType(type) + " in sectorfile '" + this.file.getAbsolutePath() + "':");
+            }
+
+            for (int localZ = 0; localZ < SECTION_SIZE; ++localZ) {
+                for (int localX = 0; localX < SECTION_SIZE; ++localX) {
+                    final int index = getIndex(localX, localZ);
+
+                    final int oldLocation = oldTypeHeader == null ? ABSENT_LOCATION : oldTypeHeader.locations[index];
+                    final int newLocation = newTypeHeader.locations[index];
+
+                    if (oldLocation == newLocation) {
+                        continue;
+                    }
+
+                    changes = true;
+
+                    if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                        if (oldLocation == ABSENT_LOCATION) {
+                            // found new data
+                            LOGGER.info("Found missing data for " + this.debugType(type) + " located at " + this.getAbsoluteCoordinate(index) + " in sectorfile " + this.file.getAbsolutePath());
+                        } else if (newLocation == ABSENT_LOCATION) {
+                            // lost data
+                            LOGGER.warn("Failed to find data for " + this.debugType(type) + " located at " + this.getAbsoluteCoordinate(index) + " in sectorfile " + this.file.getAbsolutePath());
+                        } else {
+                            // changed to last correct data
+                            LOGGER.info("Replaced with last good data for " + this.debugType(type) + " located at " + this.getAbsoluteCoordinate(index) + " in sectorfile " + this.file.getAbsolutePath());
+                        }
+                    }
+                }
+            }
+
+            if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                LOGGER.info("End of changes for type " + this.debugType(type) + " in sectorfile " + this.file.getAbsolutePath());
+            }
+        }
+        if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+            LOGGER.info("End of changes for sectorfile " + this.file.getAbsolutePath());
+        }
+
+        // replace-in memory
+        this.typeHeaders.clear();
+        this.typeHeaders.putAll(newHeaders);
+        this.fileHeader.copyFrom(newFileHeader);
+        this.sectorAllocator.copyAllocations(newSectorAllocation);
+
+        // write to disk
+        try {
+            // first, the type headers
+            for (final IntIterator iterator = newHeaders.keySet().iterator(); iterator.hasNext();) {
+                final int type = iterator.nextInt();
+                try (final BufferChoices headerBuffers = unscopedBufferChoices.scope()) {
+                    try {
+                        this.writeTypeHeader(headerBuffers.t16k().acquireDirectBuffer(), type, true, false);
+                    } catch (final IOException ex) {
+                        // to ensure we update all the type header hashes, we need call writeTypeHeader for all type headers
+                        // so, we need to catch any IO errors here
+                        LOGGER.error("Failed to write type header " + this.debugType(type) + " to disk for sectorfile " + this.file.getAbsolutePath(), ex);
+                    }
+                }
+            }
+
+            // then we can write the main header
+            try (final BufferChoices headerBuffers = unscopedBufferChoices.scope()) {
+                this.writeFileHeader(headerBuffers.t16k().acquireDirectBuffer());
+            }
+
+            if ((flags & RECALCULATE_FLAGS_NO_LOG) == 0) {
+                LOGGER.info("Successfully wrote new headers to disk for sectorfile " + this.file.getAbsolutePath());
+            }
+        } catch (final IOException ex) {
+            LOGGER.error("Failed to write new headers to disk for sectorfile " + this.file.getAbsolutePath(), ex);
+        }
+
+        return changes;
+    }
+
+    private String getAbsoluteCoordinate(final int index) {
+        return this.getAbsoluteCoordinate(getLocalX(index), getLocalZ(index));
+    }
+
+    private String getAbsoluteCoordinate(final int localX, final int localZ) {
+        return "(" + (localX | (this.sectionX << SECTION_SHIFT)) + "," + (localZ | (this.sectionZ << SECTION_SHIFT)) + ")";
+    }
+
+    private void write(final ByteBuffer buffer, long position) throws IOException {
+        int len = buffer.remaining();
+        while (len > 0) {
+            final int written = this.channel.write(buffer, position);
+            len -= written;
+            position += (long)written;
+        }
+    }
+
+    private void writeFileHeader(final ByteBuffer ioBuffer) throws IOException {
+        ioBuffer.limit(FileHeader.FILE_HEADER_SIZE_BYTES);
+        ioBuffer.position(0);
+
+        this.fileHeader.write(ioBuffer.duplicate());
+
+        this.write(ioBuffer, (long)FILE_HEADER_SECTOR << SECTOR_SHIFT);
+    }
+
+    private void readFileHeader(final BufferChoices unscopedBufferChoices) throws IOException {
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer buffer = scopedBufferChoices.t16k().acquireDirectBuffer();
+
+            // reset sector allocations + headers for debug/testing
+            this.sectorAllocator.copyAllocations(newSectorAllocator());
+            this.typeHeaders.clear();
+            this.fileHeader.reset();
+
+            buffer.limit(FileHeader.FILE_HEADER_SIZE_BYTES);
+            buffer.position(0);
+
+            final long fileLengthSectors = (this.channel.size() + (SECTOR_SIZE - 1L)) >> SECTOR_SHIFT;
+
+            int read = this.channel.read(buffer, (long)FILE_HEADER_SECTOR << SECTOR_SHIFT);
+
+            if (read != buffer.limit()) {
+                LOGGER.warn("File '" + this.file.getAbsolutePath() + "' has a truncated file header");
+                // File is truncated
+                // All headers will initialise to default
+                return;
+            }
+
+            buffer.position(0);
+
+            if (!FileHeader.validate(buffer, 0)) {
+                LOGGER.warn("File '" + this.file.getAbsolutePath() + "' has file header with hash mismatch");
+                if (!this.readOnly) {
+                    this.recalculateFile(unscopedBufferChoices, 0);
+                    return;
+                } // else: in read-only mode, try to parse the header still
+            }
+
+            FileHeader.read(buffer, this.fileHeader);
+
+            // delay recalculation so that the logs contain all errors found
+            boolean needsRecalculation = false;
+
+            // try to allocate space for written type headers
+            for (int i = 0; i < MAX_TYPES; ++i) {
+                final int typeHeaderOffset = this.fileHeader.typeHeaderOffsets[i];
+                if (typeHeaderOffset == ABSENT_TYPE_HEADER_OFFSET) {
+                    // no data
+                    continue;
+                }
+                // note: only the type headers can bypass the max limit, as the max limit is determined by SECTOR_OFFSET_BITS
+                //       but the type offset is full 31 bits
+                if (typeHeaderOffset < 0 || !this.sectorAllocator.tryAllocateDirect(typeHeaderOffset, TYPE_HEADER_SECTORS, true)) {
+                    LOGGER.error("File '" + this.file.getAbsolutePath() + "' has bad or overlapping offset for type header " + this.debugType(i) + ": " + typeHeaderOffset);
+                    needsRecalculation = true;
+                    continue;
+                }
+
+                if (!this.typeTranslationTable.containsKey(i)) {
+                    LOGGER.warn("File '" + this.file.getAbsolutePath() + "' has an unknown type header: " + i);
+                }
+
+                // parse header
+                buffer.position(0);
+                buffer.limit(TypeHeader.TYPE_HEADER_SIZE_BYTES);
+                read = this.channel.read(buffer, (long)typeHeaderOffset << SECTOR_SHIFT);
+
+                if (read != buffer.limit()) {
+                    LOGGER.error("File '" + this.file.getAbsolutePath() + "' has type header " + this.debugType(i) + " pointing to outside of file: " + typeHeaderOffset);
+                    needsRecalculation = true;
+                    continue;
+                }
+
+                final long expectedHash = this.fileHeader.xxHash64TypeHeader[i];
+                final long gotHash = TypeHeader.computeHash(buffer, 0);
+
+                if (expectedHash != gotHash) {
+                    LOGGER.error("File '" + this.file.getAbsolutePath() + "' has type header " + this.debugType(i) + " with a mismatched hash");
+                    needsRecalculation = true;
+                    if (!this.readOnly) {
+                        continue;
+                    } // else: in read-only mode, try to parse the type header still
+                }
+
+                final TypeHeader typeHeader = TypeHeader.read(buffer.flip());
+
+                final int[] locations = typeHeader.locations;
+
+                // here, we now will try to allocate space for the data in the type header
+                // we need to do it even if we don't know what type we're dealing with
+                for (int k = 0; k < locations.length; ++k) {
+                    final int location = locations[k];
+                    if (location == ABSENT_LOCATION || location == EXTERNAL_ALLOCATION_LOCATION) {
+                        // no data or it is on the external file
+                        continue;
+                    }
+
+                    final int locationOffset = getLocationOffset(location);
+                    final int locationLength = getLocationLength(location);
+
+                    if (locationOffset < 0) {
+                        LOGGER.error("File '" + this.file.getAbsolutePath() + "' has negative (o:" + locationOffset + ",l:" + locationLength + ") sector offset for type " + this.debugType(i) + " located at " + this.getAbsoluteCoordinate(k));
+                        needsRecalculation = true;
+                        continue;
+                    } else if (locationLength <= 0) {
+                        LOGGER.error("File '" + this.file.getAbsolutePath() + "' has negative (o:" + locationOffset + ",l:" + locationLength + ") length for type " + this.debugType(i) + " located at " + this.getAbsoluteCoordinate(k));
+                        needsRecalculation = true;
+                        continue;
+                    } else if ((locationOffset + locationLength) > fileLengthSectors || (locationOffset + locationLength) < 0) {
+                        LOGGER.error("File '" + this.file.getAbsolutePath() + "' has sector allocation (o:" + locationOffset + ",l:" + locationLength + ") pointing outside file for type " + this.debugType(i) + " located at " + this.getAbsoluteCoordinate(k));
+                        needsRecalculation = true;
+                        continue;
+                    } else if (!this.sectorAllocator.tryAllocateDirect(locationOffset, locationLength, false)) {
+                        LOGGER.error("File '" + this.file.getAbsolutePath() + "' has overlapping sector allocation (o:" + locationOffset + ",l:" + locationLength + ") for type " + this.debugType(i) + " located at " + this.getAbsoluteCoordinate(k));
+                        needsRecalculation = true;
+                        continue;
+                    }
+                }
+
+                this.typeHeaders.put(i, typeHeader);
+            }
+
+            if (needsRecalculation) {
+                this.recalculateFile(unscopedBufferChoices, 0);
+                return;
+            }
+
+            return;
+        }
+    }
+
+    private void writeTypeHeader(final ByteBuffer buffer, final int type, final boolean updateTypeHeaderHash,
+                                 final boolean writeFileHeader) throws IOException {
+        final TypeHeader headerData = this.typeHeaders.get(type);
+        if (headerData == null) {
+            throw new IllegalStateException("Unhandled type: " + type);
+        }
+
+        if (writeFileHeader & !updateTypeHeaderHash) {
+            throw new IllegalArgumentException("Cannot write file header without updating type header hash");
+        }
+
+        final int offset = this.fileHeader.typeHeaderOffsets[type];
+
+        buffer.position(0);
+        buffer.limit(TypeHeader.TYPE_HEADER_SIZE_BYTES);
+
+        headerData.write(buffer.duplicate());
+
+        final long hash;
+        if (updateTypeHeaderHash) {
+            hash = TypeHeader.computeHash(buffer, 0);
+            this.fileHeader.xxHash64TypeHeader[type] = hash;
+        }
+
+        this.write(buffer, (long)offset << SECTOR_SHIFT);
+
+        if (writeFileHeader) {
+            this.writeFileHeader(buffer);
+        }
+    }
+
+    private void updateAndWriteTypeHeader(final ByteBuffer ioBuffer, final int type, final int index, final int to) throws IOException {
+        final TypeHeader headerData = this.typeHeaders.get(type);
+        if (headerData == null) {
+            throw new IllegalStateException("Unhandled type: " + type);
+        }
+
+        headerData.locations[index] = to;
+
+        this.writeTypeHeader(ioBuffer, type, true, true);
+    }
+
+    private void deleteExternalFile(final int localX, final int localZ, final int type) throws IOException {
+        // use deleteIfExists for error reporting
+        Files.deleteIfExists(this.getExternalFile(localX, localZ, type).toPath());
+    }
+
+    private File getExternalFile(final int localX, final int localZ, final int type) {
+        return new File(this.file.getParentFile(), getExternalFileName(this.sectionX, this.sectionZ, localX, localZ, type));
+    }
+
+    private File getExternalTempFile(final int localX, final int localZ, final int type) {
+        return new File(this.file.getParentFile(), getExternalTempFileName(this.sectionX, this.sectionZ, localX, localZ, type));
+    }
+
+    public static Long computeExternalHash(final BufferChoices unscopedBufferChoices, final File externalFile) throws IOException {
+        if (!externalFile.isFile() || externalFile.length() < (long)DataHeader.DATA_HEADER_LENGTH) {
+            return null;
+        }
+
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope();
+             final StreamingXXHash64 streamingXXHash64 = XXHASH_JAVA_FACTORY.newStreamingHash64(XXHASH_SEED);
+             final InputStream fileInput = Files.newInputStream(externalFile.toPath(), StandardOpenOption.READ)) {
+            final byte[] bytes = scopedBufferChoices.t16k().acquireJavaBuffer();
+
+            // first, skip header
+            try {
+                fileInput.skipNBytes((long)DataHeader.DATA_HEADER_LENGTH);
+            } catch (final EOFException ex) {
+                return null;
+            }
+
+            int r;
+            while ((r = fileInput.read(bytes)) >= 0) {
+                streamingXXHash64.update(bytes, 0, r);
+            }
+
+            return streamingXXHash64.getValue();
+        }
+    }
+
+    public static final int READ_FLAG_CHECK_HEADER_HASH          = 1 << 0;
+    public static final int READ_FLAG_CHECK_INTERNAL_DATA_HASH   = 1 << 1;
+    public static final int READ_FLAG_CHECK_EXTERNAL_DATA_HASH   = 1 << 2;
+
+    // do not check external data hash, there is not much we can do if it is actually bad
+    public static final int RECOMMENDED_READ_FLAGS = READ_FLAG_CHECK_HEADER_HASH | READ_FLAG_CHECK_INTERNAL_DATA_HASH;
+    // checks external hash additionally, which requires a separate full file read
+    public static final int FULL_VALIDATION_FLAGS = READ_FLAG_CHECK_HEADER_HASH | READ_FLAG_CHECK_INTERNAL_DATA_HASH | READ_FLAG_CHECK_EXTERNAL_DATA_HASH;
+
+    public boolean hasData(final int localX, final int localZ, final int type) {
+        if (localX < 0 || localX > SECTION_MASK) {
+            throw new IllegalArgumentException("X-coordinate out of range");
+        }
+        if (localZ < 0 || localZ > SECTION_MASK) {
+            throw new IllegalArgumentException("Z-coordinate out of range");
+        }
+
+        final TypeHeader typeHeader = this.typeHeaders.get(type);
+
+        if (typeHeader == null) {
+            this.checkHeaderExists(type);
+            return false;
+        }
+
+        final int index = getIndex(localX, localZ);
+        final int location = typeHeader.locations[index];
+
+        return location != ABSENT_LOCATION;
+    }
+
+    public static record SectorFileInput(DataInputStream data, boolean external) {}
+
+    private static final SectorFileInput NULL_DATA = new SectorFileInput(null, false);
+
+    public SectorFileInput read(final BufferChoices scopedBufferChoices, final int localX, final int localZ, final int type, final int readFlags) throws IOException {
+        return this.read(scopedBufferChoices, scopedBufferChoices.t1m().acquireDirectBuffer(), localX, localZ, type, readFlags);
+    }
+
+    private SectorFileInput tryRecalculate(final String reason, final BufferChoices scopedBufferChoices, final ByteBuffer buffer, final int localX, final int localZ, final int type, final int readFlags) throws IOException {
+        LOGGER.error("File '" + this.file.getAbsolutePath() + "' has error at data for type " + this.debugType(type) + " located at " + this.getAbsoluteCoordinate(getIndex(localX, localZ)) + ": " + reason);
+        // attribute error to bad header data, which we can re-calculate and re-try
+        if (this.readOnly) {
+            // cannot re-calculate, so we can only return null
+            return NULL_DATA;
+        }
+        this.recalculateFile(scopedBufferChoices, 0);
+        // recalculate ensures valid data, so there will be no recursion
+        return this.read(scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+    }
+
+    private SectorFileInput read(final BufferChoices scopedBufferChoices, final ByteBuffer buffer, final int localX, final int localZ, final int type, final int readFlags) throws IOException {
+        if (localX < 0 || localX > SECTION_MASK) {
+            throw new IllegalArgumentException("X-coordinate out of range");
+        }
+        if (localZ < 0 || localZ > SECTION_MASK) {
+            throw new IllegalArgumentException("Z-coordinate out of range");
+        }
+
+        if (buffer.capacity() < MAX_INTERNAL_ALLOCATION_BYTES) {
+            throw new IllegalArgumentException("Buffer size must be at least " + MAX_INTERNAL_ALLOCATION_BYTES + " bytes");
+        }
+
+        buffer.limit(buffer.capacity());
+        buffer.position(0);
+
+        final TypeHeader typeHeader = this.typeHeaders.get(type);
+
+        if (typeHeader == null) {
+            this.checkHeaderExists(type);
+            return NULL_DATA;
+        }
+
+        final int index = getIndex(localX, localZ);
+
+        final int location = typeHeader.locations[index];
+
+        if (location == ABSENT_LOCATION) {
+            return NULL_DATA;
+        }
+
+        final boolean external = location == EXTERNAL_ALLOCATION_LOCATION;
+
+        final ByteBufferInputStream rawIn;
+        final File externalFile;
+        if (external) {
+            externalFile = this.getExternalFile(localX, localZ, type);
+
+            rawIn = new BufferedFileChannelInputStream(buffer, externalFile);
+        } else {
+            externalFile = null;
+
+            final int offset = getLocationOffset(location);
+            final int length = getLocationLength(location);
+
+            buffer.limit(length << SECTOR_SHIFT);
+            this.channel.read(buffer, (long)offset << SECTOR_SHIFT);
+            buffer.flip();
+
+            rawIn = new ByteBufferInputStream(buffer);
+        }
+
+        final DataHeader dataHeader = DataHeader.read(rawIn);
+
+        if (dataHeader == null) {
+            rawIn.close();
+            return this.tryRecalculate("truncated " + (external ? "external" : "internal") + " data header", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+        }
+
+        if ((readFlags & READ_FLAG_CHECK_HEADER_HASH) != 0) {
+            if (!DataHeader.validate(XXHASH64, buffer, 0)) {
+                rawIn.close();
+                return this.tryRecalculate("mismatch of " + (external ? "external" : "internal") + " data header hash", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+            }
+        }
+
+        if ((int)(dataHeader.typeId & 0xFF) != type) {
+            rawIn.close();
+            return this.tryRecalculate("mismatch of expected type and data header type", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+        }
+
+        if (((int)dataHeader.index & 0xFFFF) != index) {
+            rawIn.close();
+            return this.tryRecalculate("mismatch of expected coordinates and data header coordinates", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+        }
+
+        // this is accurate for our implementations of BufferedFileChannelInputStream / ByteBufferInputStream
+        final int bytesAvailable = rawIn.available();
+
+        if (external) {
+            // for external files, the remaining size should exactly match the compressed size
+            if (bytesAvailable != dataHeader.compressedSize) {
+                rawIn.close();
+                return this.tryRecalculate("mismatch of external size and data header size", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+            }
+        } else {
+            // for non-external files, the remaining size should be >= compressed size AND the
+            // compressed size should be on the same sector
+            if (bytesAvailable < dataHeader.compressedSize || ((bytesAvailable + DataHeader.DATA_HEADER_LENGTH + (SECTOR_SIZE - 1)) >>> SECTOR_SHIFT) != ((dataHeader.compressedSize + DataHeader.DATA_HEADER_LENGTH + (SECTOR_SIZE - 1)) >>> SECTOR_SHIFT)) {
+                rawIn.close();
+                return this.tryRecalculate("mismatch of internal size and data header size", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+            }
+            // adjust max buffer to prevent reading over
+            buffer.limit(buffer.position() + dataHeader.compressedSize);
+            if (rawIn.available() != dataHeader.compressedSize) {
+                // should not be possible
+                rawIn.close();
+                throw new IllegalStateException();
+            }
+        }
+
+        final byte compressType = dataHeader.compressionType;
+        final SectorFileCompressionType compressionType = SectorFileCompressionType.getById((int)compressType & 0xFF);
+        if (compressionType == null) {
+            LOGGER.error("File '" + this.file.getAbsolutePath() + "' has unrecognized compression type for data type " + this.debugType(type) + " located at " + this.getAbsoluteCoordinate(index));
+            // recalculate will not clobber data types if the compression is unrecognized, so we can only return null here
+            rawIn.close();
+            return NULL_DATA;
+        }
+
+        if (!external && (readFlags & READ_FLAG_CHECK_INTERNAL_DATA_HASH) != 0) {
+            final long expectedHash = XXHASH64.hash(buffer, buffer.position(), dataHeader.compressedSize, XXHASH_SEED);
+            if (expectedHash != dataHeader.xxhash64Data) {
+                rawIn.close();
+                return this.tryRecalculate("mismatch of internal data hash and data header hash", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+            }
+        } else if (external && (readFlags & READ_FLAG_CHECK_EXTERNAL_DATA_HASH) != 0) {
+            final Long externalHash = computeExternalHash(scopedBufferChoices, externalFile);
+            if (externalHash == null || externalHash.longValue() != dataHeader.xxhash64Data) {
+                rawIn.close();
+                return this.tryRecalculate("mismatch of external data hash and data header hash", scopedBufferChoices, buffer, localX, localZ, type, readFlags);
+            }
+        }
+
+        return new SectorFileInput(new DataInputStream(compressionType.createInput(scopedBufferChoices, rawIn)), external);
+    }
+
+    public boolean delete(final BufferChoices unscopedBufferChoices, final int localX, final int localZ, final int type) throws IOException {
+        if (localX < 0 || localX > SECTION_MASK) {
+            throw new IllegalArgumentException("X-coordinate out of range");
+        }
+        if (localZ < 0 || localZ > SECTION_MASK) {
+            throw new IllegalArgumentException("Z-coordinate out of range");
+        }
+
+        if (this.readOnly) {
+            throw new UnsupportedOperationException("Sectorfile is read-only");
+        }
+
+        final TypeHeader typeHeader = this.typeHeaders.get(type);
+
+        if (typeHeader == null) {
+            this.checkHeaderExists(type);
+            return false;
+        }
+
+        final int index = getIndex(localX, localZ);
+        final int location = typeHeader.locations[index];
+
+        if (location == ABSENT_LOCATION) {
+            return false;
+        }
+
+        // whether the location is external or internal, we delete from the type header before attempting anything else
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            this.updateAndWriteTypeHeader(scopedBufferChoices.t16k().acquireDirectBuffer(), type, index, ABSENT_LOCATION);
+        }
+
+        // only proceed to try to delete sector allocation or external file if we succeed in deleting the type header entry
+
+        if (location == EXTERNAL_ALLOCATION_LOCATION) {
+            // only try to delete if the header write may succeed
+            this.deleteExternalFile(localX, localZ, type);
+
+            // no sector allocation to free
+
+            return true;
+        } else {
+            final int offset = getLocationOffset(location);
+            final int length = getLocationLength(location);
+
+            this.sectorAllocator.freeAllocation(offset, length);
+
+            return true;
+        }
+    }
+
+    // performs a sync as if the sync flag is used for creating the sectorfile
+    public static final int WRITE_FLAG_SYNC         = 1 << 0;
+    // avoid compressing data, but store target compression type (use if the data is already compressed)
+    public static final int WRITE_FLAG_RAW          = 1 << 1;
+
+    public static record SectorFileOutput(
+            /* Must run save (before close()) to cause the data to be written to the file, close() will not do this */
+            SectorFileOutputStream rawOutput,
+            /* Close is required to run on the outputstream to free resources, but will not commit the data */
+            DataOutputStream outputStream
+    ) {}
+
+    public SectorFileOutput write(final BufferChoices scopedBufferChoices, final int localX, final int localZ, final int type,
+                                  final SectorFileCompressionType forceCompressionType, final int writeFlags) throws IOException {
+        if (this.readOnly) {
+            throw new UnsupportedOperationException("Sectorfile is read-only");
+        }
+
+        if (!this.typeHeaders.containsKey(type) && !this.typeTranslationTable.containsKey(type)) {
+            throw new IllegalArgumentException("Unknown type " + type);
+        }
+
+        final SectorFileCompressionType useCompressionType = forceCompressionType == null ? this.compressionType : forceCompressionType;
+
+        final SectorFileOutputStream output = new SectorFileOutputStream(
+                scopedBufferChoices, localX, localZ, type, useCompressionType, writeFlags
+        );
+        final OutputStream compressedOut = (writeFlags & WRITE_FLAG_RAW) != 0 ? output : useCompressionType.createOutput(scopedBufferChoices, output);
+
+        return new SectorFileOutput(output, new DataOutputStream(compressedOut));
+    }
+
+    // expect buffer to be flipped (pos = 0, lim = written data) AND for the buffer to have the first DATA_HEADER_LENGTH
+    // allocated to the header
+    private void writeInternal(final BufferChoices unscopedBufferChoices, final ByteBuffer buffer, final int localX,
+                               final int localZ, final int type, final long dataHash,
+                               final SectorFileCompressionType compressionType, final int writeFlags) throws IOException {
+        final int totalSize = buffer.limit();
+        final int compressedSize = totalSize - DataHeader.DATA_HEADER_LENGTH;
+
+        final int index = getIndex(localX, localZ);
+
+        DataHeader.storeHeader(
+            buffer.duplicate(), XXHASH64, dataHash, System.currentTimeMillis(), compressedSize,
+            (short)index, (byte)type, (byte)compressionType.getId()
+        );
+
+        final int requiredSectors = (totalSize + (SECTOR_SIZE - 1)) >> SECTOR_SHIFT;
+
+        if (requiredSectors > MAX_NORMAL_SECTOR_LENGTH) {
+            throw new IllegalArgumentException("Provided data is too large for internal write");
+        }
+
+        // allocate new space, write to it, and only after that is successful free the old allocation if it exists
+
+        final int sectorToStore = this.sectorAllocator.allocate(requiredSectors, true);
+        if (sectorToStore < 0) {
+            // no space left in this file, so we need to make an external allocation
+
+            final File externalTmp = this.getExternalTempFile(localX, localZ, type);
+            LOGGER.error("Ran out of space in sectorfile '" + this.file.getAbsolutePath() + "', storing data externally to " + externalTmp.getAbsolutePath());
+            Files.deleteIfExists(externalTmp.toPath());
+
+            final FileChannel channel = FileChannel.open(externalTmp.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
+            try {
+                // just need to dump the buffer to the file
+                final ByteBuffer bufferDuplicate = buffer.duplicate();
+                while (bufferDuplicate.hasRemaining()) {
+                    channel.write(bufferDuplicate);
+                }
+
+                // this call will write the header again, but that's fine - it's the same data
+                this.finishExternalWrite(
+                        unscopedBufferChoices, channel, externalTmp, compressedSize, localX, localZ,
+                        type, dataHash, compressionType, writeFlags
+                );
+            } finally {
+                channel.close();
+                Files.deleteIfExists(externalTmp.toPath());
+            }
+
+            return;
+        }
+
+        // write data to allocated space
+        this.write(buffer, (long)sectorToStore << SECTOR_SHIFT);
+
+        final int prevLocation = this.typeHeaders.get(type).locations[index];
+
+        // update header on disk
+        final int newLocation = makeLocation(sectorToStore, requiredSectors);
+
+        try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+            this.updateAndWriteTypeHeader(scopedBufferChoices.t16k().acquireDirectBuffer(), type, index, newLocation);
+        }
+
+        // force disk updates if required
+        if (!this.sync && (writeFlags & WRITE_FLAG_SYNC) != 0) {
+            this.channel.force(false);
+        }
+
+        // finally, now we are certain there are no references to the prev location, we can de-allocate
+        if (prevLocation != ABSENT_LOCATION) {
+            if (prevLocation == EXTERNAL_ALLOCATION_LOCATION) {
+                // de-allocation is done by deleting external file
+                this.deleteExternalFile(localX, localZ, type);
+            } else {
+                // just need to free the sector allocation
+                this.sectorAllocator.freeAllocation(getLocationOffset(prevLocation), getLocationLength(prevLocation));
+            }
+        } // else: nothing to free
+    }
+
+    private void finishExternalWrite(final BufferChoices unscopedBufferChoices, final FileChannel channel, final File externalTmp,
+                                     final int compressedSize, final int localX, final int localZ, final int type, final long dataHash,
+                                     final SectorFileCompressionType compressionType, final int writeFlags) throws IOException {
+        final int index = getIndex(localX, localZ);
+
+        // update header for external file
+        try (final BufferChoices headerChoices = unscopedBufferChoices.scope()) {
+            final ByteBuffer buffer = headerChoices.t16k().acquireDirectBuffer();
+
+            buffer.limit(DataHeader.DATA_HEADER_LENGTH);
+            buffer.position(0);
+
+            DataHeader.storeHeader(
+                    buffer.duplicate(), XXHASH64, dataHash, System.currentTimeMillis(), compressedSize,
+                    (short)index, (byte)type, (byte)compressionType.getId()
+            );
+
+            int offset = 0;
+            while (buffer.hasRemaining()) {
+                offset += channel.write(buffer, (long)offset);
+            }
+        }
+
+        // replace existing external file
+
+        final File external = this.getExternalFile(localX, localZ, type);
+
+        if (this.sync || (writeFlags & WRITE_FLAG_SYNC) != 0) {
+            channel.force(true);
+        }
+        channel.close();
+        try {
+            Files.move(externalTmp.toPath(), external.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+        } catch (final AtomicMoveNotSupportedException ex) {
+            Files.move(externalTmp.toPath(), external.toPath(), StandardCopyOption.REPLACE_EXISTING);
+        }
+
+        final int prevLocation = this.typeHeaders.get(type).locations[index];
+
+        // update header on disk if required
+
+        if (prevLocation != EXTERNAL_ALLOCATION_LOCATION) {
+            try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+                this.updateAndWriteTypeHeader(scopedBufferChoices.t16k().acquireDirectBuffer(), type, index, EXTERNAL_ALLOCATION_LOCATION);
+            }
+
+            // force disk updates if required
+            if (!this.sync && (writeFlags & WRITE_FLAG_SYNC) != 0) {
+                this.channel.force(false);
+            }
+        }
+
+        // finally, now we are certain there are no references to the prev location, we can de-allocate
+        if (prevLocation != ABSENT_LOCATION && prevLocation != EXTERNAL_ALLOCATION_LOCATION) {
+            this.sectorAllocator.freeAllocation(getLocationOffset(prevLocation), getLocationLength(prevLocation));
+        }
+
+        LOGGER.warn("Stored externally " + external.length() + " bytes for type " + this.debugType(type) + " to file " + external.getAbsolutePath());
+    }
+
+    public final class SectorFileOutputStream extends ByteBufferOutputStream {
+        private final BufferChoices scopedBufferChoices;
+
+        private File externalFile;
+        private FileChannel externalChannel;
+        private StreamingXXHash64 externalHash;
+        private int totalCompressedSize;
+
+        private final int localX;
+        private final int localZ;
+        private final int type;
+        private final SectorFileCompressionType compressionType;
+        private final int writeFlags;
+
+        private SectorFileOutputStream(final BufferChoices scopedBufferChoices,
+                                       final int localX, final int localZ, final int type,
+                                       final SectorFileCompressionType compressionType,
+                                       final int writeFlags) {
+            super(scopedBufferChoices.t1m().acquireDirectBuffer());
+            // we use a lower limit than capacity to force flush() to be invoked before
+            // the maximum internal size
+            this.buffer.limit(((MAX_NORMAL_SECTOR_LENGTH - 1) << SECTOR_SHIFT) + 1);
+            // make space for the header
+            for (int i = 0; i < DataHeader.DATA_HEADER_LENGTH; ++i) {
+                this.buffer.put(i, (byte)0);
+            }
+            this.buffer.position(DataHeader.DATA_HEADER_LENGTH);
+
+            this.scopedBufferChoices = scopedBufferChoices;
+
+            this.localX = localX;
+            this.localZ = localZ;
+            this.type = type;
+            this.compressionType = compressionType;
+            this.writeFlags = writeFlags;
+        }
+
+        public int getTotalCompressedSize() {
+            return this.totalCompressedSize + (this.buffer == null ? 0 : this.buffer.position());
+        }
+
+        @Override
+        protected ByteBuffer flush(final ByteBuffer current) throws IOException {
+            if (this.externalFile == null && current.hasRemaining()) {
+                return current;
+            }
+            if (current.position() == 0) {
+                // nothing to do
+                return current;
+            }
+
+            final boolean firstWrite = this.externalFile == null;
+
+            if (firstWrite) {
+                final File externalTmpFile = SectorFile.this.getExternalTempFile(this.localX, this.localZ, this.type);
+                LOGGER.warn("Storing external data at " + externalTmpFile.getAbsolutePath());
+                Files.deleteIfExists(externalTmpFile.toPath());
+
+                this.externalFile = externalTmpFile;
+                this.externalChannel = FileChannel.open(externalTmpFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
+                this.externalHash = XXHASH_JAVA_FACTORY.newStreamingHash64(XXHASH_SEED);
+            }
+
+            this.totalCompressedSize += (firstWrite ? current.position() - DataHeader.DATA_HEADER_LENGTH : current.position());
+
+            if (this.totalCompressedSize < 0 || this.totalCompressedSize >= (Integer.MAX_VALUE - DataHeader.DATA_HEADER_LENGTH)) {
+                // too large
+                throw new IOException("External file length exceeds integer maximum");
+            }
+
+            current.flip();
+
+            // update data hash
+            try (final BufferChoices hashChoices = this.scopedBufferChoices.scope()) {
+                final byte[] bytes = hashChoices.t16k().acquireJavaBuffer();
+
+                int offset = firstWrite ? DataHeader.DATA_HEADER_LENGTH : 0;
+                final int len = current.limit();
+
+                while (offset < len) {
+                    final int maxCopy = Math.min(len - offset, bytes.length);
+
+                    current.get(offset, bytes, 0, maxCopy);
+                    offset += maxCopy;
+
+                    this.externalHash.update(bytes, 0, maxCopy);
+                }
+            }
+
+            // update on disk
+            while (current.hasRemaining()) {
+                this.externalChannel.write(current);
+            }
+
+            current.limit(current.capacity());
+            current.position(0);
+            return current;
+        }
+
+        private void checkAndCreateTypeHeader() throws IOException {
+            if (SectorFile.this.typeHeaders.get(this.type) == null) {
+                SectorFile.this.createTypeHeader(this.type, this.scopedBufferChoices);
+            }
+        }
+
+        // assume flush() is called before this
+        private void save() throws IOException {
+            // lazily create type header
+            this.checkAndCreateTypeHeader();
+
+            if (this.externalFile == null) {
+                // avoid clobbering buffer positions/limits
+                final ByteBuffer buffer = this.buffer.duplicate();
+
+                buffer.flip();
+
+                final long dataHash = XXHASH64.hash(
+                        buffer, DataHeader.DATA_HEADER_LENGTH, buffer.remaining() - DataHeader.DATA_HEADER_LENGTH,
+                        XXHASH_SEED
+                );
+
+                SectorFile.this.writeInternal(
+                        this.scopedBufferChoices, buffer, this.localX, this.localZ,
+                        this.type, dataHash, this.compressionType, this.writeFlags
+                );
+            } else {
+                SectorFile.this.finishExternalWrite(
+                        this.scopedBufferChoices, this.externalChannel, this.externalFile, this.totalCompressedSize,
+                        this.localX, this.localZ, this.type, this.externalHash.getValue(), this.compressionType,
+                        this.writeFlags
+                );
+            }
+        }
+
+        public void freeResources() throws IOException {
+            if (this.externalHash != null) {
+                this.externalHash.close();
+                this.externalHash = null;
+            }
+            if (this.externalChannel != null) {
+                this.externalChannel.close();
+                this.externalChannel = null;
+            }
+            if (this.externalFile != null) {
+                // only deletes tmp file if we did not call save()
+                this.externalFile.delete();
+                this.externalFile = null;
+            }
+        }
+
+        @Override
+        public void close() throws IOException {
+            try {
+                this.flush();
+                this.save();
+            } finally {
+                try {
+                    super.close();
+                } finally {
+                    this.freeResources();
+                }
+            }
+        }
+    }
+
+    public void flush() throws IOException {
+        if (!this.channel.isOpen()) {
+            return;
+        }
+        if (!this.readOnly) {
+            if (this.sync) {
+                this.channel.force(true);
+            }
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (!this.channel.isOpen()) {
+            return;
+        }
+
+        try {
+            this.flush();
+        } finally {
+            this.channel.close();
+        }
+    }
+
+    public static final class SectorAllocator {
+
+        // smallest size first, then by lowest position in file
+        private final LongRBTreeSet freeBlocksBySize = new LongRBTreeSet((LongComparator)(final long a, final long b) -> {
+            final int sizeCompare = Integer.compare(getFreeBlockLength(a), getFreeBlockLength(b));
+            if (sizeCompare != 0) {
+                return sizeCompare;
+            }
+
+            return Integer.compare(getFreeBlockStart(a), getFreeBlockStart(b));
+        });
+
+        private final LongRBTreeSet freeBlocksByOffset = new LongRBTreeSet((LongComparator)(final long a, final long b) -> {
+            return Integer.compare(getFreeBlockStart(a), getFreeBlockStart(b));
+        });
+
+        private final int maxOffset; // inclusive
+        private final int maxLength; // inclusive
+
+        private static final int MAX_ALLOCATION = (Integer.MAX_VALUE >>> 1) + 1;
+        private static final int MAX_LENGTH = (Integer.MAX_VALUE >>> 1) + 1;
+
+        public SectorAllocator(final int maxOffset, final int maxLength) {
+            this.maxOffset = maxOffset;
+            this.maxLength = maxLength;
+
+            this.reset();
+        }
+
+        public void reset() {
+            this.freeBlocksBySize.clear();
+            this.freeBlocksByOffset.clear();
+
+            final long infiniteAllocation = makeFreeBlock(0, MAX_ALLOCATION);
+            this.freeBlocksBySize.add(infiniteAllocation);
+            this.freeBlocksByOffset.add(infiniteAllocation);
+        }
+
+        public void copyAllocations(final SectorAllocator other) {
+            this.freeBlocksBySize.clear();
+            this.freeBlocksBySize.addAll(other.freeBlocksBySize);
+
+            this.freeBlocksByOffset.clear();
+            this.freeBlocksByOffset.addAll(other.freeBlocksByOffset);
+        }
+
+        public int getLastAllocatedBlock() {
+            if (this.freeBlocksByOffset.isEmpty()) {
+                // entire space is allocated
+                return MAX_ALLOCATION - 1;
+            }
+
+            final long lastFreeBlock = this.freeBlocksByOffset.lastLong();
+            final int lastFreeStart = getFreeBlockStart(lastFreeBlock);
+            final int lastFreeEnd = lastFreeStart + getFreeBlockLength(lastFreeBlock) - 1;
+
+            if (lastFreeEnd == (MAX_ALLOCATION - 1)) {
+                // no allocations past this block, so the end must be before this block
+                // note: if lastFreeStart == 0, then we return - 1 which indicates no block has been allocated
+                return lastFreeStart - 1;
+            }
+            return MAX_ALLOCATION - 1;
+        }
+
+        private static long makeFreeBlock(final int start, final int length) {
+            return ((start & 0xFFFFFFFFL) | ((long)length << 32));
+        }
+
+        private static int getFreeBlockStart(final long freeBlock) {
+            return (int)freeBlock;
+        }
+
+        private static int getFreeBlockLength(final long freeBlock) {
+            return (int)(freeBlock >>> 32);
+        }
+
+        private void splitBlock(final long fromBlock, final int allocStart, final int allocEnd) {
+            // allocEnd is inclusive
+
+            // required to remove before adding again in case the split block's offset and/or length is the same
+            this.freeBlocksByOffset.remove(fromBlock);
+            this.freeBlocksBySize.remove(fromBlock);
+
+            final int fromStart = getFreeBlockStart(fromBlock);
+            final int fromEnd = fromStart + getFreeBlockLength(fromBlock) - 1;
+
+            if (fromStart != allocStart) {
+                // need to allocate free block to the left of the allocation
+                if (allocStart < fromStart) {
+                    throw new IllegalStateException();
+                }
+                final long leftBlock = makeFreeBlock(fromStart, allocStart - fromStart);
+                this.freeBlocksByOffset.add(leftBlock);
+                this.freeBlocksBySize.add(leftBlock);
+            }
+
+            if (fromEnd != allocEnd) {
+                // need to allocate free block to the right of the allocation
+                if (allocEnd > fromEnd) {
+                    throw new IllegalStateException();
+                }
+                // fromEnd - allocEnd = (fromEnd + 1) - (allocEnd + 1)
+                final long rightBlock = makeFreeBlock(allocEnd + 1, fromEnd - allocEnd);
+                this.freeBlocksByOffset.add(rightBlock);
+                this.freeBlocksBySize.add(rightBlock);
+            }
+        }
+
+        public boolean tryAllocateDirect(final int from, final int length, final boolean bypassMax) {
+            if (from < 0) {
+                throw new IllegalArgumentException("From must be >= 0");
+            }
+            if (length <= 0) {
+                throw new IllegalArgumentException("Length must be > 0");
+            }
+
+            final int end = from + length - 1; // inclusive
+
+            if (end < 0 || end >= MAX_ALLOCATION || length >= MAX_LENGTH) {
+                return false;
+            }
+
+            if (!bypassMax && (from > this.maxOffset || length > this.maxLength || end > this.maxOffset)) {
+                return false;
+            }
+
+            final LongBidirectionalIterator iterator = this.freeBlocksByOffset.iterator(makeFreeBlock(from, 0));
+            // iterator.next > curr
+            // iterator.prev <= curr
+
+            if (!iterator.hasPrevious()) {
+                // only free blocks starting at from+1, if any
+                return false;
+            }
+
+            final long block = iterator.previousLong();
+            final int blockStart = getFreeBlockStart(block);
+            final int blockLength = getFreeBlockLength(block);
+            final int blockEnd = blockStart + blockLength - 1; // inclusive
+
+            if (from > blockEnd || end > blockEnd) {
+                return false;
+            }
+
+            if (from < blockStart) {
+                throw new IllegalStateException();
+            }
+
+            this.splitBlock(block, from, end);
+
+            return true;
+        }
+
+        public void freeAllocation(final int from, final int length) {
+            if (from < 0) {
+                throw new IllegalArgumentException("From must be >= 0");
+            }
+            if (length <= 0) {
+                throw new IllegalArgumentException("Length must be > 0");
+            }
+
+            final int end = from + length - 1;
+            if (end < 0 || end >= MAX_ALLOCATION || length >= MAX_LENGTH) {
+                throw new IllegalArgumentException("End sector must be in allocation range");
+            }
+
+            final LongBidirectionalIterator iterator = this.freeBlocksByOffset.iterator(makeFreeBlock(from, length));
+            // iterator.next > curr
+            // iterator.prev <= curr
+
+            long prev = -1L;
+            int prevStart = 0;
+            int prevEnd = 0;
+
+            long next = -1L;
+            int nextStart = 0;
+            int nextEnd = 0;
+
+            if (iterator.hasPrevious()) {
+                prev = iterator.previousLong();
+                prevStart = getFreeBlockStart(prev);
+                prevEnd = prevStart + getFreeBlockLength(prev) - 1;
+                // advance back for next usage
+                iterator.nextLong();
+            }
+
+            if (iterator.hasNext()) {
+                next = iterator.nextLong();
+                nextStart = getFreeBlockStart(next);
+                nextEnd = nextStart + getFreeBlockLength(next) - 1;
+            }
+
+            // first, check that we are not trying to free area in another free block
+            if (prev != -1L) {
+                if (from <= prevEnd && end >= prevStart) {
+                    throw new IllegalArgumentException("free call overlaps with already free block");
+                }
+            }
+
+            if (next != -1L) {
+                if (from <= nextEnd && end >= nextStart) {
+                    throw new IllegalArgumentException("free call overlaps with already free block");
+                }
+            }
+
+            // try to merge with left & right free blocks
+            int adjustedStart = from;
+            int adjustedEnd = end;
+            if (prev != -1L && (prevEnd + 1) == from) {
+                adjustedStart = prevStart;
+                // delete merged block
+                this.freeBlocksByOffset.remove(prev);
+                this.freeBlocksBySize.remove(prev);
+            }
+
+            if (next != -1L && nextStart == (end + 1)) {
+                adjustedEnd = nextEnd;
+                // delete merged block
+                this.freeBlocksByOffset.remove(next);
+                this.freeBlocksBySize.remove(next);
+            }
+
+            final long block = makeFreeBlock(adjustedStart, adjustedEnd - adjustedStart + 1);
+            // add merged free block
+            this.freeBlocksByOffset.add(block);
+            this.freeBlocksBySize.add(block);
+        }
+
+        // returns -1 if the allocation cannot be done due to length/position limitations
+        public int allocate(final int length, final boolean checkMaxOffset) {
+            if (length <= 0) {
+                throw new IllegalArgumentException("Length must be > 0");
+            }
+            if (length > this.maxLength) {
+                return -1;
+            }
+
+            if (this.freeBlocksBySize.isEmpty()) {
+                return -1;
+            }
+
+            final LongBidirectionalIterator iterator = this.freeBlocksBySize.iterator(makeFreeBlock(-1, length));
+            // iterator.next > curr
+            // iterator.prev <= curr
+
+            // if we use start = -1, then no block retrieved is <= curr as offset < 0 is invalid. Then, the iterator next()
+            // returns >= makeFreeBlock(0, length) where the comparison is first by length then sector offset.
+            // Thus, we can just select next() as the block to split. This makes the allocation best-fit in that it selects
+            // first the smallest block that can fit the allocation, and that the smallest block selected offset is
+            // as close to 0 compared to the rest of the blocks at the same size
+
+            final long block = iterator.nextLong();
+            final int blockStart = getFreeBlockStart(block);
+
+            final int allocStart = blockStart;
+            final int allocEnd = blockStart + length - 1;
+
+            if (allocStart < 0) {
+                throw new IllegalStateException();
+            }
+
+            if (allocEnd < 0) {
+                // overflow
+                return -1;
+            }
+
+            // note: we do not need to worry about overflow in splitBlock because the free blocks are only allocated
+            // in [0, MAX_ALLOCATION - 1]
+
+            if (checkMaxOffset && (allocEnd > this.maxOffset)) {
+                return -1;
+            }
+
+            this.splitBlock(block, allocStart, allocEnd);
+
+            return blockStart;
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/SectorFileCache.java b/src/main/java/ca/spottedleaf/io/region/SectorFileCache.java
new file mode 100644
index 0000000000000000000000000000000000000000..99779c6f7cd8cbaf2c4642ffd4ba50a16a5bb5c3
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/SectorFileCache.java
@@ -0,0 +1,396 @@
+package ca.spottedleaf.io.region;
+
+import ca.spottedleaf.io.buffer.BufferChoices;
+import io.papermc.paper.util.CoordinateUtils;
+import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
+import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.nbt.NbtAccounter;
+import net.minecraft.nbt.NbtIo;
+import net.minecraft.nbt.StreamTagVisitor;
+import net.minecraft.util.ExceptionCollector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.CompletableFuture;
+
+public final class SectorFileCache implements AutoCloseable {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SectorFileCache.class);
+
+    private static final ThreadLocal<BufferChoices> BUFFER_CHOICES = ThreadLocal.withInitial(() -> BufferChoices.createNew(10));
+
+    public static BufferChoices getUnscopedBufferChoices() {
+        return BUFFER_CHOICES.get();
+    }
+
+    private final Long2ObjectLinkedOpenHashMap<SectorFile> sectorCache = new Long2ObjectLinkedOpenHashMap<>();
+    private final Long2ObjectLinkedOpenHashMap<CompletableFuture<SectorFile>> conversions = new Long2ObjectLinkedOpenHashMap<>();
+    private final File directory;
+    private final boolean sync;
+    private final SectorFileTracer tracer;
+
+    private static final int MAX_NON_EXISTING_CACHE = 1024 * 64;
+    private final LongLinkedOpenHashSet nonExistingSectorFiles = new LongLinkedOpenHashSet();
+
+    private boolean doesSectorFilePossiblyExist(final long position) {
+        synchronized (this.nonExistingSectorFiles) {
+            if (this.nonExistingSectorFiles.contains(position)) {
+                this.nonExistingSectorFiles.addAndMoveToFirst(position);
+                return false;
+            }
+            return true;
+        }
+    }
+
+    private void createSectorFile(final long position) {
+        synchronized (this.nonExistingSectorFiles) {
+            this.nonExistingSectorFiles.remove(position);
+        }
+    }
+
+    private void markNonExisting(final long position) {
+        synchronized (this.nonExistingSectorFiles) {
+            if (this.nonExistingSectorFiles.addAndMoveToFirst(position)) {
+                while (this.nonExistingSectorFiles.size() >= MAX_NON_EXISTING_CACHE) {
+                    this.nonExistingSectorFiles.removeLastLong();
+                }
+            }
+        }
+    }
+
+    public boolean doesSectorFileNotExistNoIO(final int chunkX, final int chunkZ) {
+        return !this.doesSectorFilePossiblyExist(CoordinateUtils.getChunkKey(chunkX, chunkZ));
+    }
+
+    public SectorFileCache(final File directory, final boolean sync) {
+        this.directory = directory;
+        this.sync = sync;
+        SectorFileTracer tracer = null;
+        try {
+            tracer = new SectorFileTracer(new File(directory.getParentFile(), "sectorfile.tracer"));
+        } catch (final IOException ex) {
+            LOGGER.error("Failed to start tracer", ex);
+        }
+        this.tracer = tracer;
+    }
+
+    public synchronized SectorFile getRegionFileIfLoaded(final int chunkX, final int chunkZ) {
+        return this.sectorCache.getAndMoveToFirst(CoordinateUtils.getChunkKey(chunkX >> SectorFile.SECTION_SHIFT, chunkZ >> SectorFile.SECTION_SHIFT));
+    }
+
+    public synchronized boolean chunkExists(final BufferChoices unscopedBufferChoices, final int chunkX, final int chunkZ, final int type) throws IOException {
+        final SectorFile sectorFile = this.getSectorFile(unscopedBufferChoices, chunkX, chunkZ, true);
+
+        return sectorFile != null && sectorFile.hasData(chunkX & SectorFile.SECTION_MASK, chunkZ & SectorFile.SECTION_MASK, type);
+    }
+
+    private static ca.spottedleaf.io.region.SectorFileCompressionType getCompressionType() {
+        return switch (io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.compressionFormat) {
+            case GZIP -> ca.spottedleaf.io.region.SectorFileCompressionType.GZIP;
+            case ZLIB -> ca.spottedleaf.io.region.SectorFileCompressionType.DEFLATE;
+            case NONE -> ca.spottedleaf.io.region.SectorFileCompressionType.NONE;
+            case LZ4 -> ca.spottedleaf.io.region.SectorFileCompressionType.LZ4;
+            case ZSTD -> ca.spottedleaf.io.region.SectorFileCompressionType.ZSTD;
+        };
+    }
+
+    public SectorFile getSectorFile(final BufferChoices unscopedBufferChoices, final int chunkX, final int chunkZ, boolean existingOnly) throws IOException {
+        final int sectionX = chunkX >> SectorFile.SECTION_SHIFT;
+        final int sectionZ = chunkZ >> SectorFile.SECTION_SHIFT;
+
+        final long sectionKey = CoordinateUtils.getChunkKey(sectionX, sectionZ);
+
+        final CompletableFuture<SectorFile> complete;
+        final CompletableFuture<SectorFile> existing;
+        synchronized (this) {
+            SectorFile ret = this.sectorCache.getAndMoveToFirst(sectionKey);
+            if (ret != null) {
+                return ret;
+            }
+
+            if (existingOnly && !this.doesSectorFilePossiblyExist(sectionKey)) {
+                return null;
+            }
+
+            final File file = new File(this.directory, SectorFile.getFileName(sectionX, sectionZ));
+
+            if (!this.conversions.containsKey(sectionKey) && file.isFile()) {
+                if (this.sectorCache.size() >= io.papermc.paper.configuration.GlobalConfiguration.get().misc.regionFileCacheSize) {
+                    final SectorFile sectorFile = this.sectorCache.removeLast();
+                    sectorFile.close();
+                    if (this.tracer != null) {
+                        this.tracer.add(new SectorFileTracer.FileEvent(SectorFileTracer.FileEventType.CLOSE, sectionX, sectionZ));
+                    }
+                }
+
+                if (this.tracer != null) {
+                    if (file.isFile()) {
+                        this.tracer.add(new SectorFileTracer.FileEvent(SectorFileTracer.FileEventType.OPEN, sectionX, sectionZ));
+                    } else {
+                        this.tracer.add(new SectorFileTracer.FileEvent(SectorFileTracer.FileEventType.CREATE, sectionX, sectionZ));
+                    }
+                }
+
+                this.createSectorFile(sectionKey);
+
+                this.directory.mkdirs();
+
+                ret = new SectorFile(
+                    file, sectionX, sectionZ, getCompressionType(), unscopedBufferChoices, MinecraftRegionFileType.getTranslationTable(),
+                    (this.sync ? SectorFile.OPEN_FLAGS_SYNC_WRITES : 0)
+                );
+
+                this.sectorCache.putAndMoveToFirst(sectionKey, ret);
+
+                return ret;
+            }
+
+            // try to convert old regionfiles
+
+            complete = new CompletableFuture<>();
+            existing = this.conversions.putIfAbsent(sectionKey, complete);
+        }
+
+        if (existing != null) {
+            final SectorFile ret = existing.join();
+            if (!existingOnly && ret == null) {
+                return this.getSectorFile(unscopedBufferChoices, chunkX, chunkZ, existingOnly);
+            }
+
+            return ret;
+        }
+
+        SectorFile ret = this.convert(unscopedBufferChoices, sectionX, sectionZ);
+        synchronized (this) {
+            this.conversions.remove(sectionKey);
+
+            if (existingOnly && ret == null) {
+                this.markNonExisting(sectionKey);
+                complete.complete(ret);
+                return null;
+            }
+
+            if (this.sectorCache.size() >= io.papermc.paper.configuration.GlobalConfiguration.get().misc.regionFileCacheSize) {
+                final SectorFile sectorFile = this.sectorCache.removeLast();
+                sectorFile.close();
+                if (this.tracer != null) {
+                    this.tracer.add(new SectorFileTracer.FileEvent(SectorFileTracer.FileEventType.CLOSE, sectionX, sectionZ));
+                }
+            }
+
+            if (ret == null) {
+                this.directory.mkdirs();
+                final File file = new File(this.directory, SectorFile.getFileName(sectionX, sectionZ));
+                ret = new SectorFile(
+                    file, sectionX, sectionZ, getCompressionType(), unscopedBufferChoices, MinecraftRegionFileType.getTranslationTable(),
+                    (this.sync ? SectorFile.OPEN_FLAGS_SYNC_WRITES : 0)
+                );
+            }
+
+            this.sectorCache.putAndMoveToFirst(sectionKey, ret);
+
+            complete.complete(ret);
+
+            return ret;
+        }
+    }
+
+    private SectorFile convert(final BufferChoices unscopedBufferChoices, final int sectionX, final int sectionZ) throws IOException {
+        this.directory.mkdirs();
+
+        SectorFile sectorFile = null;
+        final File root = this.directory.getParentFile();
+
+        for (final MinecraftRegionFileType type : MinecraftRegionFileType.getAll()) {
+            final File folder = new File(root, type.getFolder());
+
+            final File file = new File(folder, "r." + sectionX + "." + sectionZ + ".mca");
+
+            if (!file.isFile()) {
+                continue;
+            }
+
+            if (sectorFile == null) {
+                sectorFile = new SectorFile(
+                    new File(this.directory, SectorFile.getFileName(sectionX, sectionZ)),
+                    sectionX, sectionZ, getCompressionType(), unscopedBufferChoices, MinecraftRegionFileType.getTranslationTable(),
+                    (this.sync ? SectorFile.OPEN_FLAGS_SYNC_WRITES : 0)
+                );
+            }
+
+            final ConversionRegionFile regionFile = new ConversionRegionFile(file, sectionX, sectionZ, unscopedBufferChoices, true);
+            try {
+                regionFile.fillRaw(unscopedBufferChoices);
+
+                try (final BufferChoices readScope = unscopedBufferChoices.scope()) {
+                    final ConversionRegionFile.CustomByteArrayOutputStream decompressed = new ConversionRegionFile.CustomByteArrayOutputStream(readScope.t1m().acquireJavaBuffer());
+                    for (int i = 0; i < 32 * 32; ++i) {
+                        final int chunkX = (i & 31);
+                        final int chunkZ = ((i >>> 5) & 31);
+
+                        decompressed.reset();
+
+                        int read = -1;
+                        try {
+                            read = regionFile.read(chunkX, chunkZ, readScope, decompressed, true);
+                        } catch (final IOException ex) {
+                            LOGGER.error("Failed to read " + type.getName() + " (" + chunkX + "," + chunkZ + ") from regionfile " + regionFile.file.getAbsolutePath() + ": ", ex);
+                        }
+
+                        if (read < 0) {
+                            continue;
+                        }
+
+                        try (final BufferChoices writeScope = readScope.scope();) {
+                            final SectorFile.SectorFileOutput output = sectorFile.write(
+                                writeScope, chunkX, chunkZ, type.getNewId(),
+                                SectorFileCompressionType.fromRegionFile(read),
+                                SectorFile.WRITE_FLAG_RAW
+                            );
+                            try {
+                                decompressed.writeTo(output.outputStream());
+                                output.outputStream().close();
+                            } finally {
+                                output.rawOutput().freeResources();
+                            }
+                        }
+                    }
+                }
+            } finally {
+                regionFile.close();
+            }
+        }
+
+        return sectorFile;
+    }
+
+    public CompoundTag read(final BufferChoices unscopedBufferChoices, final int chunkX, final int chunkZ, final int type) throws IOException {
+        final SectorFile sectorFile = this.getSectorFile(unscopedBufferChoices, chunkX, chunkZ, true);
+
+        if (sectorFile == null) {
+            return null;
+        }
+
+        synchronized (sectorFile) {
+            try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope();
+                 final DataInputStream is = sectorFile.read(
+                     scopedBufferChoices, chunkX & SectorFile.SECTION_MASK, chunkZ & SectorFile.SECTION_MASK,
+                     type, SectorFile.RECOMMENDED_READ_FLAGS).data()) {
+
+                if (this.tracer != null) {
+                    // cannot estimate size, available() does not pass through some of the decompressors
+                    this.tracer.add(new SectorFileTracer.DataEvent(SectorFileTracer.DataEventType.READ, chunkX, chunkZ, (byte)type, 0));
+                }
+
+                return is == null ? null : NbtIo.read((DataInput) is);
+            }
+        }
+    }
+
+    public void scanChunk(final BufferChoices unscopedBufferChoices, final int chunkX, final int chunkZ, final int type,
+                          final StreamTagVisitor scanner) throws IOException {
+        final SectorFile sectorFile = this.getSectorFile(unscopedBufferChoices, chunkX, chunkZ, true);
+
+        if (sectorFile == null) {
+            return;
+        }
+
+        synchronized (sectorFile) {
+            try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope();
+                 final DataInputStream is = sectorFile.read(
+                     scopedBufferChoices, chunkX & SectorFile.SECTION_MASK, chunkZ & SectorFile.SECTION_MASK,
+                     type, SectorFile.RECOMMENDED_READ_FLAGS).data()) {
+
+                if (this.tracer != null) {
+                    // cannot estimate size, available() does not pass through some of the decompressors
+                    this.tracer.add(new SectorFileTracer.DataEvent(SectorFileTracer.DataEventType.READ, chunkX, chunkZ, (byte)type, 0));
+                }
+
+                if (is != null) {
+                    NbtIo.parse(is, scanner, NbtAccounter.unlimitedHeap());
+                }
+            }
+        }
+    }
+
+    public void write(final BufferChoices unscopedBufferChoices, final int chunkX, final int chunkZ, final int type, final CompoundTag nbt) throws IOException {
+        final SectorFile sectorFile = this.getSectorFile(unscopedBufferChoices, chunkX, chunkZ, nbt == null);
+        if (nbt == null && sectorFile == null) {
+            return;
+        }
+
+        synchronized (sectorFile) {
+            try (final BufferChoices scopedBufferChoices = unscopedBufferChoices.scope()) {
+                if (nbt == null) {
+                    if (this.tracer != null) {
+                        this.tracer.add(new SectorFileTracer.DataEvent(SectorFileTracer.DataEventType.DELETE, chunkX, chunkZ, (byte)type, 0));
+                    }
+                    sectorFile.delete(
+                        scopedBufferChoices, chunkX & SectorFile.SECTION_MASK,
+                        chunkZ & SectorFile.SECTION_MASK, type
+                    );
+                } else {
+                    final SectorFile.SectorFileOutput output = sectorFile.write(
+                        scopedBufferChoices, chunkX & SectorFile.SECTION_MASK, chunkZ & SectorFile.SECTION_MASK,
+                        type, null, 0
+                    );
+
+                    try {
+                        NbtIo.write(nbt, (DataOutput)output.outputStream());
+                        // need close() to force gzip/deflate/etc to write data through
+                        output.outputStream().close();
+                        if (this.tracer != null) {
+                            this.tracer.add(new SectorFileTracer.DataEvent(SectorFileTracer.DataEventType.WRITE, chunkX, chunkZ, (byte)type, output.rawOutput().getTotalCompressedSize()));
+                        }
+                    } finally {
+                        output.rawOutput().freeResources();
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        final ExceptionCollector<IOException> collector = new ExceptionCollector<>();
+
+        for (final SectorFile sectorFile : this.sectorCache.values()) {
+            try {
+                synchronized (sectorFile) {
+                    sectorFile.close();
+                }
+            } catch (final IOException ex) {
+                collector.add(ex);
+            }
+        }
+
+        this.sectorCache.clear();
+
+        if (this.tracer != null) {
+            this.tracer.close();
+        }
+
+        collector.throwIfPresent();
+    }
+
+    public synchronized void flush() throws IOException {
+        final ExceptionCollector<IOException> collector = new ExceptionCollector<>();
+
+        for (final SectorFile sectorFile : this.sectorCache.values()) {
+            try {
+                synchronized (sectorFile) {
+                    sectorFile.flush();
+                }
+            } catch (final IOException ex) {
+                collector.add(ex);
+            }
+        }
+
+        collector.throwIfPresent();
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/SectorFileCompressionType.java b/src/main/java/ca/spottedleaf/io/region/SectorFileCompressionType.java
new file mode 100644
index 0000000000000000000000000000000000000000..53de07063a7b434ab489ca7185dd563df176d9c0
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/SectorFileCompressionType.java
@@ -0,0 +1,129 @@
+package ca.spottedleaf.io.region;
+
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferInputStream;
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferOutputStream;
+import ca.spottedleaf.io.region.io.java.SimpleBufferedInputStream;
+import ca.spottedleaf.io.region.io.java.SimpleBufferedOutputStream;
+import ca.spottedleaf.io.region.io.zstd.ZSTDInputStream;
+import ca.spottedleaf.io.region.io.zstd.ZSTDOutputStream;
+import ca.spottedleaf.io.buffer.BufferChoices;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import net.jpountz.lz4.LZ4BlockInputStream;
+import net.jpountz.lz4.LZ4BlockOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
+
+public abstract class SectorFileCompressionType {
+
+    private static final Int2ObjectMap<SectorFileCompressionType> BY_ID = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>());
+
+    public static final SectorFileCompressionType GZIP = new SectorFileCompressionType(1) {
+        @Override
+        public InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException {
+            return new SimpleBufferedInputStream(new GZIPInputStream(input), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+
+        @Override
+        public OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException {
+            return new SimpleBufferedOutputStream(new GZIPOutputStream(output), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+    };
+    public static final SectorFileCompressionType DEFLATE = new SectorFileCompressionType(2) {
+        @Override
+        public InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException {
+            return new SimpleBufferedInputStream(new InflaterInputStream(input), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+
+        @Override
+        public OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException {
+            return new SimpleBufferedOutputStream(new DeflaterOutputStream(output), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+    };
+    public static final SectorFileCompressionType NONE = new SectorFileCompressionType(3) {
+        @Override
+        public InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException {
+            return input;
+        }
+
+        @Override
+        public OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException {
+            return output;
+        }
+    };
+    public static final SectorFileCompressionType LZ4 = new SectorFileCompressionType(4) {
+        @Override
+        public InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException {
+            return new SimpleBufferedInputStream(new LZ4BlockInputStream(input), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+
+        @Override
+        public OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException {
+            return new SimpleBufferedOutputStream(new LZ4BlockOutputStream(output), scopedBufferChoices.t16k().acquireJavaBuffer());
+        }
+    };
+    public static final SectorFileCompressionType ZSTD = new SectorFileCompressionType(5) {
+        @Override
+        public InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException {
+            return new ZSTDInputStream(
+                    scopedBufferChoices.t16k().acquireDirectBuffer(), scopedBufferChoices.t16k().acquireDirectBuffer(),
+                    scopedBufferChoices.zstdCtxs().acquireDecompressor(), null, input
+            );
+        }
+
+        @Override
+        public OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException {
+            return new ZSTDOutputStream(
+                    scopedBufferChoices.t16k().acquireDirectBuffer(), scopedBufferChoices.t16k().acquireDirectBuffer(),
+                    scopedBufferChoices.zstdCtxs().acquireCompressor(), null, output
+            );
+        }
+    };
+
+    private final int id;
+
+    protected SectorFileCompressionType(final int id) {
+        this.id = id;
+        if (BY_ID.putIfAbsent(id, this) != null) {
+            throw new IllegalArgumentException("Duplicate id");
+        }
+    }
+
+    public final int getId() {
+        return this.id;
+    }
+
+    public abstract InputStream createInput(final BufferChoices scopedBufferChoices, final ByteBufferInputStream input) throws IOException;
+
+    public abstract OutputStream createOutput(final BufferChoices scopedBufferChoices, final ByteBufferOutputStream output) throws IOException;
+
+    public static SectorFileCompressionType getById(final int id) {
+        return BY_ID.get(id);
+    }
+
+    public static SectorFileCompressionType fromRegionFile(final int id) {
+        switch (id) {
+            case 1: { // GZIP
+                return SectorFileCompressionType.GZIP;
+            }
+            case 2: { // DEFLATE
+                return SectorFileCompressionType.DEFLATE;
+            }
+            case 3: { // NONE
+                return SectorFileCompressionType.NONE;
+            }
+            case 4: { // LZ4
+                return SectorFileCompressionType.LZ4;
+            }
+            default: {
+                throw new IllegalArgumentException();
+            }
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/SectorFileTracer.java b/src/main/java/ca/spottedleaf/io/region/SectorFileTracer.java
new file mode 100644
index 0000000000000000000000000000000000000000..cbf8effbddadefe4004e3e3824cd9436d4f1a61e
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/SectorFileTracer.java
@@ -0,0 +1,183 @@
+package ca.spottedleaf.io.region;
+
+import ca.spottedleaf.io.region.io.bytebuffer.BufferedFileChannelInputStream;
+import ca.spottedleaf.io.region.io.bytebuffer.BufferedFileChannelOutputStream;
+import ca.spottedleaf.io.region.io.java.SimpleBufferedInputStream;
+import ca.spottedleaf.io.region.io.java.SimpleBufferedOutputStream;
+import ca.spottedleaf.io.region.io.zstd.ZSTDOutputStream;
+import com.github.luben.zstd.ZstdCompressCtx;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.io.Closeable;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class SectorFileTracer implements Closeable {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SectorFileTracer.class);
+
+    private final File file;
+    private final DataOutputStream out;
+    private final ArrayDeque<Writable> objects = new ArrayDeque<>();
+
+    private static final int MAX_STORED_OBJECTS = 128;
+    private static final TraceEventType[] EVENT_TYPES = TraceEventType.values();
+
+    public SectorFileTracer(final File file) throws IOException {
+        this.file = file;
+
+        file.getParentFile().mkdirs();
+        file.delete();
+        file.createNewFile();
+
+        final int bufferSize = 8 * 1024;
+
+        this.out = new DataOutputStream(
+            new SimpleBufferedOutputStream(
+                new BufferedFileChannelOutputStream(ByteBuffer.allocateDirect(bufferSize), file.toPath(), true),
+                new byte[bufferSize]
+            )
+        );
+    }
+
+    public synchronized void add(final Writable writable) {
+        this.objects.add(writable);
+        if (this.objects.size() >= MAX_STORED_OBJECTS) {
+            Writable polled = null;
+            try {
+                while ((polled = this.objects.poll()) != null) {
+                    polled.write(this.out);
+                }
+            } catch (final IOException ex) {
+                LOGGER.error("Failed to write " + polled + ": ", ex);
+            }
+        }
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        try {
+            Writable polled;
+            while ((polled = this.objects.poll()) != null) {
+                polled.write(this.out);
+            }
+        } finally {
+            this.out.close();
+        }
+    }
+
+    private static Writable read(final DataInputStream input) throws IOException {
+        final int next = input.read();
+        if (next == -1) {
+            return null;
+        }
+
+        final TraceEventType event = EVENT_TYPES[next & 0xFF];
+
+        switch (event) {
+            case DATA: {
+                return DataEvent.read(input);
+            }
+            case FILE: {
+                return FileEvent.read(input);
+            }
+            default: {
+                throw new IllegalStateException("Unknown event: " + event);
+            }
+        }
+    }
+
+    public static List<Writable> read(final File file) throws IOException {
+        final List<Writable> ret = new ArrayList<>();
+
+        final int bufferSize = 8 * 1024;
+
+        try (final DataInputStream is = new DataInputStream(
+            new SimpleBufferedInputStream(
+                new BufferedFileChannelInputStream(ByteBuffer.allocateDirect(bufferSize), file),
+                new byte[bufferSize]
+            )
+        )) {
+            Writable curr;
+            while ((curr = read(is)) != null) {
+                ret.add(curr);
+            }
+
+            return ret;
+        }
+    }
+
+    public static interface Writable {
+        public void write(final DataOutput out) throws IOException;
+    }
+
+    public static enum TraceEventType {
+        FILE, DATA;
+    }
+
+    public static enum FileEventType {
+        CREATE, OPEN, CLOSE;
+    }
+
+    public static record FileEvent(
+        FileEventType eventType, int sectionX, int sectionZ
+    ) implements Writable {
+        private static final FileEventType[] TYPES = FileEventType.values();
+
+        @Override
+        public void write(final DataOutput out) throws IOException {
+            out.writeByte(TraceEventType.FILE.ordinal());
+            out.writeByte(this.eventType().ordinal());
+            out.writeInt(this.sectionX());
+            out.writeInt(this.sectionZ());
+        }
+
+        public static FileEvent read(final DataInput input) throws IOException {
+            return new FileEvent(
+                TYPES[(int)input.readByte() & 0xFF],
+                input.readInt(),
+                input.readInt()
+            );
+        }
+    }
+
+    public static enum DataEventType {
+        READ, WRITE, DELETE;
+    }
+
+    public static record DataEvent(
+        DataEventType eventType, int chunkX, int chunkZ, byte type, int size
+    ) implements Writable {
+
+        private static final DataEventType[] TYPES = DataEventType.values();
+
+        @Override
+        public void write(final DataOutput out) throws IOException {
+            out.writeByte(TraceEventType.DATA.ordinal());
+            out.writeByte(this.eventType().ordinal());
+            out.writeInt(this.chunkX());
+            out.writeInt(this.chunkZ());
+            out.writeByte(this.type());
+            out.writeInt(this.size());
+        }
+
+        public static DataEvent read(final DataInput input) throws IOException {
+            return new DataEvent(
+                TYPES[(int)input.readByte() & 0xFF],
+                input.readInt(),
+                input.readInt(),
+                input.readByte(),
+                input.readInt()
+            );
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelInputStream.java b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelInputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..8c98cb471dddd19a6d7265a9abbc04aa971ede3d
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelInputStream.java
@@ -0,0 +1,68 @@
+package ca.spottedleaf.io.region.io.bytebuffer;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+public class BufferedFileChannelInputStream extends ByteBufferInputStream {
+
+    protected final FileChannel input;
+
+    public BufferedFileChannelInputStream(final ByteBuffer buffer, final File file) throws IOException {
+        this(buffer, file.toPath());
+    }
+
+    public BufferedFileChannelInputStream(final ByteBuffer buffer, final Path path) throws IOException {
+        super(buffer);
+
+        this.input = FileChannel.open(path, StandardOpenOption.READ);
+
+        // ensure we can fully utilise the buffer
+        buffer.limit(buffer.capacity());
+        buffer.position(buffer.capacity());
+    }
+
+    @Override
+    public int available() throws IOException {
+        final long avail = (long)super.available() + (this.input.size() - this.input.position());
+
+        final int ret;
+        if (avail < 0) {
+            ret = 0;
+        } else if (avail > (long)Integer.MAX_VALUE) {
+            ret = Integer.MAX_VALUE;
+        } else {
+            ret = (int)avail;
+        }
+
+        return ret;
+    }
+
+    @Override
+    protected ByteBuffer refill(final ByteBuffer current) throws IOException {
+        // note: limit = capacity
+        current.flip();
+
+        this.input.read(current);
+
+        current.flip();
+
+        return current;
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            super.close();
+        } finally {
+            // force any read calls to go to refill()
+            this.buffer.limit(this.buffer.capacity());
+            this.buffer.position(this.buffer.capacity());
+
+            this.input.close();
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelOutputStream.java b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelOutputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..98c661a8bfac97a208cd0b20fe5a666f5d2e34de
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/BufferedFileChannelOutputStream.java
@@ -0,0 +1,45 @@
+package ca.spottedleaf.io.region.io.bytebuffer;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+public class BufferedFileChannelOutputStream extends ByteBufferOutputStream {
+
+    private final FileChannel channel;
+
+    public BufferedFileChannelOutputStream(final ByteBuffer buffer, final Path path, final boolean append) throws IOException {
+        super(buffer);
+
+        if (append) {
+            this.channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
+        } else {
+            this.channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
+        }
+    }
+
+    @Override
+    protected ByteBuffer flush(final ByteBuffer current) throws IOException {
+        current.flip();
+
+        while (current.hasRemaining()) {
+            this.channel.write(current);
+        }
+
+        current.limit(current.capacity());
+        current.position(0);
+
+        return current;
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            super.close();
+        } finally {
+            this.channel.close();
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferInputStream.java b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferInputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..8ea05e286a43010afbf3bf4292bfe3d3f911d159
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferInputStream.java
@@ -0,0 +1,112 @@
+package ca.spottedleaf.io.region.io.bytebuffer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+public class ByteBufferInputStream extends InputStream {
+
+    protected ByteBuffer buffer;
+
+    public ByteBufferInputStream(final ByteBuffer buffer) {
+        this.buffer = buffer;
+    }
+
+    protected ByteBuffer refill(final ByteBuffer current) throws IOException {
+        return current;
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (this.buffer.hasRemaining()) {
+            return (int)this.buffer.get() & 0xFF;
+        }
+
+        this.buffer = this.refill(this.buffer);
+        if (!this.buffer.hasRemaining()) {
+            return -1;
+        }
+        return (int)this.buffer.get() & 0xFF;
+    }
+
+    @Override
+    public int read(final byte[] b) throws IOException {
+        return this.read(b, 0, b.length);
+    }
+
+    @Override
+    public int read(final byte[] b, final int off, final int len) throws IOException {
+        if (((len | off) | (off + len) | (b.length - (off + len))) < 0) {
+            // length < 0 || off < 0 || (off + len) < 0
+            throw new IndexOutOfBoundsException();
+        }
+
+        // only return 0 when len = 0
+        if (len == 0) {
+            return 0;
+        }
+
+        int remaining = this.buffer.remaining();
+        if (remaining <= 0) {
+            this.buffer = this.refill(this.buffer);
+            remaining = this.buffer.remaining();
+
+            if (remaining <= 0) {
+                return -1;
+            }
+        }
+
+        final int toRead = Math.min(remaining, len);
+        this.buffer.get(b, off, toRead);
+
+        return toRead;
+    }
+
+    public int read(final ByteBuffer dst) throws IOException {
+        final int off = dst.position();
+        final int len = dst.remaining();
+
+        // assume buffer position/limits are valid
+
+        if (len == 0) {
+            return 0;
+        }
+
+        int remaining = this.buffer.remaining();
+        if (remaining <= 0) {
+            this.buffer = this.refill(this.buffer);
+            remaining = this.buffer.remaining();
+
+            if (remaining <= 0) {
+                return -1;
+            }
+        }
+
+        final int toRead = Math.min(remaining, len);
+
+        dst.put(off, this.buffer, this.buffer.position(), toRead);
+
+        this.buffer.position(this.buffer.position() + toRead);
+        dst.position(off + toRead);
+
+        return toRead;
+    }
+
+    @Override
+    public long skip(final long n) throws IOException {
+        final int remaining = this.buffer.remaining();
+
+        final long toSkip = Math.min(n, (long)remaining);
+
+        if (toSkip > 0) {
+            this.buffer.position(this.buffer.position() + (int)toSkip);
+        }
+
+        return Math.max(0, toSkip);
+    }
+
+    @Override
+    public int available() throws IOException {
+        return this.buffer.remaining();
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferOutputStream.java b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferOutputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..024e756a9d88981e44b027bfe5a7a7f26d069dd2
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/bytebuffer/ByteBufferOutputStream.java
@@ -0,0 +1,114 @@
+package ca.spottedleaf.io.region.io.bytebuffer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public abstract class ByteBufferOutputStream extends OutputStream {
+
+    protected ByteBuffer buffer;
+
+    public ByteBufferOutputStream(final ByteBuffer buffer) {
+        this.buffer = buffer;
+    }
+
+    // always returns a buffer with remaining > 0
+    protected abstract ByteBuffer flush(final ByteBuffer current) throws IOException;
+
+    @Override
+    public void write(final int b) throws IOException {
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        if (this.buffer.hasRemaining()) {
+            this.buffer.put((byte)b);
+            return;
+        }
+
+        this.buffer = this.flush(this.buffer);
+        this.buffer.put((byte)b);
+    }
+
+    @Override
+    public void write(final byte[] b) throws IOException {
+        this.write(b, 0, b.length);
+    }
+
+    @Override
+    public void write(final byte[] b, int off, int len) throws IOException {
+        if (((len | off) | (off + len) | (b.length - (off + len))) < 0) {
+            // length < 0 || off < 0 || (off + len) < 0
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        while (len > 0) {
+            final int maxWrite = Math.min(this.buffer.remaining(), len);
+
+            if (maxWrite == 0) {
+                this.buffer = this.flush(this.buffer);
+                continue;
+            }
+
+            this.buffer.put(b, off, maxWrite);
+
+            off += maxWrite;
+            len -= maxWrite;
+        }
+    }
+
+    public void write(final ByteBuffer buffer) throws IOException {
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        int off = buffer.position();
+        int remaining = buffer.remaining();
+
+        while (remaining > 0) {
+            final int maxWrite = Math.min(this.buffer.remaining(), remaining);
+
+            if (maxWrite == 0) {
+                this.buffer = this.flush(this.buffer);
+                continue;
+            }
+
+            final int thisOffset = this.buffer.position();
+
+            this.buffer.put(thisOffset, buffer, off, maxWrite);
+
+            off += maxWrite;
+            remaining -= maxWrite;
+
+            // update positions in case flush() throws or needs to be called
+            this.buffer.position(thisOffset + maxWrite);
+            buffer.position(off);
+        }
+    }
+
+    @Override
+    public void flush() throws IOException {
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        this.buffer = this.flush(this.buffer);
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (this.buffer == null) {
+            return;
+        }
+
+        try {
+            this.flush();
+        } finally {
+            this.buffer = null;
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedInputStream.java b/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedInputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..7a53f69fcd13cc4b784244bc35a768cfbf0ffd41
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedInputStream.java
@@ -0,0 +1,137 @@
+package ca.spottedleaf.io.region.io.java;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class SimpleBufferedInputStream extends InputStream {
+
+    protected static final int DEFAULT_BUFFER_SIZE = 8192;
+
+    protected InputStream input;
+    protected byte[] buffer;
+    protected int pos;
+    protected int max;
+
+    public SimpleBufferedInputStream(final InputStream input) {
+        this(input, DEFAULT_BUFFER_SIZE);
+    }
+
+    public SimpleBufferedInputStream(final InputStream input, final int bufferSize) {
+        this(input, new byte[bufferSize]);
+    }
+
+    public SimpleBufferedInputStream(final InputStream input, final byte[] buffer) {
+        if (buffer.length == 0) {
+            throw new IllegalArgumentException("Buffer size must be > 0");
+        }
+
+        this.input = input;
+        this.buffer = buffer;
+        this.pos = this.max = 0;
+    }
+
+    private void fill() throws IOException {
+        if (this.max < 0) {
+            // already read EOF
+            return;
+        }
+        // assume pos = buffer.length
+        this.max = this.input.read(this.buffer, 0, this.buffer.length);
+        this.pos = 0;
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        if (this.pos < this.max) {
+            return (int)this.buffer[this.pos++] & 0xFF;
+        }
+
+        this.fill();
+
+        if (this.pos < this.max) {
+            return (int)this.buffer[this.pos++] & 0xFF;
+        }
+
+        return -1;
+    }
+
+    @Override
+    public int read(final byte[] b) throws IOException {
+        return this.read(b, 0, b.length);
+    }
+
+    @Override
+    public int read(final byte[] b, final int off, final int len) throws IOException {
+        if (((len | off) | (off + len) | (b.length - (off + len))) < 0) {
+            // length < 0 || off < 0 || (off + len) < 0
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        if (len == 0) {
+            return 0;
+        }
+
+        if (this.pos >= this.max) {
+            if (len >= this.buffer.length) {
+                // bypass buffer
+                return this.input.read(b, off, len);
+            }
+
+            this.fill();
+            if (this.pos >= this.max) {
+                return -1;
+            }
+        }
+
+        final int maxRead = Math.min(this.max - this.pos, len);
+
+        System.arraycopy(this.buffer, this.pos, b, off, maxRead);
+
+        this.pos += maxRead;
+
+        return maxRead;
+    }
+
+    @Override
+    public long skip(final long n) throws IOException {
+        final int remaining = this.max - this.pos;
+
+        final long toSkip = Math.min(n, (long)remaining);
+
+        if (toSkip > 0) {
+            this.pos += (int)toSkip;
+        }
+
+        return Math.max(0, toSkip);
+    }
+
+    @Override
+    public int available() throws IOException {
+        if (this.input == null) {
+            throw new IOException("Closed stream");
+        }
+
+        final int upper = Math.max(0, this.input.available());
+        final int ret = upper + Math.max(0, this.max - this.pos);
+
+        return ret < 0 ? Integer.MAX_VALUE : ret; // ret < 0 when overflow
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            this.input.close();
+        } finally {
+            this.input = null;
+            this.buffer = null;
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedOutputStream.java b/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedOutputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..a237b642b5f30d87098d43055fe044121473bcb1
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/java/SimpleBufferedOutputStream.java
@@ -0,0 +1,113 @@
+package ca.spottedleaf.io.region.io.java;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class SimpleBufferedOutputStream extends OutputStream {
+
+    protected static final int DEFAULT_BUFFER_SIZE = 8192;
+
+    protected OutputStream output;
+    protected byte[] buffer;
+    protected int pos;
+
+    public SimpleBufferedOutputStream(final OutputStream output) {
+        this(output, DEFAULT_BUFFER_SIZE);
+    }
+
+    public SimpleBufferedOutputStream(final OutputStream output, final int bufferSize) {
+        this(output, new byte[bufferSize]);
+    }
+
+    public SimpleBufferedOutputStream(final OutputStream output, final byte[] buffer) {
+        if (buffer.length == 0) {
+            throw new IllegalArgumentException("Buffer size must be > 0");
+        }
+
+        this.output = output;
+        this.buffer = buffer;
+        this.pos = 0;
+    }
+
+    protected void writeBuffer() throws IOException {
+        if (this.pos > 0) {
+            this.output.write(this.buffer, 0, this.pos);
+            this.pos = 0;
+        }
+    }
+
+    @Override
+    public void write(final int b) throws IOException {
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        if (this.pos < this.buffer.length) {
+            this.buffer[this.pos++] = (byte)b;
+        } else {
+            this.writeBuffer();
+            this.buffer[this.pos++] = (byte)b;
+        }
+    }
+
+    @Override
+    public void write(final byte[] b) throws IOException {
+        this.write(b, 0, b.length);
+    }
+
+    @Override
+    public void write(final byte[] b, int off, int len) throws IOException {
+        if (((len | off) | (off + len) | (b.length - (off + len))) < 0) {
+            // length < 0 || off < 0 || (off + len) < 0
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (this.buffer == null) {
+            throw new IOException("Closed stream");
+        }
+
+        while (len > 0) {
+            final int maxBuffer = Math.min(len, this.buffer.length - this.pos);
+
+            if (maxBuffer == 0) {
+                this.writeBuffer();
+
+                if (len >= this.buffer.length) {
+                    // bypass buffer
+                    this.output.write(b, off, len);
+                    return;
+                }
+
+                continue;
+            }
+
+            System.arraycopy(b, off, this.buffer, this.pos, maxBuffer);
+            this.pos += maxBuffer;
+            off += maxBuffer;
+            len -= maxBuffer;
+        }
+    }
+
+    @Override
+    public void flush() throws IOException {
+        this.writeBuffer();
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (this.buffer == null) {
+            return;
+        }
+
+        try {
+            this.flush();
+        } finally {
+            try {
+                this.output.close();
+            } finally {
+                this.output = null;
+                this.buffer = null;
+            }
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDInputStream.java b/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDInputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..99d9ef991a715a7c06bf0ceee464ea1b9ce2b7dc
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDInputStream.java
@@ -0,0 +1,148 @@
+package ca.spottedleaf.io.region.io.zstd;
+
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferInputStream;
+import com.github.luben.zstd.Zstd;
+import com.github.luben.zstd.ZstdDecompressCtx;
+import com.github.luben.zstd.ZstdIOException;
+import java.io.EOFException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.function.Consumer;
+
+public class ZSTDInputStream extends ByteBufferInputStream {
+
+    private ByteBuffer compressedBuffer;
+    private ZstdDecompressCtx decompressor;
+    private Consumer<ZstdDecompressCtx> closeDecompressor;
+    private ByteBufferInputStream wrap;
+    private boolean lastDecompressFlushed;
+    private boolean done;
+
+    public ZSTDInputStream(final ByteBuffer decompressedBuffer, final ByteBuffer compressedBuffer,
+                           final ZstdDecompressCtx decompressor,
+                           final Consumer<ZstdDecompressCtx> closeDecompressor,
+                           final ByteBufferInputStream wrap) {
+        super(decompressedBuffer);
+
+        if (!decompressedBuffer.isDirect() || !compressedBuffer.isDirect()) {
+            throw new IllegalArgumentException("Buffers must be direct");
+        }
+
+        // set position to max so that we force the first read to go to wrap
+
+        decompressedBuffer.limit(decompressedBuffer.capacity());
+        decompressedBuffer.position(decompressedBuffer.capacity());
+
+        compressedBuffer.limit(compressedBuffer.capacity());
+        compressedBuffer.position(compressedBuffer.capacity());
+
+        synchronized (this) {
+            this.decompressor = decompressor;
+            this.closeDecompressor = closeDecompressor;
+            this.compressedBuffer = compressedBuffer;
+            this.wrap = wrap;
+        }
+    }
+
+    protected synchronized ByteBuffer refillCompressed(final ByteBuffer current) throws IOException {
+        current.limit(current.capacity());
+        current.position(0);
+
+        try {
+            this.wrap.read(current);
+        } finally {
+            current.flip();
+        }
+
+        return current;
+    }
+
+    @Override
+    public synchronized int available() throws IOException {
+        if (this.decompressor == null) {
+            return 0;
+        }
+
+        final long ret = (long)super.available() + (long)this.compressedBuffer.remaining() + (long)this.wrap.available();
+
+        if (ret < 0L) {
+            return 0;
+        } else if (ret > (long)Integer.MAX_VALUE) {
+            return Integer.MAX_VALUE;
+        }
+
+        return (int)ret;
+    }
+
+    @Override
+    protected synchronized final ByteBuffer refill(final ByteBuffer current) throws IOException {
+        if (this.decompressor == null) {
+            throw new EOFException();
+        }
+
+        if (this.done) {
+            return current;
+        }
+
+        ByteBuffer compressedBuffer = this.compressedBuffer;
+        final ZstdDecompressCtx decompressor = this.decompressor;
+
+        for (;;) {
+            if (!compressedBuffer.hasRemaining()) {
+                // try to read more data into source
+                this.compressedBuffer = compressedBuffer = this.refillCompressed(compressedBuffer);
+
+                if (!compressedBuffer.hasRemaining()) {
+                    // EOF
+                    if (!this.lastDecompressFlushed) {
+                        throw new ZstdIOException(Zstd.errCorruptionDetected(), "Truncated stream");
+                    }
+                    return current;
+                } else {
+                    // more data to decompress, so reset the last flushed
+                    this.lastDecompressFlushed = false;
+                }
+            }
+
+            current.limit(current.capacity());
+            current.position(0);
+
+            try {
+                this.lastDecompressFlushed = decompressor.decompressDirectByteBufferStream(current, compressedBuffer);
+            } finally {
+                // if decompressDirectByteBufferStream throws, then current.limit = position = 0
+                current.flip();
+            }
+
+            if (current.hasRemaining()) {
+                return current;
+            } else if (this.lastDecompressFlushed) {
+                this.done = true;
+                return current;
+            } // else: need more data
+        }
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        if (this.decompressor == null) {
+            return;
+        }
+
+        final ZstdDecompressCtx decompressor = this.decompressor;
+        final ByteBufferInputStream wrap = this.wrap;
+        final Consumer<ZstdDecompressCtx> closeDecompressor = this.closeDecompressor;
+        this.decompressor = null;
+        this.compressedBuffer = null;
+        this.closeDecompressor = null;
+        this.wrap = null;
+
+        try {
+            if (closeDecompressor != null) {
+                closeDecompressor.accept(decompressor);
+            }
+        } finally {
+            wrap.close();
+        }
+    }
+}
diff --git a/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDOutputStream.java b/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDOutputStream.java
new file mode 100644
index 0000000000000000000000000000000000000000..797f079800984607bb9022badf9ebb27b5d3043d
--- /dev/null
+++ b/src/main/java/ca/spottedleaf/io/region/io/zstd/ZSTDOutputStream.java
@@ -0,0 +1,141 @@
+package ca.spottedleaf.io.region.io.zstd;
+
+import ca.spottedleaf.io.region.io.bytebuffer.ByteBufferOutputStream;
+import com.github.luben.zstd.EndDirective;
+import com.github.luben.zstd.ZstdCompressCtx;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.function.Consumer;
+
+public class ZSTDOutputStream extends ByteBufferOutputStream {
+
+    protected static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocateDirect(0);
+
+    private ByteBuffer compressedBuffer;
+    private ZstdCompressCtx compressor;
+    private Consumer<ZstdCompressCtx> closeCompressor;
+    private ByteBufferOutputStream wrap;
+
+    public ZSTDOutputStream(final ByteBuffer decompressedBuffer, final ByteBuffer compressedBuffer,
+                            final ZstdCompressCtx compressor,
+                            final Consumer<ZstdCompressCtx> closeCompressor,
+                            final ByteBufferOutputStream wrap) {
+        super(decompressedBuffer);
+
+        if (!decompressedBuffer.isDirect() || !compressedBuffer.isDirect()) {
+            throw new IllegalArgumentException("Buffers must be direct");
+        }
+
+        decompressedBuffer.limit(decompressedBuffer.capacity());
+        decompressedBuffer.position(0);
+
+        compressedBuffer.limit(compressedBuffer.capacity());
+        compressedBuffer.position(0);
+
+        synchronized (this) {
+            this.compressedBuffer = compressedBuffer;
+            this.compressor = compressor;
+            this.closeCompressor = closeCompressor;
+            this.wrap = wrap;
+        }
+    }
+
+    protected synchronized ByteBuffer emptyBuffer(final ByteBuffer toFlush) throws IOException {
+        toFlush.flip();
+
+        if (toFlush.hasRemaining()) {
+            this.wrap.write(toFlush);
+        }
+
+        toFlush.limit(toFlush.capacity());
+        toFlush.position(0);
+
+        return toFlush;
+    }
+
+    @Override
+    protected synchronized final ByteBuffer flush(final ByteBuffer current) throws IOException {
+        current.flip();
+
+        while (current.hasRemaining()) {
+            if (!this.compressedBuffer.hasRemaining()) {
+                this.compressedBuffer = this.emptyBuffer(this.compressedBuffer);
+            }
+            this.compressor.compressDirectByteBufferStream(this.compressedBuffer, current, EndDirective.CONTINUE);
+        }
+
+        current.limit(current.capacity());
+        current.position(0);
+
+        return current;
+    }
+
+    @Override
+    public synchronized void flush() throws IOException {
+        // flush all buffered data to zstd stream first
+        super.flush();
+
+        // now try to dump compressor buffers
+        do {
+            if (!this.compressedBuffer.hasRemaining()) {
+                this.compressedBuffer = this.emptyBuffer(this.compressedBuffer);
+            }
+        } while (!this.compressor.compressDirectByteBufferStream(this.compressedBuffer, EMPTY_BUFFER, EndDirective.FLUSH));
+
+        // empty compressed buffer into wrap
+        if (this.compressedBuffer.position() != 0) {
+            this.compressedBuffer = this.emptyBuffer(this.compressedBuffer);
+        }
+
+        this.wrap.flush();
+    }
+
+    @Override
+    public synchronized void close() throws IOException {
+        if (this.compressor == null) {
+            // already closed
+            return;
+        }
+
+        try {
+            // flush data to compressor
+            try {
+                super.flush();
+            } finally {
+                // perform super.close
+                // the reason we inline this is so that we do not call our flush(), so that we do not perform ZSTD FLUSH + END,
+                // which is slightly more inefficient than just END
+                this.buffer = null;
+            }
+
+            // perform end stream
+            do {
+                if (!this.compressedBuffer.hasRemaining()) {
+                    this.compressedBuffer = this.emptyBuffer(this.compressedBuffer);
+                }
+            } while (!this.compressor.compressDirectByteBufferStream(this.compressedBuffer, EMPTY_BUFFER, EndDirective.END));
+
+            // flush compressed buffer
+            if (this.compressedBuffer.position() != 0) {
+                this.compressedBuffer = this.emptyBuffer(this.compressedBuffer);
+            }
+
+            // try-finally will flush wrap
+        } finally {
+            try {
+                if (this.closeCompressor != null) {
+                    this.closeCompressor.accept(this.compressor);
+                }
+            } finally {
+                try {
+                    this.wrap.close();
+                } finally {
+                    this.compressor = null;
+                    this.closeCompressor = null;
+                    this.compressedBuffer = null;
+                    this.wrap = null;
+                }
+            }
+        }
+    }
+}
diff --git a/src/main/java/io/papermc/paper/chunk/system/io/RegionFileIOThread.java b/src/main/java/io/papermc/paper/chunk/system/io/RegionFileIOThread.java
index 2096e57c025858519e7c46788993b9aac1ec60e8..7f5a5267f36b3076fbda35f581d3985ee04e6ee2 100644
--- a/src/main/java/io/papermc/paper/chunk/system/io/RegionFileIOThread.java
+++ b/src/main/java/io/papermc/paper/chunk/system/io/RegionFileIOThread.java
@@ -6,6 +6,9 @@ import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor;
 import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedQueueExecutorThread;
 import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue;
 import ca.spottedleaf.concurrentutil.util.ConcurrentUtil;
+import ca.spottedleaf.io.region.MinecraftRegionFileType;
+import ca.spottedleaf.io.region.SectorFile;
+import ca.spottedleaf.io.region.SectorFileCache;
 import com.mojang.logging.LogUtils;
 import io.papermc.paper.util.CoordinateUtils;
 import io.papermc.paper.util.TickThread;
@@ -50,9 +53,16 @@ public final class RegionFileIOThread extends PrioritisedQueueExecutorThread {
      * getControllerFor is updated.
      */
     public static enum RegionFileType {
-        CHUNK_DATA,
-        POI_DATA,
-        ENTITY_DATA;
+        CHUNK_DATA(MinecraftRegionFileType.CHUNK),
+        POI_DATA(MinecraftRegionFileType.POI),
+        ENTITY_DATA(MinecraftRegionFileType.ENTITY);
+
+        public final MinecraftRegionFileType regionFileType;
+
+        private RegionFileType(final MinecraftRegionFileType regionType) {
+            this.regionFileType = regionType;
+        }
+
     }
 
     protected static final RegionFileType[] CACHED_REGIONFILE_TYPES = RegionFileType.values();
@@ -1058,12 +1068,16 @@ public final class RegionFileIOThread extends PrioritisedQueueExecutorThread {
         protected final ConcurrentHashMap<ChunkCoordinate, ChunkDataTask> tasks = new ConcurrentHashMap<>(8192, 0.10f);
 
         public final RegionFileType type;
+        public final ServerLevel world;
 
-        public ChunkDataController(final RegionFileType type) {
+        public ChunkDataController(final RegionFileType type, final ServerLevel world) {
             this.type = type;
+            this.world = world;
         }
 
-        public abstract RegionFileStorage getCache();
+        private SectorFileCache getCache() {
+            return this.world.sectorFileCache;
+        }
 
         public abstract void writeData(final int chunkX, final int chunkZ, final CompoundTag compound) throws IOException;
 
@@ -1074,46 +1088,31 @@ public final class RegionFileIOThread extends PrioritisedQueueExecutorThread {
         }
 
         public boolean doesRegionFileNotExist(final int chunkX, final int chunkZ) {
-            return this.getCache().doesRegionFileNotExistNoIO(new ChunkPos(chunkX, chunkZ));
+            return this.getCache().doesSectorFileNotExistNoIO(chunkX, chunkZ);
         }
 
-        public <T> T computeForRegionFile(final int chunkX, final int chunkZ, final boolean existingOnly, final Function<RegionFile, T> function) {
-            final RegionFileStorage cache = this.getCache();
-            final RegionFile regionFile;
+        public <T> T computeForSectorFile(final int chunkX, final int chunkZ, final boolean existingOnly, final BiFunction<RegionFileType, SectorFile, T> function) {
+            final SectorFileCache cache = this.getCache();
+            final SectorFile regionFile;
             synchronized (cache) {
                 try {
-                    regionFile = cache.getRegionFile(new ChunkPos(chunkX, chunkZ), existingOnly, true);
+                    regionFile = cache.getSectorFile(SectorFileCache.getUnscopedBufferChoices(), chunkX, chunkZ, existingOnly);
                 } catch (final IOException ex) {
                     throw new RuntimeException(ex);
                 }
             }
 
-            try {
-                return function.apply(regionFile);
-            } finally {
-                if (regionFile != null) {
-                    regionFile.fileLock.unlock();
-                }
-            }
+            return function.apply(this.type, regionFile);
         }
 
-        public <T> T computeForRegionFileIfLoaded(final int chunkX, final int chunkZ, final Function<RegionFile, T> function) {
-            final RegionFileStorage cache = this.getCache();
-            final RegionFile regionFile;
+        public <T> T computeForRegionFileIfLoaded(final int chunkX, final int chunkZ, final BiFunction<RegionFileType, SectorFile, T> function) {
+            final SectorFileCache cache = this.getCache();
+            final SectorFile regionFile;
 
             synchronized (cache) {
-                regionFile = cache.getRegionFileIfLoaded(new ChunkPos(chunkX, chunkZ));
-                if (regionFile != null) {
-                    regionFile.fileLock.lock();
-                }
-            }
+                regionFile = cache.getRegionFileIfLoaded(chunkX, chunkZ);
 
-            try {
-                return function.apply(regionFile);
-            } finally {
-                if (regionFile != null) {
-                    regionFile.fileLock.unlock();
-                }
+                return function.apply(this.type, regionFile);
             }
         }
     }
diff --git a/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java b/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java
index 6bc7c6f16a1649fc9e24e7cf90fca401e5bd4875..26aeafc36afb7b39638ac70959497694413a7d6d 100644
--- a/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java
+++ b/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java
@@ -185,22 +185,12 @@ public final class ChunkHolderManager {
             RegionFileIOThread.flush();
         }
 
-        // kill regionfile cache
+        // kill sectorfile cache
         try {
-            this.world.chunkDataControllerNew.getCache().close();
+            this.world.sectorFileCache.close();
         } catch (final IOException ex) {
             LOGGER.error("Failed to close chunk regionfile cache for world '" + this.world.getWorld().getName() + "'", ex);
         }
-        try {
-            this.world.entityDataControllerNew.getCache().close();
-        } catch (final IOException ex) {
-            LOGGER.error("Failed to close entity regionfile cache for world '" + this.world.getWorld().getName() + "'", ex);
-        }
-        try {
-            this.world.poiDataControllerNew.getCache().close();
-        } catch (final IOException ex) {
-            LOGGER.error("Failed to close poi regionfile cache for world '" + this.world.getWorld().getName() + "'", ex);
-        }
     }
 
     void ensureInAutosave(final NewChunkHolder holder) {
@@ -298,7 +288,7 @@ public final class ChunkHolderManager {
             RegionFileIOThread.flush();
             if (this.world.paperConfig().chunks.flushRegionsOnSave) {
                 try {
-                    this.world.chunkSource.chunkMap.regionFileCache.flush();
+                    this.world.sectorFileCache.flush();
                 } catch (IOException ex) {
                     LOGGER.error("Exception when flushing regions in world {}", this.world.getWorld().getName(), ex);
                 }
diff --git a/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java b/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java
index 0467ad99b144aa81a04baa45d4c8bbb2b70185a2..930af52b8dc9729b5b6d56c5bf3a92b63a219612 100644
--- a/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java
+++ b/src/main/java/io/papermc/paper/configuration/GlobalConfiguration.java
@@ -191,7 +191,9 @@ public class GlobalConfiguration extends ConfigurationPart {
         public enum CompressionFormat {
             GZIP,
             ZLIB,
-            NONE
+            NONE,
+            LZ4,
+            ZSTD;
         }
     }
 
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index 7fb9ba3dadb1eca4a1000ea8cf4d13fed2b7db1e..39ff3b4eaf6f41ecd7051a5fd930a1b46caf483c 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -250,7 +250,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
     // Paper end - optimise chunk tick iteration
 
     public ChunkMap(ServerLevel world, LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, StructureTemplateManager structureTemplateManager, Executor executor, BlockableEventLoop<Runnable> mainThreadExecutor, LightChunkGetter chunkProvider, ChunkGenerator chunkGenerator, ChunkProgressListener worldGenerationProgressListener, ChunkStatusUpdateListener chunkStatusChangeListener, Supplier<DimensionDataStorage> persistentStateManagerFactory, int viewDistance, boolean dsync) {
-        super(new RegionStorageInfo(session.getLevelId(), world.dimension(), "chunk"), session.getDimensionPath(world.dimension()).resolve("region"), dataFixer, dsync);
+        super(world.sectorFileCache, session.getDimensionPath(world.dimension()).resolve("region"), dataFixer, dsync);
         // Paper - rewrite chunk system
         this.tickingGenerated = new AtomicInteger();
         this.playerMap = new PlayerMap();
@@ -889,34 +889,13 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
         return nbttagcompound;
     }
 
-    public ChunkStatus getChunkStatusOnDiskIfCached(ChunkPos chunkPos) {
-        net.minecraft.world.level.chunk.storage.RegionFile regionFile = regionFileCache.getRegionFileIfLoaded(chunkPos);
-
-        return regionFile == null ? null : regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
-    }
-
     public ChunkStatus getChunkStatusOnDisk(ChunkPos chunkPos) throws IOException {
-        net.minecraft.world.level.chunk.storage.RegionFile regionFile = regionFileCache.getRegionFile(chunkPos, true);
-
-        if (regionFile == null || !regionFileCache.chunkExists(chunkPos)) {
-            return null;
-        }
-
-        ChunkStatus status = regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
-
-        if (status != null) {
-            return status;
-        }
-
-        this.readChunk(chunkPos);
-
-        return regionFile.getStatusIfCached(chunkPos.x, chunkPos.z);
+        CompoundTag nbt = this.readConvertChunkSync(chunkPos);
+        return nbt == null ? null : ChunkSerializer.getStatus(nbt);
     }
 
     public void updateChunkStatusOnDisk(ChunkPos chunkPos, @Nullable CompoundTag compound) throws IOException {
-        net.minecraft.world.level.chunk.storage.RegionFile regionFile = regionFileCache.getRegionFile(chunkPos, false);
 
-        regionFile.setStatus(chunkPos.x, chunkPos.z, ChunkSerializer.getStatus(compound));
     }
 
     public ChunkAccess getUnloadingChunk(int chunkX, int chunkZ) {
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index ca56a0b596976448da6bb2a0e82b3d5cd4133e12..3ea9ba2481e1920e850169322909be1ff01f77fa 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -364,14 +364,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
     }
 
     // Paper start - rewrite chunk system
+    public final ca.spottedleaf.io.region.SectorFileCache sectorFileCache;
     public final io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler chunkTaskScheduler;
     public final io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController chunkDataControllerNew
-        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.CHUNK_DATA) {
-
-        @Override
-        public net.minecraft.world.level.chunk.storage.RegionFileStorage getCache() {
-            return ServerLevel.this.getChunkSource().chunkMap.regionFileCache;
-        }
+        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.CHUNK_DATA, this) {
 
         @Override
         public void writeData(int chunkX, int chunkZ, net.minecraft.nbt.CompoundTag compound) throws IOException {
@@ -384,12 +380,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
         }
     };
     public final io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController poiDataControllerNew
-        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.POI_DATA) {
-
-        @Override
-        public net.minecraft.world.level.chunk.storage.RegionFileStorage getCache() {
-            return ServerLevel.this.getChunkSource().chunkMap.getPoiManager();
-        }
+        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.POI_DATA, this) {
 
         @Override
         public void writeData(int chunkX, int chunkZ, net.minecraft.nbt.CompoundTag compound) throws IOException {
@@ -402,12 +393,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
         }
     };
     public final io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController entityDataControllerNew
-        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.ENTITY_DATA) {
-
-        @Override
-        public net.minecraft.world.level.chunk.storage.RegionFileStorage getCache() {
-            return ServerLevel.this.entityStorage;
-        }
+        = new io.papermc.paper.chunk.system.io.RegionFileIOThread.ChunkDataController(io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.ENTITY_DATA, this) {
 
         @Override
         public void writeData(int chunkX, int chunkZ, net.minecraft.nbt.CompoundTag compound) throws IOException {
@@ -419,25 +405,6 @@ public class ServerLevel extends Level implements WorldGenLevel {
             return ServerLevel.this.readEntityChunk(chunkX, chunkZ);
         }
     };
-    private final EntityRegionFileStorage entityStorage;
-
-    private static final class EntityRegionFileStorage extends net.minecraft.world.level.chunk.storage.RegionFileStorage {
-
-        public EntityRegionFileStorage(RegionStorageInfo storageKey, Path directory, boolean dsync) {
-            super(storageKey, directory, dsync);
-        }
-
-        protected void write(ChunkPos pos, net.minecraft.nbt.CompoundTag nbt) throws IOException {
-            ChunkPos nbtPos = nbt == null ? null : EntityStorage.readChunkPos(nbt);
-            if (nbtPos != null && !pos.equals(nbtPos)) {
-                throw new IllegalArgumentException(
-                    "Entity chunk coordinate and serialized data do not have matching coordinates, trying to serialize coordinate " + pos.toString()
-                        + " but compound says coordinate is " + nbtPos + " for world: " + this
-                );
-            }
-            super.write(pos, nbt);
-        }
-    }
 
     private void writeEntityChunk(int chunkX, int chunkZ, net.minecraft.nbt.CompoundTag compound) throws IOException {
         if (!io.papermc.paper.chunk.system.io.RegionFileIOThread.isRegionFileThread()) {
@@ -446,7 +413,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
                 io.papermc.paper.chunk.system.io.RegionFileIOThread.RegionFileType.ENTITY_DATA);
             return;
         }
-        this.entityStorage.write(new ChunkPos(chunkX, chunkZ), compound);
+        this.sectorFileCache.write(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(), chunkX, chunkZ,
+            ca.spottedleaf.io.region.MinecraftRegionFileType.ENTITY.getNewId(), compound
+        );
     }
 
     private net.minecraft.nbt.CompoundTag readEntityChunk(int chunkX, int chunkZ) throws IOException {
@@ -456,7 +426,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
                 io.papermc.paper.chunk.system.io.RegionFileIOThread.getIOBlockingPriorityForCurrentThread()
             );
         }
-        return this.entityStorage.read(new ChunkPos(chunkX, chunkZ));
+        return this.sectorFileCache.read(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(), chunkX, chunkZ,
+            ca.spottedleaf.io.region.MinecraftRegionFileType.ENTITY.getNewId()
+        );
     }
 
     private final io.papermc.paper.chunk.system.entity.EntityLookup entityLookup;
@@ -734,7 +707,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
         // CraftBukkit end
         boolean flag2 = minecraftserver.forceSynchronousWrites();
         DataFixer datafixer = minecraftserver.getFixerUpper();
-        this.entityStorage = new EntityRegionFileStorage(new RegionStorageInfo(convertable_conversionsession.getLevelId(), resourcekey, "entities"), convertable_conversionsession.getDimensionPath(resourcekey).resolve("entities"), flag2); // Paper - rewrite chunk system
+        this.sectorFileCache = new ca.spottedleaf.io.region.SectorFileCache(convertable_conversionsession.getDimensionPath(resourcekey).resolve("sectors").toFile(), flag2);
 
         // this.entityManager = new PersistentEntitySectionManager<>(Entity.class, new ServerLevel.EntityCallbacks(), entitypersistentstorage, this.entitySliceManager); // Paper // Paper - rewrite chunk system
         StructureTemplateManager structuretemplatemanager = minecraftserver.getStructureManager();
diff --git a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
index 954d468459fe167ede0e7fca5b9f99da565d59e1..74361ca5a43529650277e08b9e3532f9f748885d 100644
--- a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
+++ b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
@@ -80,6 +80,7 @@ public class WorldUpgrader {
     final DimensionDataStorage overworldDataStorage;
 
     public WorldUpgrader(LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, RegistryAccess dynamicRegistryManager, boolean eraseCache, boolean recreateRegionFiles) {
+        if (true) throw new UnsupportedOperationException();
         this.dimensions = dynamicRegistryManager.registryOrThrow(Registries.LEVEL_STEM);
         this.levels = (Set) java.util.stream.Stream.of(session.dimensionType).map(Registries::levelStemToLevel).collect(Collectors.toUnmodifiableSet()); // CraftBukkit
         this.eraseCache = eraseCache;
@@ -249,7 +250,7 @@ public class WorldUpgrader {
 
         @Override
         protected ChunkStorage createStorage(RegionStorageInfo key, Path worldDirectory) {
-            return (ChunkStorage) (WorldUpgrader.this.recreateRegionFiles ? new RecreatingChunkStorage(key.withTypeSuffix("source"), worldDirectory, key.withTypeSuffix("target"), WorldUpgrader.resolveRecreateDirectory(worldDirectory), WorldUpgrader.this.dataFixer, true) : new ChunkStorage(key, worldDirectory, WorldUpgrader.this.dataFixer, true));
+            return (ChunkStorage) (WorldUpgrader.this.recreateRegionFiles ? new RecreatingChunkStorage(key.withTypeSuffix("source"), worldDirectory, key.withTypeSuffix("target"), WorldUpgrader.resolveRecreateDirectory(worldDirectory), WorldUpgrader.this.dataFixer, true) : new ChunkStorage(null, worldDirectory, WorldUpgrader.this.dataFixer, true));
         }
     }
 
diff --git a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
index c6f193339fdcbcc938d4eafdcad0b112cf1698d5..cc4380ef8b47d51c57a7ff4e022f2069279058ba 100644
--- a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
+++ b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java
@@ -438,7 +438,10 @@ public class PoiManager extends SectionStorage<PoiSection> {
             );
         }
         // Paper end - rewrite chunk system
-        return super.read(chunkcoordintpair);
+        return this.world.sectorFileCache.read(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(), chunkcoordintpair.x, chunkcoordintpair.z,
+            ca.spottedleaf.io.region.MinecraftRegionFileType.POI.getNewId()
+        );
     }
 
     @Override
@@ -451,7 +454,10 @@ public class PoiManager extends SectionStorage<PoiSection> {
             return;
         }
         // Paper end - rewrite chunk system
-        super.write(chunkcoordintpair, nbttagcompound);
+        this.world.sectorFileCache.write(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(), chunkcoordintpair.x, chunkcoordintpair.z,
+            ca.spottedleaf.io.region.MinecraftRegionFileType.POI.getNewId(), nbttagcompound
+        );
     }
     // Paper end
 
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
index 7801fac96d728f951989fca36f6a4890a0638c36..93825a9ee8805693f3890f2246b0341cf136e017 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkStorage.java
@@ -33,15 +33,15 @@ public class ChunkStorage implements AutoCloseable {
     public static final int LAST_MONOLYTH_STRUCTURE_DATA_VERSION = 1493;
     // Paper start - rewrite chunk system; async chunk IO
     private final Object persistentDataLock = new Object();
-    public final RegionFileStorage regionFileCache;
     // Paper end - rewrite chunk system
     protected final DataFixer fixerUpper;
     @Nullable
     private volatile LegacyStructureDataHandler legacyStructureHandler;
+    protected final ca.spottedleaf.io.region.SectorFileCache sectorCache;
 
-    public ChunkStorage(RegionStorageInfo storageKey, Path directory, DataFixer dataFixer, boolean dsync) {
+    public ChunkStorage(ca.spottedleaf.io.region.SectorFileCache sectorCache, Path directory, DataFixer dataFixer, boolean dsync) {
         this.fixerUpper = dataFixer;
-        this.regionFileCache = new RegionFileStorage(storageKey, directory, dsync, true); // Paper - rewrite chunk system; async chunk IO & Attempt to recalculate regionfile header if it is corrupt
+        this.sectorCache = sectorCache;
     }
 
     public boolean isOldChunkAround(ChunkPos chunkPos, int checkRadius) {
@@ -181,7 +181,10 @@ public class ChunkStorage implements AutoCloseable {
     }
     @Nullable
     public CompoundTag readSync(ChunkPos chunkPos) throws IOException {
-        return this.regionFileCache.read(chunkPos);
+        return this.sectorCache.read(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(),
+            chunkPos.x, chunkPos.z, ca.spottedleaf.io.region.MinecraftRegionFileType.CHUNK.getNewId()
+        );
     }
     // Paper end - async chunk io
 
@@ -193,7 +196,10 @@ public class ChunkStorage implements AutoCloseable {
                 + " but compound says coordinate is " + ChunkSerializer.getChunkCoordinate(nbt) + (world == null ? " for an unknown world" : (" for world: " + world)));
         }
         // Paper end - guard against serializing mismatching coordinates
-        this.regionFileCache.write(chunkPos, nbt); // Paper - rewrite chunk system; async chunk io, move above legacy structure index
+        this.sectorCache.write(
+            ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(),
+            chunkPos.x, chunkPos.z, ca.spottedleaf.io.region.MinecraftRegionFileType.CHUNK.getNewId(), nbt
+        );
         this.handleLegacyStructureIndex(chunkPos);
         // Paper - rewrite chunk system; async chunk io, move above legacy structure index
         return null;
@@ -213,14 +219,18 @@ public class ChunkStorage implements AutoCloseable {
     }
 
     public void close() throws IOException {
-        this.regionFileCache.close(); // Paper - nuke IO worker
+
     }
 
     public ChunkScanAccess chunkScanner() {
         // Paper start - nuke IO worker
         return ((chunkPos, streamTagVisitor) -> {
             try {
-                this.regionFileCache.scanChunk(chunkPos, streamTagVisitor);
+                this.sectorCache.scanChunk(
+                    ca.spottedleaf.io.region.SectorFileCache.getUnscopedBufferChoices(),
+                    chunkPos.x, chunkPos.z, ca.spottedleaf.io.region.MinecraftRegionFileType.CHUNK.getNewId(),
+                    streamTagVisitor
+                );
                 return java.util.concurrent.CompletableFuture.completedFuture(null);
             } catch (IOException e) {
                 throw new RuntimeException(e);
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileVersion.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileVersion.java
index f4a39f49b354c560d614483db1cd3dfc154e94b4..2bbea87a5ffe3986b5bfdbaf84e7dc7dccb9a7ec 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileVersion.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFileVersion.java
@@ -60,11 +60,7 @@ public class RegionFileVersion {
 
     // Paper start - Configurable region compression format
     public static RegionFileVersion getCompressionFormat() {
-        return switch (io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.compressionFormat) {
-            case GZIP -> VERSION_GZIP;
-            case ZLIB -> VERSION_DEFLATE;
-            case NONE -> VERSION_NONE;
-        };
+        throw new UnsupportedOperationException();
     }
     // Paper end - Configurable region compression format
     private RegionFileVersion(
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java b/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
index a4a919d8373f1535e336de7e648d41a07efb1cba..6d411313379e2b64e76484ef003f9dc873629e5f 100644
--- a/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/SectionStorage.java
@@ -32,7 +32,7 @@ import net.minecraft.world.level.ChunkPos;
 import net.minecraft.world.level.LevelHeightAccessor;
 import org.slf4j.Logger;
 
-public class SectionStorage<R> extends RegionFileStorage implements AutoCloseable { // Paper - nuke IOWorker
+public class SectionStorage<R> implements AutoCloseable { // Paper - nuke IOWorker
     private static final Logger LOGGER = LogUtils.getLogger();
     private static final String SECTIONS_TAG = "Sections";
     // Paper - remove mojang I/O thread
@@ -55,13 +55,20 @@ public class SectionStorage<R> extends RegionFileStorage implements AutoCloseabl
         RegistryAccess registryManager,
         LevelHeightAccessor world
     ) {
-        super(regionStorageInfo, path, dsync); // Paper - remove mojang I/O thread
         this.codec = codecFactory;
         this.factory = factory;
         this.registryAccess = registryManager;
         this.levelHeightAccessor = world;
     }
 
+    protected CompoundTag read(ChunkPos pos) throws IOException {
+        throw new AbstractMethodError();
+    }
+
+    protected void write(ChunkPos pos, CompoundTag tag) throws IOException {
+        throw new AbstractMethodError();
+    }
+
     protected void tick(BooleanSupplier shouldKeepTicking) {
         while (this.hasWork() && shouldKeepTicking.getAsBoolean()) {
             ChunkPos chunkPos = SectionPos.of(this.dirty.firstLong()).chunk();
@@ -233,6 +240,6 @@ public class SectionStorage<R> extends RegionFileStorage implements AutoCloseabl
 
     @Override
     public void close() throws IOException {
-        super.close(); // Paper - nuke I/O worker - don't call the worker
+
     }
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 6303760f10af17f1da1d92d6c4dc7dd6f5778f94..6853341347ac35ab7c7d0da3715a2f79e1079311 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -609,17 +609,6 @@ public class CraftWorld extends CraftRegionAccessor implements World {
                 world.getChunk(x, z); // make sure we're at ticket level 32 or lower
                 return true;
             }
-            net.minecraft.world.level.chunk.storage.RegionFile file;
-            try {
-                file = world.getChunkSource().chunkMap.regionFileCache.getRegionFile(chunkPos, false);
-            } catch (java.io.IOException ex) {
-                throw new RuntimeException(ex);
-            }
-
-            ChunkStatus status = file.getStatusIfCached(x, z);
-            if (!file.hasChunk(chunkPos) || (status != null && status != ChunkStatus.FULL)) {
-                return false;
-            }
 
             ChunkAccess chunk = world.getChunkSource().getChunk(x, z, ChunkStatus.EMPTY, true);
             if (!(chunk instanceof ImposterProtoChunk) && !(chunk instanceof net.minecraft.world.level.chunk.LevelChunk)) {