Extract TaskManager lambdas for better debugging

This commit is contained in:
Alexander Söderberg 2020-04-07 23:18:36 +02:00
parent 7fdb7961ce
commit 0d4af3023d
6 changed files with 112 additions and 66 deletions

View file

@ -8,9 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc; import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.events.PlayerAutoPlotEvent; import com.github.intellectualsites.plotsquared.plot.events.PlayerAutoPlotEvent;
import com.github.intellectualsites.plotsquared.plot.events.PlotAutoMergeEvent; import com.github.intellectualsites.plotsquared.plot.events.PlotAutoMergeEvent;
import com.github.intellectualsites.plotsquared.plot.events.PlotMergeEvent;
import com.github.intellectualsites.plotsquared.plot.events.Result; import com.github.intellectualsites.plotsquared.plot.events.Result;
import com.github.intellectualsites.plotsquared.plot.object.Direction;
import com.github.intellectualsites.plotsquared.plot.object.Expression; import com.github.intellectualsites.plotsquared.plot.object.Expression;
import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
@ -19,6 +17,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause; import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.AutoClaimFinishTask;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler; import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions; import com.github.intellectualsites.plotsquared.plot.util.Permissions;
@ -42,7 +41,7 @@ public class Auto extends SubCommand {
return id.getNextId(step); return id.getNextId(step);
} }
private static boolean checkAllowedPlots(PlotPlayer player, PlotArea plotarea, public static boolean checkAllowedPlots(PlotPlayer player, PlotArea plotarea,
@Nullable Integer allowedPlots, int sizeX, int sizeZ) { @Nullable Integer allowedPlots, int sizeX, int sizeZ) {
if (allowedPlots == null) { if (allowedPlots == null) {
allowedPlots = player.getAllowedPlots(); allowedPlots = player.getAllowedPlots();
@ -126,31 +125,7 @@ public class Auto extends SubCommand {
player.setMeta(Auto.class.getName(), true); player.setMeta(Auto.class.getName(), true);
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) {
TaskManager.IMP.sync(new RunnableVal<Object>() { TaskManager.IMP.sync(new AutoClaimFinishTask(player, plot, area, allowedPlots, schematic));
@Override public void run(Object ignore) {
player.deleteMeta(Auto.class.getName());
if (plot == null) {
MainUtil.sendMessage(player, Captions.NO_FREE_PLOTS);
return;
}
if (checkAllowedPlots(player, area, allowedPlots, 1, 1)) {
plot.claim(player, true, schematic, false);
if (area.isAutoMerge()) {
PlotMergeEvent event = PlotSquared.get().getEventDispatcher()
.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
if (event.getEventResult() == Result.DENY) {
sendMessage(player, Captions.EVENT_DENIED, "Auto merge");
} else {
plot.autoMerge(event.getDir(), event.getMax(), player.getUUID(),
true);
}
}
} else {
DBFunc.delete(plot);
}
}
});
} }
}); });
} }

View file

@ -1042,10 +1042,6 @@ public class Plot {
if (!isLoaded()) { if (!isLoaded()) {
return; return;
} }
if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
TaskManager.runTask(() -> Plot.this.setSign(name));
return;
}
PlotManager manager = this.area.getPlotManager(); PlotManager manager = this.area.getPlotManager();
if (this.area.allowSigns()) { if (this.area.allowSigns()) {
Location location = manager.getSignLoc(this); Location location = manager.getSignLoc(this);
@ -1058,9 +1054,7 @@ public class Plot {
"%plr%", name), "%plr%", name),
Captions.OWNER_SIGN_LINE_4.formatted().replaceAll("%id%", id).replaceAll( Captions.OWNER_SIGN_LINE_4.formatted().replaceAll("%id%", id).replaceAll(
"%plr%", name)}; "%plr%", name)};
WorldUtil.IMP WorldUtil.IMP.setSign(this.getWorldName(), location.getX(), location.getY(), location.getZ(), lines);
.setSign(this.getWorldName(), location.getX(), location.getY(), location.getZ(),
lines);
} }
} }

View file

@ -0,0 +1,50 @@
package com.github.intellectualsites.plotsquared.plot.util;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.commands.Auto;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.events.PlotMergeEvent;
import com.github.intellectualsites.plotsquared.plot.events.Result;
import com.github.intellectualsites.plotsquared.plot.object.Direction;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import lombok.RequiredArgsConstructor;
import static com.github.intellectualsites.plotsquared.plot.util.MainUtil.sendMessage;
@RequiredArgsConstructor public final class AutoClaimFinishTask extends RunnableVal<Object> {
private final PlotPlayer player;
private final Plot plot;
private final PlotArea area;
private final int allowedPlots;
private final String schematic;
@Override public void run(Object value) {
player.deleteMeta(Auto.class.getName());
if (plot == null) {
sendMessage(player, Captions.NO_FREE_PLOTS);
return;
}
if (Auto.checkAllowedPlots(player, area, allowedPlots, 1, 1)) {
plot.claim(player, true, schematic, false);
if (area.isAutoMerge()) {
PlotMergeEvent event = PlotSquared.get().getEventDispatcher()
.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
if (event.getEventResult() == Result.DENY) {
sendMessage(player, Captions.EVENT_DENIED, "Auto merge");
} else {
plot.autoMerge(event.getDir(), event.getMax(), player.getUUID(),
true);
}
}
} else {
DBFunc.delete(plot);
}
}
}

View file

@ -0,0 +1,28 @@
package com.github.intellectualsites.plotsquared.plot.util;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import lombok.RequiredArgsConstructor;
import java.util.Iterator;
@RequiredArgsConstructor public class ObjectTaskRunnable<T> implements Runnable {
private final Iterator<T> iterator;
private final RunnableVal<T> task;
private final Runnable whenDone;
@Override public void run() {
long start = System.currentTimeMillis();
boolean hasNext;
while ((hasNext = iterator.hasNext()) && System.currentTimeMillis() - start < 5) {
task.value = iterator.next();
task.run();
}
if (!hasNext) {
TaskManager.runTaskLater(whenDone, 1);
} else {
TaskManager.runTaskLater(this, 1);
}
}
}

View file

@ -0,0 +1,28 @@
package com.github.intellectualsites.plotsquared.plot.util;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import lombok.RequiredArgsConstructor;
import java.util.concurrent.atomic.AtomicBoolean;
@RequiredArgsConstructor public class RuntimeExceptionRunnableVal<T> extends RunnableVal<RuntimeException> {
private final RunnableVal<T> function;
private final AtomicBoolean running;
@Override public void run(RuntimeException value) {
try {
function.run();
} catch (RuntimeException e) {
this.value = e;
} catch (Throwable neverHappens) {
neverHappens.printStackTrace();
} finally {
running.set(false);
}
synchronized (function) {
function.notifyAll();
}
}
}

View file

@ -93,21 +93,7 @@ public abstract class TaskManager {
public static <T> void objectTask(Collection<T> objects, final RunnableVal<T> task, public static <T> void objectTask(Collection<T> objects, final RunnableVal<T> task,
final Runnable whenDone) { final Runnable whenDone) {
final Iterator<T> iterator = objects.iterator(); final Iterator<T> iterator = objects.iterator();
TaskManager.runTask(new Runnable() { TaskManager.runTask(new ObjectTaskRunnable<>(iterator, task, whenDone));
@Override public void run() {
long start = System.currentTimeMillis();
boolean hasNext;
while ((hasNext = iterator.hasNext()) && System.currentTimeMillis() - start < 5) {
task.value = iterator.next();
task.run();
}
if (!hasNext) {
TaskManager.runTaskLater(whenDone, 1);
} else {
TaskManager.runTaskLater(this, 1);
}
}
});
} }
public <T> T sync(final RunnableVal<T> function) { public <T> T sync(final RunnableVal<T> function) {
@ -120,22 +106,7 @@ public abstract class TaskManager {
return function.value; return function.value;
} }
final AtomicBoolean running = new AtomicBoolean(true); final AtomicBoolean running = new AtomicBoolean(true);
RunnableVal<RuntimeException> run = new RunnableVal<RuntimeException>() { final RuntimeExceptionRunnableVal<T> run = new RuntimeExceptionRunnableVal<>(function, running);
@Override public void run(RuntimeException value) {
try {
function.run();
} catch (RuntimeException e) {
this.value = e;
} catch (Throwable neverHappens) {
neverHappens.printStackTrace();
} finally {
running.set(false);
}
synchronized (function) {
function.notifyAll();
}
}
};
TaskManager.IMP.task(run); TaskManager.IMP.task(run);
try { try {
synchronized (function) { synchronized (function) {