From 0e88bb438b4b416c4da5c48669304c97dfadbfcc Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 25 Jan 2024 09:31:45 +0100 Subject: [PATCH] History: Read change positions at once (#2542) --- .../history/change/BlockPositionChange.java | 16 ++++ .../history/change/MutableBlockChange.java | 10 +-- .../change/MutableFullBlockChange.java | 9 +- .../changeset/FaweStreamChangeSet.java | 84 +++++++------------ 4 files changed, 50 insertions(+), 69 deletions(-) create mode 100644 worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/BlockPositionChange.java diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/BlockPositionChange.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/BlockPositionChange.java new file mode 100644 index 000000000..e9c233269 --- /dev/null +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/BlockPositionChange.java @@ -0,0 +1,16 @@ +package com.fastasyncworldedit.core.history.change; + +import com.sk89q.worldedit.history.change.Change; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a change that is associated with {@code (x, y, z)} block coordinates. + * @since TODO + */ +@ApiStatus.Internal +public sealed abstract class BlockPositionChange implements Change + permits MutableBlockChange, MutableFullBlockChange { + public int x; + public int y; + public int z; +} diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableBlockChange.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableBlockChange.java index f4797581f..473940507 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableBlockChange.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableBlockChange.java @@ -2,17 +2,13 @@ package com.fastasyncworldedit.core.history.change; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.history.UndoContext; -import com.sk89q.worldedit.history.change.Change; import com.sk89q.worldedit.world.block.BlockState; +import org.jetbrains.annotations.ApiStatus; -public class MutableBlockChange implements Change { - - public int z; - public int y; - public int x; +@ApiStatus.Internal +public final class MutableBlockChange extends BlockPositionChange { public int ordinal; - public MutableBlockChange(int x, int y, int z, int ordinal) { this.x = x; this.y = y; diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableFullBlockChange.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableFullBlockChange.java index 2b99cb8a9..b23903f45 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableFullBlockChange.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/change/MutableFullBlockChange.java @@ -4,15 +4,12 @@ import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBagException; import com.sk89q.worldedit.history.UndoContext; -import com.sk89q.worldedit.history.change.Change; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypesCache; +import org.jetbrains.annotations.ApiStatus; -public class MutableFullBlockChange implements Change { - - public int z; - public int y; - public int x; +@ApiStatus.Internal +public final class MutableFullBlockChange extends BlockPositionChange { public int from; public int to; public BlockBag blockBag; diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSet.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSet.java index e50030d21..661248546 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSet.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSet.java @@ -7,6 +7,7 @@ import com.fastasyncworldedit.core.history.change.MutableBlockChange; import com.fastasyncworldedit.core.history.change.MutableEntityChange; import com.fastasyncworldedit.core.history.change.MutableFullBlockChange; import com.fastasyncworldedit.core.history.change.MutableTileChange; +import com.fastasyncworldedit.core.history.change.BlockPositionChange; import com.fastasyncworldedit.core.internal.exception.FaweSmallEditUnsupportedException; import com.fastasyncworldedit.core.internal.io.FaweInputStream; import com.fastasyncworldedit.core.internal.io.FaweOutputStream; @@ -22,6 +23,7 @@ import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BlockTypes; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,7 +32,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayDeque; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -42,6 +43,7 @@ import java.util.function.BiConsumer; /** * FAWE stream ChangeSet offering support for extended-height worlds */ +@ApiStatus.Internal public abstract class FaweStreamChangeSet extends AbstractChangeSet { public static final int HEADER_SIZE = 9; @@ -85,19 +87,15 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { } } - public interface FaweStreamPositionDelegate { + interface FaweStreamPositionDelegate { void write(OutputStream out, int x, int y, int z) throws IOException; - int readX(FaweInputStream in) throws IOException; - - int readY(FaweInputStream in) throws IOException; - - int readZ(FaweInputStream in) throws IOException; + void read(FaweInputStream in, BlockPositionChange change) throws IOException; } - public interface FaweStreamIdDelegate { + interface FaweStreamIdDelegate { void writeChange(FaweOutputStream out, int from, int to) throws IOException; @@ -155,6 +153,7 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { } if (mode == 1 || mode == 4) { // small posDel = new FaweStreamPositionDelegate() { + final byte[] buffer = new byte[4]; int lx; int ly; int lz; @@ -179,23 +178,14 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { out.write(b4); } - final byte[] buffer = new byte[4]; - @Override - public int readX(FaweInputStream in) throws IOException { + public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException { in.readFully(buffer); - return lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20); + change.x = lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20); + change.y = (ly = ly + buffer[0]) & 0xFF; + change.z = lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20); } - @Override - public int readY(FaweInputStream in) { - return (ly = ly + buffer[0]) & 0xFF; - } - - @Override - public int readZ(FaweInputStream in) throws IOException { - return lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20); - } }; } else { posDel = new FaweStreamPositionDelegate() { @@ -232,25 +222,11 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { } @Override - public int readX(FaweInputStream is) throws IOException { - is.readFully(buffer); - // Don't break reading version 1 history (just in case) - if (version == 2 && Arrays.equals(buffer, MAGIC_NEW_RELATIVE)) { - lx = ((is.read() << 24) + (is.read() << 16) + (is.read() << 8) + is.read()); - lz = ((is.read() << 24) + (is.read() << 16) + (is.read() << 8) + is.read()); - is.readFully(buffer); - } - return lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8)); - } - - @Override - public int readY(FaweInputStream is) throws IOException { - return ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8); - } - - @Override - public int readZ(FaweInputStream is) throws IOException { - return lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8); + public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException { + in.readFully(buffer); + change.x = lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8)); + change.z = lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8); + change.y = ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8); } }; } @@ -453,9 +429,9 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { public MutableBlockChange read() { try { - change.x = posDel.readX(is) + originX; - change.y = posDel.readY(is); - change.z = posDel.readZ(is) + originZ; + posDel.read(is, change); + change.x += originX; + change.z += originZ; idDel.readCombined(is, change, dir); return change; } catch (EOFException ignored) { @@ -570,9 +546,9 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { public MutableFullBlockChange read() { try { - change.x = posDel.readX(is) + originX; - change.y = posDel.readY(is); - change.z = posDel.readZ(is) + originZ; + posDel.read(is, change); + change.x += originX; + change.z += originZ; idDel.readCombined(is, change); return change; } catch (EOFException ignored) { @@ -872,10 +848,9 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { @Override public @Nullable MutableFullBlockChange populate(@NotNull final MutableFullBlockChange change) { try { - change.x = posDel.readX(is) + originX; - change.y = posDel.readY(is); - change.z = posDel.readZ(is) + originZ; idDel.readCombined(is, change); + change.x += originX; + change.z += originZ; return change; } catch (EOFException ignored) { } catch (Exception e) { @@ -914,10 +889,9 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { @Override public @Nullable MutableBlockChange populate(@NotNull final MutableBlockChange change) { try { - change.x = posDel.readX(is) + originX; - change.y = posDel.readY(is); - change.z = posDel.readZ(is) + originZ; idDel.readCombined(is, change, dir); + change.x += originX; + change.z += originZ; return change; } catch (EOFException ignored) { } catch (Exception e) { @@ -1068,11 +1042,9 @@ public abstract class FaweStreamChangeSet extends AbstractChangeSet { int amount = (Settings.settings().HISTORY.BUFFER_SIZE - HEADER_SIZE) / 9; MutableFullBlockChange change = new MutableFullBlockChange(null, 0, false); for (int i = 0; i < amount; i++) { - int x = posDel.readX(fis) + ox; - int y = posDel.readY(fis); - int z = posDel.readZ(fis) + ox; + posDel.read(fis, change); idDel.readCombined(fis, change); - summary.add(x, z, change.to); + summary.add(change.x + ox, change.z + oz, change.to); } } } catch (EOFException ignored) {