editsession util

This commit is contained in:
Jesse Boyd 2019-11-04 21:55:40 +00:00
parent 88732bb88c
commit 2cb734bba2
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
9 changed files with 151 additions and 3 deletions

View File

@ -65,6 +65,10 @@ import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider; import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider;
import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper; import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitCommandSender;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extension.platform.Actor;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
@ -74,6 +78,7 @@ import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
@ -823,4 +828,10 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
} }
return names; return names;
} }
@Override public Actor getConsole() {
@NotNull ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
WorldEditPlugin wePlugin = ((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"));
return wePlugin.wrapCommandSender(console);
}
} }

View File

@ -12,6 +12,8 @@ import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.StringMan; import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -55,6 +57,10 @@ public class BukkitPlayer extends PlotPlayer {
super.populatePersistentMetaMap(); super.populatePersistentMetaMap();
} }
@Override public Actor toActor() {
return BukkitAdapter.adapt(player);
}
@NotNull @Override public Location getLocation() { @NotNull @Override public Location getLocation() {
final Location location = super.getLocation(); final Location location = super.getLocation();
return location == null ? BukkitUtil.getLocation(this.player) : location; return location == null ? BukkitUtil.getLocation(this.player) : location;

View File

@ -3,6 +3,8 @@ package com.github.intellectualsites.plotsquared.bukkit.util;
import com.github.intellectualsites.plotsquared.bukkit.BukkitMain; import com.github.intellectualsites.plotsquared.bukkit.BukkitMain;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
public class BukkitTaskManager extends TaskManager { public class BukkitTaskManager extends TaskManager {
@ -24,8 +26,8 @@ public class BukkitTaskManager extends TaskManager {
} }
@Override public void taskAsync(Runnable runnable) { @Override public void taskAsync(Runnable runnable) {
this.bukkitMain.getServer().getScheduler().runTaskAsynchronously(this.bukkitMain, runnable) @NotNull BukkitTask task = this.bukkitMain.getServer().getScheduler()
.getTaskId(); .runTaskAsynchronously(this.bukkitMain, runnable);
} }
@Override public void task(Runnable runnable) { @Override public void task(Runnable runnable) {

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.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper; import com.github.intellectualsites.plotsquared.plot.uuid.UUIDWrapper;
import com.sk89q.worldedit.extension.platform.Actor;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.Collections; 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.UUIDHandlerImplementation;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider; import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider;
import com.sk89q.worldedit.extension.platform.Actor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -248,4 +249,6 @@ import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil;
@NotNull IndependentPlotGenerator getDefaultGenerator(); @NotNull IndependentPlotGenerator getDefaultGenerator();
List<String> getPluginIds(); 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.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode; import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather; 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.regions.CuboidRegion;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -36,6 +38,10 @@ public class ConsolePlayer extends PlotPlayer {
return instance; return instance;
} }
@Override public Actor toActor() {
return PlotSquared.get().IMP.getConsole();
}
@Override public boolean canTeleport(@NotNull Location location) { @Override public boolean canTeleport(@NotNull Location location) {
return true; 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) * Delete a plot (use null for the runnable if you don't need to be notified on completion)
* *
* @see PlotSquared#removePlot(Plot, boolean) * @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) { public boolean deletePlot(final Runnable whenDone) {
if (!this.hasOwner()) { 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.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager; import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import lombok.NonNull; import lombok.NonNull;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -86,6 +87,8 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer {
return UUIDHandler.getPlayer(name); return UUIDHandler.getPlayer(name);
} }
public abstract Actor toActor();
/** /**
* Set some session only metadata for this player. * 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;
}
}