Skip to content
Open
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: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ group = 'tf.tuff'
version = '1.0.1-beta'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
options.release = 17
}

repositories {
Expand All @@ -31,6 +33,8 @@ dependencies {

implementation 'com.github.retrooper:packetevents-spigot:2.12.1'

implementation 'com.github.TechnicallyCoded:FoliaLib:0.4.4'

compileOnly 'com.viaversion:viabackwards:5.3.2'
compileOnly 'com.viaversion:viaversion:5.9.1'

Expand Down Expand Up @@ -72,6 +76,7 @@ shadowJar {
relocate 'io.github.retrooper.packetevents', 'tf.tuff.packetevents'
relocate 'com.fasterxml.jackson', 'tf.tuff.jackson'
relocate 'org.java_websocket', 'tf.tuff.websocket'
relocate 'com.tcoded.folialib', 'tf.tuff.folialib'

exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/tf/tuff/ServerRegistry.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package tf.tuff;

import org.bukkit.plugin.java.JavaPlugin;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;

public class ServerRegistry {
private final JavaPlugin p;
private final TuffX p;
private final String wsUrl;
private final String server;
private WebSocketClient client;
private boolean running = true;
private volatile boolean running = true;

public ServerRegistry(JavaPlugin pl, String registryUrl, String serverAddr) {
public ServerRegistry(TuffX pl, String registryUrl, String serverAddr) {
p = pl;
wsUrl = registryUrl;
server = serverAddr;
}

public void connect() {
p.getServer().getScheduler().runTaskAsynchronously(p, this::doConnect);
p.foliaLib.getScheduler().runAsync(t -> doConnect());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private void doConnect() {
Expand All @@ -40,7 +39,7 @@ public void onMessage(String msg) {
@Override
public void onClose(int code, String reason, boolean remote) {
if (running) {
p.getServer().getScheduler().runTaskLaterAsynchronously(p, () -> doConnect(), 100L);
p.foliaLib.getScheduler().runLaterAsync(t -> doConnect(), 100L);
}
}

Expand All @@ -52,7 +51,7 @@ public void onError(Exception e) {
client.connect();
} catch (Exception e) {
if (running) {
p.getServer().getScheduler().runTaskLaterAsynchronously(p, () -> doConnect(), 100L);
p.foliaLib.getScheduler().runLaterAsync(t -> doConnect(), 100L);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/tf/tuff/TuffX.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.tcoded.folialib.FoliaLib;

import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;

import tf.tuff.netty.ChunkInjector;
Expand All @@ -34,6 +36,9 @@ public class TuffX extends JavaPlugin implements Listener, PluginMessageListener

public ServerRegistry serverRegistry;

/** Platform-aware scheduler facade. Works on Spigot, Paper and Folia. */
public FoliaLib foliaLib;

public Y0Plugin y0Plugin;
public ViaBlocksPlugin viaBlocksPlugin;
public TuffActions tuffActions;
Expand All @@ -47,6 +52,8 @@ public TuffX(JavaPluginLoader loader, PluginDescriptionFile description, File da

@Override
public void onLoad() {
this.foliaLib = new FoliaLib(this);

this.y0Plugin = new Y0Plugin(this);
this.viaBlocksPlugin = new ViaBlocksPlugin(this);
this.tuffActions = new TuffActions(this);
Expand Down Expand Up @@ -109,6 +116,10 @@ public void onDisable() {
serverRegistry = null;
}

if (foliaLib != null) {
foliaLib.getScheduler().cancelAllTasks();
}

PacketEvents.getAPI().terminate();

getServer().getMessenger().unregisterIncomingPluginChannel(this);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/tf/tuff/netty/ChunkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import tf.tuff.viablocks.CustomBlockListener;
Expand Down Expand Up @@ -195,12 +195,14 @@ private void handleMultiBlockChange(ChannelHandlerContext ctx, ByteBuf buf, Chan
}

private void requestViaCache(int cx, int cz, long key) {
Bukkit.getScheduler().runTask(viaBlocks.plugin.plugin, () -> {
World world = player.getWorld();
Location chunkLoc = new Location(world, cx << 4, 0, cz << 4);
viaBlocks.plugin.plugin.foliaLib.getScheduler().runAtLocation(chunkLoc, t -> {
if (!player.isOnline()) {
release(key);
return;
}
viaBlocks.cacheChunkWithCallback(player.getWorld(), cx, cz, data -> {
viaBlocks.cacheChunkWithCallback(world, cx, cz, data -> {
QueuedPacket q = queue.get(key);
if (q != null) {
q.viaData = data;
Expand All @@ -212,7 +214,8 @@ private void requestViaCache(int cx, int cz, long key) {
}

private void requestY0Cache(int cx, int cz, long key) {
Bukkit.getScheduler().runTask(viaBlocks.plugin.plugin, () -> {
Location chunkLoc = new Location(player.getWorld(), cx << 4, 0, cz << 4);
y0.getTuffX().foliaLib.getScheduler().runAtLocation(chunkLoc, t -> {
if (!player.isOnline()) {
release(key);
return;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/tf/tuff/tuffactions/creative/CreativeMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import tf.tuff.tuffactions.TuffActionBase;
import tf.tuff.tuffactions.TuffActions;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
Expand Down Expand Up @@ -99,7 +98,7 @@ public void onPlayerInventoryClick(InventoryClickEvent event) {
InventoryAction action = event.getAction();
if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_ONE || action == InventoryAction.SWAP_WITH_CURSOR) {

Bukkit.getScheduler().runTaskLater(plugin, () -> {
plugin.foliaLib.getScheduler().runAtEntityLater(player, t -> {
ItemStack realItemStack = playerHoldingPlaceholder.get(playerUUID);

if (realItemStack != null && event.getClickedInventory() != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tf/tuff/tuffactions/swimming/Swimming.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void disable() {
/*** CUSTOM, SERVER-BOUND PACKETS ***/
public void handleSwimReady(Player player) {
if (!isEnabled()) return;
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
plugin.foliaLib.getScheduler().runAtEntityLater(player, t -> {
for (UUID swimmingPlayerId : swimmingPlayers) {
Player swimmingPlayer = Bukkit.getPlayer(swimmingPlayerId);
if (swimmingPlayer != null && swimmingPlayer.isOnline() && player.canSee(swimmingPlayer)) {
Expand Down
40 changes: 21 additions & 19 deletions src/main/java/tf/tuff/viablocks/CustomBlockListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -52,8 +52,10 @@ public class CustomBlockListener {
private static final int Y_SHIFT = 0;
private static final int Z_SHIFT = 12;
private static final int X_SHIFT = 12 + 26;
private final Map<UUID, Map<Integer, List<Long>>> pendingUpdates = new HashMap<>();
private final Set<UUID> pendingFlush = new HashSet<>();
// Accessed from per-player region threads on Folia: each player's entry is only
// touched on that player's own thread, so the outer map just needs to be concurrent.
private final Map<UUID, Map<Integer, List<Long>>> pendingUpdates = new ConcurrentHashMap<>();
private final Set<UUID> pendingFlush = ConcurrentHashMap.newKeySet();
private static final double UPDATE_RADIUS_SQUARED = 6400;
private static final @Nonnull byte[] EMPTY_PACKET = new byte[0];

Expand Down Expand Up @@ -165,7 +167,8 @@ private void sendChunksBatched(Player player, String worldName, List<int[]> chun

if (endIndex < chunks.size()) {
final int nextStart = endIndex;
runSyncLater(() -> sendChunksBatched(player, worldName, chunks, nextStart), 1);
plugin.plugin.foliaLib.getScheduler().runAtEntityLater(player,
t -> sendChunksBatched(player, worldName, chunks, nextStart), 1);
}
}

Expand Down Expand Up @@ -511,22 +514,27 @@ private void sendBlockStateUpdateToNearbyPlayers(Location location, BlockData da
if (stateId == -1) return;

World world = location.getWorld();
final int finalStateId = stateId;
for (Player player : world.getPlayers()) {
if (plugin.isPlayerEnabled(player) && player.getLocation().distanceSquared(location) < UPDATE_RADIUS_SQUARED) {
sendPacket(player, stateId, location);
}
plugin.plugin.foliaLib.getScheduler().runAtEntity(player, t -> {
if (plugin.isPlayerEnabled(player) && player.getLocation().distanceSquared(location) < UPDATE_RADIUS_SQUARED) {
sendPacket(player, finalStateId, location);
}
});
}
}

private void sendClearUpdateToNearbyPlayers(Location location) {
if (!plugin.isEnabled() || plugin.viaBlocksEnabledPlayers.isEmpty() || location.getWorld() == null) return;
final int AIR_ID = 0;
World world = location.getWorld();

for (Player player : world.getPlayers()) {
if (plugin.isPlayerEnabled(player) && player.getLocation().distanceSquared(location) < UPDATE_RADIUS_SQUARED) {
sendPacket(player, AIR_ID, location);
}
plugin.plugin.foliaLib.getScheduler().runAtEntity(player, t -> {
if (plugin.isPlayerEnabled(player) && player.getLocation().distanceSquared(location) < UPDATE_RADIUS_SQUARED) {
sendPacket(player, AIR_ID, location);
}
});
}
}

Expand All @@ -550,7 +558,8 @@ private void sendPacket(Player player, int stateId, Location location) {
}
stateList.add(packLocation(location));
if (pendingFlush.add(playerId)) {
runSyncLater(() -> flushPendingUpdates(playerId), plugin.getUpdateBatchDelayTicks());
plugin.plugin.foliaLib.getScheduler().runAtEntityLater(player,
t -> flushPendingUpdates(playerId), plugin.getUpdateBatchDelayTicks());
}
}

Expand Down Expand Up @@ -622,13 +631,6 @@ public void clearCache() {
recentModernChanges.invalidateAll();
}

private void runSyncLater(Runnable task, long delay) {
if (!plugin.plugin.isEnabled()) return;
try {
plugin.plugin.getServer().getScheduler().runTaskLater(plugin.plugin, task, delay);
} catch (Exception e) {}
}

public long packLocation(int x, int y, int z) {
return ((long)x & X_MASK) << X_SHIFT | ((long)z & Z_MASK) << Z_SHIFT | ((long)y & Y_MASK);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/tf/tuff/viablocks/PaletteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ private void broadcastNewPaletteEntry(String state) {
out.writeUTF(state);
byte[] data = out.toByteArray();

Bukkit.getScheduler().runTask(plugin.plugin, () -> {
plugin.plugin.foliaLib.getScheduler().runNextTick(gt -> {
if (!plugin.plugin.isEnabled() || !plugin.isEnabled()) return;
for (Player player : Bukkit.getOnlinePlayers()) {
if (plugin.isPlayerEnabled(player)) {
player.sendPluginMessage(plugin.plugin, ViaBlocksPlugin.CLIENTBOUND_CHANNEL, data);
}
plugin.plugin.foliaLib.getScheduler().runAtEntity(player, t -> {
if (plugin.isPlayerEnabled(player)) {
player.sendPluginMessage(plugin.plugin, ViaBlocksPlugin.CLIENTBOUND_CHANNEL, data);
}
});
}
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/tf/tuff/viablocks/ViaBlocksPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public void onTuffXEnable() {

this.paletteManager = new PaletteManager(this.versionAdapter);

try {
Class.forName("io.papermc.paper.threadedregions.scheduler.AsyncScheduler");
this.isPaper = true;
info("Paper detected. Enabling optimized asynchronous scheduling.");
} catch (ClassNotFoundException e) {
this.isPaper = false;
info("Running on Spigot/Bukkit. Using standard scheduler.");
this.isPaper = plugin.foliaLib.isPaper();
if (plugin.foliaLib.isFolia()) {
info("Folia detected. Using region-threaded scheduling.");
} else if (this.isPaper) {
info("Paper detected. Using standard scheduling.");
} else {
info("Running on Spigot/Bukkit. Using standard scheduling.");
}

plugin.saveDefaultConfig();
Expand Down Expand Up @@ -213,7 +213,7 @@ public void sendWelcomeGui(Player player) {
disclaimer.setItalic(true);
meta.spigot().addPage(new ComponentBuilder("").append(welcome).append(body).append(link).append(new TextComponent(".")).append(disclaimer).create());
book.setItemMeta(meta);
plugin.getServer().getScheduler().runTask(plugin, () -> player.openBook(book));
plugin.foliaLib.getScheduler().runAtEntity(player, t -> player.openBook(book));
}

public boolean isEnabled() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tf/tuff/viaentities/EntityInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected ChannelHandler createHandler(Player player) {

@Override
protected void onPostInject(Player player) {
plugin.plugin.getServer().getScheduler().runTask(plugin.plugin, () -> {
plugin.plugin.foliaLib.getScheduler().runAtEntity(player, t -> {
sendExistingEntities(player);
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/tf/tuff/y0/ChunkPacketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tf.tuff.TuffX;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

Expand All @@ -17,7 +18,8 @@ public ChunkPacketListener(Y0Plugin plugin) {
public void handleChunk(TuffX plugin, Player player, World world, int chunkX, int chunkZ){
if (!this.plugin.isPlayerReady(player)) return;

plugin.getServer().getScheduler().runTask(plugin, () -> {
Location chunkLoc = new Location(world, chunkX << 4, 0, chunkZ << 4);
plugin.foliaLib.getScheduler().runAtLocation(chunkLoc, t -> {
if (player.isOnline() && world.isChunkLoaded(chunkX, chunkZ)) {
Chunk chunk = world.getChunkAt(chunkX, chunkZ);
this.plugin.processAndSendChunk(player, chunk);
Expand Down
Loading