Skip to content
Merged
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
15 changes: 15 additions & 0 deletions src/main/java/top/vulpine/simpleLobby/SimpleLobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import top.vulpine.simpleLobby.command.SpawnCommand;
import top.vulpine.simpleLobby.listener.PlayerListener;
import top.vulpine.simpleLobby.listener.WorldListener;
import top.vulpine.simpleLobby.scheduler.BukkitSchedulerAdapter;
import top.vulpine.simpleLobby.scheduler.FoliaScheduler;
import top.vulpine.simpleLobby.scheduler.SchedulerAdapter;
import top.vulpine.simpleLobby.utils.ActionParser;
import top.vulpine.simpleLobby.utils.logger.LogLevel;
import top.vulpine.simpleLobby.utils.logger.Logger;
Expand All @@ -19,6 +22,7 @@
public final class SimpleLobby extends JavaPlugin {

private ActionParser actionParser;
private SchedulerAdapter scheduler;

private static final int PLUGIN_ID = 28227;

Expand All @@ -36,6 +40,17 @@ public void onEnable() {
}
Logger.init(logLevel);

boolean folia;
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
folia = true;
} catch (ClassNotFoundException e) {
folia = false;
}
this.scheduler = folia ? new FoliaScheduler(this) : new BukkitSchedulerAdapter(this);
Logger.debug("Detected " + (folia ? "Folia" : "Bukkit/Spigot") + " server, using "
+ scheduler.getClass().getSimpleName());

String[] message = {
"",
"<white> _____ <green>__",
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/top/vulpine/simpleLobby/UpdateNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import top.vulpine.simpleLobby.utils.Colorize;
import top.vulpine.simpleLobby.utils.PermissionChecker;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class UpdateNotifier implements Listener {

private final JavaPlugin plugin;
private final SimpleLobby plugin;
private final String projectSlug;
private final String message;
private volatile String cachedLatestVersion;

public UpdateNotifier(JavaPlugin plugin, String projectSlug, String message) {
public UpdateNotifier(SimpleLobby plugin, String projectSlug, String message) {
this.plugin = plugin;
this.projectSlug = projectSlug;
this.message = message;

Bukkit.getPluginManager().registerEvents(this, plugin);

Bukkit.getScheduler().runTaskAsynchronously(plugin, this::updateCache);
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::updateCache, 20L * 60L * 30L, 20L * 60L * 30L);
plugin.getScheduler().runAsync(this::updateCache);
plugin.getScheduler().runAsyncRepeating(this::updateCache, 30, 30, TimeUnit.MINUTES);
}

private void updateCache() {
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/top/vulpine/simpleLobby/command/SpawnCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import top.vulpine.simpleLobby.SimpleLobby;
import top.vulpine.simpleLobby.scheduler.Cancellable;
import top.vulpine.simpleLobby.utils.ActionParser;
import top.vulpine.simpleLobby.utils.Colorize;
import top.vulpine.simpleLobby.utils.PermissionChecker;
Expand All @@ -31,7 +30,7 @@ public class SpawnCommand implements CommandExecutor, TabCompleter, Listener {

private final SimpleLobby plugin;
private final ActionParser actionParser;
private final Map<UUID, BukkitTask> tasks = new ConcurrentHashMap<>();
private final Map<UUID, Cancellable> tasks = new ConcurrentHashMap<>();
private final Map<UUID, Location> locations = new ConcurrentHashMap<>();

public SpawnCommand(SimpleLobby plugin) {
Expand Down Expand Up @@ -78,30 +77,22 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
UUID uuid = player.getUniqueId();
locations.put(uuid, player.getLocation().clone());

BukkitTask task = new BukkitRunnable() {
Cancellable task = plugin.getScheduler().runEntityLater(player, () -> {
PlayerUtils.teleportPlayer(plugin, player);
tasks.remove(uuid);
locations.remove(uuid);

@Override
public void run() {
PlayerUtils.teleportPlayer(plugin, player);
tasks.remove(uuid);
locations.remove(uuid);

List<String> teleportActions = plugin.getConfig().getStringList("spawn.actions.teleported");
actionParser.executeActions(teleportActions, player, 0, new HashMap<>());
}

}.runTaskLater(plugin, seconds * 20L);
List<String> teleportActions = plugin.getConfig().getStringList("spawn.actions.teleported");
actionParser.executeActions(teleportActions, player, 0, new HashMap<>());
}, seconds * 20L);

tasks.put(uuid, task);

} else {

new BukkitRunnable() {
@Override
public void run() {
PlayerUtils.teleportPlayer(plugin, player);
}
}.runTaskLater(plugin, seconds * 20L);
plugin.getScheduler().runEntityLater(player,
() -> PlayerUtils.teleportPlayer(plugin, player),
seconds * 20L);

}

Expand Down Expand Up @@ -136,7 +127,7 @@ public void onPlayerMove(PlayerMoveEvent event) {

if (from.getBlockX() != to.getBlockX() || from.getBlockY() != to.getBlockY() || from.getBlockZ() != to.getBlockZ()) {

BukkitTask task = tasks.remove(uuid);
Cancellable task = tasks.remove(uuid);
if (task != null) task.cancel();
locations.remove(uuid);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package top.vulpine.simpleLobby.scheduler;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.concurrent.TimeUnit;

public class BukkitSchedulerAdapter implements SchedulerAdapter {

private final Plugin plugin;

public BukkitSchedulerAdapter(Plugin plugin) {
this.plugin = plugin;
}

@Override
public void runGlobal(Runnable task) {
if (Bukkit.isPrimaryThread()) {
task.run();
} else {
Bukkit.getScheduler().runTask(plugin, task);
}
}

@Override
public void runEntity(Entity entity, Runnable task) {
if (Bukkit.isPrimaryThread()) {
task.run();
} else {
Bukkit.getScheduler().runTask(plugin, task);
}
}

@Override
public Cancellable runEntityLater(Entity entity, Runnable task, long ticks) {
BukkitTask bt = Bukkit.getScheduler().runTaskLater(plugin, task, Math.max(1L, ticks));
return bt::cancel;
}

@Override
public Cancellable runGlobalLater(Runnable task, long ticks) {
BukkitTask bt = Bukkit.getScheduler().runTaskLater(plugin, task, Math.max(1L, ticks));
return bt::cancel;
}

@Override
public Cancellable runAsync(Runnable task) {
BukkitTask bt = Bukkit.getScheduler().runTaskAsynchronously(plugin, task);
return bt::cancel;
}

@Override
public Cancellable runAsyncLater(Runnable task, long delay, TimeUnit unit) {
long ticks = Math.max(1L, unit.toMillis(delay) / 50L);
BukkitTask bt = Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, task, ticks);
return bt::cancel;
}

@Override
public Cancellable runAsyncRepeating(Runnable task, long initialDelay, long period, TimeUnit unit) {
long delayTicks = Math.max(1L, unit.toMillis(initialDelay) / 50L);
long periodTicks = Math.max(1L, unit.toMillis(period) / 50L);
BukkitTask bt = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delayTicks, periodTicks);
return bt::cancel;
}

@Override
public void teleport(Player player, Location location) {
if (Bukkit.isPrimaryThread()) {
player.teleport(location);
} else {
Bukkit.getScheduler().runTask(plugin, () -> player.teleport(location));
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/top/vulpine/simpleLobby/scheduler/Cancellable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package top.vulpine.simpleLobby.scheduler;

/**
* Handle to a scheduled task that can be cancelled.
* Returned by {@link SchedulerAdapter} delayed-task methods.
*/
@FunctionalInterface
public interface Cancellable {

Cancellable NOOP = () -> {};

void cancel();
}
Loading
Loading