Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions build-logic/src/main/kotlin/buildlogic.common.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ configurations.all {
}
}

plugins.withId("java") {
the<JavaPluginExtension>().toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
allprojects {
plugins.withId("java") {
the<JavaPluginExtension>().toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
vendor.set(JvmVendorSpec.ADOPTIUM)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fastasyncworldedit.core.queue.IBatchProcessor;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.implementation.packet.ChunkPacket;
import com.fastasyncworldedit.core.util.FoliaUtil;
import com.fastasyncworldedit.core.util.NbtUtils;
import com.fastasyncworldedit.core.util.TaskManager;
import com.google.common.base.Preconditions;
Expand All @@ -21,6 +22,7 @@
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2.regen.PaperweightRegen;
Expand Down Expand Up @@ -54,6 +56,7 @@
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import io.papermc.lib.PaperLib;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.SectionPos;
Expand Down Expand Up @@ -127,6 +130,8 @@
import java.util.Objects;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -350,28 +355,45 @@ public Set<SideEffect> getSupportedSideEffects() {
public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
Preconditions.checkNotNull(entity);

CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();

String id = getEntityId(mcEntity);

if (id != null) {
EntityType type = com.sk89q.worldedit.world.entity.EntityTypes.get(id);
Supplier<LinCompoundTag> saveTag = () -> {
final net.minecraft.nbt.CompoundTag minecraftTag = new net.minecraft.nbt.CompoundTag();
if (!readEntityIntoTag(mcEntity, minecraftTag)) {
String id = entity.getType().getKey().toString();
EntityType type = com.sk89q.worldedit.world.entity.EntityTypes.get(id);
Supplier<LinCompoundTag> saveTag = () -> {
net.minecraft.nbt.CompoundTag minecraftTag = new net.minecraft.nbt.CompoundTag();
Entity mcEntity;

if (FoliaUtil.isFoliaServer()) {
CompletableFuture<Entity> handleFuture = new CompletableFuture<>();
entity.getScheduler().run(
WorldEditPlugin.getInstance(),
(ScheduledTask task) -> {
try {
handleFuture.complete(((CraftEntity) entity).getHandle());
} catch (Throwable t) {
handleFuture.completeExceptionally(t);
}
},
() -> handleFuture.completeExceptionally(new CancellationException("Entity scheduler task cancelled"))
);
try {
mcEntity = handleFuture.join();
} catch (Throwable t) {
LOGGER.error("Failed to safely get NMS handle for {}", id, t);
return null;
}
//add Id for AbstractChangeSet to work
final LinCompoundTag tag = (LinCompoundTag) toNativeLin(minecraftTag);
final Map<String, LinTag<?>> tags = NbtUtils.getLinCompoundTagValues(tag);
tags.put("Id", LinStringTag.of(id));
return LinCompoundTag.of(tags);
};
return new LazyBaseEntity(type, saveTag);
} else {
return null;
}
} else {
mcEntity = ((CraftEntity) entity).getHandle();
}

if (mcEntity == null || !readEntityIntoTag(mcEntity, minecraftTag)) {
return null;
}

LinCompoundTag tag = (LinCompoundTag) toNativeLin(minecraftTag);
Map<String, LinTag<?>> tags = NbtUtils.getLinCompoundTagValues(tag);
tags.put("Id", LinStringTag.of(id));
return LinCompoundTag.of(tags);
};
return new LazyBaseEntity(type, saveTag);
}

@Override
Expand Down Expand Up @@ -550,20 +572,62 @@ public org.bukkit.inventory.ItemStack adapt(BaseItemStack baseItemStack) {

@Override
protected void preCaptureStates(final ServerLevel serverLevel) {
serverLevel.captureTreeGeneration = true;
serverLevel.captureBlockStates = true;
try {
Field captureTreeField = ServerLevel.class.getDeclaredField("captureTreeGeneration");
captureTreeField.setAccessible(true);
captureTreeField.setBoolean(serverLevel, true);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Unable to read captureTreeGeneration field
}
try {
Field captureBlockField = ServerLevel.class.getDeclaredField("captureBlockStates");
captureBlockField.setAccessible(true);
captureBlockField.setBoolean(serverLevel, true);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Unable to read captureTreeGeneration field
}
}

@Override
protected List<org.bukkit.block.BlockState> getCapturedBlockStatesCopy(final ServerLevel serverLevel) {
return new ArrayList<>(serverLevel.capturedBlockStates.values());
try {
Field capturedStatesField = ServerLevel.class.getDeclaredField("capturedBlockStates");
capturedStatesField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<BlockPos, org.bukkit.block.BlockState> capturedStates = (Map<BlockPos, org.bukkit.block.BlockState>) capturedStatesField.get(serverLevel);
return capturedStates != null ? new ArrayList<>(capturedStates.values()) : new ArrayList<>();
} catch (NoSuchFieldException | IllegalAccessException e) {
return new ArrayList<>();
}
}

@Override
protected void postCaptureBlockStates(final ServerLevel serverLevel) {
serverLevel.captureBlockStates = false;
serverLevel.captureTreeGeneration = false;
serverLevel.capturedBlockStates.clear();
try {
Field captureBlockField = ServerLevel.class.getDeclaredField("captureBlockStates");
captureBlockField.setAccessible(true);
captureBlockField.setBoolean(serverLevel, false);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Unable to read captureTreeGeneration field
}
try {
Field captureTreeField = ServerLevel.class.getDeclaredField("captureTreeGeneration");
captureTreeField.setAccessible(true);
captureTreeField.setBoolean(serverLevel, false);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Unable to read captureTreeGeneration field
}
try {
Field capturedStatesField = ServerLevel.class.getDeclaredField("capturedBlockStates");
capturedStatesField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<BlockPos, org.bukkit.block.BlockState> capturedStates = (Map<BlockPos, org.bukkit.block.BlockState>) capturedStatesField.get(serverLevel);
if (capturedStates != null) {
capturedStates.clear();
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// Unable to read captureTreeGeneration field
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData;
import org.bukkit.event.block.BlockPhysicsEvent;
Expand Down Expand Up @@ -58,7 +59,7 @@ public PaperweightFaweWorldNativeAccess(PaperweightFaweAdapter paperweightFaweAd
this.level = level;
// Use the actual tick as minecraft-defined so we don't try to force blocks into the world when the server's already lagging.
// - With the caveat that we don't want to have too many cached changed (1024) so we'd flush those at 1024 anyway.
this.lastTick = new AtomicInteger(MinecraftServer.currentTick);
this.lastTick = new AtomicInteger(getCurrentTick());
}

private Level getLevel() {
Expand Down Expand Up @@ -94,7 +95,7 @@ public synchronized net.minecraft.world.level.block.state.BlockState setBlockSta
LevelChunk levelChunk, BlockPos blockPos,
net.minecraft.world.level.block.state.BlockState blockState
) {
int currentTick = MinecraftServer.currentTick;
int currentTick = getCurrentTick();
if (Fawe.isMainThread()) {
return levelChunk.setBlockState(blockPos, blockState,
this.sideEffectSet != null && this.sideEffectSet.shouldApply(SideEffect.UPDATE)
Expand Down Expand Up @@ -289,4 +290,16 @@ private record CachedChange(

}

private int getCurrentTick() {
try {
return MinecraftServer.currentTick;
} catch (NoSuchFieldError e) {
try {
return Bukkit.getCurrentTick();
} catch (Exception ex) {
return 0;
}
}
}

}
Loading
Loading