editsession util

This commit is contained in:
Jesse Boyd
2019-11-04 21:55:40 +00:00
parent 88732bb88c
commit 2cb734bba2
9 changed files with 151 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import com.github.intellectualsites.plotsquared.plot.util.SchematicHandler;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper;
import com.sk89q.worldedit.extension.platform.Actor;
import lombok.NoArgsConstructor;
import java.util.Collections;

View File

@ -16,6 +16,7 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandlerImplementation;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider;
import com.sk89q.worldedit.extension.platform.Actor;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@ -248,4 +249,6 @@ import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil;
@NotNull IndependentPlotGenerator getDefaultGenerator();
List<String> getPluginIds();
Actor getConsole();
}

View File

@ -5,6 +5,8 @@ import com.github.intellectualsites.plotsquared.plot.commands.RequiredType;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.item.ItemType;
import org.jetbrains.annotations.NotNull;
@ -36,6 +38,10 @@ public class ConsolePlayer extends PlotPlayer {
return instance;
}
@Override public Actor toActor() {
return PlotSquared.get().IMP.getConsole();
}
@Override public boolean canTeleport(@NotNull Location location) {
return true;
}

View File

@ -1116,7 +1116,7 @@ public class Plot {
* Delete a plot (use null for the runnable if you don't need to be notified on completion)
*
* @see PlotSquared#removePlot(Plot, boolean)
* @see #clear(Runnable) to simply clear a plot
* @see #clear(boolean, boolean, Runnable) to simply clear a plot
*/
public boolean deletePlot(final Runnable whenDone) {
if (!this.hasOwner()) {

View File

@ -19,6 +19,7 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Preconditions;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.item.ItemType;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
@ -86,6 +87,8 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer {
return UUIDHandler.getPlayer(name);
}
public abstract Actor toActor();
/**
* Set some session only metadata for this player.
*

View File

@ -0,0 +1,116 @@
package com.github.intellectualsites.plotsquared.plot.util.world;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.concurrent.Future;
import java.util.function.Consumer;
public class OperationUtil {
private static final boolean ASYNC;
static {
boolean hasFawe = true;
try {
Class.forName("com.boydti.fawe.Fawe");
} catch (ClassNotFoundException ignore) {
hasFawe = false;
}
ASYNC = hasFawe;
}
public Future<?> withEditSession(@NotNull PlotPlayer plotPlayer, @NotNull Consumer<EditSession> consumer, @Nullable Consumer<Throwable> exceptionHandler) {
if (ASYNC) {
ListeningExecutorService exec = WorldEdit.getInstance().getExecutorService();
return exec.submit(
() -> withEditSessionOnThread(plotPlayer, consumer, exceptionHandler));
} else {
withEditSessionOnThread(plotPlayer, consumer, exceptionHandler);
}
return Futures.immediateFuture(true);
}
private void withEditSessionOnThread(PlotPlayer plotPlayer, Consumer<EditSession> consumer, Consumer<Throwable> exceptionHandler) {
Actor actor = plotPlayer.toActor();
World weWorld = getWorld(plotPlayer, actor);
LocalSession session = getSession(actor);
try (EditSession ess = createEditSession(weWorld, actor, session)) {
try {
consumer.accept(ess);
} finally {
ess.close();
session.remember(ess);
}
} catch (Throwable e) {
if (exceptionHandler != null) {
exceptionHandler.accept(e);
} else {
e.printStackTrace();
}
}
}
private static World getWorld(String worldName) {
Platform platform = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING);
List<? extends World> worlds = platform.getWorlds();
for (World current : worlds) {
if (current.getName().equals(worldName)) {
return current;
}
}
return null;
}
private static World getWorld(PlotPlayer plotPlayer, Actor actor) {
World weWorld;
if (actor instanceof Player) {
weWorld = ((Player) actor).getWorld();
} else {
@NotNull Location loc = plotPlayer.getLocation();
String world = loc.getWorld();
weWorld = getWorld(world);
}
return weWorld;
}
private static EditSession createEditSession(PlotPlayer plotPlayer) {
Actor actor = plotPlayer.toActor();
World weWorld = getWorld(plotPlayer, actor);
return createEditSession(weWorld, actor);
}
private static LocalSession getSession(Actor actor) {
return WorldEdit.getInstance().getSessionManager().get(actor);
}
private static EditSession createEditSession(World world, Actor actor) {
return createEditSession(world, actor, getSession(actor));
}
private static EditSession createEditSession(World world, Actor actor, LocalSession session) {
EditSession editSession;
Player player = actor.isPlayer() ? (Player) actor : null;
editSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(world, -1, null, player);
editSession.setFastMode(!actor.isPlayer());
editSession.setReorderMode(EditSession.ReorderMode.FAST);
return editSession;
}
}