Make pick item provider Folia-compatible

This commit is contained in:
Nassim Jahnke 2024-12-09 15:47:42 +01:00
parent bccbf80446
commit 366675f7f8
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
2 changed files with 33 additions and 12 deletions

View File

@ -47,6 +47,7 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -56,6 +57,8 @@ import org.bukkit.plugin.java.JavaPlugin;
public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player> { public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player> {
private static final boolean FOLIA = PaperViaInjector.hasClass("io.papermc.paper.threadedregions.RegionizedServer"); private static final boolean FOLIA = PaperViaInjector.hasClass("io.papermc.paper.threadedregions.RegionizedServer");
private static final Runnable DUMMY_RUNNABLE = () -> {
};
private static ViaVersionPlugin instance; private static ViaVersionPlugin instance;
private final BukkitCommandHandler commandHandler = new BukkitCommandHandler(); private final BukkitCommandHandler commandHandler = new BukkitCommandHandler();
private final BukkitViaConfig conf; private final BukkitViaConfig conf;
@ -179,6 +182,24 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player>
return new BukkitViaTask(getServer().getScheduler().runTaskLater(this, runnable, delay)); return new BukkitViaTask(getServer().getScheduler().runTaskLater(this, runnable, delay));
} }
public PlatformTask<?> runSyncAt(Runnable runnable, Block block) {
if (FOLIA) {
return new FoliaViaTask(getServer().getRegionScheduler().run(this, block.getLocation(), (e) -> runnable.run()));
}
return runSync(runnable);
}
public PlatformTask<?> runSyncFor(Runnable runnable, Player player) {
if (FOLIA) {
return new FoliaViaTask(player.getScheduler().run(this, (e) -> runnable.run(), DUMMY_RUNNABLE));
}
return runSync(() -> {
if (player.isOnline()) {
runnable.run();
}
});
}
@Override @Override
public PlatformTask runRepeatingSync(Runnable runnable, long period) { public PlatformTask runRepeatingSync(Runnable runnable, long period) {
if (FOLIA) { if (FOLIA) {

View File

@ -52,12 +52,12 @@ public final class BukkitPickItemProvider extends PickItemProvider {
@Override @Override
public void pickItemFromBlock(final UserConnection connection, final BlockPosition blockPosition, final boolean includeData) { public void pickItemFromBlock(final UserConnection connection, final BlockPosition blockPosition, final boolean includeData) {
final UUID uuid = connection.getProtocolInfo().getUuid(); final UUID uuid = connection.getProtocolInfo().getUuid();
plugin.getServer().getScheduler().runTask(plugin, () -> { final Player player = plugin.getServer().getPlayer(uuid);
final Player player = plugin.getServer().getPlayer(uuid); if (player == null) {
if (player == null) { return;
return; }
}
plugin.runSyncFor(() -> {
final Location playerLocation = player.getLocation(); final Location playerLocation = player.getLocation();
if (blockPosition.distanceFromCenterSquared(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ()) > BLOCK_RANGE_SQUARED) { if (blockPosition.distanceFromCenterSquared(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ()) > BLOCK_RANGE_SQUARED) {
return; return;
@ -72,7 +72,7 @@ public final class BukkitPickItemProvider extends PickItemProvider {
if (item != null) { if (item != null) {
pickItem(player, item); pickItem(player, item);
} }
}); }, player);
} }
private @Nullable ItemStack blockToItem(final Block block, final boolean includeData) { private @Nullable ItemStack blockToItem(final Block block, final boolean includeData) {
@ -96,12 +96,12 @@ public final class BukkitPickItemProvider extends PickItemProvider {
} }
final UUID uuid = connection.getProtocolInfo().getUuid(); final UUID uuid = connection.getProtocolInfo().getUuid();
plugin.getServer().getScheduler().runTask(plugin, () -> { final Player player = plugin.getServer().getPlayer(uuid);
final Player player = plugin.getServer().getPlayer(uuid); if (player == null) {
if (player == null) { return;
return; }
}
plugin.runSyncFor(() -> {
final Entity entity = player.getWorld().getNearbyEntities(player.getLocation(), ENTITY_RANGE, ENTITY_RANGE, ENTITY_RANGE).stream() final Entity entity = player.getWorld().getNearbyEntities(player.getLocation(), ENTITY_RANGE, ENTITY_RANGE, ENTITY_RANGE).stream()
.filter(e -> e.getEntityId() == entityId) .filter(e -> e.getEntityId() == entityId)
.findAny() .findAny()
@ -114,7 +114,7 @@ public final class BukkitPickItemProvider extends PickItemProvider {
if (spawnEggType != null) { if (spawnEggType != null) {
pickItem(player, new ItemStack(spawnEggType, 1)); pickItem(player, new ItemStack(spawnEggType, 1));
} }
}); }, player);
} }
private void pickItem(final Player player, final ItemStack item) { private void pickItem(final Player player, final ItemStack item) {