From e7a80981bb5090f6cb40122538ba6597a8952c99 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 17 Jul 2023 17:24:30 +0200 Subject: [PATCH] Fix FAWE exec() Signed-off-by: yoyosource --- .../script/lua/SteamWarLuaPlugin.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/SteamWarLuaPlugin.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/SteamWarLuaPlugin.java index eeb28976..c8e51070 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/SteamWarLuaPlugin.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/SteamWarLuaPlugin.java @@ -19,13 +19,21 @@ package de.steamwar.bausystem.features.script.lua; +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.event.platform.CommandEvent; +import com.sk89q.worldedit.extension.platform.Actor; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.lua.libs.LuaLib; +import de.steamwar.bausystem.features.world.WorldEditListener; +import de.steamwar.bausystem.utils.WorldEditUtils; import de.steamwar.inventory.SWAnvilInv; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaValue; @@ -36,9 +44,12 @@ import org.luaj.vm2.lib.TwoArgFunction; import org.luaj.vm2.lib.VarArgFunction; import java.util.*; +import java.util.logging.Level; public class SteamWarLuaPlugin extends TwoArgFunction { + private static final boolean hasFAWE = Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit") != null; + protected static final Map, List> LUA_LIBS = new HashMap<>(); public static void add(LuaLib luaLib) { @@ -103,8 +114,24 @@ public class SteamWarLuaPlugin extends TwoArgFunction { env.set("exec", new VarArgFunction() { @Override public Varargs invoke(Varargs args) { - player.performCommand(varArgsToString(args)); + String command = varArgsToString(args); + PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); + Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); + if (preprocessEvent.isCancelled()) { + return LuaValue.NIL; + } + Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command); + String[] commandSplit = command.split(" "); + if (!commandSplit[0].equals("select") && hasFAWE && WorldEditListener.isWorldEditCommand("/" + commandSplit[0])) { + EditSession editSession = WorldEditUtils.getEditSession(player); + Actor actor = BukkitAdapter.adapt(player); + WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().handleCommandOnCurrentThread(new CommandEvent(actor, command, editSession)); + editSession.flushSession(); + WorldEditUtils.addToPlayer(player, editSession); + } else { + Bukkit.getServer().dispatchCommand(player, command); + } return LuaValue.NIL; } });