77import com .fastasyncworldedit .core .history .change .MutableEntityChange ;
88import com .fastasyncworldedit .core .history .change .MutableFullBlockChange ;
99import com .fastasyncworldedit .core .history .change .MutableTileChange ;
10+ import com .fastasyncworldedit .core .history .change .BlockPositionChange ;
1011import com .fastasyncworldedit .core .internal .exception .FaweSmallEditUnsupportedException ;
1112import com .fastasyncworldedit .core .internal .io .FaweInputStream ;
1213import com .fastasyncworldedit .core .internal .io .FaweOutputStream ;
2223import com .sk89q .worldedit .world .World ;
2324import com .sk89q .worldedit .world .biome .BiomeType ;
2425import com .sk89q .worldedit .world .block .BlockTypes ;
26+ import org .jetbrains .annotations .ApiStatus ;
2527import org .jetbrains .annotations .NotNull ;
2628import org .jetbrains .annotations .Nullable ;
2729
3032import java .io .InputStream ;
3133import java .io .OutputStream ;
3234import java .util .ArrayDeque ;
33- import java .util .Arrays ;
3435import java .util .Collections ;
3536import java .util .Iterator ;
3637import java .util .List ;
4243/**
4344 * FAWE stream ChangeSet offering support for extended-height worlds
4445 */
46+ @ ApiStatus .Internal
4547public abstract class FaweStreamChangeSet extends AbstractChangeSet {
4648
4749 public static final int HEADER_SIZE = 9 ;
@@ -85,19 +87,15 @@ private void init(boolean storeRedo, boolean smallLoc) {
8587 }
8688 }
8789
88- public interface FaweStreamPositionDelegate {
90+ interface FaweStreamPositionDelegate {
8991
9092 void write (OutputStream out , int x , int y , int z ) throws IOException ;
9193
92- int readX (FaweInputStream in ) throws IOException ;
93-
94- int readY (FaweInputStream in ) throws IOException ;
95-
96- int readZ (FaweInputStream in ) throws IOException ;
94+ void read (FaweInputStream in , BlockPositionChange change ) throws IOException ;
9795
9896 }
9997
100- public interface FaweStreamIdDelegate {
98+ interface FaweStreamIdDelegate {
10199
102100 void writeChange (FaweOutputStream out , int from , int to ) throws IOException ;
103101
@@ -155,6 +153,7 @@ public void readCombined(FaweInputStream is, MutableFullBlockChange change) thro
155153 }
156154 if (mode == 1 || mode == 4 ) { // small
157155 posDel = new FaweStreamPositionDelegate () {
156+ final byte [] buffer = new byte [4 ];
158157 int lx ;
159158 int ly ;
160159 int lz ;
@@ -179,23 +178,14 @@ public void write(OutputStream out, int x, int y, int z) throws IOException {
179178 out .write (b4 );
180179 }
181180
182- final byte [] buffer = new byte [4 ];
183-
184181 @ Override
185- public int readX ( FaweInputStream in ) throws IOException {
182+ public void read ( final FaweInputStream in , final BlockPositionChange change ) throws IOException {
186183 in .readFully (buffer );
187- return lx = lx + ((((buffer [1 ] & 0xFF ) | ((MathMan .unpair16x (buffer [3 ])) << 8 )) << 20 ) >> 20 );
184+ change .x = lx = lx + ((((buffer [1 ] & 0xFF ) | ((MathMan .unpair16x (buffer [3 ])) << 8 )) << 20 ) >> 20 );
185+ change .y = (ly = ly + buffer [0 ]) & 0xFF ;
186+ change .z = lz = lz + ((((buffer [2 ] & 0xFF ) | ((MathMan .unpair16y (buffer [3 ])) << 8 )) << 20 ) >> 20 );
188187 }
189188
190- @ Override
191- public int readY (FaweInputStream in ) {
192- return (ly = ly + buffer [0 ]) & 0xFF ;
193- }
194-
195- @ Override
196- public int readZ (FaweInputStream in ) throws IOException {
197- return lz = lz + ((((buffer [2 ] & 0xFF ) | ((MathMan .unpair16y (buffer [3 ])) << 8 )) << 20 ) >> 20 );
198- }
199189 };
200190 } else {
201191 posDel = new FaweStreamPositionDelegate () {
@@ -232,25 +222,11 @@ public void write(OutputStream stream, int x, int y, int z) throws IOException {
232222 }
233223
234224 @ Override
235- public int readX (FaweInputStream is ) throws IOException {
236- is .readFully (buffer );
237- // Don't break reading version 1 history (just in case)
238- if (version == 2 && Arrays .equals (buffer , MAGIC_NEW_RELATIVE )) {
239- lx = ((is .read () << 24 ) + (is .read () << 16 ) + (is .read () << 8 ) + is .read ());
240- lz = ((is .read () << 24 ) + (is .read () << 16 ) + (is .read () << 8 ) + is .read ());
241- is .readFully (buffer );
242- }
243- return lx = lx + ((buffer [0 ] & 0xFF ) | (buffer [1 ] << 8 ));
244- }
245-
246- @ Override
247- public int readY (FaweInputStream is ) throws IOException {
248- return ly = ly + ((buffer [4 ] & 0xFF ) | (buffer [5 ]) << 8 );
249- }
250-
251- @ Override
252- public int readZ (FaweInputStream is ) throws IOException {
253- return lz = lz + ((buffer [2 ] & 0xFF ) | (buffer [3 ]) << 8 );
225+ public void read (final FaweInputStream in , final BlockPositionChange change ) throws IOException {
226+ in .readFully (buffer );
227+ change .x = lx = lx + ((buffer [0 ] & 0xFF ) | (buffer [1 ] << 8 ));
228+ change .z = lz = lz + ((buffer [2 ] & 0xFF ) | (buffer [3 ]) << 8 );
229+ change .y = ly = ly + ((buffer [4 ] & 0xFF ) | (buffer [5 ]) << 8 );
254230 }
255231 };
256232 }
@@ -453,9 +429,9 @@ public Iterator<MutableBlockChange> getBlockIterator(final boolean dir) throws I
453429
454430 public MutableBlockChange read () {
455431 try {
456- change . x = posDel .readX (is ) + originX ;
457- change .y = posDel . readY ( is ) ;
458- change .z = posDel . readZ ( is ) + originZ ;
432+ posDel .read (is , change ) ;
433+ change .x += originX ;
434+ change .z += originZ ;
459435 idDel .readCombined (is , change , dir );
460436 return change ;
461437 } catch (EOFException ignored ) {
@@ -569,9 +545,9 @@ public Iterator<MutableFullBlockChange> getFullBlockIterator(BlockBag blockBag,
569545
570546 public MutableFullBlockChange read () {
571547 try {
572- change . x = posDel .readX (is ) + originX ;
573- change .y = posDel . readY ( is ) ;
574- change .z = posDel . readZ ( is ) + originZ ;
548+ posDel .read (is , change ) ;
549+ change .x += originX ;
550+ change .z += originZ ;
575551 idDel .readCombined (is , change );
576552 return change ;
577553 } catch (EOFException ignored ) {
@@ -873,10 +849,10 @@ class Populator implements ChangePopulator<MutableFullBlockChange> {
873849 @ Override
874850 public @ Nullable MutableFullBlockChange populate (@ NotNull final MutableFullBlockChange change ) {
875851 try {
876- change .x = posDel .readX (is ) + originX ;
877- change .y = posDel .readY (is );
878- change .z = posDel .readZ (is ) + originZ ;
852+ posDel .read (is , change );
879853 idDel .readCombined (is , change );
854+ change .x += originX ;
855+ change .z += originZ ;
880856 return change ;
881857 } catch (EOFException ignored ) {
882858 } catch (Exception e ) {
@@ -915,10 +891,10 @@ class Populator implements ChangePopulator<MutableBlockChange> {
915891 @ Override
916892 public @ Nullable MutableBlockChange populate (@ NotNull final MutableBlockChange change ) {
917893 try {
918- change .x = posDel .readX (is ) + originX ;
919- change .y = posDel .readY (is );
920- change .z = posDel .readZ (is ) + originZ ;
894+ posDel .read (is , change );
921895 idDel .readCombined (is , change , dir );
896+ change .x += originX ;
897+ change .z += originZ ;
922898 return change ;
923899 } catch (EOFException ignored ) {
924900 } catch (Exception e ) {
@@ -1069,11 +1045,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
10691045 int amount = (Settings .settings ().HISTORY .BUFFER_SIZE - HEADER_SIZE ) / 9 ;
10701046 MutableFullBlockChange change = new MutableFullBlockChange (null , 0 , false );
10711047 for (int i = 0 ; i < amount ; i ++) {
1072- int x = posDel .readX (fis ) + ox ;
1073- int y = posDel .readY (fis );
1074- int z = posDel .readZ (fis ) + ox ;
1048+ posDel .read (fis , change );
10751049 idDel .readCombined (fis , change );
1076- summary .add (x , z , change .to );
1050+ summary .add (change . x + ox , change . z + oz , change .to );
10771051 }
10781052 }
10791053 } catch (EOFException ignored ) {
0 commit comments