Folia scheduler and owned region API

Pulling Folia API to Paper is primarily intended for plugins
that want to target both Paper and Folia without unnecessary
compatibility layers.

Add both a location based scheduler, an entity based scheduler,
and a global region scheduler.

Owned region API may be useful for plugins which want to perform
operations over large areas outside of the buffer zone provided
by the regionaliser, as it is not guaranteed that anything
outside of the buffer zone is owned. Then, the plugins may use
the schedulers depending on the result of the ownership check.
This commit is contained in:
Spottedleaf
2023-06-17 11:52:52 +02:00
parent 31871f6b40
commit a13bff4a5f
11 changed files with 1374 additions and 135 deletions

View File

@@ -927,24 +927,22 @@
this.onServerExit();
}
@@ -889,9 +1336,16 @@
@@ -889,7 +1336,14 @@
}
private boolean haveTime() {
- return this.runningTask() || Util.getNanos() < (this.mayHaveDelayedTasks ? this.delayedTasksMaxNextTickTimeNanos : this.nextTickTimeNanos);
+ // CraftBukkit start
+ return this.forceTicks || this.runningTask() || Util.getNanos() < (this.mayHaveDelayedTasks ? this.delayedTasksMaxNextTickTimeNanos : this.nextTickTimeNanos);
}
+ }
+
+ private void executeModerately() {
+ this.runAllTasks();
+ java.util.concurrent.locks.LockSupport.parkNanos("executing tasks", 1000L);
+ // CraftBukkit end
+ }
+
public static boolean throwIfFatalException() {
RuntimeException runtimeexception = (RuntimeException) MinecraftServer.fatalException.get();
}
public static boolean throwIfFatalException() {
@@ -903,7 +1357,7 @@
}
@@ -1061,11 +1059,25 @@
ObjectArrayList<GameProfile> objectarraylist = new ObjectArrayList(j);
int k = Mth.nextInt(this.random, 0, list.size() - j);
@@ -1154,24 +1623,58 @@
@@ -1154,24 +1623,72 @@
this.getPlayerList().getPlayers().forEach((entityplayer) -> {
entityplayer.connection.suspendFlushing();
});
+ this.server.getScheduler().mainThreadHeartbeat(); // CraftBukkit
+ // Paper start - Folia scheduler API
+ ((io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler) Bukkit.getGlobalRegionScheduler()).tick();
+ getAllLevels().forEach(level -> {
+ for (final Entity entity : level.getEntities().getAll()) {
+ if (entity.isRemoved()) {
+ continue;
+ }
+ final org.bukkit.craftbukkit.entity.CraftEntity bukkit = entity.getBukkitEntityRaw();
+ if (bukkit != null) {
+ bukkit.taskScheduler.executeTick();
+ }
+ }
+ });
+ // Paper end - Folia scheduler API
+ io.papermc.paper.adventure.providers.ClickCallbackProviderImpl.CALLBACK_MANAGER.handleQueue(this.tickCount); // Paper
gameprofilerfiller.push("commandFunctions");
this.getFunctions().tick();
@@ -1121,7 +1133,7 @@
gameprofilerfiller.push("tick");
@@ -1186,7 +1689,9 @@
@@ -1186,7 +1703,9 @@
gameprofilerfiller.pop();
gameprofilerfiller.pop();
@@ -1131,7 +1143,7 @@
gameprofilerfiller.popPush("connection");
this.tickConnection();
@@ -1267,6 +1772,22 @@
@@ -1267,6 +1786,22 @@
return (ServerLevel) this.levels.get(key);
}
@@ -1154,7 +1166,7 @@
public Set<ResourceKey<Level>> levelKeys() {
return this.levels.keySet();
}
@@ -1296,7 +1817,7 @@
@@ -1296,7 +1831,7 @@
@DontObfuscate
public String getServerModName() {
@@ -1163,7 +1175,7 @@
}
public SystemReport fillSystemReport(SystemReport details) {
@@ -1347,7 +1868,7 @@
@@ -1347,7 +1882,7 @@
@Override
public void sendSystemMessage(Component message) {
@@ -1172,7 +1184,7 @@
}
public KeyPair getKeyPair() {
@@ -1385,11 +1906,14 @@
@@ -1385,11 +1920,14 @@
}
}
@@ -1192,7 +1204,7 @@
}
}
@@ -1403,7 +1927,7 @@
@@ -1403,7 +1941,7 @@
while (iterator.hasNext()) {
ServerLevel worldserver = (ServerLevel) iterator.next();
@@ -1201,7 +1213,7 @@
}
}
@@ -1481,10 +2005,20 @@
@@ -1481,10 +2019,20 @@
@Override
public String getMotd() {
@@ -1223,7 +1235,7 @@
this.motd = motd;
}
@@ -1507,7 +2041,7 @@
@@ -1507,7 +2055,7 @@
}
public ServerConnectionListener getConnection() {
@@ -1232,7 +1244,7 @@
}
public boolean isReady() {
@@ -1593,7 +2127,7 @@
@@ -1593,7 +2141,7 @@
@Override
public void executeIfPossible(Runnable runnable) {
if (this.isStopped()) {
@@ -1241,7 +1253,7 @@
} else {
super.executeIfPossible(runnable);
}
@@ -1632,13 +2166,19 @@
@@ -1632,13 +2180,19 @@
return this.functionManager;
}
@@ -1263,7 +1275,7 @@
}, this).thenCompose((immutablelist) -> {
MultiPackResourceManager resourcemanager = new MultiPackResourceManager(PackType.SERVER_DATA, immutablelist);
List<Registry.PendingTags<?>> list = TagLoader.loadTagsForExistingRegistries(resourcemanager, this.registries.compositeAccess());
@@ -1654,17 +2194,21 @@
@@ -1654,17 +2208,21 @@
}).thenAcceptAsync((minecraftserver_reloadableresources) -> {
this.resources.close();
this.resources = minecraftserver_reloadableresources;
@@ -1285,7 +1297,7 @@
}, this);
if (this.isSameThread()) {
@@ -1789,14 +2333,15 @@
@@ -1789,14 +2347,15 @@
if (this.isEnforceWhitelist()) {
PlayerList playerlist = source.getServer().getPlayerList();
UserWhiteList whitelist = playerlist.getWhiteList();
@@ -1303,7 +1315,7 @@
}
}
@@ -1952,7 +2497,7 @@
@@ -1952,7 +2511,7 @@
final List<String> list = Lists.newArrayList();
final GameRules gamerules = this.getGameRules();
@@ -1312,7 +1324,7 @@
@Override
public <T extends GameRules.Value<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> type) {
list.add(String.format(Locale.ROOT, "%s=%s\n", key.getId(), gamerules.getRule(key)));
@@ -2058,7 +2603,7 @@
@@ -2058,7 +2617,7 @@
try {
label51:
{
@@ -1321,19 +1333,22 @@
try {
arraylist = Lists.newArrayList(NativeModuleLister.listModules());
@@ -2108,6 +2653,21 @@
}
@@ -2105,9 +2664,24 @@
if (bufferedwriter != null) {
bufferedwriter.close();
}
+
+ }
+
+ // CraftBukkit start
+ public boolean isDebugging() {
+ return false;
+ }
+
+ public static MinecraftServer getServer() {
+ return SERVER; // Paper
+ }
+
}
+ @Deprecated
+ public static RegistryAccess getDefaultRegistryAccess() {
+ return CraftRegistry.getMinecraftRegistry();
@@ -1343,7 +1358,7 @@
private ProfilerFiller createProfiler() {
if (this.willStartRecordingMetrics) {
this.metricsRecorder = ActiveMetricsRecorder.createStarted(new ServerMetricsSamplersProvider(Util.timeSource, this.isDedicatedServer()), Util.timeSource, Util.ioPool(), new MetricsPersister("server"), this.onMetricsRecordingStopped, (path) -> {
@@ -2225,18 +2785,24 @@
@@ -2225,18 +2799,24 @@
}
public void logChatMessage(Component message, ChatType.Bound params, @Nullable String prefix) {
@@ -1372,7 +1387,7 @@
}
public boolean logIPs() {
@@ -2377,6 +2943,32 @@
@@ -2377,6 +2957,32 @@
}
public static record ServerResourcePackInfo(UUID id, String url, String hash, boolean isRequired, @Nullable Component prompt) {

View File

@@ -397,7 +397,7 @@
if (advancementdataplayer != null) {
advancementdataplayer.save();
@@ -334,95 +525,209 @@
@@ -334,95 +525,210 @@
}
@@ -489,6 +489,7 @@
- this.server.getCustomBossEvents().onPlayerDisconnect(player);
- UUID uuid = player.getUUID();
+ worldserver.removePlayerImmediately(entityplayer, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
+ entityplayer.retireScheduler(); // Paper - Folia schedulers
+ entityplayer.getAdvancements().stopListening();
+ this.players.remove(entityplayer);
+ this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
@@ -645,7 +646,7 @@
if (entityplayer1 != null) {
set.add(entityplayer1);
@@ -431,72 +736,160 @@
@@ -431,72 +737,160 @@
Iterator iterator1 = set.iterator();
while (iterator1.hasNext()) {
@@ -827,7 +828,7 @@
return entityplayer1;
}
@@ -516,15 +909,32 @@
@@ -516,15 +910,32 @@
}
public void sendPlayerPermissionLevel(ServerPlayer player) {
@@ -862,7 +863,7 @@
this.sendAllPlayerInfoIn = 0;
}
@@ -541,6 +951,25 @@
@@ -541,6 +952,25 @@
}
@@ -888,7 +889,7 @@
public void broadcastAll(Packet<?> packet, ResourceKey<Level> dimension) {
Iterator iterator = this.players.iterator();
@@ -554,7 +983,7 @@
@@ -554,7 +984,7 @@
}
@@ -897,7 +898,7 @@
PlayerTeam scoreboardteam = source.getTeam();
if (scoreboardteam != null) {
@@ -573,7 +1002,7 @@
@@ -573,7 +1003,7 @@
}
}
@@ -906,7 +907,7 @@
PlayerTeam scoreboardteam = source.getTeam();
if (scoreboardteam == null) {
@@ -619,7 +1048,7 @@
@@ -619,7 +1049,7 @@
}
public void deop(GameProfile profile) {
@@ -915,7 +916,7 @@
ServerPlayer entityplayer = this.getPlayer(profile.getId());
if (entityplayer != null) {
@@ -629,6 +1058,11 @@
@@ -629,6 +1059,11 @@
}
private void sendPlayerPermissionLevel(ServerPlayer player, int permissionLevel) {
@@ -927,7 +928,7 @@
if (player.connection != null) {
byte b0;
@@ -643,36 +1077,53 @@
@@ -643,36 +1078,53 @@
player.connection.send(new ClientboundEntityEventPacket(player, b0));
}
@@ -994,7 +995,7 @@
if (entityplayer != player && entityplayer.level().dimension() == worldKey) {
double d4 = x - entityplayer.getX();
double d5 = y - entityplayer.getY();
@@ -687,10 +1138,12 @@
@@ -687,10 +1139,12 @@
}
public void saveAll() {
@@ -1007,7 +1008,7 @@
}
public UserWhiteList getWhiteList() {
@@ -712,15 +1165,19 @@
@@ -712,15 +1166,19 @@
public void reloadWhiteList() {}
public void sendLevelInfo(ServerPlayer player, ServerLevel world) {
@@ -1031,7 +1032,7 @@
}
player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.LEVEL_CHUNKS_LOAD_START, 0.0F));
@@ -729,8 +1186,16 @@
@@ -729,8 +1187,16 @@
public void sendAllPlayerInfo(ServerPlayer player) {
player.inventoryMenu.sendAllDataToRemote();
@@ -1049,7 +1050,7 @@
}
public int getPlayerCount() {
@@ -746,6 +1211,7 @@
@@ -746,6 +1212,7 @@
}
public void setUsingWhiteList(boolean whitelistEnabled) {
@@ -1057,7 +1058,7 @@
this.doWhiteList = whitelistEnabled;
}
@@ -786,12 +1252,36 @@
@@ -786,12 +1253,36 @@
}
public void removeAll() {
@@ -1096,7 +1097,7 @@
public void broadcastSystemMessage(Component message, boolean overlay) {
this.broadcastSystemMessage(message, (entityplayer) -> {
return message;
@@ -819,24 +1309,43 @@
@@ -819,24 +1310,43 @@
}
public void broadcastChatMessage(PlayerChatMessage message, ServerPlayer sender, ChatType.Bound params) {
@@ -1143,7 +1144,7 @@
}
if (flag1 && sender != null) {
@@ -845,20 +1354,27 @@
@@ -845,20 +1355,27 @@
}
@@ -1176,7 +1177,7 @@
Path path = file2.toPath();
if (FileUtil.isPathNormalized(path) && FileUtil.isPathPortable(path) && path.startsWith(file.getPath()) && file2.isFile()) {
@@ -867,7 +1383,7 @@
@@ -867,7 +1384,7 @@
}
serverstatisticmanager = new ServerStatsCounter(this.server, file1);
@@ -1185,7 +1186,7 @@
}
return serverstatisticmanager;
@@ -875,13 +1391,13 @@
@@ -875,13 +1392,13 @@
public PlayerAdvancements getPlayerAdvancements(ServerPlayer player) {
UUID uuid = player.getUUID();
@@ -1201,7 +1202,7 @@
}
advancementdataplayer.setPlayer(player);
@@ -932,15 +1448,28 @@
@@ -932,15 +1449,28 @@
}
public void reloadResources() {