Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
add folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyGalbreath committed Apr 27, 2023
1 parent 6521ed9 commit f62937d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
41 changes: 37 additions & 4 deletions bukkit/src/main/java/net/pl3x/map/bukkit/Pl3xMapBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package net.pl3x.map.bukkit;

import com.destroystokyo.paper.event.server.ServerTickEndEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import net.pl3x.map.bukkit.command.BukkitCommandManager;
import net.pl3x.map.core.Pl3xMap;
Expand All @@ -38,12 +41,23 @@
import org.checkerframework.checker.nullness.qual.NonNull;

public class Pl3xMapBukkit extends JavaPlugin implements Listener {
private final Pl3xMap pl3xmap;
private final Pl3xMapImpl pl3xmap;
private final PlayerListener playerListener = new PlayerListener();

private final boolean isFolia;
private Timer foliaTimer;

public Pl3xMapBukkit() {
super();
this.pl3xmap = new Pl3xMapImpl(this);

boolean isFolia = false;
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler");
isFolia = true;
} catch (ClassNotFoundException ignore) {
}
this.isFolia = isFolia;
}

@Override
Expand All @@ -52,22 +66,41 @@ public void onEnable() {

getServer().getPluginManager().registerEvents(this, this);

getServer().getScheduler().runTaskTimer(this, () -> this.pl3xmap.getScheduler().tick(), 1, 1);

try {
new BukkitCommandManager(this);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (this.isFolia) {
// try to replicate a main tick heartbeat, 50ms
this.foliaTimer = new Timer();
this.foliaTimer.schedule(new TimerTask() {
@Override
public void run() {
pl3xmap.getScheduler().tick();
}
}, 50L, 50L);
}
}

@Override
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
if (this.foliaTimer != null) {
this.foliaTimer.cancel();
this.foliaTimer = null;
}

this.pl3xmap.disable();
}

@EventHandler
public void onServerTick(ServerTickEndEvent event) {
if (this.pl3xmap != null && !this.isFolia) {
this.pl3xmap.getScheduler().tick();
}
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerJoin(@NonNull PlayerJoinEvent event) {
PlayerRegistry registry = Pl3xMap.api().getPlayerRegistry();
Expand Down
1 change: 1 addition & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: "${name}"
main: "${group}.Pl3xMapBukkit"
version: "${version}"
api-version: "1.19"
folia-supported: true
load: "POSTWORLD"
authors: ${authors}
description: "${description}"
Expand Down

0 comments on commit f62937d

Please sign in to comment.