2525public class BranchDestructionData {
2626 public final Species species ; // The species of the tree that was harvested
2727 public final int [] destroyedBranchesRadiusPosition ; // Encoded branch radius and relative positions
28- public final int [] destroyedBranchesConnections ; // Encoded branch shapes
28+ public final long [] destroyedBranchesConnections ; // Encoded branch shapes
2929 public final int [] destroyedBranchesBlockIndex ; // Encoded valid branch block index for family
3030 public final int [] destroyedLeaves ; // Encoded leaves relative positions
3131 public final int [] destroyedLeavesBlockIndex ; // Encoded valid leaves block index for species
@@ -42,7 +42,7 @@ public class BranchDestructionData {
4242
4343 public BranchDestructionData () {
4444 this .species = Species .NULL_SPECIES ;
45- this .destroyedBranchesConnections = new int [0 ];
45+ this .destroyedBranchesConnections = new long [0 ];
4646 this .destroyedBranchesRadiusPosition = new int [0 ];
4747 this .destroyedBranchesBlockIndex = new int [0 ];
4848 this .destroyedLeaves = new int [0 ];
@@ -64,8 +64,8 @@ public BranchDestructionData(Species species, Map<BlockPos, BranchConnectionData
6464 this .species = species ;
6565 int [][] encodedBranchData = convertBranchesToIntArrays (branches );
6666 this .destroyedBranchesRadiusPosition = encodedBranchData [0 ];
67- this .destroyedBranchesConnections = encodedBranchData [ 1 ] ;
68- this .destroyedBranchesBlockIndex = encodedBranchData [2 ];
67+ this .destroyedBranchesConnections = convertBranchConnectionsToLongArray ( branches ) ;
68+ this .destroyedBranchesBlockIndex = encodedBranchData [1 ];
6969 int [][] encodedLeavesData = convertLeavesToIntArray (leaves , species );
7070 this .destroyedLeaves = encodedLeavesData [0 ];
7171 this .destroyedLeavesBlockIndex = encodedLeavesData [1 ];
@@ -122,7 +122,7 @@ public BranchDestructionData merge (BranchDestructionData other){
122122 public BranchDestructionData (CompoundTag nbt ) {
123123 this .species = TreeRegistry .findSpecies (new ResourceLocation (nbt .getString ("species" )));
124124 this .destroyedBranchesRadiusPosition = nbt .getIntArray ("branchpos" );
125- this .destroyedBranchesConnections = nbt .getIntArray ("branchcon" );
125+ this .destroyedBranchesConnections = nbt .getLongArray ("branchcon" );
126126 this .destroyedBranchesBlockIndex = nbt .getIntArray ("branchblock" );
127127 this .destroyedLeaves = nbt .getIntArray ("leavespos" );
128128 this .destroyedLeavesBlockIndex = nbt .getIntArray ("leavesblock" );
@@ -139,7 +139,7 @@ public BranchDestructionData(CompoundTag nbt) {
139139 public CompoundTag writeToNBT (CompoundTag tag ) {
140140 tag .putString ("species" , species .getRegistryName ().toString ());
141141 tag .putIntArray ("branchpos" , destroyedBranchesRadiusPosition );
142- tag .putIntArray ("branchcon" , destroyedBranchesConnections );
142+ tag .putLongArray ("branchcon" , destroyedBranchesConnections );
143143 tag .putIntArray ("branchblock" , destroyedBranchesBlockIndex );
144144 tag .putIntArray ("leavespos" , destroyedLeaves );
145145 tag .putIntArray ("leavesblock" , destroyedLeavesBlockIndex );
@@ -163,7 +163,6 @@ public CompoundTag writeToNBT(CompoundTag tag) {
163163
164164 private int [][] convertBranchesToIntArrays (Map <BlockPos , BranchConnectionData > branchList ) {
165165 int [] radPosData = new int [branchList .size ()];
166- int [] connectionData = new int [branchList .size ()];
167166 int [] blockIndexData = new int [branchList .size ()];
168167 int index = 0 ;
169168
@@ -172,75 +171,71 @@ private int[][] convertBranchesToIntArrays(Map<BlockPos, BranchConnectionData> b
172171 if (origConnData != null ) {
173172 BlockState origState = origConnData .getBlockState ();
174173 radPosData [index ] = encodeBranchesRadiusPos (BlockPos .ZERO , (BranchBlock ) origState .getBlock (), origState );
175- connectionData [index ] = encodeBranchesConnections (origConnData .getConnections ());
176174 blockIndexData [index ++] = encodeBranchBlocks ((BranchBlock ) origState .getBlock ());
177175 }
178176
179- //Encode the remaining blocks
180177 for (Entry <BlockPos , BranchConnectionData > set : branchList .entrySet ()) {
181178 if (set .getKey ().equals (BlockPos .ZERO )) continue ;
182179 BlockPos relPos = set .getKey ();
183180 BranchConnectionData connData = set .getValue ();
184181 BlockState state = connData .getBlockState ();
185182 Block block = state .getBlock ();
186183
187- if (block instanceof BranchBlock && bounds .inBounds (relPos )) { //Place comfortable limits on the system
184+ if (block instanceof BranchBlock && bounds .inBounds (relPos )) {
188185 radPosData [index ] = encodeBranchesRadiusPos (relPos , (BranchBlock ) block , state );
189- connectionData [index ] = encodeBranchesConnections (connData .getConnections ());
190186 blockIndexData [index ++] = encodeBranchBlocks ((BranchBlock ) block );
191187 }
192188 }
193189
194- //Shrink down the arrays
195190 radPosData = Arrays .copyOf (radPosData , index );
196- connectionData = Arrays .copyOf (connectionData , index );
197191 blockIndexData = Arrays .copyOf (blockIndexData , index );
198192
199- return new int [][]{radPosData , connectionData , blockIndexData };
193+ return new int [][]{radPosData , blockIndexData };
194+ }
195+
196+ private long [] convertBranchConnectionsToLongArray (Map <BlockPos , BranchConnectionData > branchList ) {
197+ long [] connectionData = new long [branchList .size ()];
198+ int index = 0 ;
199+
200+ //Ensure the origin block is at the first index
201+ BranchConnectionData origConnData = branchList .get (BlockPos .ZERO );
202+ if (origConnData != null ) {
203+ connectionData [index ++] = encodeBranchesConnections (origConnData .getConnections ());
204+ }
205+
206+ for (Entry <BlockPos , BranchConnectionData > set : branchList .entrySet ()) {
207+ if (set .getKey ().equals (BlockPos .ZERO )) continue ;
208+ BlockPos relPos = set .getKey ();
209+ BranchConnectionData connData = set .getValue ();
210+ BlockState state = connData .getBlockState ();
211+ Block block = state .getBlock ();
212+
213+ if (block instanceof BranchBlock && bounds .inBounds (relPos )) {
214+ connectionData [index ++] = encodeBranchesConnections (connData .getConnections ());
215+ }
216+ }
217+
218+ return Arrays .copyOf (connectionData , index );
200219 }
201220
202221 private int encodeBranchesRadiusPos (BlockPos relPos , BranchBlock branchBlock , BlockState state ) {
203- return ((branchBlock .getRadius (state ) & 0x1F ) << 24 ) | //Radius 0 - 31
222+ return ((branchBlock .getRadius (state ) & 0xFF ) << 24 ) | // Radius 0 - 255
204223 encodeRelBlockPos (relPos );
205224 }
206225
207- private int encodeBranchesConnections (Connections exState ) {
208- int result = 0 ;
226+ private long encodeBranchesConnections (Connections exState ) {
227+ long result = 0 ;
209228 int [] radii = exState .getAllRadii ();
210229 for (Direction face : Direction .values ()) {
211230 int faceIndex = face .get3DDataValue ();
212231 int rad = radii [faceIndex ];
213- result |= (rad & 0x1F ) << (faceIndex * 5 ); //5 bits per face * 6 faces = 30bits
232+ result |= (( long )( rad & 0xFF )) << (faceIndex * 8 ); // 8 bits per face * 6 faces = 48 bits
214233 }
215234 return result ;
216235 }
217236
218- private int encodeBranchBlocks (BranchBlock branch ) {
219- return branch .getFamily ().getBranchBlockIndex (branch );
220- }
221-
222- public int getNumBranches () {
223- return destroyedBranchesRadiusPosition .length ;
224- }
225-
226- public BlockPos getBranchRelPos (int index ) {
227- BlockPos pos = decodeRelPos (destroyedBranchesRadiusPosition [index ]);
228- if (basePos != cutPos ){ //When a root system is involved, the relative positions are moved down
229- return pos .offset (getRelativeCutPos ());
230- }
231- return pos ;
232- }
233-
234- public BlockPos getRelativeCutPos (){
235- return cutPos .subtract (basePos );
236- }
237-
238- public int getBranchRadius (int index ) {
239- return decodeBranchRadius (destroyedBranchesRadiusPosition [index ]);
240- }
241-
242237 private int decodeBranchRadius (int encoded ) {
243- return (encoded >> 24 ) & 0x1F ;
238+ return (encoded >> 24 ) & 0xFF ;
244239 }
245240
246241 @ Nullable
@@ -256,14 +251,34 @@ public BlockState getBranchBlockState(int index) {
256251 }
257252
258253 public void getConnections (int index , int [] connections ) {
259- int encodedConnections = destroyedBranchesConnections [index ];
254+ long encodedConnections = destroyedBranchesConnections [index ];
260255
261256 for (Direction face : Direction .values ()) {
262- int rad = (encodedConnections >> (face .get3DDataValue () * 5 ) & 0x1F );
257+ int rad = (int )(( encodedConnections >> (face .get3DDataValue () * 8 )) & 0xFF );
263258 connections [face .get3DDataValue ()] = Math .max (0 , rad );
264259 }
265260 }
266261
262+ private int encodeBranchBlocks (BranchBlock branch ) {
263+ return branch .getFamily ().getBranchBlockIndex (branch );
264+ }
265+
266+ public int getNumBranches () {
267+ return destroyedBranchesRadiusPosition .length ;
268+ }
269+
270+ public BlockPos getBranchRelPos (int index ) {
271+ BlockPos pos = decodeRelPos (destroyedBranchesRadiusPosition [index ]);
272+ if (basePos != cutPos ) { // When a root system is involved, the relative positions are moved down
273+ return pos .offset (getRelativeCutPos ());
274+ }
275+ return pos ;
276+ }
277+
278+ public int getBranchRadius (int index ) {
279+ return decodeBranchRadius (destroyedBranchesRadiusPosition [index ]);
280+ }
281+
267282 public static class BlockStateWithConnections {
268283 private final BlockState blockState ;
269284 private final int [] connections ;
@@ -459,4 +474,8 @@ public static BlockPos decodeRelPos(int encoded) {
459474 );
460475 }
461476
477+ public BlockPos getRelativeCutPos () {
478+ return cutPos .subtract (basePos );
479+ }
480+
462481}
0 commit comments