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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fastasyncworldedit.bukkit.adapter.CachedBukkitAdapter;
import com.fastasyncworldedit.bukkit.adapter.IDelegateBukkitImplAdapter;
import com.fastasyncworldedit.bukkit.adapter.NMSRelighterFactory;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.entity.LazyBaseEntity;
import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory;
Expand Down Expand Up @@ -259,7 +260,13 @@ public BlockState getBlock(Location location) {
int y = location.getBlockY();
int z = location.getBlockZ();
final ServerLevel handle = craftWorld.getHandle();
LevelChunk chunk = handle.getChunk(x >> 4, z >> 4);
LevelChunk chunk;
if (Fawe.isTickThread()) {
// TODO check if is owned by this thread, else synchronize
chunk = handle.getChunk(x >> 4, z >> 4);
} else {
chunk = TaskManager.taskManager().syncAt(() -> handle.getChunk(x >> 4, z >> 4), BukkitAdapter.adapt(location));
}
final BlockPos blockPos = new BlockPos(x, y, z);
final net.minecraft.world.level.block.state.BlockState blockData = chunk.getBlockState(blockPos);
BlockState state = adapt(blockData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ 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);
// TODO
this.lastTick = new AtomicInteger(0);
}

private Level getLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import com.fastasyncworldedit.core.util.ReflectionUtils;
import com.fastasyncworldedit.core.util.TaskManager;
import com.mojang.datafixers.util.Either;
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.Refraction;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.block.BlockState;
Expand Down Expand Up @@ -352,13 +356,19 @@ public static void sendChunk(ServerLevel nmsWorld, int chunkX, int chunkZ, boole
);
}
nearbyPlayers(nmsWorld, coordIntPair).forEach(p -> p.connection.send(packet));
});
}, toLocation(nmsWorld, coordIntPair));
}

private static List<ServerPlayer> nearbyPlayers(ServerLevel serverLevel, ChunkPos coordIntPair) {
return serverLevel.getChunkSource().chunkMap.getPlayers(coordIntPair, false);
}

public static Location toLocation(ServerLevel serverLevel, ChunkPos chunkPos) {
final World adapt = BukkitAdapter.adapt(serverLevel.getWorld());
final Vector3 pos = Vector3.at(chunkPos.getMinBlockX(), 0, chunkPos.getMinBlockZ());
return new Location(adapt, pos);
}

/*
NMS conversion
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.fastasyncworldedit.core.extent.processor.lighting.Relighter;
import com.fastasyncworldedit.core.queue.IQueueChunk;
import com.fastasyncworldedit.core.queue.IQueueExtent;
import com.fastasyncworldedit.core.util.FoliaSupport;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.TaskManager;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.util.Location;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArraySet;
import it.unimi.dsi.fastutil.longs.LongIterator;
Expand Down Expand Up @@ -101,39 +103,53 @@ private void fixLighting(LongSet chunks, Runnable andThen) {
while (iterator.hasNext()) {
coords.add(new ChunkPos(iterator.nextLong()));
}
if (FoliaSupport.isFolia()) {
relightRegion(andThen, coords);
return;
}
TaskManager.taskManager().task(() -> {
// trigger chunk load and apply ticket on main thread
List<CompletableFuture<?>> futures = new ArrayList<>();
for (ChunkPos pos : coords) {
futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z)
.thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel(
FAWE_TICKET,
pos,
LIGHT_LEVEL,
Unit.INSTANCE
))
);
}
// collect futures and trigger relight once all chunks are loaded
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v ->
invokeRelight(
coords,
c -> {
}, // no callback for single chunks required
i -> {
if (i != coords.size()) {
LOGGER.warn("Processed {} chunks instead of {}", i, coords.size());
}
// post process chunks on main thread
TaskManager.taskManager().task(() -> postProcessChunks(coords));
// call callback on our own threads
TaskManager.taskManager().async(andThen);
}
)
);
relightRegion(andThen, coords);
});
}

private void relightRegion(Runnable andThen, Set<ChunkPos> coords) {
List<CompletableFuture<?>> futures = new ArrayList<>();
for (ChunkPos pos : coords) {
futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z)
.thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel(
FAWE_TICKET,
pos,
LIGHT_LEVEL,
Unit.INSTANCE
))
);
}
Location location = toLocation(coords.iterator().next());
// collect futures and trigger relight once all chunks are loaded
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAcceptAsync(v ->
invokeRelight(
coords,
c -> {
}, // no callback for single chunks required
i -> {
if (i != coords.size()) {
LOGGER.warn("Processed {} chunks instead of {}", i, coords.size());
}
// post process chunks on main thread
TaskManager.taskManager().task(() -> postProcessChunks(coords), location);
// call callback on our own threads
TaskManager.taskManager().async(andThen);
}
),
task -> TaskManager.taskManager().task(task, location)
);
}

private Location toLocation(ChunkPos chunkPos) {
return PaperweightPlatformAdapter.toLocation(this.serverLevel, chunkPos);
}

private void invokeRelight(
Set<ChunkPos> coords,
Consumer<ChunkPos> chunkCallback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.fastasyncworldedit.bukkit.regions.TownyFeature;
import com.fastasyncworldedit.bukkit.regions.WorldGuardFeature;
import com.fastasyncworldedit.bukkit.util.BukkitTaskManager;
import com.fastasyncworldedit.bukkit.util.FoliaTaskManager;
import com.fastasyncworldedit.core.util.FoliaSupport;
import com.fastasyncworldedit.bukkit.util.ItemUtil;
import com.fastasyncworldedit.bukkit.util.MinecraftVersion;
import com.fastasyncworldedit.bukkit.util.image.BukkitImageViewer;
Expand Down Expand Up @@ -63,6 +65,7 @@ public class FaweBukkit implements IFawe, Listener {
private ItemUtil itemUtil;
private Preloader preloader;
private volatile boolean keepUnloaded;
private static final Thread startingThread = Thread.currentThread();

public FaweBukkit(Plugin plugin) {
this.plugin = plugin;
Expand All @@ -74,7 +77,7 @@ public FaweBukkit(Plugin plugin) {
} catch (Throwable e) {
LOGGER.error("Brush Listener Failed", e);
}
if (PaperLib.isPaper() && Settings.settings().EXPERIMENTAL.DYNAMIC_CHUNK_RENDERING > 1) {
if (!FoliaSupport.isFolia() && PaperLib.isPaper() && Settings.settings().EXPERIMENTAL.DYNAMIC_CHUNK_RENDERING > 1) {
new RenderListener(plugin);
}
} catch (final Throwable e) {
Expand All @@ -89,20 +92,22 @@ public FaweBukkit(Plugin plugin) {
platformAdapter = new NMSAdapter();

//PlotSquared support is limited to Spigot/Paper as of 02/20/2020
TaskManager.taskManager().later(this::setupPlotSquared, 0);
// TODO plotsquared support
// TaskManager.taskManager().later(this::setupPlotSquared, 0);

// TODO moved out of task below??
Bukkit.getPluginManager().registerEvents(FaweBukkit.this, FaweBukkit.this.plugin);
// Registered delayed Event Listeners
TaskManager.taskManager().task(() -> {
/*TaskManager.taskManager().task(() -> {
// Fix for ProtocolSupport
Settings.settings().PROTOCOL_SUPPORT_FIX =
Bukkit.getPluginManager().isPluginEnabled("ProtocolSupport");

// This class
Bukkit.getPluginManager().registerEvents(FaweBukkit.this, FaweBukkit.this.plugin);

// The tick limiter
new ChunkListener9();
});
});*/

// Warn if small-edits are enabled with extended world heights
if (version.isEqualOrHigherThan(MinecraftVersion.CAVES_18) && Settings.settings().HISTORY.SMALL_EDITS) {
Expand Down Expand Up @@ -192,6 +197,9 @@ public String getDebugInfo() {
*/
@Override
public TaskManager getTaskManager() {
if (FoliaSupport.isFolia()) {
return new FoliaTaskManager();
}
return new BukkitTaskManager(plugin);
}

Expand Down Expand Up @@ -312,6 +320,14 @@ public FAWEPlatformAdapterImpl getPlatformAdapter() {
return platformAdapter;
}

@Override
public boolean isTickThread() {
if (FoliaSupport.isFolia()) {
return FoliaSupport.isTickThread();
}
return Thread.currentThread() == startingThread;
}

private void setupPlotSquared() {
Plugin plotSquared = this.plugin.getServer().getPluginManager().getPlugin("PlotSquared");
if (plotSquared == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fastasyncworldedit.bukkit.listener;

import com.fastasyncworldedit.bukkit.FaweBukkit;
import com.fastasyncworldedit.core.util.FoliaSupport;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.configuration.Settings;
import com.fastasyncworldedit.core.util.FaweTimer;
Expand Down Expand Up @@ -59,6 +60,9 @@ public abstract class ChunkListener implements Listener {
Settings.settings().TICK_LIMITER.FALLING, Settings.settings().TICK_LIMITER.ITEMS};

public ChunkListener() {
if (FoliaSupport.isFolia()) {
return;
}
if (Settings.settings().TICK_LIMITER.ENABLED) {
PluginManager plm = Bukkit.getPluginManager();
Plugin plugin = Fawe.<FaweBukkit>platform().getPlugin();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.fastasyncworldedit.bukkit.util;

import com.fastasyncworldedit.core.util.TaskManager;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.util.Location;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import java.util.function.Supplier;

public class BukkitTaskManager extends TaskManager {

Expand Down Expand Up @@ -34,11 +38,21 @@ public void task(@Nonnull final Runnable runnable) {
this.plugin.getServer().getScheduler().runTask(this.plugin, runnable).getTaskId();
}

@Override
public void task(@NotNull final Runnable runnable, @NotNull final Location context) {

}

@Override
public void later(@Nonnull final Runnable runnable, final int delay) {
this.plugin.getServer().getScheduler().runTaskLater(this.plugin, runnable, delay).getTaskId();
}

@Override
public void later(@NotNull final Runnable runnable, final Location location, final int delay) {

}

@Override
public void laterAsync(@Nonnull final Runnable runnable, final int delay) {
this.plugin.getServer().getScheduler().runTaskLaterAsynchronously(this.plugin, runnable, delay);
Expand All @@ -51,4 +65,16 @@ public void cancel(final int task) {
}
}

// TODO

@Override
public <T> T syncAt(final Supplier<T> supplier, final Location context) {
return sync(supplier);
}

@Override
public <T> T syncWith(final Supplier<T> supplier, final Player context) {
return sync(supplier);
}

}
Loading