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
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");
@ -510,7 +510,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
this.startMetrics();
if (Settings.Enabled_Components.WORLDS) {
TaskManager.getImplementation().taskRepeat(this::unload, TaskTime.seconds(1L));
TaskManager.getPlatformImplementation().taskRepeat(this::unload, TaskTime.seconds(1L));
try {
singleWorldListener = getInjector().getInstance(SingleWorldListener.class);
} catch (Exception e) {

View File

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

View File

@ -28,6 +28,7 @@ package com.plotsquared.bukkit.util.task;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.util.task.PlotSquaredTask;
import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
@ -37,6 +38,7 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* 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) {
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>() {
@Override public void run(final Plot plot) {
try {
TaskManager.getImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic,
TaskManager.getPlatformImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic,
PlotSquared.get().getEventDispatcher()));
} catch (final Exception e) {
e.printStackTrace();

View File

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

View File

@ -182,7 +182,7 @@ public class DebugExec extends SubCommand {
if (this.worldEdit != null) {
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("SchematicHandler", this.schematicHandler);
this.scope.put("ChunkManager", this.chunkManager);

View File

@ -40,7 +40,7 @@ import com.plotsquared.core.util.task.TaskManager;
public class PluginCmd extends SubCommand {
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
TaskManager.getImplementation().taskAsync(() -> {
TaskManager.getPlatformImplementation().taskAsync(() -> {
MainUtil.sendMessage(player, String.format(
"$2>> $1&l" + PlotSquared.platform().getPluginName() + " $2($1Version$2: $1%s$2)",
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.RunnableVal2;
import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion;
import org.slf4j.Logger;
@ -102,7 +103,7 @@ public class Trim extends SubCommand {
MainUtil.sendMessage(null, " - MCA #: " + result.value1.size());
MainUtil.sendMessage(null, " - CHUNKS: " + (result.value1.size() * 1024) + " (max)");
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) {
Location pos1 = plot.getCorners()[0];
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;
}
@ -189,11 +191,12 @@ public class Trim extends SubCommand {
}
}
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) {
queue.regenChunk(value.getX(), value.getZ());
}
}, this);
}).thenAccept(ignore -> TaskManager.getPlatformImplementation()
.taskLater(this, TaskTime.ticks(1)));
}
};
} else {

View File

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

View File

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

View File

@ -141,7 +141,7 @@ public class SinglePlotArea extends GridPlotWorld {
}
try {
TaskManager.getImplementation().sync(() -> {
TaskManager.getPlatformImplementation().sync(() -> {
final String name = id.getX() + "." + id.getY();
if (!PlotSquared.platform().getWorldUtil().isWorld(name)) {
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) {
PlotSquared.platform().getSetupUtils().unload(plot.getWorldName(), false);
final File worldFolder = new File(PlotSquared.platform().getWorldContainer(), plot.getWorldName());
TaskManager.getImplementation().taskAsync(() -> {
TaskManager.getPlatformImplementation().taskAsync(() -> {
MainUtil.deleteDirectory(worldFolder);
if (whenDone != null) {
whenDone.run();

View File

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

View File

@ -33,6 +33,7 @@ import com.plotsquared.core.plot.PlotManager;
import com.plotsquared.core.queue.LocalBlockQueue;
import com.plotsquared.core.util.task.RunnableVal;
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.math.BlockVector2;
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) {
chunkManager.loadChunk(world, value, false)
.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;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor
public class ObjectTaskRunnable<T> implements Runnable {
@Getter private final CompletableFuture<Void> completionFuture = new CompletableFuture<>();
private final Iterator<T> iterator;
private final RunnableVal<T> task;
private final Runnable whenDone;
@Override public void run() {
long start = System.currentTimeMillis();
@ -44,7 +47,7 @@ public class ObjectTaskRunnable<T> implements Runnable {
task.run();
}
if (!hasNext) {
TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L));
completionFuture.complete(null);
} else {
TaskManager.runTaskLater(this, TaskTime.ticks(1L));
}

View File

@ -25,7 +25,6 @@
*/
package com.plotsquared.core.util.task;
import com.plotsquared.core.PlotSquared;
import lombok.Getter;
import lombok.Setter;
@ -37,8 +36,8 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
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 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,
@ -69,10 +68,10 @@ public abstract class TaskManager {
@Nonnull public static PlotSquaredTask runTaskRepeat(@Nullable final Runnable runnable,
@Nonnull final TaskTime taskTime) {
if (runnable != null) {
if (getImplementation() == null) {
if (getPlatformImplementation() == null) {
throw new IllegalArgumentException("disabled");
}
return getImplementation().taskRepeat(runnable, taskTime);
return getPlatformImplementation().taskRepeat(runnable, taskTime);
}
return PlotSquaredTask.nullTask();
}
@ -84,11 +83,11 @@ public abstract class TaskManager {
*/
public static void runTaskAsync(@Nullable final Runnable runnable) {
if (runnable != null) {
if (getImplementation() == null) {
if (getPlatformImplementation() == null) {
runnable.run();
return;
}
getImplementation().taskAsync(runnable);
getPlatformImplementation().taskAsync(runnable);
}
}
@ -100,11 +99,11 @@ public abstract class TaskManager {
*/
public static void runTask(@Nullable final Runnable runnable) {
if (runnable != null) {
if (getImplementation() == null) {
if (getPlatformImplementation() == null) {
runnable.run();
return;
}
getImplementation().task(runnable);
getPlatformImplementation().task(runnable);
}
}
@ -118,11 +117,11 @@ public abstract class TaskManager {
public static void runTaskLater(@Nullable final Runnable runnable,
@Nonnull final TaskTime taskTime) {
if (runnable != null) {
if (getImplementation() == null) {
if (getPlatformImplementation() == null) {
runnable.run();
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,
@Nonnull final TaskTime taskTime) {
if (runnable != null) {
if (getImplementation() == null) {
if (getPlatformImplementation() == null) {
runnable.run();
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,
final Runnable whenDone) {
public <T> CompletableFuture<Void> objectTask(@Nonnull final Collection<T> objects,
@Nonnull final RunnableVal<T> task) {
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
* @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);
}
@ -174,12 +180,8 @@ public abstract class TaskManager {
* @return Method result
* @throws Exception If the call fails
*/
public <T> T sync(final Callable<T> function, int timeout) throws Exception {
if (PlotSquared.get().isMainThread(Thread.currentThread())) {
return function.call();
}
return this.callMethodSync(function).get(timeout, TimeUnit.MILLISECONDS);
}
public abstract <T> T sync(@Nonnull final Callable<T> function, final int timeout)
throws Exception;
/**
* Call a method synchronously and return a future with