66import com .fastasyncworldedit .core .history .change .MutableEntityChange ;
77import com .fastasyncworldedit .core .history .change .MutableFullBlockChange ;
88import com .fastasyncworldedit .core .history .change .MutableTileChange ;
9+ import com .fastasyncworldedit .core .history .change .BlockPositionChange ;
910import com .fastasyncworldedit .core .internal .exception .FaweSmallEditUnsupportedException ;
1011import com .fastasyncworldedit .core .internal .io .FaweInputStream ;
1112import com .fastasyncworldedit .core .internal .io .FaweOutputStream ;
2021import com .sk89q .worldedit .world .World ;
2122import com .sk89q .worldedit .world .biome .BiomeType ;
2223import com .sk89q .worldedit .world .block .BlockTypes ;
24+ import org .jetbrains .annotations .ApiStatus ;
2325
2426import java .io .EOFException ;
2527import java .io .IOException ;
3335/**
3436 * FAWE stream ChangeSet offering support for extended-height worlds
3537 */
38+ @ ApiStatus .Internal
3639public abstract class FaweStreamChangeSet extends AbstractChangeSet {
3740
3841 public static final int HEADER_SIZE = 9 ;
@@ -76,19 +79,15 @@ private void init(boolean storeRedo, boolean smallLoc) {
7679 }
7780 }
7881
79- public interface FaweStreamPositionDelegate {
82+ interface FaweStreamPositionDelegate {
8083
8184 void write (OutputStream out , int x , int y , int z ) throws IOException ;
8285
83- int readX (FaweInputStream in ) throws IOException ;
84-
85- int readY (FaweInputStream in ) throws IOException ;
86-
87- int readZ (FaweInputStream in ) throws IOException ;
86+ void read (FaweInputStream in , BlockPositionChange change ) throws IOException ;
8887
8988 }
9089
91- public interface FaweStreamIdDelegate {
90+ interface FaweStreamIdDelegate {
9291
9392 void writeChange (FaweOutputStream out , int from , int to ) throws IOException ;
9493
@@ -146,6 +145,7 @@ public void readCombined(FaweInputStream is, MutableFullBlockChange change) thro
146145 }
147146 if (mode == 1 || mode == 4 ) { // small
148147 posDel = new FaweStreamPositionDelegate () {
148+ final byte [] buffer = new byte [4 ];
149149 int lx ;
150150 int ly ;
151151 int lz ;
@@ -170,23 +170,14 @@ public void write(OutputStream out, int x, int y, int z) throws IOException {
170170 out .write (b4 );
171171 }
172172
173- final byte [] buffer = new byte [4 ];
174-
175173 @ Override
176- public int readX ( FaweInputStream in ) throws IOException {
174+ public void read ( final FaweInputStream in , final BlockPositionChange change ) throws IOException {
177175 in .readFully (buffer );
178- return lx = lx + ((((buffer [1 ] & 0xFF ) | ((MathMan .unpair16x (buffer [3 ])) << 8 )) << 20 ) >> 20 );
176+ change .x = lx = lx + ((((buffer [1 ] & 0xFF ) | ((MathMan .unpair16x (buffer [3 ])) << 8 )) << 20 ) >> 20 );
177+ change .y = (ly = ly + buffer [0 ]) & 0xFF ;
178+ change .z = lz = lz + ((((buffer [2 ] & 0xFF ) | ((MathMan .unpair16y (buffer [3 ])) << 8 )) << 20 ) >> 20 );
179179 }
180180
181- @ Override
182- public int readY (FaweInputStream in ) {
183- return (ly = ly + buffer [0 ]) & 0xFF ;
184- }
185-
186- @ Override
187- public int readZ (FaweInputStream in ) throws IOException {
188- return lz = lz + ((((buffer [2 ] & 0xFF ) | ((MathMan .unpair16y (buffer [3 ])) << 8 )) << 20 ) >> 20 );
189- }
190181 };
191182 } else {
192183 posDel = new FaweStreamPositionDelegate () {
@@ -223,25 +214,11 @@ public void write(OutputStream stream, int x, int y, int z) throws IOException {
223214 }
224215
225216 @ Override
226- public int readX (FaweInputStream is ) throws IOException {
227- is .readFully (buffer );
228- // Don't break reading version 1 history (just in case)
229- if (version == 2 && Arrays .equals (buffer , MAGIC_NEW_RELATIVE )) {
230- lx = ((is .read () << 24 ) + (is .read () << 16 ) + (is .read () << 8 ) + is .read ());
231- lz = ((is .read () << 24 ) + (is .read () << 16 ) + (is .read () << 8 ) + is .read ());
232- is .readFully (buffer );
233- }
234- return lx = lx + ((buffer [0 ] & 0xFF ) | (buffer [1 ] << 8 ));
235- }
236-
237- @ Override
238- public int readY (FaweInputStream is ) throws IOException {
239- return ly = ly + ((buffer [4 ] & 0xFF ) | (buffer [5 ]) << 8 );
240- }
241-
242- @ Override
243- public int readZ (FaweInputStream is ) throws IOException {
244- return lz = lz + ((buffer [2 ] & 0xFF ) | (buffer [3 ]) << 8 );
217+ public void read (final FaweInputStream in , final BlockPositionChange change ) throws IOException {
218+ in .readFully (buffer );
219+ change .x = lx = lx + ((buffer [0 ] & 0xFF ) | (buffer [1 ] << 8 ));
220+ change .z = lz = lz + ((buffer [2 ] & 0xFF ) | (buffer [3 ]) << 8 );
221+ change .y = ly = ly + ((buffer [4 ] & 0xFF ) | (buffer [5 ]) << 8 );
245222 }
246223 };
247224 }
@@ -456,9 +433,9 @@ public Iterator<MutableBlockChange> getBlockIterator(final boolean dir) throws I
456433
457434 public MutableBlockChange read () {
458435 try {
459- change . x = posDel .readX (is ) + originX ;
460- change .y = posDel . readY ( is ) ;
461- change .z = posDel . readZ ( is ) + originZ ;
436+ posDel .read (is , change ) ;
437+ change .x += originX ;
438+ change .z += originZ ;
462439 idDel .readCombined (is , change , dir );
463440 return change ;
464441 } catch (EOFException ignored ) {
@@ -573,9 +550,9 @@ public Iterator<MutableFullBlockChange> getFullBlockIterator(BlockBag blockBag,
573550
574551 public MutableFullBlockChange read () {
575552 try {
576- change . x = posDel .readX (is ) + originX ;
577- change .y = posDel . readY ( is ) ;
578- change .z = posDel . readZ ( is ) + originZ ;
553+ posDel .read (is , change ) ;
554+ change .x += originX ;
555+ change .z += originZ ;
579556 idDel .readCombined (is , change );
580557 return change ;
581558 } catch (EOFException ignored ) {
@@ -793,11 +770,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
793770 int amount = (Settings .settings ().HISTORY .BUFFER_SIZE - HEADER_SIZE ) / 9 ;
794771 MutableFullBlockChange change = new MutableFullBlockChange (null , 0 , false );
795772 for (int i = 0 ; i < amount ; i ++) {
796- int x = posDel .readX (fis ) + ox ;
797- int y = posDel .readY (fis );
798- int z = posDel .readZ (fis ) + ox ;
773+ posDel .read (fis , change );
799774 idDel .readCombined (fis , change );
800- summary .add (x , z , change .to );
775+ summary .add (change . x + ox , change . z + oz , change .to );
801776 }
802777 }
803778 } catch (EOFException ignored ) {
0 commit comments