Skip to content

Commit 28bd065

Browse files
committed
History: Read change positions at once (#2542)
1 parent c252850 commit 28bd065

File tree

4 files changed

+52
-69
lines changed

4 files changed

+52
-69
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fastasyncworldedit.core.history.change;
2+
3+
import com.sk89q.worldedit.history.change.Change;
4+
import org.jetbrains.annotations.ApiStatus;
5+
6+
/**
7+
* Represents a change that is associated with {@code (x, y, z)} block coordinates.
8+
* @since TODO
9+
*/
10+
@ApiStatus.Internal
11+
public sealed abstract class BlockPositionChange implements Change
12+
permits MutableBlockChange, MutableFullBlockChange {
13+
public int x;
14+
public int y;
15+
public int z;
16+
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableBlockChange.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
import com.sk89q.worldedit.WorldEditException;
44
import com.sk89q.worldedit.history.UndoContext;
5-
import com.sk89q.worldedit.history.change.Change;
65
import com.sk89q.worldedit.world.block.BlockState;
6+
import org.jetbrains.annotations.ApiStatus;
77

8-
public class MutableBlockChange implements Change {
9-
10-
public int z;
11-
public int y;
12-
public int x;
8+
@ApiStatus.Internal
9+
public final class MutableBlockChange extends BlockPositionChange {
1310
public int ordinal;
1411

15-
1612
public MutableBlockChange(int x, int y, int z, int ordinal) {
1713
this.x = x;
1814
this.y = y;

worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableFullBlockChange.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
import com.sk89q.worldedit.extent.inventory.BlockBag;
55
import com.sk89q.worldedit.extent.inventory.BlockBagException;
66
import com.sk89q.worldedit.history.UndoContext;
7-
import com.sk89q.worldedit.history.change.Change;
87
import com.sk89q.worldedit.world.block.BlockState;
98
import com.sk89q.worldedit.world.block.BlockTypesCache;
9+
import org.jetbrains.annotations.ApiStatus;
1010

11-
public class MutableFullBlockChange implements Change {
12-
13-
public int z;
14-
public int y;
15-
public int x;
11+
@ApiStatus.Internal
12+
public final class MutableFullBlockChange extends BlockPositionChange {
1613
public int from;
1714
public int to;
1815
public BlockBag blockBag;

worldedit-core/src/main/java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSet.java

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fastasyncworldedit.core.history.change.MutableEntityChange;
88
import com.fastasyncworldedit.core.history.change.MutableFullBlockChange;
99
import com.fastasyncworldedit.core.history.change.MutableTileChange;
10+
import com.fastasyncworldedit.core.history.change.BlockPositionChange;
1011
import com.fastasyncworldedit.core.internal.exception.FaweSmallEditUnsupportedException;
1112
import com.fastasyncworldedit.core.internal.io.FaweInputStream;
1213
import com.fastasyncworldedit.core.internal.io.FaweOutputStream;
@@ -22,6 +23,7 @@
2223
import com.sk89q.worldedit.world.World;
2324
import com.sk89q.worldedit.world.biome.BiomeType;
2425
import com.sk89q.worldedit.world.block.BlockTypes;
26+
import org.jetbrains.annotations.ApiStatus;
2527
import org.jetbrains.annotations.NotNull;
2628
import org.jetbrains.annotations.Nullable;
2729

@@ -30,7 +32,6 @@
3032
import java.io.InputStream;
3133
import java.io.OutputStream;
3234
import java.util.ArrayDeque;
33-
import java.util.Arrays;
3435
import java.util.Collections;
3536
import java.util.Iterator;
3637
import java.util.List;
@@ -42,6 +43,7 @@
4243
/**
4344
* FAWE stream ChangeSet offering support for extended-height worlds
4445
*/
46+
@ApiStatus.Internal
4547
public 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,10 @@ 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;
851+
posDel.read(is, change);
878852
idDel.readCombined(is, change);
853+
change.x += originX;
854+
change.z += originZ;
879855
return change;
880856
} catch (EOFException ignored) {
881857
} catch (Exception e) {
@@ -914,10 +890,10 @@ class Populator implements ChangePopulator<MutableBlockChange> {
914890
@Override
915891
public @Nullable MutableBlockChange populate(@NotNull final MutableBlockChange change) {
916892
try {
917-
change.x = posDel.readX(is) + originX;
918-
change.y = posDel.readY(is);
919-
change.z = posDel.readZ(is) + originZ;
893+
posDel.read(is, change);
920894
idDel.readCombined(is, change, dir);
895+
change.x += originX;
896+
change.z += originZ;
921897
return change;
922898
} catch (EOFException ignored) {
923899
} catch (Exception e) {
@@ -1068,11 +1044,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
10681044
int amount = (Settings.settings().HISTORY.BUFFER_SIZE - HEADER_SIZE) / 9;
10691045
MutableFullBlockChange change = new MutableFullBlockChange(null, 0, false);
10701046
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;
1047+
posDel.read(fis, change);
10741048
idDel.readCombined(fis, change);
1075-
summary.add(x, z, change.to);
1049+
summary.add(change.x + ox, change.z + oz, change.to);
10761050
}
10771051
}
10781052
} catch (EOFException ignored) {

0 commit comments

Comments
 (0)