44import java .util .ArrayList ;
55import java .util .BitSet ;
66import java .util .List ;
7+ import java .util .Map ;
78
9+ import com .comphenix .protocol .reflect .EquivalentConverter ;
810import org .jetbrains .annotations .Nullable ;
911
1012import com .comphenix .protocol .injector .StructureCache ;
@@ -41,6 +43,7 @@ public static final class ChunkData extends AbstractWrapper {
4143
4244 private static final FieldAccessor BLOCK_ENTITIES_DATA_ACCESSOR ;
4345 private static final FieldAccessor HEIGHTMAPS_ACCESSOR ;
46+ private static final EquivalentConverter <Map <EnumWrappers .HeightmapType , long []>> HEIGHTMAPS_CONVERTER ;
4447 private static final FieldAccessor BUFFER_ACCESSOR ;
4548
4649 static {
@@ -54,9 +57,17 @@ public static final class ChunkData extends AbstractWrapper {
5457 BLOCK_ENTITIES_DATA_ACCESSOR = Accessors .getFieldAccessor (reflection .getField (FuzzyFieldContract .newBuilder ()
5558 .typeExact (List .class )
5659 .build ()));
57- HEIGHTMAPS_ACCESSOR = Accessors .getFieldAccessor (reflection .getField (FuzzyFieldContract .newBuilder ()
58- .typeExact (MinecraftReflection .getNBTCompoundClass ())
59- .build ()));
60+ if (MinecraftVersion .v1_21_5 .atOrAbove ()) {
61+ HEIGHTMAPS_ACCESSOR = Accessors .getFieldAccessor (reflection .getField (FuzzyFieldContract .newBuilder ()
62+ .typeExact (Map .class )
63+ .build ()));
64+ HEIGHTMAPS_CONVERTER = BukkitConverters .getMapConverter (EnumWrappers .getHeightmapTypeConverter (), Converters .passthrough (long [].class ));
65+ } else {
66+ HEIGHTMAPS_ACCESSOR = Accessors .getFieldAccessor (reflection .getField (FuzzyFieldContract .newBuilder ()
67+ .typeExact (MinecraftReflection .getNBTCompoundClass ())
68+ .build ()));
69+ HEIGHTMAPS_CONVERTER = null ;
70+ }
6071 BUFFER_ACCESSOR = Accessors .getFieldAccessor (reflection .getField (FuzzyFieldContract .newBuilder ().typeExact (byte [].class ).build ()));
6172 }
6273
@@ -68,22 +79,50 @@ public ChunkData(Object handle) {
6879
6980 /**
7081 * The heightmap of this chunk.
82+ * <p>
83+ * Removed in Minecraft 1.21.5.
7184 *
7285 * @return an NBT-Tag
86+ * @deprecated Use {@link ChunkData#getHeightmaps()} instead.
7387 */
88+ @ Deprecated
7489 public NbtCompound getHeightmapsTag () {
7590 return NbtFactory .fromNMSCompound (HEIGHTMAPS_ACCESSOR .get (handle ));
7691 }
7792
7893 /**
7994 * Sets the heightmap tag of this chunk.
95+ * <p>
96+ * Removed in Minecraft 1.21.5.
8097 *
8198 * @param heightmapsTag the new heightmaps tag.
99+ * @deprecated Use {@link ChunkData#setHeightmaps(Map)} instead.
82100 */
101+ @ Deprecated
83102 public void setHeightmapsTag (NbtCompound heightmapsTag ) {
84103 HEIGHTMAPS_ACCESSOR .set (handle , NbtFactory .fromBase (heightmapsTag ).getHandle ());
85104 }
86105
106+ /**
107+ * The heightmap of this chunk.
108+ *
109+ * @return a map containing the heightmaps
110+ */
111+ public Map <EnumWrappers .HeightmapType , long []> getHeightmaps () {
112+ return HEIGHTMAPS_CONVERTER .getSpecific (HEIGHTMAPS_ACCESSOR .get (handle ));
113+ }
114+
115+ /**
116+ * Sets the heightmap tag of this chunk.
117+ * <p>
118+ * Removed in Minecraft 1.21.5.
119+ *
120+ * @param heightmaps the new heightmaps.
121+ */
122+ public void setHeightmaps (Map <EnumWrappers .HeightmapType , long []> heightmaps ) {
123+ HEIGHTMAPS_ACCESSOR .set (handle , HEIGHTMAPS_CONVERTER .getGeneric (heightmaps ));
124+ }
125+
87126 /**
88127 * The actual structural data of this chunk as bytes.
89128 *
@@ -130,12 +169,16 @@ public void setBlockEntityInfo(List<BlockEntityInfo> blockEntityInfo) {
130169
131170 /**
132171 * Creates a new wrapper using predefined values.
172+ * <p>
173+ * Removed in Minecraft 1.21.5.
133174 *
134175 * @param heightmapsTag the heightmaps tag
135176 * @param buffer the buffer
136177 * @param blockEntityInfo a list of wrapped block entities
137178 * @return a newly created wrapper
179+ * @deprecated Use {@link ChunkData#fromValues(Map, byte[], List)} instead.
138180 */
181+ @ Deprecated
139182 public static ChunkData fromValues (NbtCompound heightmapsTag , byte [] buffer , List <BlockEntityInfo > blockEntityInfo ) {
140183 ChunkData data = new ChunkData (LEVEL_CHUNK_PACKET_DATA_CONSTRUCTOR .invoke (StructureCache .newNullDataSerializer (), 0 , 0 ));
141184
@@ -145,6 +188,24 @@ public static ChunkData fromValues(NbtCompound heightmapsTag, byte[] buffer, Lis
145188
146189 return new ChunkData (data );
147190 }
191+
192+ /**
193+ * Creates a new wrapper using predefined values.
194+ *
195+ * @param heightmaps the heightmaps
196+ * @param buffer the buffer
197+ * @param blockEntityInfo a list of wrapped block entities
198+ * @return a newly created wrapper
199+ */
200+ public static ChunkData fromValues (Map <EnumWrappers .HeightmapType , long []> heightmaps , byte [] buffer , List <BlockEntityInfo > blockEntityInfo ) {
201+ ChunkData data = new ChunkData (LEVEL_CHUNK_PACKET_DATA_CONSTRUCTOR .invoke (StructureCache .newNullDataSerializer (), 0 , 0 ));
202+
203+ data .setHeightmaps (heightmaps );
204+ data .setBuffer (buffer );
205+ data .setBlockEntityInfo (blockEntityInfo );
206+
207+ return new ChunkData (data );
208+ }
148209 }
149210
150211 /**
0 commit comments