Skip to content

Commit

Permalink
Apply DataArray Patch and update bukkit adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
JayemCeekay authored and SirYwell committed Aug 22, 2024
1 parent c0edab6 commit aadd6ac
Show file tree
Hide file tree
Showing 53 changed files with 824 additions and 811 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.util.concurrent.Futures;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Lifecycle;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.NBTConstants;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItem;
Expand Down Expand Up @@ -145,7 +146,6 @@
import org.spigotmc.SpigotConfig;
import org.spigotmc.WatchdogThread;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -168,6 +168,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.apache.logging.log4j.Logger;
import org.enginehub.linbus.tree.LinCompoundTag;

import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.EnumMap;
Expand All @@ -63,6 +62,7 @@
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
* Handles converting all Pre 1.13.2 data using the Legacy DataFix System (ported to 1.13.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.bukkit.event.block.BlockPhysicsEvent;
import org.enginehub.linbus.tree.LinCompoundTag;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.Objects;
import javax.annotation.Nullable;

public class PaperweightWorldNativeAccess implements WorldNativeAccess<LevelChunk, net.minecraft.world.level.block.state.BlockState, BlockPos> {
private static final int UPDATE = 1;
Expand Down
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 @@ -86,7 +87,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 @@ -495,7 +496,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 @@ -506,7 +507,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 Down Expand Up @@ -536,9 +537,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 @@ -555,10 +554,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 @@ -616,10 +612,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 @@ -889,7 +883,7 @@ private void updateGet(
LevelChunk nmsChunk,
LevelChunkSection[] chunkSections,
LevelChunkSection section,
char[] arr,
DataArray arr,
int layer
) {
try {
Expand All @@ -914,7 +908,7 @@ 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]) {
Expand Down Expand Up @@ -945,21 +939,14 @@ public void send() {
*/
@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 @@ -972,7 +959,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 @@ -989,9 +976,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 @@ -1004,17 +991,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 @@ -40,7 +41,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 @@ -52,7 +53,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 @@ -167,7 +168,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 @@ -204,17 +205,16 @@ public boolean hasSection(int layer) {
}

@Override
public char[] load(int layer) {
public DataArray load(int layer) {
layer -= getMinSectionPosition();
if (blocks[layer] == null) {
blocks[layer] = new char[4096];
Arrays.fill(blocks[layer], (char) BlockTypesCache.ReservedIDs.AIR);
blocks[layer] = DataArray.createFilled(BlockTypesCache.ReservedIDs.AIR);
}
return blocks[layer];
}

@Override
public char[] loadIfPresent(int layer) {
public DataArray loadIfPresent(int layer) {
layer -= getMinSectionPosition();
return blocks[layer];
}
Expand Down Expand Up @@ -247,7 +247,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 @@ -393,7 +394,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 @@ -403,8 +404,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
Loading

0 comments on commit aadd6ac

Please sign in to comment.