2014-04-04 00:52:53 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2014-04-04 00:52:53 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2014-04-04 00:52:53 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-04 00:52:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-12-23 08:56:26 +00:00
|
|
|
package com.sk89q.worldedit.extension.factory.parser.pattern;
|
2014-04-04 00:52:53 +00:00
|
|
|
|
|
|
|
import com.sk89q.worldedit.EmptyClipboardException;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.extension.input.InputParseException;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.extension.input.ParserContext;
|
2014-07-02 09:56:21 +00:00
|
|
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.function.pattern.ClipboardPattern;
|
|
|
|
import com.sk89q.worldedit.function.pattern.Pattern;
|
2019-02-11 22:40:30 +00:00
|
|
|
import com.sk89q.worldedit.internal.registry.InputParser;
|
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2014-07-02 09:56:21 +00:00
|
|
|
import com.sk89q.worldedit.session.ClipboardHolder;
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2019-05-26 06:20:02 +00:00
|
|
|
import java.util.Locale;
|
2019-05-18 06:47:08 +00:00
|
|
|
import java.util.stream.Stream;
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2019-02-11 22:40:30 +00:00
|
|
|
public class ClipboardPatternParser extends InputParser<Pattern> {
|
2018-12-23 08:56:26 +00:00
|
|
|
|
|
|
|
public ClipboardPatternParser(WorldEdit worldEdit) {
|
2014-04-04 00:52:53 +00:00
|
|
|
super(worldEdit);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-19 03:49:37 +00:00
|
|
|
public Stream<String> getSuggestions(String input) {
|
2019-05-26 06:20:02 +00:00
|
|
|
if (input.isEmpty()) {
|
2019-07-03 17:01:59 +00:00
|
|
|
return Stream.of("#clipboard");
|
2019-05-26 06:20:02 +00:00
|
|
|
}
|
|
|
|
String[] offsetParts = input.split("@", 2);
|
|
|
|
String firstLower = offsetParts[0].toLowerCase(Locale.ROOT);
|
|
|
|
final boolean isClip = "#clipboard".startsWith(firstLower);
|
|
|
|
final boolean isCopy = "#copy".startsWith(firstLower);
|
|
|
|
if (isClip || isCopy) {
|
|
|
|
if (offsetParts.length == 2) {
|
|
|
|
String coords = offsetParts[1];
|
|
|
|
if (coords.isEmpty()) {
|
|
|
|
return Stream.of(input + "[x,y,z]");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isClip) {
|
|
|
|
return Stream.of("#clipboard", "#clipboard@[x,y,z]");
|
|
|
|
}
|
|
|
|
return Stream.of("#copy", "#copy@[x,y,z]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Stream.empty();
|
2018-12-23 08:56:26 +00:00
|
|
|
}
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2018-12-23 08:56:26 +00:00
|
|
|
@Override
|
2019-02-11 22:40:30 +00:00
|
|
|
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
|
2019-02-12 13:46:31 +00:00
|
|
|
String[] offsetParts = input.split("@", 2);
|
|
|
|
if (!offsetParts[0].equalsIgnoreCase("#clipboard") && !offsetParts[0].equalsIgnoreCase("#copy")) {
|
2019-02-11 22:40:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-12-23 08:56:26 +00:00
|
|
|
LocalSession session = context.requireSession();
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2019-02-11 22:40:30 +00:00
|
|
|
BlockVector3 offset = BlockVector3.ZERO;
|
2019-02-12 13:46:31 +00:00
|
|
|
if (offsetParts.length == 2) {
|
2019-03-28 03:36:59 +00:00
|
|
|
String coords = offsetParts[1];
|
|
|
|
if (coords.length() < 7 // min length of `[x,y,z]`
|
|
|
|
|| coords.charAt(0) != '[' || coords.charAt(coords.length() - 1) != ']') {
|
|
|
|
throw new InputParseException("Offset specified with @ but no offset given. Use '#copy@[x,y,z]'.");
|
|
|
|
}
|
|
|
|
String[] offsetSplit = coords.substring(1, coords.length() - 1).split(",");
|
2019-02-12 13:46:31 +00:00
|
|
|
if (offsetSplit.length != 3) {
|
2019-02-11 22:40:30 +00:00
|
|
|
throw new InputParseException("Clipboard offset needs x,y,z coordinates.");
|
|
|
|
}
|
|
|
|
offset = BlockVector3.at(
|
2019-03-28 03:36:59 +00:00
|
|
|
Integer.valueOf(offsetSplit[0]),
|
|
|
|
Integer.valueOf(offsetSplit[1]),
|
|
|
|
Integer.valueOf(offsetSplit[2])
|
|
|
|
);
|
2019-02-11 22:40:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 08:56:26 +00:00
|
|
|
if (session != null) {
|
|
|
|
try {
|
|
|
|
ClipboardHolder holder = session.getClipboard();
|
|
|
|
Clipboard clipboard = holder.getClipboard();
|
2019-02-11 22:40:30 +00:00
|
|
|
return new ClipboardPattern(clipboard, offset);
|
2018-12-23 08:56:26 +00:00
|
|
|
} catch (EmptyClipboardException e) {
|
|
|
|
throw new InputParseException("To use #clipboard, please first copy something to your clipboard");
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-23 08:56:26 +00:00
|
|
|
throw new InputParseException("No session is available, so no clipboard is available");
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|