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 ) {
@@ -570,9 +546,9 @@ public Iterator<MutableFullBlockChange> getFullBlockIterator(BlockBag blockBag,
570546
571547 public MutableFullBlockChange read () {
572548 try {
573- change . x = posDel .readX (is ) + originX ;
574- change .y = posDel . readY ( is ) ;
575- change .z = posDel . readZ ( is ) + originZ ;
549+ posDel .read (is , change ) ;
550+ change .x += originX ;
551+ change .z += originZ ;
576552 idDel .readCombined (is , change );
577553 return change ;
578554 } catch (EOFException ignored ) {
@@ -872,10 +848,9 @@ class Populator implements ChangePopulator<MutableFullBlockChange> {
872848 @ Override
873849 public @ Nullable MutableFullBlockChange populate (@ NotNull final MutableFullBlockChange change ) {
874850 try {
875- change .x = posDel .readX (is ) + originX ;
876- change .y = posDel .readY (is );
877- change .z = posDel .readZ (is ) + originZ ;
878851 idDel .readCombined (is , change );
852+ change .x += originX ;
853+ change .z += originZ ;
879854 return change ;
880855 } catch (EOFException ignored ) {
881856 } catch (Exception e ) {
@@ -914,10 +889,9 @@ class Populator implements ChangePopulator<MutableBlockChange> {
914889 @ Override
915890 public @ Nullable MutableBlockChange populate (@ NotNull final MutableBlockChange change ) {
916891 try {
917- change .x = posDel .readX (is ) + originX ;
918- change .y = posDel .readY (is );
919- change .z = posDel .readZ (is ) + originZ ;
920892 idDel .readCombined (is , change , dir );
893+ change .x += originX ;
894+ change .z += originZ ;
921895 return change ;
922896 } catch (EOFException ignored ) {
923897 } catch (Exception e ) {
@@ -1068,11 +1042,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
10681042 int amount = (Settings .settings ().HISTORY .BUFFER_SIZE - HEADER_SIZE ) / 9 ;
10691043 MutableFullBlockChange change = new MutableFullBlockChange (null , 0 , false );
10701044 for (int i = 0 ; i < amount ; i ++) {
1071- int x = posDel .readX (fis ) + ox ;
1072- int y = posDel .readY (fis );
1073- int z = posDel .readZ (fis ) + ox ;
1045+ posDel .read (fis , change );
10741046 idDel .readCombined (fis , change );
1075- summary .add (x , z , change .to );
1047+ summary .add (change . x + ox , change . z + oz , change .to );
10761048 }
10771049 }
10781050 } catch (EOFException ignored ) {
0 commit comments