Search in sources :

Example 1 with Vector3

use of com.sk89q.worldedit.math.Vector3 in project RedProtect by FabioZumbi12.

the class WEHook method pasteWithWE.

public static Region pasteWithWE(Player p, File file) {
    World world = p.getWorld();
    Location loc = p.getLocation();
    final Region[] r = { null };
    if (!p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().isSolid()) {
        RedProtect.get().getLanguageManager().sendMessage(p, "playerlistener.region.needground");
        return null;
    }
    Clipboard clipboard = null;
    ClipboardFormat format = ClipboardFormats.findByFile(file);
    if (format == null) {
        RedProtect.get().getLanguageManager().sendMessage(p, "playerlistener.region.copyfail");
        return null;
    }
    try (ClipboardReader reader = format.getReader(new FileInputStream(file))) {
        clipboard = reader.read();
    } catch (IOException e) {
        CoreUtil.printJarVersion();
        e.printStackTrace();
    }
    if (clipboard != null) {
        try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(world), -1)) {
            BlockVector3 to = BlockVector3.at(loc.getX(), loc.getY(), loc.getZ());
            // Rotate to player looking direction
            float yaw = loc.getYaw();
            if (yaw < 0) {
                yaw += 360;
            }
            int rotate = 0;
            if (yaw >= 315 || yaw < 45) {
                rotate = 180;
            } else if (yaw < 135) {
                rotate = 270;
            } else if (yaw < 225) {
                rotate = 0;
            } else if (yaw < 315) {
                rotate = 90;
            }
            ClipboardHolder holder = new ClipboardHolder(clipboard);
            AffineTransform transform = new AffineTransform();
            transform = transform.rotateY(-rotate);
            holder.setTransform(holder.getTransform().combine(transform));
            Operation operation = holder.createPaste(editSession).to(to).ignoreAirBlocks(false).build();
            Operations.complete(operation);
            // Select the region min and max
            BlockVector3 clipboardOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
            Vector3 realTo = to.toVector3().add(holder.getTransform().apply(clipboardOffset.toVector3()));
            Vector3 locMax = realTo.add(holder.getTransform().apply(clipboard.getRegion().getMaximumPoint().subtract(clipboard.getRegion().getMinimumPoint()).toVector3()));
            Location min = new Location(world, realTo.getX(), realTo.getY(), realTo.getZ());
            Location max = new Location(world, locMax.getX(), locMax.getY(), locMax.getZ());
            if (RedProtect.get().getConfigManager().configRoot().region_settings.autoexpandvert_ondefine) {
                min.setY(p.getWorld().getMinHeight());
                max.setY(p.getWorld().getMaxHeight());
                if (RedProtect.get().getConfigManager().configRoot().region_settings.claim.miny != -1)
                    min.setY(RedProtect.get().getConfigManager().configRoot().region_settings.claim.miny);
                if (RedProtect.get().getConfigManager().configRoot().region_settings.claim.maxy != -1)
                    max.setY(RedProtect.get().getConfigManager().configRoot().region_settings.claim.maxy);
            }
            RedProtect.get().getLanguageManager().sendMessage(p, "regionbuilder.creating");
            // Run claim async
            Bukkit.getScheduler().runTaskAsynchronously(RedProtect.get(), () -> {
                RegionBuilder rb2 = new DefineRegionBuilder(p, min, max, "", new PlayerRegion(p.getUniqueId().toString(), p.getName()), new HashSet<>(), false);
                if (rb2.ready()) {
                    r[0] = rb2.build();
                }
            });
        } catch (WorldEditException e) {
            CoreUtil.printJarVersion();
            e.printStackTrace();
            r[0] = null;
        }
    }
    return r[0];
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) IOException(java.io.IOException) Operation(com.sk89q.worldedit.function.operation.Operation) World(org.bukkit.World) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) FileInputStream(java.io.FileInputStream) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) AffineTransform(com.sk89q.worldedit.math.transform.AffineTransform) DefineRegionBuilder(br.net.fabiozumbi12.RedProtect.Bukkit.actions.DefineRegionBuilder) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) DefineRegionBuilder(br.net.fabiozumbi12.RedProtect.Bukkit.actions.DefineRegionBuilder) RegionBuilder(br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionBuilder) Location(org.bukkit.Location)

Aggregations

Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)1 DefineRegionBuilder (br.net.fabiozumbi12.RedProtect.Bukkit.actions.DefineRegionBuilder)1 RegionBuilder (br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionBuilder)1 PlayerRegion (br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)1 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)1 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)1 Operation (com.sk89q.worldedit.function.operation.Operation)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Vector3 (com.sk89q.worldedit.math.Vector3)1 AffineTransform (com.sk89q.worldedit.math.transform.AffineTransform)1 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1