From 2cb734bba2955ef55b16042f145736e13f327a28 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 4 Nov 2019 21:55:40 +0000 Subject: [PATCH] editsession util --- .../plotsquared/bukkit/BukkitMain.java | 11 ++ .../bukkit/object/BukkitPlayer.java | 6 + .../bukkit/util/BukkitTaskManager.java | 6 +- .../plotsquared/api/PlotAPI.java | 1 + .../plotsquared/plot/IPlotMain.java | 3 + .../plot/object/ConsolePlayer.java | 6 + .../plotsquared/plot/object/Plot.java | 2 +- .../plotsquared/plot/object/PlotPlayer.java | 3 + .../plot/util/world/OperationUtil.java | 116 ++++++++++++++++++ 9 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/OperationUtil.java diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java index 25ad69723..7d4c8bf4b 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java @@ -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.uuid.UUIDWrapper; 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 lombok.Getter; import lombok.NonNull; @@ -74,6 +78,7 @@ import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.World; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; @@ -823,4 +828,10 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain } return names; } + + @Override public Actor getConsole() { + @NotNull ConsoleCommandSender console = Bukkit.getServer().getConsoleSender(); + WorldEditPlugin wePlugin = ((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit")); + return wePlugin.wrapCommandSender(console); + } } diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java index 35fa0d20b..9ca1bbbae 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java @@ -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.UUIDHandler; 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 io.papermc.lib.PaperLib; import org.bukkit.GameMode; @@ -55,6 +57,10 @@ public class BukkitPlayer extends PlotPlayer { super.populatePersistentMetaMap(); } + @Override public Actor toActor() { + return BukkitAdapter.adapt(player); + } + @NotNull @Override public Location getLocation() { final Location location = super.getLocation(); return location == null ? BukkitUtil.getLocation(this.player) : location; diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitTaskManager.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitTaskManager.java index 56448ff34..cbe09142c 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitTaskManager.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitTaskManager.java @@ -3,6 +3,8 @@ package com.github.intellectualsites.plotsquared.bukkit.util; import com.github.intellectualsites.plotsquared.bukkit.BukkitMain; import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; public class BukkitTaskManager extends TaskManager { @@ -24,8 +26,8 @@ public class BukkitTaskManager extends TaskManager { } @Override public void taskAsync(Runnable runnable) { - this.bukkitMain.getServer().getScheduler().runTaskAsynchronously(this.bukkitMain, runnable) - .getTaskId(); + @NotNull BukkitTask task = this.bukkitMain.getServer().getScheduler() + .runTaskAsynchronously(this.bukkitMain, runnable); } @Override public void task(Runnable runnable) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java index 610362953..740db8fca 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/api/PlotAPI.java @@ -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; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java index 3a47773b7..b532cb4ce 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java @@ -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 getPluginIds(); + + Actor getConsole(); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java index 28ad5669b..ae9fffb89 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java @@ -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; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 9718eb198..33e482428 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -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()) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java index 5eddb6364..f48cbc981 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java @@ -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. * diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/OperationUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/OperationUtil.java new file mode 100644 index 000000000..05b969065 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/OperationUtil.java @@ -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 consumer, @Nullable Consumer 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 consumer, Consumer 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 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; + } +}