Made WorldEdit optional

This commit is contained in:
Stijn Bannink 2023-08-19 20:06:24 +02:00
parent ee9aecce8e
commit b8e068480c
5 changed files with 91 additions and 63 deletions

View file

@ -30,6 +30,7 @@ public class V10LiftPlugin extends JavaPlugin {
@Getter @Getter
private static YamlFile items; private static YamlFile items;
private static boolean vault = false; private static boolean vault = false;
private static boolean worldEdit = false;
@Override @Override
public void onEnable() { public void onEnable() {
@ -72,6 +73,12 @@ public class V10LiftPlugin extends JavaPlugin {
vault = true; 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 //Load the command
getCommand("v10lift").setExecutor(new V10LiftCommand()); getCommand("v10lift").setExecutor(new V10LiftCommand());
getCommand("v10lift").setTabCompleter(new V10LiftTabCompleter()); getCommand("v10lift").setTabCompleter(new V10LiftTabCompleter());
@ -148,4 +155,8 @@ public class V10LiftPlugin extends JavaPlugin {
public static boolean isVaultEnabled() { public static boolean isVaultEnabled() {
return vault; return vault;
} }
public static boolean isWorldEditEnabled() {
return worldEdit;
}
} }

View file

@ -1,14 +1,7 @@
package tech.sbdevelopment.v10lift.commands; package tech.sbdevelopment.v10lift.commands;
import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.XMaterial;
import com.sk89q.worldedit.IncompleteRegionException; import com.ibm.icu.impl.Pair;
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.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; 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.managers.VaultManager;
import tech.sbdevelopment.v10lift.sbutils.LocationSerializer; import tech.sbdevelopment.v10lift.sbutils.LocationSerializer;
import tech.sbdevelopment.v10lift.utils.ConfigUtil; import tech.sbdevelopment.v10lift.utils.ConfigUtil;
import tech.sbdevelopment.v10lift.utils.WorldEditUtil;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.sql.SQLException; import java.sql.SQLException;
@ -146,6 +140,11 @@ public class V10LiftCommand implements CommandExecutor {
return true; return true;
} }
if (sender.hasPermission("v10lift.build") || sender.hasPermission("v10lift.admin")) { if (sender.hasPermission("v10lift.build") || sender.hasPermission("v10lift.admin")) {
if (!V10LiftPlugin.isWorldEditEnabled()) {
ConfigUtil.sendMessage(sender, "Build.WorldEditNotEnabled");
return true;
}
return buildWorldEditCommand(sender); return buildWorldEditCommand(sender);
} else { } else {
ConfigUtil.sendMessage(sender, "General.NoPermission"); ConfigUtil.sendMessage(sender, "General.NoPermission");
@ -814,64 +813,15 @@ public class V10LiftCommand implements CommandExecutor {
DataManager.addBuilderPlayer(p.getUniqueId()); DataManager.addBuilderPlayer(p.getUniqueId());
} }
LocalSession ls = WorldEdit.getInstance().getSessionManager().get(BukkitAdapter.adapt(p)); Integer blocks = WorldEditUtil.getBlocksInSelection(p);
Region region; if (blocks == -1) {
try {
region = ls.getSelection(BukkitAdapter.adapt(p.getWorld()));
} catch (IncompleteRegionException e) {
throw new RuntimeException(e);
}
if (region == null) {
ConfigUtil.sendMessage(sender, "Build.NoSelection"); ConfigUtil.sendMessage(sender, "Build.NoSelection");
return true; } else if (blocks == -2) {
}
List<Block> 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 {
ConfigUtil.sendMessage(sender, "Build.UnsupportedSelection"); ConfigUtil.sendMessage(sender, "Build.UnsupportedSelection");
return true; } else if (blocks == 0) {
}
if (success) {
ConfigUtil.sendMessage(sender, "Build.BlocksAdded"); ConfigUtil.sendMessage(sender, "Build.BlocksAdded");
} else { } 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())) { if (DataManager.containsBuilderPlayer(p.getUniqueId())) {

View file

@ -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;
}
}
}

View file

@ -76,6 +76,7 @@ Build:
UnsupportedSelection: '&cThe selection must be cuboid or polygonal!' UnsupportedSelection: '&cThe selection must be cuboid or polygonal!'
BlocksAdded: '&aBlocks added to the elevator.' BlocksAdded: '&aBlocks added to the elevator.'
BlocksFailed: '&cNot all blocks could be added to the elevator. Failure amount: %Failed%' BlocksFailed: '&cNot all blocks could be added to the elevator. Failure amount: %Failed%'
WorldEditNotEnabled: '&cWorldEdit is not enabled on this server!'
Rope: Rope:
StillAdjusting: '&cYou are still adjusting a rope.' StillAdjusting: '&cYou are still adjusting a rope.'

View file

@ -3,7 +3,7 @@ main: tech.sbdevelopment.v10lift.V10LiftPlugin
version: ${project.version} version: ${project.version}
api-version: "1.13" api-version: "1.13"
author: SBDeveloper author: SBDeveloper
softdepend: ["Vault"] softdepend: ["Vault", "WorldEdit"]
commands: commands:
v10lift: v10lift:
description: The V10Lift Command description: The V10Lift Command