More fixes

This commit is contained in:
Alexander Söderberg 2020-07-17 16:38:07 +02:00 committed by Alexander Söderberg
parent 5a3eacde0b
commit 020b030667
16 changed files with 65 additions and 47 deletions

View File

@ -227,7 +227,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
// Stuff that needs to be created before the PlotSquared instance // Stuff that needs to be created before the PlotSquared instance
PlotPlayer.registerConverter(Player.class, BukkitUtil::getPlayer); PlotPlayer.registerConverter(Player.class, BukkitUtil::getPlayer);
TaskManager.setImplementation(new BukkitTaskManager(this, timeConverter)); TaskManager.setPlatformImplementation(new BukkitTaskManager(this, timeConverter));
final PlotSquared plotSquared = new PlotSquared(this, "Bukkit"); final PlotSquared plotSquared = new PlotSquared(this, "Bukkit");
@ -510,7 +510,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
this.startMetrics(); this.startMetrics();
if (Settings.Enabled_Components.WORLDS) { if (Settings.Enabled_Components.WORLDS) {
TaskManager.getImplementation().taskRepeat(this::unload, TaskTime.seconds(1L)); TaskManager.getPlatformImplementation().taskRepeat(this::unload, TaskTime.seconds(1L));
try { try {
singleWorldListener = getInjector().getInstance(SingleWorldListener.class); singleWorldListener = getInjector().getInstance(SingleWorldListener.class);
} catch (Exception e) { } catch (Exception e) {

View File

@ -422,7 +422,7 @@ import java.util.stream.Stream;
Block block = getWorld(location.getWorldName()) Block block = getWorld(location.getWorldName())
.getBlockAt(location.getX(), location.getY(), location.getZ()); .getBlockAt(location.getX(), location.getY(), location.getZ());
try { try {
return TaskManager.getImplementation().sync(() -> { return TaskManager.getPlatformImplementation().sync(() -> {
if (block.getState() instanceof Sign) { if (block.getState() instanceof Sign) {
Sign sign = (Sign) block.getState(); Sign sign = (Sign) block.getState();
return sign.getLines(); return sign.getLines();

View File

@ -28,6 +28,7 @@ package com.plotsquared.bukkit.util.task;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.util.task.PlotSquaredTask; import com.plotsquared.core.util.task.PlotSquaredTask;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime; import com.plotsquared.core.util.task.TaskTime;
@ -37,6 +38,7 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/** /**
* Bukkit implementation of {@link TaskManager} using * Bukkit implementation of {@link TaskManager} using
@ -79,6 +81,13 @@ import java.util.concurrent.Future;
} }
} }
@Override public <T> T sync(@Nonnull final Callable<T> function, final int timeout) throws Exception {
if (PlotSquared.get().isMainThread(Thread.currentThread())) {
return function.call();
}
return this.callMethodSync(function).get(timeout, TimeUnit.MILLISECONDS);
}
@Override public <T> Future<T> callMethodSync(@NotNull final Callable<T> method) { @Override public <T> Future<T> callMethodSync(@NotNull final Callable<T> method) {
return Bukkit.getScheduler().callSyncMethod(this.bukkitMain, method); return Bukkit.getScheduler().callSyncMethod(this.bukkitMain, method);
} }

View File

@ -153,7 +153,7 @@ public class Auto extends SubCommand {
autoClaimFromDatabase(player, area, start, new RunnableVal<Plot>() { autoClaimFromDatabase(player, area, start, new RunnableVal<Plot>() {
@Override public void run(final Plot plot) { @Override public void run(final Plot plot) {
try { try {
TaskManager.getImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic, TaskManager.getPlatformImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic,
PlotSquared.get().getEventDispatcher())); PlotSquared.get().getEventDispatcher()));
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -142,7 +142,7 @@ public class Claim extends SubCommand {
final String finalSchematic = schematic; final String finalSchematic = schematic;
DBFunc.createPlotSafe(plot, () -> { DBFunc.createPlotSafe(plot, () -> {
try { try {
TaskManager.getImplementation().sync(() -> { TaskManager.getPlatformImplementation().sync(() -> {
if (!plot.claim(player, true, finalSchematic, false)) { if (!plot.claim(player, true, finalSchematic, false)) {
logger.info(Captions.PREFIX.getTranslated() + String logger.info(Captions.PREFIX.getTranslated() + String
.format("Failed to claim plot %s", plot.getId().toCommaSeparatedString())); .format("Failed to claim plot %s", plot.getId().toCommaSeparatedString()));

View File

@ -182,7 +182,7 @@ public class DebugExec extends SubCommand {
if (this.worldEdit != null) { if (this.worldEdit != null) {
this.scope.put("WEManager", new WEManager()); this.scope.put("WEManager", new WEManager());
} }
this.scope.put("TaskManager", TaskManager.getImplementation()); this.scope.put("TaskManager", TaskManager.getPlatformImplementation());
this.scope.put("ConsolePlayer", ConsolePlayer.getConsole()); this.scope.put("ConsolePlayer", ConsolePlayer.getConsole());
this.scope.put("SchematicHandler", this.schematicHandler); this.scope.put("SchematicHandler", this.schematicHandler);
this.scope.put("ChunkManager", this.chunkManager); this.scope.put("ChunkManager", this.chunkManager);

View File

@ -40,7 +40,7 @@ import com.plotsquared.core.util.task.TaskManager;
public class PluginCmd extends SubCommand { public class PluginCmd extends SubCommand {
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) { @Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
TaskManager.getImplementation().taskAsync(() -> { TaskManager.getPlatformImplementation().taskAsync(() -> {
MainUtil.sendMessage(player, String.format( MainUtil.sendMessage(player, String.format(
"$2>> $1&l" + PlotSquared.platform().getPluginName() + " $2($1Version$2: $1%s$2)", "$2>> $1&l" + PlotSquared.platform().getPluginName() + " $2($1Version$2: $1%s$2)",
PlotSquared.get().getVersion())); PlotSquared.get().getVersion()));

View File

@ -43,6 +43,7 @@ import com.plotsquared.core.util.query.PlotQuery;
import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal;
import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -102,7 +103,7 @@ public class Trim extends SubCommand {
MainUtil.sendMessage(null, " - MCA #: " + result.value1.size()); MainUtil.sendMessage(null, " - MCA #: " + result.value1.size());
MainUtil.sendMessage(null, " - CHUNKS: " + (result.value1.size() * 1024) + " (max)"); MainUtil.sendMessage(null, " - CHUNKS: " + (result.value1.size() * 1024) + " (max)");
MainUtil.sendMessage(null, " - TIME ESTIMATE: 12 Parsecs"); MainUtil.sendMessage(null, " - TIME ESTIMATE: 12 Parsecs");
TaskManager.objectTask(plots, new RunnableVal<Plot>() { TaskManager.getPlatformImplementation().objectTask(plots, new RunnableVal<Plot>() {
@Override public void run(Plot plot) { @Override public void run(Plot plot) {
Location pos1 = plot.getCorners()[0]; Location pos1 = plot.getCorners()[0];
Location pos2 = plot.getCorners()[1]; Location pos2 = plot.getCorners()[1];
@ -119,7 +120,8 @@ public class Trim extends SubCommand {
} }
} }
} }
}, result); }).thenAccept(ignore ->
TaskManager.getPlatformImplementation().taskLater(result, TaskTime.ticks(1)));
return true; return true;
} }
@ -189,11 +191,12 @@ public class Trim extends SubCommand {
} }
} }
final LocalBlockQueue queue = blockQueue.getNewQueue(world, false); final LocalBlockQueue queue = blockQueue.getNewQueue(world, false);
TaskManager.objectTask(chunks, new RunnableVal<BlockVector2>() { TaskManager.getPlatformImplementation().objectTask(chunks, new RunnableVal<BlockVector2>() {
@Override public void run(BlockVector2 value) { @Override public void run(BlockVector2 value) {
queue.regenChunk(value.getX(), value.getZ()); queue.regenChunk(value.getX(), value.getZ());
} }
}, this); }).thenAccept(ignore -> TaskManager.getPlatformImplementation()
.taskLater(this, TaskTime.ticks(1)));
} }
}; };
} else { } else {

View File

@ -26,7 +26,6 @@
package com.plotsquared.core.generator; package com.plotsquared.core.generator;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.events.PlotFlagAddEvent;
import com.plotsquared.core.events.Result; import com.plotsquared.core.events.Result;
@ -479,7 +478,7 @@ public class HybridUtils {
} }
} }
if (!chunks.isEmpty()) { if (!chunks.isEmpty()) {
TaskManager.getImplementation().sync(() -> { TaskManager.getPlatformImplementation().sync(() -> {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Iterator<BlockVector2> iterator = chunks.iterator(); Iterator<BlockVector2> iterator = chunks.iterator();
while (System.currentTimeMillis() - start < 20 && !chunks while (System.currentTimeMillis() - start < 20 && !chunks

View File

@ -326,7 +326,7 @@ public class ExpireManager {
} }
for (ExpiryTask expiryTask : expired) { for (ExpiryTask expiryTask : expired) {
if (!expiryTask.needsAnalysis()) { if (!expiryTask.needsAnalysis()) {
expiredTask.run(newPlot, () -> TaskManager.getImplementation() expiredTask.run(newPlot, () -> TaskManager.getPlatformImplementation()
.taskLaterAsync(task, TaskTime.ticks(1L)), .taskLaterAsync(task, TaskTime.ticks(1L)),
expiryTask.requiresConfirmation()); expiryTask.requiresConfirmation());
return; return;
@ -338,7 +338,7 @@ public class ExpireManager {
passesComplexity(changed, expired, new RunnableVal<Boolean>() { passesComplexity(changed, expired, new RunnableVal<Boolean>() {
@Override public void run(Boolean confirmation) { @Override public void run(Boolean confirmation) {
expiredTask.run(newPlot, expiredTask.run(newPlot,
() -> TaskManager.getImplementation().taskLaterAsync(task, TaskTime.ticks(1L)), () -> TaskManager.getPlatformImplementation().taskLaterAsync(task, TaskTime.ticks(1L)),
confirmation); confirmation);
} }
}, () -> { }, () -> {
@ -364,7 +364,7 @@ public class ExpireManager {
@Override public void run(Boolean value) { @Override public void run(Boolean value) {
doAnalysis.run(); doAnalysis.run();
} }
}, () -> TaskManager.getImplementation().taskLaterAsync(task, TaskTime.ticks(1L))); }, () -> TaskManager.getPlatformImplementation().taskLaterAsync(task, TaskTime.ticks(1L)));
} else { } else {
doAnalysis.run(); doAnalysis.run();
} }

View File

@ -141,7 +141,7 @@ public class SinglePlotArea extends GridPlotWorld {
} }
try { try {
TaskManager.getImplementation().sync(() -> { TaskManager.getPlatformImplementation().sync(() -> {
final String name = id.getX() + "." + id.getY(); final String name = id.getX() + "." + id.getY();
if (!PlotSquared.platform().getWorldUtil().isWorld(name)) { if (!PlotSquared.platform().getWorldUtil().isWorld(name)) {
PlotSquared.platform().getSetupUtils().setupWorld(builder); PlotSquared.platform().getSetupUtils().setupWorld(builder);

View File

@ -64,7 +64,7 @@ public class SinglePlotManager extends PlotManager {
@Override public boolean clearPlot(Plot plot, final Runnable whenDone) { @Override public boolean clearPlot(Plot plot, final Runnable whenDone) {
PlotSquared.platform().getSetupUtils().unload(plot.getWorldName(), false); PlotSquared.platform().getSetupUtils().unload(plot.getWorldName(), false);
final File worldFolder = new File(PlotSquared.platform().getWorldContainer(), plot.getWorldName()); final File worldFolder = new File(PlotSquared.platform().getWorldContainer(), plot.getWorldName());
TaskManager.getImplementation().taskAsync(() -> { TaskManager.getPlatformImplementation().taskAsync(() -> {
MainUtil.deleteDirectory(worldFolder); MainUtil.deleteDirectory(worldFolder);
if (whenDone != null) { if (whenDone != null) {
whenDone.run(); whenDone.run();

View File

@ -185,7 +185,7 @@ public abstract class BasicLocalBlockQueue extends LocalBlockQueue {
@Override public void flush() { @Override public void flush() {
this.globalBlockQueue.dequeue(this); this.globalBlockQueue.dequeue(this);
try { try {
TaskManager.getImplementation().sync(() -> { TaskManager.getPlatformImplementation().sync(() -> {
while (next()) { while (next()) {
} }
return null; return null;

View File

@ -33,6 +33,7 @@ import com.plotsquared.core.plot.PlotManager;
import com.plotsquared.core.queue.LocalBlockQueue; import com.plotsquared.core.queue.LocalBlockQueue;
import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
@ -94,12 +95,13 @@ public abstract class RegionManager {
} }
} }
} }
TaskManager.objectTask(chunks, new RunnableVal<BlockVector2>() { TaskManager.getPlatformImplementation().objectTask(chunks, new RunnableVal<BlockVector2>() {
@Override public void run(BlockVector2 value) { @Override public void run(BlockVector2 value) {
chunkManager.loadChunk(world, value, false) chunkManager.loadChunk(world, value, false)
.thenRun(() -> task.run(value)); .thenRun(() -> task.run(value));
} }
}, whenDone); }).thenAccept(ignore ->
TaskManager.getPlatformImplementation().taskLater(whenDone, TaskTime.ticks(1)));
}); });
} }

View File

@ -25,16 +25,19 @@
*/ */
package com.plotsquared.core.util.task; package com.plotsquared.core.util.task;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor @RequiredArgsConstructor
public class ObjectTaskRunnable<T> implements Runnable { public class ObjectTaskRunnable<T> implements Runnable {
@Getter private final CompletableFuture<Void> completionFuture = new CompletableFuture<>();
private final Iterator<T> iterator; private final Iterator<T> iterator;
private final RunnableVal<T> task; private final RunnableVal<T> task;
private final Runnable whenDone;
@Override public void run() { @Override public void run() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@ -44,7 +47,7 @@ public class ObjectTaskRunnable<T> implements Runnable {
task.run(); task.run();
} }
if (!hasNext) { if (!hasNext) {
TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L)); completionFuture.complete(null);
} else { } else {
TaskManager.runTaskLater(this, TaskTime.ticks(1L)); TaskManager.runTaskLater(this, TaskTime.ticks(1L));
} }

View File

@ -25,7 +25,6 @@
*/ */
package com.plotsquared.core.util.task; package com.plotsquared.core.util.task;
import com.plotsquared.core.PlotSquared;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -37,8 +36,8 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@ -56,7 +55,7 @@ public abstract class TaskManager {
public static final HashMap<Integer, PlotSquaredTask> tasks = new HashMap<>(); public static final HashMap<Integer, PlotSquaredTask> tasks = new HashMap<>();
public static AtomicInteger index = new AtomicInteger(0); public static AtomicInteger index = new AtomicInteger(0);
@Getter @Setter private static TaskManager implementation; @Getter @Setter private static TaskManager platformImplementation;
/** /**
* Run a repeating synchronous task. If using a platform scheduler, * Run a repeating synchronous task. If using a platform scheduler,
@ -69,10 +68,10 @@ public abstract class TaskManager {
@Nonnull public static PlotSquaredTask runTaskRepeat(@Nullable final Runnable runnable, @Nonnull public static PlotSquaredTask runTaskRepeat(@Nullable final Runnable runnable,
@Nonnull final TaskTime taskTime) { @Nonnull final TaskTime taskTime) {
if (runnable != null) { if (runnable != null) {
if (getImplementation() == null) { if (getPlatformImplementation() == null) {
throw new IllegalArgumentException("disabled"); throw new IllegalArgumentException("disabled");
} }
return getImplementation().taskRepeat(runnable, taskTime); return getPlatformImplementation().taskRepeat(runnable, taskTime);
} }
return PlotSquaredTask.nullTask(); return PlotSquaredTask.nullTask();
} }
@ -84,11 +83,11 @@ public abstract class TaskManager {
*/ */
public static void runTaskAsync(@Nullable final Runnable runnable) { public static void runTaskAsync(@Nullable final Runnable runnable) {
if (runnable != null) { if (runnable != null) {
if (getImplementation() == null) { if (getPlatformImplementation() == null) {
runnable.run(); runnable.run();
return; return;
} }
getImplementation().taskAsync(runnable); getPlatformImplementation().taskAsync(runnable);
} }
} }
@ -100,11 +99,11 @@ public abstract class TaskManager {
*/ */
public static void runTask(@Nullable final Runnable runnable) { public static void runTask(@Nullable final Runnable runnable) {
if (runnable != null) { if (runnable != null) {
if (getImplementation() == null) { if (getPlatformImplementation() == null) {
runnable.run(); runnable.run();
return; return;
} }
getImplementation().task(runnable); getPlatformImplementation().task(runnable);
} }
} }
@ -118,11 +117,11 @@ public abstract class TaskManager {
public static void runTaskLater(@Nullable final Runnable runnable, public static void runTaskLater(@Nullable final Runnable runnable,
@Nonnull final TaskTime taskTime) { @Nonnull final TaskTime taskTime) {
if (runnable != null) { if (runnable != null) {
if (getImplementation() == null) { if (getPlatformImplementation() == null) {
runnable.run(); runnable.run();
return; return;
} }
getImplementation().taskLater(runnable, taskTime); getPlatformImplementation().taskLater(runnable, taskTime);
} }
} }
@ -136,21 +135,28 @@ public abstract class TaskManager {
public static void runTaskLaterAsync(@Nullable final Runnable runnable, public static void runTaskLaterAsync(@Nullable final Runnable runnable,
@Nonnull final TaskTime taskTime) { @Nonnull final TaskTime taskTime) {
if (runnable != null) { if (runnable != null) {
if (getImplementation() == null) { if (getPlatformImplementation() == null) {
runnable.run(); runnable.run();
return; return;
} }
getImplementation().taskLaterAsync(runnable, taskTime); getPlatformImplementation().taskLaterAsync(runnable, taskTime);
} }
} }
/** /**
* Break up a series of tasks so that they can run without lagging the server. * Break up a series of tasks so that they can run without lagging the server
*
* @param objects Objects to perform the task on
* @param task Task to perform
* @param <T> Object type
* @return Future that completes when the tasks are done
*/ */
public static <T> void objectTask(Collection<T> objects, final RunnableVal<T> task, public <T> CompletableFuture<Void> objectTask(@Nonnull final Collection<T> objects,
final Runnable whenDone) { @Nonnull final RunnableVal<T> task) {
final Iterator<T> iterator = objects.iterator(); final Iterator<T> iterator = objects.iterator();
TaskManager.runTask(new ObjectTaskRunnable<>(iterator, task, whenDone)); final ObjectTaskRunnable<T> taskRunnable = new ObjectTaskRunnable<>(iterator, task);
TaskManager.runTask(taskRunnable);
return taskRunnable.getCompletionFuture();
} }
/** /**
@ -161,7 +167,7 @@ public abstract class TaskManager {
* @return Method result * @return Method result
* @throws Exception If the call fails * @throws Exception If the call fails
*/ */
public <T> T sync(final Callable<T> function) throws Exception { public <T> T sync(@Nonnull final Callable<T> function) throws Exception {
return sync(function, Integer.MAX_VALUE); return sync(function, Integer.MAX_VALUE);
} }
@ -174,12 +180,8 @@ public abstract class TaskManager {
* @return Method result * @return Method result
* @throws Exception If the call fails * @throws Exception If the call fails
*/ */
public <T> T sync(final Callable<T> function, int timeout) throws Exception { public abstract <T> T sync(@Nonnull final Callable<T> function, final int timeout)
if (PlotSquared.get().isMainThread(Thread.currentThread())) { throws Exception;
return function.call();
}
return this.callMethodSync(function).get(timeout, TimeUnit.MILLISECONDS);
}
/** /**
* Call a method synchronously and return a future with * Call a method synchronously and return a future with