Skip to content

Commit

Permalink
More stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizjany committed Mar 8, 2019
1 parent b85751d commit f7a0c22
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 16 deletions.
10 changes: 10 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java
Expand Up @@ -57,6 +57,7 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -369,6 +370,15 @@ public ClipboardHolder getClipboard() throws EmptyClipboardException {
return clipboard;
}

/**
* Gets the clipboard as an optional.
*
* @return an Optional containing the clipboard holder if it exists, or empty
*/
public Optional<ClipboardHolder> getClipboardOptional() {
return Optional.of(clipboard);
}

/**
* Sets the clipboard.
*
Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.MultiClipboard;
import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
Expand All @@ -51,6 +52,8 @@
import com.sk89q.worldedit.util.command.binding.Switch;
import com.sk89q.worldedit.util.command.parametric.Optional;

import java.util.List;

/**
* Clipboard commands.
*/
Expand All @@ -70,53 +73,63 @@ public ClipboardCommands(WorldEdit worldEdit) {

@Command(
aliases = { "/copy" },
flags = "em",
flags = "ema",
desc = "Copy the selection to the clipboard",
help = "Copy the selection to the clipboard\n" +
"Flags:\n" +
" -e will also copy entities\n" +
" -m sets a source mask so that excluded blocks become air\n" +
" -m <mask> sets a source mask so that excluded blocks become air\n" +
" -a <name> will add the clipboard to a multi-clipboard\n" +
"WARNING: Pasting entities cannot yet be undone!",
min = 0,
max = 0
)
@CommandPermissions("worldedit.clipboard.copy")
public void copy(Player player, LocalSession session, EditSession editSession,
@Selection Region region, @Switch('e') boolean copyEntities,
@Switch('m') Mask mask) throws WorldEditException {
@Switch('m') Mask mask, @Switch('a') String addMulti) throws WorldEditException {

BlockArrayClipboard clipboard = new BlockArrayClipboard(region);

ClipboardHolder holder = createHolderOrAddMulti(player, session, addMulti, clipboard);
if (holder == null) return;

clipboard.setOrigin(session.getPlacementPosition(player));
ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());
copy.setCopyingEntities(copyEntities);
if (mask != null) {
copy.setSourceMask(mask);
}
Operations.completeLegacy(copy);
session.setClipboard(new ClipboardHolder(clipboard));
session.setClipboard(holder);

player.print(region.getArea() + " block(s) were copied.");
}

@Command(
aliases = { "/cut" },
flags = "em",
flags = "ema",
usage = "[leave-id]",
desc = "Cut the selection to the clipboard",
help = "Copy the selection to the clipboard\n" +
"Flags:\n" +
" -e will also cut entities\n" +
" -m sets a source mask so that excluded blocks become air\n" +
" -m <mask> sets a source mask so that excluded blocks become air\n" +
" -a <name> will add the clipboard to a multi-clipboard\n" +
"WARNING: Cutting and pasting entities cannot yet be undone!",
max = 1
)
@CommandPermissions("worldedit.clipboard.cut")
@Logging(REGION)
public void cut(Player player, LocalSession session, EditSession editSession,
@Selection Region region, @Optional("air") Pattern leavePattern, @Switch('e') boolean copyEntities,
@Switch('m') Mask mask) throws WorldEditException {
@Switch('m') Mask mask, @Switch('a') String addMulti) throws WorldEditException {

BlockArrayClipboard clipboard = new BlockArrayClipboard(region);

ClipboardHolder holder = createHolderOrAddMulti(player, session, addMulti, clipboard);
if (holder == null) return;

clipboard.setOrigin(session.getPlacementPosition(player));
ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());
copy.setSourceFunction(new BlockReplace(editSession, leavePattern));
Expand All @@ -126,11 +139,56 @@ public void cut(Player player, LocalSession session, EditSession editSession,
copy.setSourceMask(mask);
}
Operations.completeLegacy(copy);
session.setClipboard(new ClipboardHolder(clipboard));

session.setClipboard(holder);

player.print(region.getArea() + " block(s) were cut.");
}

static ClipboardHolder createHolderOrAddMulti(Player player, LocalSession session, String multiName, Clipboard clipboard) {
if (multiName == null || multiName.isEmpty()) {
return new ClipboardHolder(clipboard);
}
java.util.Optional<ClipboardHolder> holder = session.getClipboardOptional();
if (!holder.isPresent()) {
MultiClipboard multi = new MultiClipboard();
multi.addClipboard(multiName, clipboard);
return new ClipboardHolder(multi);
}
Clipboard existing = holder.get().getClipboard();
if (existing instanceof MultiClipboard) {
MultiClipboard existingMulti = (MultiClipboard) existing;
if (clipboard instanceof MultiClipboard) {
MultiClipboard multi = ((MultiClipboard) clipboard);
for (String name : multi.getNames()) {
Clipboard clip = multi.getClipboard(name);
addToMulti(player, name, clip, existingMulti, true);
}
} else {
if (!addToMulti(player, multiName, clipboard, existingMulti, false)) return null;
}
return holder.get();
} else {
MultiClipboard multi = new MultiClipboard();
multi.addClipboard(multiName, clipboard);
addToMulti(player, "existing", existing, multi, true);
return new ClipboardHolder(multi);
}
}

private static boolean addToMulti(Player player, String multiName, Clipboard clipboard, MultiClipboard existingMulti, boolean tryRename) {
int i = 0;
String baseName = multiName;
while (!existingMulti.addClipboard(multiName, clipboard)) {
if (!tryRename || i > 10) {
player.printError("Clipboard with name '" + baseName + "' already exists in multi-clipboard.");
return false;
}
multiName = baseName + "#" + i++;
}
return true;
}

@Command(
aliases = { "/paste" },
usage = "",
Expand Down Expand Up @@ -220,6 +278,40 @@ public void flip(Player player, LocalSession session, EditSession editSession,
player.print("The clipboard copy has been flipped.");
}

@Command(
aliases = { "/selclip", "selectclipboard", "selclip" },
usage = "[name]",
desc = "Select a clipboard variant",
help = "If you have a multi-clipboard, selects one of the clipboard variants from it as your active clipboard.\n" +
"If you don't specify one, list the available clipboard variants in your current multi-clipboard.",
max = 1
)
@CommandPermissions("worldedit.clipboard.select")
public void selectClipboard(Player player, LocalSession session, @Optional String name) throws WorldEditException {
ClipboardHolder holder = session.getClipboard();
Clipboard clip = holder.getClipboard();
if (!(clip instanceof MultiClipboard)) {
player.printError("You don't currently have a multi-clipboard.");
return;
}
MultiClipboard multi = (MultiClipboard) clip;
List<String> names = multi.getNames();
if (name == null || name.isEmpty()) {
if (names.isEmpty()) {
player.printError("Multi-clipboard is empty.");
return;
}
player.print("Selected: '" + multi.getCurrentName() + "'. Available: " + String.join(", ", names));
} else {
if (names.contains(name)) {
multi.setCurrent(name);
player.print("Selected clipboard '" + name + "'.");
} else {
player.printError("That clipboard doesn't exist. Available: " + String.join(", ", names));
}
}
}

@Command(
aliases = { "clearclipboard" },
usage = "",
Expand Down
Expand Up @@ -36,6 +36,7 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.MultiClipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
Expand Down Expand Up @@ -86,12 +87,16 @@ public SchematicCommands(WorldEdit worldEdit) {

@Command(
aliases = { "load" },
flags = "a",
usage = "[<format>] <filename>",
desc = "Load a schematic into your clipboard",
min = 1, max = 2
help = "Load a schematic with the given filename and format into your clipboard.\n" +
" -a <name> will add the schematic to a multi-clipboard with the given name.",
min = 1, max = 3
)
@CommandPermissions({ "worldedit.clipboard.load", "worldedit.schematic.load" })
public void load(Player player, LocalSession session, @Optional("sponge") String formatName, String filename) throws FilenameException {
public void load(Player player, LocalSession session, @Optional("sponge") String formatName, String filename,
@Switch('a') String multiName) throws FilenameException {
LocalConfiguration config = worldEdit.getConfiguration();

File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
Expand All @@ -117,7 +122,7 @@ public void load(Player player, LocalSession session, @Optional("sponge") String
ClipboardReader reader = closer.register(format.getReader(bis));

Clipboard clipboard = reader.read();
session.setClipboard(new ClipboardHolder(clipboard));
session.setClipboard(ClipboardCommands.createHolderOrAddMulti(player, session, multiName, clipboard));

log.info(player.getName() + " loaded " + f.getCanonicalPath());
player.print(filename + " loaded. Paste it with //paste");
Expand Down Expand Up @@ -149,6 +154,13 @@ public void save(Player player, LocalSession session, @Optional("sponge") String

ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard();
if (clipboard instanceof MultiClipboard) {
MultiClipboard multi = ((MultiClipboard) clipboard);
clipboard = multi.getCurrentClipboard();
if (multi.getNames().size() > 1) {
player.printDebug("Note: you have multiple clipboards. Only the currently selected one will be saved.");
}
}
Transform transform = holder.getTransform();
Clipboard target;

Expand Down

0 comments on commit f7a0c22

Please sign in to comment.