Skip to content

Commit

Permalink
update 1.20.2 adapters to use DataArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
JayemCeekay committed Oct 31, 2023
1 parent 6fbd229 commit 6ce7f42
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.fastasyncworldedit.core.queue.implementation.QueueHandler;
import com.fastasyncworldedit.core.queue.implementation.blocks.CharGetBlocks;
import com.fastasyncworldedit.core.queue.implementation.blocks.DataArray;
import com.fastasyncworldedit.core.queue.implementation.blocks.DataArrayGetBlocks;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.google.common.base.Suppliers;
Expand Down Expand Up @@ -63,7 +64,7 @@

import static net.minecraft.core.registries.Registries.BIOME;

public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBlocks {
public class PaperweightGetBlocks extends DataArrayGetBlocks implements BukkitGetBlocks {

private static final Logger LOGGER = LogManagerCompat.getLogger();

Expand Down Expand Up @@ -445,7 +446,7 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
);
LevelChunkSection newSection = PaperweightPlatformAdapter.newChunkSection(
layerNo,
new char[4096],
DataArray.createEmpty(),
adapter,
biomeRegistry,
biomeData
Expand All @@ -456,7 +457,7 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
newSection,
getSectionIndex
)) {
updateGet(nmsChunk, levelChunkSections, newSection, new char[4096], getSectionIndex);
updateGet(nmsChunk, levelChunkSections, newSection, DataArray.createEmpty(), getSectionIndex);
continue;
} else {
existingSection = levelChunkSections[getSectionIndex];
Expand All @@ -479,9 +480,7 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz

// setArr is modified by PaperweightPlatformAdapter#newChunkSection. This is in order to write changes to
// this chunk GET when #updateGet is called. Future dords, please listen this time.
char[] tmp = set.load(layerNo);
char[] setArr = new char[tmp.length];
System.arraycopy(tmp, 0, setArr, 0, tmp.length);
DataArray setArr = DataArray.createCopy(set.load(layerNo));

// synchronise on internal section to avoid circular locking with a continuing edit if the chunk was
// submitted to keep loaded internal chunks to queue target size.
Expand All @@ -498,10 +497,7 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
}

if (createCopy) {
char[] tmpLoad = loadPrivately(layerNo);
char[] copyArr = new char[4096];
System.arraycopy(tmpLoad, 0, copyArr, 0, 4096);
copy.storeSection(getSectionIndex, copyArr);
copy.storeSection(getSectionIndex, DataArray.createCopy(loadPrivately(layerNo)));
if (biomes != null && existingSection != null) {
copy.storeBiomes(getSectionIndex, existingSection.getBiomes());
}
Expand Down Expand Up @@ -563,10 +559,8 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
} else if (existingSection != getSections(false)[getSectionIndex]) {
this.sections[getSectionIndex] = existingSection;
this.reset();
} else if (!Arrays.equals(
update(getSectionIndex, new char[4096], true),
loadPrivately(layerNo)
)) {
} else if (!update(getSectionIndex, DataArray.createEmpty(), true)
.equals(loadPrivately(layerNo))) {
this.reset(layerNo);
/*} else if (lock.isModified()) {
this.reset(layerNo);*/
Expand Down Expand Up @@ -837,7 +831,7 @@ private void updateGet(
LevelChunk nmsChunk,
LevelChunkSection[] chunkSections,
LevelChunkSection section,
char[] arr,
DataArray arr,
int layer
) {
try {
Expand All @@ -862,14 +856,12 @@ private void updateGet(
this.blocks[layer] = arr;
}

private char[] loadPrivately(int layer) {
private DataArray loadPrivately(int layer) {
layer -= getMinSectionPosition();
if (super.sections[layer] != null) {
synchronized (super.sectionLocks[layer]) {
if (super.sections[layer].isFull() && super.blocks[layer] != null) {
char[] blocks = new char[4096];
System.arraycopy(super.blocks[layer], 0, blocks, 0, 4096);
return blocks;
return DataArray.createCopy(super.blocks[layer]);
}
}
}
Expand All @@ -893,21 +885,14 @@ public synchronized void send(int mask, boolean lighting) {
*/
@Override
@SuppressWarnings("unchecked")
public char[] update(int layer, char[] data, boolean aggressive) {
public DataArray update(int layer, DataArray data, boolean aggressive) {
LevelChunkSection section = getSections(aggressive)[layer];
// Section is null, return empty array
if (section == null) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
return data;
return DataArray.createFilled(BlockTypesCache.ReservedIDs.AIR);
}
if (data != null && data.length != 4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
}
if (data == null || data == FaweCache.INSTANCE.EMPTY_CHAR_4096) {
data = new char[4096];
Arrays.fill(data, (char) BlockTypesCache.ReservedIDs.AIR);
if (data == null || data == FaweCache.INSTANCE.EMPTY_DATA) {
data = DataArray.createFilled(BlockTypesCache.ReservedIDs.AIR);
}
Semaphore lock = PaperweightPlatformAdapter.applyLock(section);
synchronized (lock) {
Expand All @@ -920,7 +905,7 @@ public char[] update(int layer, char[] data, boolean aggressive) {
final BitStorage bits = (BitStorage) PaperweightPlatformAdapter.fieldStorage.get(dataObject);

if (bits instanceof ZeroBitStorage) {
Arrays.fill(data, adapter.adaptToChar(blocks.get(0, 0, 0))); // get(int) is only public on paper
data.setAll(adapter.adaptToChar(blocks.get(0, 0, 0))); // get(int) is only public on paper
return data;
}

Expand All @@ -937,9 +922,9 @@ public char[] update(int layer, char[] data, boolean aggressive) {
} else {
// The section's palette is the global block palette.
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char paletteVal = (char) data.getAt(i);
char ordinal = adapter.ibdIDToOrdinal(paletteVal);
data[i] = ordinal;
data.setAt(i, ordinal);
}
return data;
}
Expand All @@ -952,17 +937,17 @@ public char[] update(int layer, char[] data, boolean aggressive) {
paletteToOrdinal[i] = ordinal;
}
for (int i = 0; i < 4096; i++) {
char paletteVal = data[i];
char paletteVal = (char) data.getAt(i);
char val = paletteToOrdinal[paletteVal];
if (val == Character.MAX_VALUE) {
val = ordinal(palette.valueFor(i), adapter);
paletteToOrdinal[i] = val;
}
data[i] = val;
data.setAt(i, val);
}
} else {
char ordinal = ordinal(palette.valueFor(0), adapter);
Arrays.fill(data, ordinal);
data.setAll(ordinal);
}
} finally {
for (int i = 0; i < num_palette; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fastasyncworldedit.core.queue.IBlocks;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.fastasyncworldedit.core.queue.implementation.blocks.DataArray;
import com.google.common.base.Suppliers;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
Expand Down Expand Up @@ -39,7 +40,7 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {

private final Map<BlockVector3, CompoundTag> tiles = new HashMap<>();
private final Set<CompoundTag> entities = new HashSet<>();
private final char[][] blocks;
private final DataArray[] blocks;
private final int minHeight;
private final int maxHeight;
final ServerLevel serverLevel;
Expand All @@ -51,7 +52,7 @@ protected PaperweightGetBlocks_Copy(LevelChunk levelChunk) {
this.serverLevel = levelChunk.level;
this.minHeight = serverLevel.getMinBuildHeight();
this.maxHeight = serverLevel.getMaxBuildHeight() - 1; // Minecraft max limit is exclusive.
this.blocks = new char[getSectionCount()][];
this.blocks = new DataArray[getSectionCount()];
}

protected void storeTile(BlockEntity blockEntity) {
Expand Down Expand Up @@ -165,7 +166,7 @@ public int getSectionCount() {
return serverLevel.getSectionsCount();
}

protected void storeSection(int layer, char[] data) {
protected void storeSection(int layer, DataArray data) {
blocks[layer] = data;
}

Expand Down Expand Up @@ -197,13 +198,13 @@ public boolean hasSection(int layer) {
}

@Override
public char[] load(int layer) {
public DataArray load(int layer) {
layer -= getMinSectionPosition();
return blocks[layer];
}

@Override
public char[] loadIfPresent(int layer) {
public DataArray loadIfPresent(int layer) {
layer -= getMinSectionPosition();
return blocks[layer];
}
Expand Down Expand Up @@ -236,7 +237,7 @@ public <T extends Future<T>> T call(IChunkSet set, Runnable finalize) {
public char get(int x, int y, int z) {
final int layer = (y >> 4) - getMinSectionPosition();
final int index = (y & 15) << 8 | z << 4 | x;
return blocks[layer][index];
return (char) blocks[layer].getAt(index);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.math.BitArrayUnstretched;
import com.fastasyncworldedit.core.queue.implementation.blocks.DataArray;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.ReflectionUtils;
import com.fastasyncworldedit.core.util.TaskManager;
Expand Down Expand Up @@ -386,7 +387,7 @@ private static List<ServerPlayer> nearbyPlayers(ServerLevel serverLevel, ChunkPo
*/
public static LevelChunkSection newChunkSection(
final int layer,
final char[] blocks,
final DataArray blocks,
CachedBukkitAdapter adapter,
Registry<Biome> biomeRegistry,
@Nullable PalettedContainer<Holder<Biome>> biomes
Expand All @@ -396,8 +397,8 @@ public static LevelChunkSection newChunkSection(

public static LevelChunkSection newChunkSection(
final int layer,
final Function<Integer, char[]> get,
char[] set,
final Function<Integer, DataArray> get,
DataArray set,
CachedBukkitAdapter adapter,
Registry<Biome> biomeRegistry,
@Nullable PalettedContainer<Holder<Biome>> biomes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fastasyncworldedit.core.queue.IChunk;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.fastasyncworldedit.core.queue.implementation.blocks.DataArray;
import com.fastasyncworldedit.core.registry.state.PropertyKey;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.block.BlockState;
Expand Down Expand Up @@ -36,14 +37,14 @@ public void postProcess(final IChunk chunk, final IChunkGet iChunkGet, final ICh
PaperweightGetBlocks_Copy getBlocks = (PaperweightGetBlocks_Copy) iChunkGet;
layer:
for (int layer = iChunkSet.getMinSectionPosition(); layer <= iChunkSet.getMaxSectionPosition(); layer++) {
char[] set = iChunkSet.loadIfPresent(layer);
DataArray set = iChunkSet.loadIfPresent(layer);
if (set == null) {
// No edit means no need to process
continue;
}
char[] get = null;
DataArray get = null;
for (int i = 0; i < 4096; i++) {
char ordinal = set[i];
char ordinal = (char) set.getAt(i);
char replacedOrdinal = BlockTypesCache.ReservedIDs.__RESERVED__;
boolean fromGet = false; // Used for liquids
if (ordinal == BlockTypesCache.ReservedIDs.__RESERVED__) {
Expand All @@ -56,15 +57,15 @@ public void postProcess(final IChunk chunk, final IChunkGet iChunkGet, final ICh
continue layer;
}
fromGet = true;
ordinal = replacedOrdinal = get[i];
ordinal = replacedOrdinal = (char) get.getAt(i);
}
if (ordinal == BlockTypesCache.ReservedIDs.__RESERVED__) {
continue;
} else if (!fromGet) { // if fromGet, don't do the same again
if (get == null) {
get = getBlocks.load(layer);
}
replacedOrdinal = get[i];
replacedOrdinal = (char) get.getAt(i);
}
boolean ticking = BlockTypesCache.ticking[ordinal];
boolean replacedWasTicking = BlockTypesCache.ticking[replacedOrdinal];
Expand Down Expand Up @@ -115,39 +116,39 @@ public ProcessorScope getScope() {
return ProcessorScope.READING_SET_BLOCKS;
}

private boolean wasAdjacentToWater(char[] get, char[] set, int i, int x, int y, int z) {
private boolean wasAdjacentToWater(DataArray get, DataArray set, int i, int x, int y, int z) {
if (set == null || get == null) {
return false;
}
char ordinal;
char reserved = BlockTypesCache.ReservedIDs.__RESERVED__;
if (x > 0 && set[i - 1] != reserved) {
if (BlockTypesCache.ticking[(ordinal = get[i - 1])] && isFluid(ordinal)) {
if (x > 0 && set.getAt(i - 1) != reserved) {
if (BlockTypesCache.ticking[(ordinal = (char) get.getAt(i - 1))] && isFluid(ordinal)) {
return true;
}
}
if (x < 15 && set[i + 1] != reserved) {
if (BlockTypesCache.ticking[(ordinal = get[i + 1])] && isFluid(ordinal)) {
if (x < 15 && set.getAt(i + 1) != reserved) {
if (BlockTypesCache.ticking[(ordinal = (char) get.getAt(i + 1))] && isFluid(ordinal)) {
return true;
}
}
if (z > 0 && set[i - 16] != reserved) {
if (BlockTypesCache.ticking[(ordinal = get[i - 16])] && isFluid(ordinal)) {
if (z > 0 && set.getAt(i - 16) != reserved) {
if (BlockTypesCache.ticking[(ordinal = (char) get.getAt(i - 16))] && isFluid(ordinal)) {
return true;
}
}
if (z < 15 && set[i + 16] != reserved) {
if (BlockTypesCache.ticking[(ordinal = get[i + 16])] && isFluid(ordinal)) {
if (z < 15 && set.getAt(i + 16) != reserved) {
if (BlockTypesCache.ticking[(ordinal = (char) get.getAt(i + 16))] && isFluid(ordinal)) {
return true;
}
}
if (y > 0 && set[i - 256] != reserved) {
if (BlockTypesCache.ticking[(ordinal = get[i - 256])] && isFluid(ordinal)) {
if (y > 0 && set.getAt(i - 256) != reserved) {
if (BlockTypesCache.ticking[(ordinal = (char) get.getAt(i - 256))] && isFluid(ordinal)) {
return true;
}
}
if (y < 15 && set[i + 256] != reserved) {
return BlockTypesCache.ticking[(ordinal = get[i + 256])] && isFluid(ordinal);
if (y < 15 && set.getAt(i + 256) != reserved) {
return BlockTypesCache.ticking[(ordinal = (char) get.getAt(i + 256))] && isFluid(ordinal);
}
return false;
}
Expand Down

0 comments on commit 6ce7f42

Please sign in to comment.