|
| 1 | +Clipboard Examples |
| 2 | +================== |
| 3 | + |
| 4 | +.. note:: |
| 5 | + This documentation covers the API for using clipboards. |
| 6 | + See :doc:`/usage/clipboard` for in-game usage & explanations of what clipboards are. |
| 7 | + |
| 8 | +Concepts used in these examples: :doc:`../concepts/regions`, :doc:`../concepts/edit-sessions`, |
| 9 | +:doc:`../concepts/extents` |
| 10 | + |
| 11 | +Copying |
| 12 | +------- |
| 13 | +Copying is the most common way to create a clipboard. To do it, you'll need a ``Region``, a target ``Clipboard``, |
| 14 | +and an ``EditSession``. In this example we use a ``CuboidRegion`` and the standard ``BlockArrayClipboard``. |
| 15 | +Then, all you need to do is pass the parameters to the ``ForwardExtentCopy``, apply configuration (such as calling |
| 16 | +``setCopyingEntities(true))`` to copy entities), and call ``Operations.complete``. |
| 17 | + |
| 18 | +.. code-block:: java |
| 19 | +
|
| 20 | + CuboidRegion region = new CuboidRegion(world, min, max); |
| 21 | + BlockArrayClipboard clipboard = new BlockArrayClipboard(region); |
| 22 | +
|
| 23 | + try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1)) { |
| 24 | + ForwardExtentCopy forwardExtentCopy = new ForwardExtentCopy( |
| 25 | + editSession, region, clipboard, region.getMinimumPoint() |
| 26 | + ); |
| 27 | + // configure here |
| 28 | + Operations.complete(forwardExtentCopy); |
| 29 | + } |
| 30 | +
|
| 31 | +You may want to :ref:`save <Saving>` the clipboard after this. |
| 32 | + |
| 33 | +Pasting |
| 34 | +------- |
| 35 | +Pasting is the only way to move blocks from a ``Clipboard`` to another ``Extent``, typically a ``World``. |
| 36 | +To paste, you'll need a ``World``, an ``EditSession`` and a ``Clipboard``. Create a ``ClipboardHolder`` |
| 37 | +with your clipboard, then get a ``PasteBuilder`` by calling ``createPaste`` with the ``EditSession``. |
| 38 | +Call ``.to`` to set the position at which you want to paste (this will be offset by the clipboard offset, |
| 39 | +see the clipboard page above for more information). Add any other configuration you want (masks, paste entities, |
| 40 | +paste biomes, etc.), and then call ``build()`` to get an operation. Complete the operation, and all the blocks |
| 41 | +will be pasted. |
| 42 | + |
| 43 | +Full example: |
| 44 | + |
| 45 | +.. code-block:: java |
| 46 | +
|
| 47 | + try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1)) { |
| 48 | + Operation operation = new ClipboardHolder(clipboard) |
| 49 | + .createPaste(editSession) |
| 50 | + .to(BlockVector3.at(x, y, z)) |
| 51 | + // configure here |
| 52 | + .build(); |
| 53 | + Operations.complete(operation); |
| 54 | + } |
| 55 | +
|
| 56 | +You may want to :ref:`load <Loading>` a clipboard before this. |
| 57 | + |
| 58 | +Schematic Examples |
| 59 | +================== |
| 60 | +This section deals with schematics, which are a related but distinct concept. Schematics |
| 61 | +specifically refer to a saved clipboard, not a clipboard in-memory. |
| 62 | + |
| 63 | +.. _saving: |
| 64 | + |
| 65 | +Saving |
| 66 | +------ |
| 67 | +A ``Clipboard`` can be saved to disk very easily. All you need is a ``ClipboardFormat``, a ``Clipboard``, and an |
| 68 | +``OutputStream``. Then you can call ``getWriter`` on the format and ``write`` on the writer with |
| 69 | +your ``Clipboard``. Here's an example for saving a clipboard to file. |
| 70 | + |
| 71 | +.. code-block:: java |
| 72 | +
|
| 73 | + File file = /* figure out where to save the clipboard */; |
| 74 | +
|
| 75 | + try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) { |
| 76 | + writer.write(clipboard); |
| 77 | + } |
| 78 | +
|
| 79 | +.. _loading: |
| 80 | + |
| 81 | +Loading |
| 82 | +------- |
| 83 | +Loading a ``Clipboard`` is nearly as simple. You can either force a specific ``ClipboardFormat``, or have WorldEdit |
| 84 | +discover the format of the schematic you want to load. The example does the latter. Then you can call ``getReader`` |
| 85 | +on the format and ``read`` on the reader to get a ``Clipboard`` instance. |
| 86 | + |
| 87 | +.. code-block:: java |
| 88 | +
|
| 89 | + Clipboard clipboard; |
| 90 | +
|
| 91 | + ClipboardFormat format = ClipboardFormats.findByFile(file); |
| 92 | + try (ClipboardReader reader = format.getReader(new FileInputStream(file))) { |
| 93 | + clipboard = reader.read(); |
| 94 | + } |
| 95 | + /* use the clipboard here */ |
0 commit comments