From b8e068480c9afc95e44c815f940c83856fe33dc4 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Sat, 19 Aug 2023 20:06:24 +0200 Subject: [PATCH] Made WorldEdit optional --- .../sbdevelopment/v10lift/V10LiftPlugin.java | 11 +++ .../v10lift/commands/V10LiftCommand.java | 74 +++---------------- .../v10lift/utils/WorldEditUtil.java | 66 +++++++++++++++++ src/main/resources/messages.yml | 1 + src/main/resources/plugin.yml | 2 +- 5 files changed, 91 insertions(+), 63 deletions(-) create mode 100644 src/main/java/tech/sbdevelopment/v10lift/utils/WorldEditUtil.java diff --git a/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java b/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java index ceb8087..48a6321 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java +++ b/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java @@ -30,6 +30,7 @@ public class V10LiftPlugin extends JavaPlugin { @Getter private static YamlFile items; private static boolean vault = false; + private static boolean worldEdit = false; @Override public void onEnable() { @@ -72,6 +73,12 @@ public class V10LiftPlugin extends JavaPlugin { vault = true; } + //Load worldedit if found + if (Bukkit.getPluginManager().getPlugin("WorldEdit") != null) { + Bukkit.getLogger().info("[V10Lift] Loading WorldEdit hook for selection support."); + worldEdit = true; + } + //Load the command getCommand("v10lift").setExecutor(new V10LiftCommand()); getCommand("v10lift").setTabCompleter(new V10LiftTabCompleter()); @@ -148,4 +155,8 @@ public class V10LiftPlugin extends JavaPlugin { public static boolean isVaultEnabled() { return vault; } + + public static boolean isWorldEditEnabled() { + return worldEdit; + } } diff --git a/src/main/java/tech/sbdevelopment/v10lift/commands/V10LiftCommand.java b/src/main/java/tech/sbdevelopment/v10lift/commands/V10LiftCommand.java index 381b439..11e230c 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/commands/V10LiftCommand.java +++ b/src/main/java/tech/sbdevelopment/v10lift/commands/V10LiftCommand.java @@ -1,14 +1,7 @@ package tech.sbdevelopment.v10lift.commands; import com.cryptomorin.xseries.XMaterial; -import com.sk89q.worldedit.IncompleteRegionException; -import com.sk89q.worldedit.LocalSession; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldedit.math.BlockVector2; -import com.sk89q.worldedit.regions.CuboidRegion; -import com.sk89q.worldedit.regions.Polygonal2DRegion; -import com.sk89q.worldedit.regions.Region; +import com.ibm.icu.impl.Pair; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockState; @@ -29,6 +22,7 @@ import tech.sbdevelopment.v10lift.managers.ForbiddenBlockManager; import tech.sbdevelopment.v10lift.managers.VaultManager; import tech.sbdevelopment.v10lift.sbutils.LocationSerializer; import tech.sbdevelopment.v10lift.utils.ConfigUtil; +import tech.sbdevelopment.v10lift.utils.WorldEditUtil; import javax.annotation.Nonnull; import java.sql.SQLException; @@ -146,6 +140,11 @@ public class V10LiftCommand implements CommandExecutor { return true; } if (sender.hasPermission("v10lift.build") || sender.hasPermission("v10lift.admin")) { + if (!V10LiftPlugin.isWorldEditEnabled()) { + ConfigUtil.sendMessage(sender, "Build.WorldEditNotEnabled"); + return true; + } + return buildWorldEditCommand(sender); } else { ConfigUtil.sendMessage(sender, "General.NoPermission"); @@ -814,64 +813,15 @@ public class V10LiftCommand implements CommandExecutor { DataManager.addBuilderPlayer(p.getUniqueId()); } - LocalSession ls = WorldEdit.getInstance().getSessionManager().get(BukkitAdapter.adapt(p)); - Region region; - try { - region = ls.getSelection(BukkitAdapter.adapt(p.getWorld())); - } catch (IncompleteRegionException e) { - throw new RuntimeException(e); - } - if (region == null) { + Integer blocks = WorldEditUtil.getBlocksInSelection(p); + if (blocks == -1) { ConfigUtil.sendMessage(sender, "Build.NoSelection"); - return true; - } - - List blocks = new ArrayList<>(); - boolean success = true; - int failed = 0; - if (region instanceof Polygonal2DRegion) { - //Get all blocks in region - Polygonal2DRegion poly = (Polygonal2DRegion) region; - for (BlockVector2 bv : poly.getPoints()) { - for (int y = poly.getMinimumPoint().getBlockY(); y <= poly.getMaximumPoint().getBlockY(); y++) { - Block b = p.getWorld().getBlockAt(bv.getBlockX(), y, bv.getBlockZ()); - if (b.getType() == Material.AIR) continue; - - if (V10LiftAPI.getInstance().switchBlockAtLift(DataManager.getEditPlayer(p.getUniqueId()), b) == 0) { - blocks.add(b); - continue; - } - success = false; - failed++; - } - } - } else if (region instanceof CuboidRegion) { - //Get all blocks in region - CuboidRegion cuboid = (CuboidRegion) region; - for (int x = cuboid.getMinimumPoint().getBlockX(); x <= cuboid.getMaximumPoint().getBlockX(); x++) { - for (int y = cuboid.getMinimumPoint().getBlockY(); y <= cuboid.getMaximumPoint().getBlockY(); y++) { - for (int z = cuboid.getMinimumPoint().getBlockZ(); z <= cuboid.getMaximumPoint().getBlockZ(); z++) { - Block b = p.getWorld().getBlockAt(x, y, z); - if (b.getType() == Material.AIR) continue; - - if (V10LiftAPI.getInstance().switchBlockAtLift(DataManager.getEditPlayer(p.getUniqueId()), b) == 0) { - blocks.add(b); - continue; - } - success = false; - failed++; - } - } - } - } else { + } else if (blocks == -2) { ConfigUtil.sendMessage(sender, "Build.UnsupportedSelection"); - return true; - } - - if (success) { + } else if (blocks == 0) { ConfigUtil.sendMessage(sender, "Build.BlocksAdded"); } else { - ConfigUtil.sendMessage(sender, "Build.BlocksFailed", Collections.singletonMap("%Failed%", String.valueOf(failed))); + ConfigUtil.sendMessage(sender, "Build.BlocksFailed", Collections.singletonMap("%Failed%", String.valueOf(blocks))); } if (DataManager.containsBuilderPlayer(p.getUniqueId())) { diff --git a/src/main/java/tech/sbdevelopment/v10lift/utils/WorldEditUtil.java b/src/main/java/tech/sbdevelopment/v10lift/utils/WorldEditUtil.java new file mode 100644 index 0000000..8fe62f8 --- /dev/null +++ b/src/main/java/tech/sbdevelopment/v10lift/utils/WorldEditUtil.java @@ -0,0 +1,66 @@ +package tech.sbdevelopment.v10lift.utils; + +import com.sk89q.worldedit.IncompleteRegionException; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.math.BlockVector2; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.Polygonal2DRegion; +import com.sk89q.worldedit.regions.Region; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import tech.sbdevelopment.v10lift.api.V10LiftAPI; +import tech.sbdevelopment.v10lift.managers.DataManager; + +public class WorldEditUtil { + public static Integer getBlocksInSelection(Player p) { + LocalSession ls = WorldEdit.getInstance().getSessionManager().get(BukkitAdapter.adapt(p)); + Region region; + try { + region = ls.getSelection(BukkitAdapter.adapt(p.getWorld())); + } catch (IncompleteRegionException e) { + throw new RuntimeException(e); + } + if (region == null) { + return -1; + } + + int failed = 0; + if (region instanceof Polygonal2DRegion) { + //Get all blocks in region + Polygonal2DRegion poly = (Polygonal2DRegion) region; + for (BlockVector2 bv : poly.getPoints()) { + for (int y = poly.getMinimumPoint().getBlockY(); y <= poly.getMaximumPoint().getBlockY(); y++) { + Block b = p.getWorld().getBlockAt(bv.getBlockX(), y, bv.getBlockZ()); + if (b.getType() == Material.AIR) continue; + + if (V10LiftAPI.getInstance().switchBlockAtLift(DataManager.getEditPlayer(p.getUniqueId()), b) != 0) { + failed++; + } + } + } + + return failed; + } else if (region instanceof CuboidRegion) { + //Get all blocks in region + CuboidRegion cuboid = (CuboidRegion) region; + for (int x = cuboid.getMinimumPoint().getBlockX(); x <= cuboid.getMaximumPoint().getBlockX(); x++) { + for (int y = cuboid.getMinimumPoint().getBlockY(); y <= cuboid.getMaximumPoint().getBlockY(); y++) { + for (int z = cuboid.getMinimumPoint().getBlockZ(); z <= cuboid.getMaximumPoint().getBlockZ(); z++) { + Block b = p.getWorld().getBlockAt(x, y, z); + if (b.getType() == Material.AIR) continue; + + if (V10LiftAPI.getInstance().switchBlockAtLift(DataManager.getEditPlayer(p.getUniqueId()), b) != 0) { + failed++; + } + } + } + } + return failed; + } else { + return -2; + } + } +} diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 2962512..ad600fd 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -76,6 +76,7 @@ Build: UnsupportedSelection: '&cThe selection must be cuboid or polygonal!' BlocksAdded: '&aBlocks added to the elevator.' BlocksFailed: '&cNot all blocks could be added to the elevator. Failure amount: %Failed%' + WorldEditNotEnabled: '&cWorldEdit is not enabled on this server!' Rope: StillAdjusting: '&cYou are still adjusting a rope.' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index dab432b..1316ce0 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ main: tech.sbdevelopment.v10lift.V10LiftPlugin version: ${project.version} api-version: "1.13" author: SBDeveloper -softdepend: ["Vault"] +softdepend: ["Vault", "WorldEdit"] commands: v10lift: description: The V10Lift Command