Skip to content

Commit 37738ab

Browse files
committed
History: Read change positions at once (#2542)
1 parent a64e09a commit 37738ab

4 files changed

Lines changed: 46 additions & 62 deletions

File tree

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: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fastasyncworldedit.core.history.change.MutableEntityChange;
77
import com.fastasyncworldedit.core.history.change.MutableFullBlockChange;
88
import com.fastasyncworldedit.core.history.change.MutableTileChange;
9+
import com.fastasyncworldedit.core.history.change.BlockPositionChange;
910
import com.fastasyncworldedit.core.internal.exception.FaweSmallEditUnsupportedException;
1011
import com.fastasyncworldedit.core.internal.io.FaweInputStream;
1112
import com.fastasyncworldedit.core.internal.io.FaweOutputStream;
@@ -20,6 +21,7 @@
2021
import com.sk89q.worldedit.world.World;
2122
import com.sk89q.worldedit.world.biome.BiomeType;
2223
import com.sk89q.worldedit.world.block.BlockTypes;
24+
import org.jetbrains.annotations.ApiStatus;
2325

2426
import java.io.EOFException;
2527
import java.io.IOException;
@@ -33,6 +35,7 @@
3335
/**
3436
* FAWE stream ChangeSet offering support for extended-height worlds
3537
*/
38+
@ApiStatus.Internal
3639
public 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

Comments
 (0)