Java Code Examples for com.sk89q.worldedit.session.ClipboardHolder

The following examples show how to use com.sk89q.worldedit.session.ClipboardHolder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: IridiumSkyblock   Source File: WorldEdit7.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paste(File file, Location location, Island island) {
    try {
        ClipboardFormat format = ClipboardFormats.findByFile(file);
        ClipboardReader reader = format.getReader(new FileInputStream(file));
        Clipboard clipboard = reader.read();
        try (EditSession editSession = com.sk89q.worldedit.WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(location.getWorld()), -1)) {
            Operation operation = new ClipboardHolder(clipboard)
                    .createPaste(editSession)
                    .to(BlockVector3.at(location.getX(), location.getY(), location.getZ()))
                    .copyEntities(true)
                    .ignoreAirBlocks(true)
                    .build();
            Operations.complete(operation);
        }
    } catch (Exception e) {
        IridiumSkyblock.getInstance().getLogger().warning("Failed to paste schematic using worldedit");
        IridiumSkyblock.schematic.paste(file, location, island);
    }
}
 
Example 2
@Override
public boolean apply(Extent extent, Vector setPosition, Vector getPosition) throws WorldEditException {
    ClipboardHolder holder = clipboards.get(PseudoRandom.random.random(clipboards.size()));
    AffineTransform transform = new AffineTransform();
    if (randomRotate) {
        transform = transform.rotateY(PseudoRandom.random.random(4) * 90);
        holder.setTransform(new AffineTransform().rotateY(PseudoRandom.random.random(4) * 90));
    }
    if (randomFlip) {
        transform = transform.scale(new Vector(1, 0, 0).multiply(-2).add(1, 1, 1));
    }
    if (!transform.isIdentity()) {
        holder.setTransform(transform);
    }
    Clipboard clipboard = holder.getClipboard();
    Schematic schematic = new Schematic(clipboard);
    Transform newTransform = holder.getTransform();
    if (newTransform.isIdentity()) {
        schematic.paste(extent, setPosition, false);
    } else {
        schematic.paste(extent, worldData, setPosition, false, newTransform);
    }
    return true;
}
 
Example 3
Source Project: FastAsyncWorldedit   Source File: SchemGen.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean spawn(PseudoRandom random, int x, int z) throws WorldEditException {
    mutable.mutX(x);
    mutable.mutZ(z);
    int y = extent.getNearestSurfaceTerrainBlock(x, z, mutable.getBlockY(), 0, 255);
    if (y == -1) return false;
    mutable.mutY(y);
    if (!mask.test(mutable)) {
        return false;
    }
    mutable.mutY(y + 1);
    ClipboardHolder holder = clipboards.get(PseudoRandom.random.random(clipboards.size()));
    if (randomRotate) {
        holder.setTransform(new AffineTransform().rotateY(PseudoRandom.random.random(4) * 90));
    }
    Clipboard clipboard = holder.getClipboard();
    Schematic schematic = new Schematic(clipboard);
    Transform transform = holder.getTransform();
    if (transform.isIdentity()) {
        schematic.paste(extent, mutable, false);
    } else {
        schematic.paste(extent, worldData, mutable, false, transform);
    }
    mutable.mutY(y);
    return true;
}
 
Example 4
public void addClipboard(@Nonnull MultiClipboardHolder toAppend) {
    checkNotNull(toAppend);
    ClipboardHolder existing = getExistingClipboard();
    MultiClipboardHolder multi;
    if (existing instanceof MultiClipboardHolder) {
        multi = (MultiClipboardHolder) existing;
        for (ClipboardHolder holder : toAppend.getHolders()) {
            multi.add(holder);
        }
    } else  {
        multi = toAppend;
        if (existing != null) {
            multi.add(existing);
        }
    }
    setClipboard(multi);
}
 
Example 5
@Command(
        aliases = {"clipboard"},
        usage = "",
        desc = "Choose the clipboard brush (Recommended: `/br copypaste`)",
        help =
                "Chooses the clipboard brush.\n" +
                        "The -a flag makes it not paste air.\n" +
                        "Without the -p flag, the paste will appear centered at the target location. " +
                        "With the flag, then the paste will appear relative to where you had " +
                        "stood relative to the copied area when you copied it."
)
@CommandPermissions("worldedit.brush.clipboard")
public BrushSettings clipboardBrush(Player player, LocalSession session, @Switch('a') boolean ignoreAir, @Switch('p') boolean usingOrigin, CommandContext context) throws WorldEditException {
    ClipboardHolder holder = session.getClipboard();
    Clipboard clipboard = holder.getClipboard();

    Vector size = clipboard.getDimensions();

    WorldEdit.getInstance().checkMaxBrushRadius(size.getBlockX());
    WorldEdit.getInstance().checkMaxBrushRadius(size.getBlockY());
    WorldEdit.getInstance().checkMaxBrushRadius(size.getBlockZ());
    return set(session, context, new ClipboardBrush(holder, ignoreAir, usingOrigin));
}
 
Example 6
@Command(
        aliases = {"/rotate"},
        usage = "<y-axis> [<x-axis>] [<z-axis>]",
        desc = "Rotate the contents of the clipboard",
        help = "Non-destructively rotate the contents of the clipboard.\n" +
                "Angles are provided in degrees and a positive angle will result in a clockwise rotation. " +
                "Multiple rotations can be stacked. Interpolation is not performed so angles should be a multiple of 90 degrees.\n"
)
@CommandPermissions("worldedit.clipboard.rotate")
public void rotate(Player player, LocalSession session, Double yRotate, @Optional Double xRotate, @Optional Double zRotate) throws WorldEditException {
    ClipboardHolder holder = session.getClipboard();
    AffineTransform transform = new AffineTransform();
    transform = transform.rotateY(-(yRotate != null ? yRotate : 0));
    transform = transform.rotateX(-(xRotate != null ? xRotate : 0));
    transform = transform.rotateZ(-(zRotate != null ? zRotate : 0));
    holder.setTransform(transform.combine(holder.getTransform()));
    BBC.COMMAND_ROTATE.send(player);
    if (!FawePlayer.wrap(player).hasPermission("fawe.tips"))
        BBC.TIP_FLIP.or(BBC.TIP_DEFORM, BBC.TIP_TRANSFORM).send(player);
}
 
Example 7
@Command(
        aliases = {"/flip"},
        usage = "[<direction>]",
        desc = "Flip the contents of the clipboard",
        help =
                "Flips the contents of the clipboard across the point from which the copy was made.\n",
        min = 0,
        max = 1
)
@CommandPermissions("worldedit.clipboard.flip")
public void flip(Player player, LocalSession session,
                 @Optional(Direction.AIM) @Direction Vector direction) throws WorldEditException {
    ClipboardHolder holder = session.getClipboard();
    Clipboard clipboard = holder.getClipboard();
    AffineTransform transform = new AffineTransform();
    transform = transform.scale(direction.positive().multiply(-2).add(1, 1, 1));
    holder.setTransform(transform.combine(holder.getTransform()));
    BBC.COMMAND_FLIPPED.send(player);
}
 
Example 8
Source Project: FunnyGuilds   Source File: SchematicHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean pasteSchematic(File schematicFile, Location location, boolean withAir) {
    try {
        Vector pasteLocation = new Vector(location.getX(), location.getY(), location.getZ());
        World pasteWorld = new BukkitWorld(location.getWorld());
        WorldData pasteWorldData = pasteWorld.getWorldData();

        Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(schematicFile)).read(pasteWorldData);
        ClipboardHolder clipboardHolder = new ClipboardHolder(clipboard, pasteWorldData);

        EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(pasteWorld, -1);

        Operation operation = clipboardHolder
                .createPaste(editSession, pasteWorldData)
                .to(pasteLocation)
                .ignoreAirBlocks(!withAir)
                .build();

        Operations.completeLegacy(operation);
        return true;
    }
    catch (IOException | MaxChangedBlocksException e) {
        FunnyGuilds.getInstance().getPluginLogger().error("Could not paste schematic: " + schematicFile.getAbsolutePath(), e);
        return false;
    }
}
 
Example 9
@Override
public void regenerate(Floor floor, EditSession session, RegenerationCause cause) {
	Clipboard clipboard = floor.getClipboard();
	
	Region region = clipboard.getRegion();
	World world = region.getWorld();
	WorldData data = world.getWorldData();
	
	ClipboardHolder holder = new ClipboardHolder(clipboard, data);
	
	Operation pasteOperation = holder.createPaste(session, data)
			.to(region.getMinimumPoint())
			.ignoreAirBlocks(true)
			.build();
	
	try {
		Operations.complete(pasteOperation);
	} catch (WorldEditException e) {
		throw new RuntimeException(e);
	}
}
 
Example 10
@Override
@Deprecated
public void generate(EditSession session) {
	Region region = floorClipboard.getRegion();
	World world = region.getWorld();
	WorldData data = world.getWorldData();
	
	ClipboardHolder holder = new ClipboardHolder(floorClipboard, data);
	
	Operation pasteOperation = holder.createPaste(session, data)
			.to(region.getMinimumPoint())
			.ignoreAirBlocks(true)
			.build();
	
	try {
		Operations.complete(pasteOperation);
	} catch (WorldEditException e) {
		throw new RuntimeException(e);
	}
}
 
Example 11
Source Project: UhcCore   Source File: SchematicHandler13.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static ArrayList<Integer> pasteSchematic(Location loc, String path) throws Exception{
	Bukkit.getLogger().info("[UhcCore] Pasting "+path);
	File schematic = new File(path);
       World world = BukkitAdapter.adapt(loc.getWorld());

       ClipboardFormat format = ClipboardFormats.findByFile(schematic);
       ClipboardReader reader = format.getReader(new FileInputStream(schematic));
       Clipboard clipboard = reader.read();

       EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);

       enableWatchdog(editSession);

       Operation operation = new ClipboardHolder(clipboard)
               .createPaste(editSession)
               .to(BlockVector3.at(loc.getX(), loc.getY(), loc.getZ()))
               .ignoreAirBlocks(false)
               .build();

       Operations.complete(operation);
       editSession.flushSession();

	ArrayList<Integer> dimensions = new ArrayList<>();
	dimensions.add(clipboard.getDimensions().getY());
	dimensions.add(clipboard.getDimensions().getX());
	dimensions.add(clipboard.getDimensions().getZ());
	
	Bukkit.getLogger().info("[UhcCore] Successfully pasted '"+path+"' at "+loc.getBlockX()+" "+loc.getBlockY()+" "+loc.getBlockZ());
	return dimensions;
}
 
Example 12
Source Project: WorldEditSelectionVisualizer   Source File: WorldEditHelper.java    License: MIT License 5 votes vote down vote up
@Nullable
private ClipboardHolder getClipboardHolder(@Nullable LocalSession session) {
    if (session != null) {
        try {
            return session.getClipboard();
        } catch (EmptyClipboardException e) {
            // ignore, clipboard is empty
        }
    }
    return null;
}
 
Example 13
public RandomFullClipboardPattern(Extent extent, WorldData worldData, List<ClipboardHolder> clipboards, boolean randomRotate, boolean randomFlip) {
    checkNotNull(clipboards);
    this.clipboards = clipboards;
    this.extent = extent;
    this.randomRotate = randomRotate;
    this.worldData = worldData;
}
 
Example 14
/**
 * Loads any history items from disk:
 * - Should already be called if history on disk is enabled
 */
public void loadClipboardFromDisk() {
    File file = MainUtil.getFile(Fawe.imp().getDirectory(), Settings.IMP.PATHS.CLIPBOARD + File.separator + getUUID() + ".bd");
    try {
        if (file.exists() && file.length() > 5) {
            DiskOptimizedClipboard doc = new DiskOptimizedClipboard(file);
            Player player = toWorldEditPlayer();
            LocalSession session = getSession();
            try {
                if (session.getClipboard() != null) {
                    return;
                }
            } catch (EmptyClipboardException e) {
            }
            if (player != null && session != null) {
                WorldData worldData = player.getWorld().getWorldData();
                Clipboard clip = doc.toClipboard();
                ClipboardHolder holder = new ClipboardHolder(clip, worldData);
                getSession().setClipboard(holder);
            }
        }
    } catch (Exception ignore) {
        Fawe.debug("====== INVALID CLIPBOARD ======");
        MainUtil.handleError(ignore, false);
        Fawe.debug("===============---=============");
        Fawe.debug("This shouldn't result in any failure");
        Fawe.debug("File: " + file.getName() + " (len:" + file.length() + ")");
        Fawe.debug("===============---=============");
    }
}
 
Example 15
@Override
public boolean increment(Player player, int amount) {
    index = MathMan.wrap(index + amount, 0, clipboards.size() - 1);
    ClipboardHolder clipboard = clipboards.get(index);
    session.setClipboard(clipboard);
    return true;
}
 
Example 16
@Override
public URI getURI(Clipboard clipboard) {
    for (ClipboardHolder holder : getHolders()) {
        if (holder instanceof URIClipboardHolder) {
            URIClipboardHolder uriHolder = (URIClipboardHolder) holder;
            URI uri = uriHolder.getURI(clipboard);
            if (uri != null) return uri;
        }
    }
    return null;
}
 
Example 17
@Override
public boolean contains(Clipboard clipboard) {
    for (ClipboardHolder holder : holders) {
        if (holder.contains(clipboard)) return true;
    }
    return false;
}
 
Example 18
@Deprecated
public void add(ClipboardHolder holder) {
    checkNotNull(holder);
    if (holder instanceof URIClipboardHolder) {
        holders.add((URIClipboardHolder) holder);
    } else {
        URI uri = URI.create(UUID.randomUUID().toString());
        if (!contains(uri)) {
            holders.add(new URIClipboardHolder(uri, holder.getClipboard(), holder.getWorldData()));
        }
    }
    cached = null;
}
 
Example 19
@Override
public List<Clipboard> getClipboards() {
    ArrayList<Clipboard> all = new ArrayList<>();
    for (ClipboardHolder holder : holders) {
        all.addAll(holder.getClipboards());
    }
    return all;
}
 
Example 20
@Override
public List<ClipboardHolder> getHolders() {
    ArrayList<ClipboardHolder> holders = new ArrayList<>();
    for (ClipboardHolder holder : this.holders) {
        holders.addAll(holder.getHolders());
    }
    return holders;
}
 
Example 21
@Override
public Set<URI> getURIs() {
    Set<URI> set = new HashSet<>();
    for (ClipboardHolder holder : getHolders()) {
        if (holder instanceof URIClipboardHolder) {
            URI uri = ((URIClipboardHolder) holder).getUri();
            if (!uri.toString().isEmpty()) set.add(uri);
        }
    }
    return set;
}
 
Example 22
@Override
public void close() {
    cached = null;
    for (ClipboardHolder holder : holders) {
        holder.close();
    }
}
 
Example 23
Source Project: FastAsyncWorldedit   Source File: SchemVis.java    License: GNU General Public License v3.0 5 votes vote down vote up
private boolean isSelected(File file) {
    ClipboardHolder clipboard = player.getSession().getExistingClipboard();
    if (clipboard != null) {
        if (clipboard instanceof URIClipboardHolder) {
            return ((URIClipboardHolder) clipboard).contains(file.toURI());
        }
    }
    return false;
}
 
Example 24
Source Project: FastAsyncWorldedit   Source File: SchemGen.java    License: GNU General Public License v3.0 5 votes vote down vote up
public SchemGen(Mask mask, Extent extent, WorldData worldData, List<ClipboardHolder> clipboards, boolean randomRotate) {
    this.mask = mask;
    this.extent = extent;
    this.worldData = worldData;
    this.clipboards = clipboards;
    this.randomRotate = randomRotate;
}
 
Example 25
/**
 * Gets the clipboard.
 *
 * @return clipboard
 * @throws EmptyClipboardException thrown if no clipboard is set
 */
public ClipboardHolder getClipboard() throws EmptyClipboardException {
    if (clipboard == null) {
        throw new EmptyClipboardException();
    }
    return clipboard;
}
 
Example 26
/**
 * Sets the clipboard.
 * <p>
 * <p>Pass {@code null} to clear the clipboard.</p>
 *
 * @param clipboard the clipboard, or null if the clipboard is to be cleared
 */
public void setClipboard(@Nullable ClipboardHolder clipboard) {
    if (this.clipboard == clipboard) return;

    if (this.clipboard != null) {
        if (clipboard == null || !clipboard.contains(this.clipboard.getClipboard())) {
            this.clipboard.close();
        }
    }
    this.clipboard = clipboard;
}
 
Example 27
@Command(
        aliases = {"populateschematic", "populateschem", "popschem", "pschem", "ps"},
        usage = "<mask> <file|folder|url> [radius=30] [points=5]",
        desc = "Scatter a schematic on a surface",
        help =
                "Chooses the scatter schematic brush.\n" +
                        "The -r flag will apply random rotation",
        flags = "r",
        min = 2,
        max = 4
)
@CommandPermissions("worldedit.brush.populateschematic")
public BrushSettings scatterSchemBrush(Player player, EditSession editSession, LocalSession session, Mask mask, String clipboard, @Optional("30") Expression radius, @Optional("50") double density, @Switch('r') boolean rotate, CommandContext context) throws WorldEditException {
    checkMaxBrushRadius(radius);


    try {
        MultiClipboardHolder clipboards = ClipboardFormat.SCHEMATIC.loadAllFromInput(player, player.getWorld().getWorldData(), clipboard, true);
        if (clipboards == null) {
            BBC.SCHEMATIC_NOT_FOUND.send(player, clipboard);
            return null;
        }
        List<ClipboardHolder> holders = clipboards.getHolders();
        if (holders == null) {
            BBC.SCHEMATIC_NOT_FOUND.send(player, clipboard);
            return null;
        }

        return set(session, context,
                new PopulateSchem(mask, holders, (int) density, rotate))
                .setSize(radius);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 28
@Command(
        aliases = {"unload"},
        usage = "[file]",
        desc = "Remove a clipboard from your multi-clipboard",
        min = 1,
        max = 1
)
@CommandPermissions({"worldedit.clipboard.clear", "worldedit.schematic.clear"})
public void unload(Player player, LocalSession session, String fileName) throws WorldEditException {
    URI uri;
    if (fileName.startsWith("file:/") || fileName.startsWith("http://") || fileName.startsWith("https://")) {
        uri = URI.create(fileName);
    } else {
        final LocalConfiguration config = this.worldEdit.getConfiguration();
        File working = this.worldEdit.getWorkingDirectoryFile(config.saveDir);
        File root = Settings.IMP.PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
        uri = new File(root, fileName).toURI();
    }

    boolean removed = false;
    ClipboardHolder clipboard = session.getClipboard();
    if (clipboard instanceof URIClipboardHolder) {
        URIClipboardHolder identifiable = (URIClipboardHolder) clipboard;
        if (identifiable.contains(uri)) {
            if (identifiable instanceof MultiClipboardHolder) {
                MultiClipboardHolder multi = (MultiClipboardHolder) identifiable;
                multi.remove(uri);
                if (multi.getHolders().isEmpty()) session.setClipboard(null);
            } else {
                session.setClipboard(null);
            }
            BBC.CLIPBOARD_CLEARED.send(player);
            return;
        }
    }
    BBC.CLIPBOARD_URI_NOT_FOUND.send(player, fileName);
}
 
Example 29
private List<File> getFiles(ClipboardHolder clipboard) {
    List<File> files = new ArrayList<>();
    Collection<URI> uris = Collections.emptyList();
    if (clipboard instanceof URIClipboardHolder) {
        uris = ((URIClipboardHolder) clipboard).getURIs();
    }
    for (URI uri : uris) {
        File file = new File(uri);
        if (file.exists()) files.add(file);
    }
    return files;
}
 
Example 30
@Command(
        aliases = {"#fullcopy"},
        desc = "Places your full clipboard at each block",
        usage = "[schem|folder|url=#copy] [rotate=false] [flip=false]",
        min = 0,
        max = 2
)
public Pattern fullcopy(Player player, Extent extent, LocalSession session, @Optional("#copy") String location, @Optional("false") boolean rotate, @Optional("false") boolean flip) throws EmptyClipboardException, InputParseException, IOException {
    List<ClipboardHolder> clipboards;
    switch (location.toLowerCase()) {
        case "#copy":
        case "#clipboard":
            ClipboardHolder clipboard = session.getExistingClipboard();
            if (clipboard == null) {
                throw new InputParseException("To use #fullcopy, please first copy something to your clipboard");
            }
            if (!rotate && !flip) {
                return new FullClipboardPattern(extent, clipboard.getClipboard());
            }
            clipboards = Collections.singletonList(clipboard);
            break;
        default:
            MultiClipboardHolder multi = ClipboardFormat.SCHEMATIC.loadAllFromInput(player, player.getWorld().getWorldData(), location, true);
            clipboards = multi != null ? multi.getHolders() : null;
            break;
    }
    if (clipboards == null) {
        throw new InputParseException("#fullcopy:<source>");
    }
    return new RandomFullClipboardPattern(extent, player.getWorld().getWorldData(), clipboards, rotate, flip);
}