Skip to content

Commit a31f2f3

Browse files
authored
History: Read change positions at once (#2542)
1 parent fe626a9 commit a31f2f3

4 files changed

Lines changed: 46 additions & 56 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 & 43 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;
@@ -32,6 +34,7 @@
3234
/**
3335
* FAWE stream ChangeSet offering support for extended-height worlds
3436
*/
37+
@ApiStatus.Internal
3538
public abstract class FaweStreamChangeSet extends AbstractChangeSet {
3639

3740
public static final int HEADER_SIZE = 9;
@@ -68,19 +71,15 @@ private void init(boolean storeRedo, boolean smallLoc) {
6871
}
6972
}
7073

71-
public interface FaweStreamPositionDelegate {
74+
interface FaweStreamPositionDelegate {
7275

7376
void write(OutputStream out, int x, int y, int z) throws IOException;
7477

75-
int readX(FaweInputStream in) throws IOException;
76-
77-
int readY(FaweInputStream in) throws IOException;
78-
79-
int readZ(FaweInputStream in) throws IOException;
78+
void read(FaweInputStream in, BlockPositionChange change) throws IOException;
8079

8180
}
8281

83-
public interface FaweStreamIdDelegate {
82+
interface FaweStreamIdDelegate {
8483

8584
void writeChange(FaweOutputStream out, int from, int to) throws IOException;
8685

@@ -138,6 +137,7 @@ public void readCombined(FaweInputStream is, MutableFullBlockChange change) thro
138137
}
139138
if (mode == 1 || mode == 4) { // small
140139
posDel = new FaweStreamPositionDelegate() {
140+
final byte[] buffer = new byte[4];
141141
int lx;
142142
int ly;
143143
int lz;
@@ -162,23 +162,14 @@ public void write(OutputStream out, int x, int y, int z) throws IOException {
162162
out.write(b4);
163163
}
164164

165-
final byte[] buffer = new byte[4];
166-
167165
@Override
168-
public int readX(FaweInputStream in) throws IOException {
166+
public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException {
169167
in.readFully(buffer);
170-
return lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
168+
change.x = lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
169+
change.y = (ly = ly + buffer[0]) & 0xFF;
170+
change.z = lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
171171
}
172172

173-
@Override
174-
public int readY(FaweInputStream in) {
175-
return (ly = ly + buffer[0]) & 0xFF;
176-
}
177-
178-
@Override
179-
public int readZ(FaweInputStream in) throws IOException {
180-
return lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
181-
}
182173
};
183174
} else {
184175
posDel = new FaweStreamPositionDelegate() {
@@ -201,19 +192,11 @@ public void write(OutputStream stream, int x, int y, int z) throws IOException {
201192
}
202193

203194
@Override
204-
public int readX(FaweInputStream is) throws IOException {
205-
is.readFully(buffer);
206-
return lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8));
207-
}
208-
209-
@Override
210-
public int readY(FaweInputStream is) throws IOException {
211-
return ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8);
212-
}
213-
214-
@Override
215-
public int readZ(FaweInputStream is) throws IOException {
216-
return lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8);
195+
public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException {
196+
in.readFully(buffer);
197+
change.x = lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8));
198+
change.z = lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8);
199+
change.y = ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8);
217200
}
218201
};
219202
}
@@ -428,9 +411,9 @@ public Iterator<MutableBlockChange> getBlockIterator(final boolean dir) throws I
428411

429412
public MutableBlockChange read() {
430413
try {
431-
change.x = posDel.readX(is) + originX;
432-
change.y = posDel.readY(is);
433-
change.z = posDel.readZ(is) + originZ;
414+
posDel.read(is, change);
415+
change.x += originX;
416+
change.z += originZ;
434417
idDel.readCombined(is, change, dir);
435418
return change;
436419
} catch (EOFException ignored) {
@@ -545,9 +528,9 @@ public Iterator<MutableFullBlockChange> getFullBlockIterator(BlockBag blockBag,
545528

546529
public MutableFullBlockChange read() {
547530
try {
548-
change.x = posDel.readX(is) + originX;
549-
change.y = posDel.readY(is);
550-
change.z = posDel.readZ(is) + originZ;
531+
posDel.read(is, change);
532+
change.x += originX;
533+
change.z += originZ;
551534
idDel.readCombined(is, change);
552535
return change;
553536
} catch (EOFException ignored) {
@@ -765,11 +748,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
765748
int amount = (Settings.settings().HISTORY.BUFFER_SIZE - HEADER_SIZE) / 9;
766749
MutableFullBlockChange change = new MutableFullBlockChange(null, 0, false);
767750
for (int i = 0; i < amount; i++) {
768-
int x = posDel.readX(fis) + ox;
769-
int y = posDel.readY(fis);
770-
int z = posDel.readZ(fis) + ox;
751+
posDel.read(fis, change);
771752
idDel.readCombined(fis, change);
772-
summary.add(x, z, change.to);
753+
summary.add(change.x + ox, change.z + oz, change.to);
773754
}
774755
}
775756
} catch (EOFException ignored) {

0 commit comments

Comments
 (0)