Skip to content

Prepare support Folia #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>${bukkit.version}</version>
<scope>provided</scope>
</dependency>
Expand Down Expand Up @@ -74,6 +78,12 @@
<version>4.3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<url>http://www.citizensnpcs.co</url>
<ciManagement>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/citizensnpcs/api/CitizensAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;

import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.api.util.schedulers.SchedulerAdapter;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
Expand All @@ -20,6 +22,9 @@
* Contains methods used in order to utilize the Citizens API.
*/
public final class CitizensAPI {

private static SchedulerAdapter scheduler;

private CitizensAPI() {
}

Expand Down Expand Up @@ -195,6 +200,21 @@ public static void setImplementation(CitizensPlugin implementation) {
instance = implementation;
}

/**
* The new scheduler that works on Folia and Spigot
* @return scheduler Folia or Spigot
*/
public static SchedulerAdapter getScheduler() {
if (scheduler == null) {
if (SpigotUtil.isFoliaServer()) {
scheduler = new net.citizensnpcs.api.util.schedulers.adapter.FoliaScheduler(getPlugin());
} else {
scheduler = new net.citizensnpcs.api.util.schedulers.adapter.SpigotScheduler(getPlugin());
}
}
return scheduler;
}

/**
* Shuts down any resources currently being held.
*/
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/net/citizensnpcs/api/LocationLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.Future;
import java.util.function.BiConsumer;

import net.citizensnpcs.api.util.schedulers.SchedulerRunnable;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
Expand All @@ -20,7 +21,6 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
Expand All @@ -31,7 +31,7 @@
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;

public class LocationLookup extends BukkitRunnable {
public class LocationLookup extends SchedulerRunnable {
private final Map<String, PerPlayerMetadata<?>> metadata = Maps.newHashMap();
private Future<Map<UUID, PhTreeF<NPC>>> npcFuture = null;
private Map<UUID, PhTreeF<NPC>> npcWorlds = Maps.newHashMap();
Expand Down Expand Up @@ -123,7 +123,7 @@ public Iterable<Player> getNearbyVisiblePlayers(Entity base, Location location,

@SuppressWarnings({ "unchecked", "rawtypes" })
public void onJoin(PlayerJoinEvent event) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
CitizensAPI.getScheduler().runEntityTask(event.getPlayer(), () -> {
updateWorld(event.getPlayer().getWorld());
for (PerPlayerMetadata meta : metadata.values()) {
if (meta.onJoin != null) {
Expand All @@ -134,7 +134,7 @@ public void onJoin(PlayerJoinEvent event) {
}

public void onQuit(PlayerQuitEvent event) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
CitizensAPI.getScheduler().runEntityTask(event.getPlayer(), () -> {
updateWorld(event.getPlayer().getWorld());
for (PerPlayerMetadata<?> meta : metadata.values()) {
meta.sent.remove(event.getPlayer().getUniqueId());
Expand Down Expand Up @@ -173,12 +173,14 @@ public void run() {
Map<UUID, Collection<TreeFactory.Node<NPC>>> map = Maps.newHashMap();
Location loc = new Location(null, 0, 0, 0);
for (NPC npc : sourceRegistry) {
if (!npc.isSpawned())
continue;
npc.getEntity().getLocation(loc);
Collection<TreeFactory.Node<NPC>> nodes = map.computeIfAbsent(npc.getEntity().getWorld().getUID(),
uid -> Lists.newArrayList());
nodes.add(new TreeFactory.Node<>(new double[] { loc.getX(), loc.getY(), loc.getZ() }, npc));
CitizensAPI.getScheduler().runEntityTask(npc.getEntity(), () -> {
if (!npc.isSpawned())
return;
npc.getEntity().getLocation(loc);
Collection<TreeFactory.Node<NPC>> nodes = map.computeIfAbsent(npc.getEntity().getWorld().getUID(),
uid -> Lists.newArrayList());
nodes.add(new TreeFactory.Node<>(new double[] { loc.getX(), loc.getY(), loc.getZ() }, npc));
});
}
npcFuture = ForkJoinPool.commonPool().submit(new TreeFactory<>(map));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.concurrent.Callable;

import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.util.SpigotUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
Expand All @@ -24,10 +26,11 @@ protected ChunkSnapshot getChunkObject(int x, int z) {
// TODO: pre-load multiple chunks on cache miss
Callable<ChunkSnapshot> call = () -> world.getChunkAt(x, z).getChunkSnapshot(false, false, false);
try {
if (!Bukkit.isPrimaryThread()) {
if (!SpigotUtil.isFoliaServer() && !Bukkit.isPrimaryThread()) {
return Bukkit.getScheduler().callSyncMethod(null, call).get();
} else
return call.call();
}
return call.call();

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import net.citizensnpcs.api.util.schedulers.SchedulerRunnable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -86,7 +87,7 @@ private void close(NPC npc, Block point) {
@Override
public void onReached(NPC npc, Block point) {
Location doorCentre = point.getLocation().add(0.5, 0, 0.5);
new BukkitRunnable() {
new SchedulerRunnable() {
@Override
public void run() {
if (!npc.getNavigator().isNavigating()) {
Expand All @@ -101,7 +102,7 @@ public void run() {
cancel();
}
}
}.runTaskTimer(CitizensAPI.getPlugin(), 3, 1);
}.runRegionTaskTimer(CitizensAPI.getPlugin(), doorCentre, 3, 1);
}

private void open(NPC npc, Block point, Material type) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/gui/InputMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
HandlerList.unregisterAll(this);
String chat = event.getMessage();
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
CitizensAPI.getScheduler().runEntityTask(event.getPlayer(), () -> {
if (chat.equals("\"\"") || chat.equals("''") || chat.equals("null")) {
callback.accept("");
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/gui/InventoryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void run() {

private void runViewerModifier(Runnable run) {
if (delayViewerChanges) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), run);
CitizensAPI.getScheduler().runTask(run);
} else {
run.run();
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/citizensnpcs/api/npc/AbstractNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,17 @@ private void teleport(final Entity entity, Location location, int delay, Telepor
final Entity passenger = entity.getPassenger();
entity.eject();
if (!location.getWorld().equals(entity.getWorld())) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> entity.teleport(location, cause), delay++);
CitizensAPI.getScheduler().runEntityTaskLater(entity,
() -> SpigotUtil.teleportAsync(entity, location, cause), delay++);
} else {
entity.teleport(location, cause);
SpigotUtil.teleportAsync(entity, location, cause);
}
if (passenger == null)
return;
teleport(passenger, location, delay++, cause);
Runnable task = () -> entity.setPassenger(passenger);
if (!location.getWorld().equals(entity.getWorld())) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), task, delay);
CitizensAPI.getScheduler().runEntityTaskLater(entity, task, delay);
} else {
task.run();
}
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/net/citizensnpcs/api/util/SpigotUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import java.util.Locale;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -277,6 +276,18 @@ public static ItemStack parseItemStack(ItemStack base, String item) {
return base;
}

public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location) {
return ASYNC_TELEPORT ? entity.teleportAsync(location) : CompletableFuture.completedFuture(entity.teleport(location));
}

public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, PlayerTeleportEvent.TeleportCause cause) {
return ASYNC_TELEPORT ? entity.teleportAsync(location, cause) : CompletableFuture.completedFuture(entity.teleport(location, cause));
}

public static boolean isFoliaServer() {
return FOLIA_SERVER;
}

private static ChronoUnit toChronoUnit(TimeUnit tu) {
switch (tu) {
case NANOSECONDS:
Expand Down Expand Up @@ -306,12 +317,26 @@ private static ChronoUnit toChronoUnit(TimeUnit tu) {
private static boolean SUPPORT_WORLD_HEIGHT = true;
private static boolean SUPPORTS_KEYED;
private static Boolean using1_13API;
private static boolean FOLIA_SERVER = false;
private static boolean ASYNC_TELEPORT = false;

static {
try {
Class.forName("org.bukkit.Keyed");
SUPPORTS_KEYED = true;
} catch (ClassNotFoundException e) {
}
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
FOLIA_SERVER = true;
} catch (ClassNotFoundException ignored) {
FOLIA_SERVER = false;
}
try {
Entity.class.getMethod("teleportAsync", Location.class, PlayerTeleportEvent.TeleportCause.class);
ASYNC_TELEPORT = true;
} catch (NoSuchMethodException exception) {
ASYNC_TELEPORT = false;
}
}
}
Loading