Skip to content

Commit

Permalink
Start addition of get tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicallyCoded committed Mar 11, 2024
1 parent 8073c26 commit 271d576
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
allprojects {
apply plugin: 'java'

version = '0.3.1'
version = '0.3.2'

repositories {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -437,6 +438,18 @@ public interface ServerImplementation {
*/
void cancelAllTasks();

/**
* Get all tasks owned by this plugin
* @return WrappedTask instances
*/
List<WrappedTask> getAllTasks();

/**
* Get all tasks across the server
* @return WrappedTask instances
*/
List<WrappedTask> getAllServerTasks();

/**
* Get a player by name (approximately).
* When using folia, this can be run sync or async. If this is run async on non-folia platforms, it will block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ public interface WrappedTask {

Plugin getOwningPlugin();

/**
* Whether the task is async or not
* <p>
* Async tasks are never run on any world threads, including on Folia
*
* @return true if the task is async
*/
boolean isAsync();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler;
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;


@SuppressWarnings("unused")
Expand Down Expand Up @@ -363,7 +370,72 @@ public void cancelAllTasks() {
this.asyncScheduler.cancelTasks(plugin);
}

@Override
@Override
public List<WrappedTask> getAllTasks() {
try {
// Filter and wrap
return getAllScheduledTasks().stream()
.filter(task -> task.getOwningPlugin().equals(plugin))
.map(this::wrapTask)
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
public List<WrappedTask> getAllServerTasks() {
try {
// Filter and wrap
return getAllScheduledTasks().stream()
.map(this::wrapTask)
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@NotNull
private List<ScheduledTask> getAllScheduledTasks() throws NoSuchFieldException, IllegalAccessException {
// Global tasks
Class<? extends GlobalRegionScheduler> globalClass = this.globalRegionScheduler.getClass();

Field tasksByDeadlineField = globalClass.getDeclaredField("tasksByDeadline");
boolean wasAccessible = tasksByDeadlineField.isAccessible();
tasksByDeadlineField.setAccessible(true);

// noinspection unchecked
Long2ObjectOpenHashMap<List<ScheduledTask>> globalTasksMap = (Long2ObjectOpenHashMap<List<ScheduledTask>>) tasksByDeadlineField.get(this.globalRegionScheduler);
tasksByDeadlineField.setAccessible(wasAccessible);

// Async tasks
Class<? extends AsyncScheduler> asyncClass = this.asyncScheduler.getClass();

Field asyncTasksField = asyncClass.getDeclaredField("tasks");
wasAccessible = asyncTasksField.isAccessible();
asyncTasksField.setAccessible(true);

Set<ScheduledTask> asyncTasks = (Set<ScheduledTask>) asyncTasksField.get(this.asyncScheduler);
asyncTasksField.setAccessible(wasAccessible);

// Combine global tasks
List<ScheduledTask> globalTasks = new ArrayList<>();
for (List<ScheduledTask> list : globalTasksMap.values()) {
globalTasks.addAll(list);
}

// Combine all tasks
List<ScheduledTask> allTasks = new ArrayList<>(globalTasks.size() + asyncTasks.size());
allTasks.addAll(globalTasks);
allTasks.addAll(asyncTasks);
return allTasks;
}

@Override
public Player getPlayer(String name) {
// This is thread-safe in folia
return this.plugin.getServer().getPlayer(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@

public class WrappedFoliaTask implements WrappedTask {

private static final Class<? extends ScheduledTask> ASYNC_TASK_CLASS;

static {
Class<? extends ScheduledTask> asyncTaskClass = null;
try {
// noinspection unchecked
asyncTaskClass = (Class<? extends ScheduledTask>) Class.forName("io.papermc.paper.threadedregions.scheduler.FoliaAsyncScheduler.AsyncScheduledTask");
} catch (ClassNotFoundException e) {
// ignore
}
ASYNC_TASK_CLASS = asyncTaskClass;
}

private final ScheduledTask task;

private final boolean async;

public WrappedFoliaTask(ScheduledTask task) {
this.task = task;

if (ASYNC_TASK_CLASS == null) this.async = false;
else this.async = ASYNC_TASK_CLASS.isAssignableFrom(task.getClass());
}

@Override
Expand All @@ -26,4 +44,9 @@ public boolean isCancelled() {
public Plugin getOwningPlugin() {
return this.task.getOwningPlugin();
}

@Override
public boolean isAsync() {
return this.async;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class LegacySpigotImplementation implements ServerImplementation {
Expand Down Expand Up @@ -297,6 +299,21 @@ public void cancelAllTasks() {
this.scheduler.cancelTasks(plugin);
}

@Override
public List<WrappedTask> getAllTasks() {
return this.scheduler.getPendingTasks().stream()
.filter(task -> task.getOwner().equals(plugin))
.map(this::wrapTask)
.collect(Collectors.toList());
}

@Override
public List<WrappedTask> getAllServerTasks() {
return this.scheduler.getPendingTasks().stream()
.map(this::wrapTask)
.collect(Collectors.toList());
}

@Override
public Player getPlayer(String name) {
return this.getPlayerFromMainThread(() -> this.plugin.getServer().getPlayer(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public boolean isCancelled() {
public Plugin getOwningPlugin() {
return this.task.getOwner();
}

@Override
public boolean isAsync() {
return !this.task.isSync();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class SpigotImplementation implements ServerImplementation {
Expand Down Expand Up @@ -259,6 +261,21 @@ public void cancelAllTasks() {
this.scheduler.cancelTasks(plugin);
}

@Override
public List<WrappedTask> getAllTasks() {
return this.scheduler.getPendingTasks().stream()
.filter(task -> task.getOwner().equals(plugin))
.map(this::wrapTask)
.collect(Collectors.toList());
}

@Override
public List<WrappedTask> getAllServerTasks() {
return this.scheduler.getPendingTasks().stream()
.map(this::wrapTask)
.collect(Collectors.toList());
}

@Override
public Player getPlayer(String name) {
return this.getPlayerFromMainThread(() -> this.plugin.getServer().getPlayer(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public boolean isCancelled() {
public Plugin getOwningPlugin() {
return this.task.getOwner();
}

@Override
public boolean isAsync() {
return !this.task.isSync();
}

}

0 comments on commit 271d576

Please sign in to comment.