use of com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.
the class ClipboardFormat method hold.
// FAWE start
/**
* Sets the actor's clipboard.
*
* @param actor the actor
* @param uri the URI of the schematic to hold
* @param inputStream the input stream
* @throws IOException thrown on I/O error
*/
default URIClipboardHolder hold(Actor actor, URI uri, InputStream inputStream) throws IOException {
checkNotNull(actor);
checkNotNull(uri);
checkNotNull(inputStream);
final ClipboardReader reader = getReader(inputStream);
final Clipboard clipboard;
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
session.setClipboard(null);
clipboard = reader.read(actor.getUniqueId());
URIClipboardHolder holder = new URIClipboardHolder(uri, clipboard);
session.setClipboard(holder);
return holder;
}
use of com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.
the class ClipboardFormats method loadAllFromInput.
public static MultiClipboardHolder loadAllFromInput(Actor player, String input, ClipboardFormat format, boolean message) throws IOException {
checkNotNull(player);
checkNotNull(input);
WorldEdit worldEdit = WorldEdit.getInstance();
LocalConfiguration config = worldEdit.getConfiguration();
if (input.startsWith("url:")) {
if (!player.hasPermission("worldedit.schematic.load.web")) {
if (message) {
player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.web"));
}
return null;
}
URL base = new URL(Settings.settings().WEB.URL);
input = new URL(base, "uploads/" + input.substring(4) + "." + format.getPrimaryFileExtension()).toString();
}
if (input.startsWith("http")) {
return null;
}
if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").matcher(input).find() && !player.hasPermission("worldedit.schematic.load.other")) {
player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other"));
return null;
}
File working = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, player.getUniqueId().toString()) : working;
File f;
if (input.startsWith("#")) {
String[] extensions;
if (format != null) {
extensions = format.getFileExtensions().toArray(new String[0]);
} else {
extensions = ClipboardFormats.getFileExtensionArray();
}
f = player.openFileOpenDialog(extensions);
if (f == null || !f.exists()) {
if (message) {
player.print(Caption.of("worldedit.schematic.load.does-not-exist", TextComponent.of(input)));
}
return null;
}
} else {
if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").matcher(input).find() && !player.hasPermission("worldedit.schematic.load.other")) {
if (message) {
player.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other"));
}
return null;
}
if (format == null && input.matches(".*\\.[\\w].*")) {
String extension = input.substring(input.lastIndexOf('.') + 1);
format = findByExtension(extension);
}
f = MainUtil.resolve(dir, input, format, true);
}
if (f == null || !f.exists()) {
if (!input.contains("../")) {
f = MainUtil.resolve(dir, input, null, true);
}
}
if (f == null || !f.exists() || !MainUtil.isInSubDirectory(working, f)) {
if (message) {
player.printError("Schematic " + input + " does not exist! (" + ((f != null) && f.exists()) + "|" + f + "|" + (f != null && !MainUtil.isInSubDirectory(working, f)) + ")");
}
return null;
}
if (format == null && f.isFile()) {
format = findByFile(f);
if (format == null) {
player.print(Caption.of("fawe.worldedit.clipboard.clipboard.invalid.format", f.getName()));
return null;
}
}
if (!f.exists()) {
if (message) {
player.print(Caption.of("fawe.error.schematic.not.found", input));
}
return null;
}
if (!f.isDirectory()) {
ByteSource source = Files.asByteSource(f);
URI uri = f.toURI();
return new MultiClipboardHolder(uri, new LazyClipboardHolder(f.toURI(), source, format, null));
}
URIClipboardHolder[] clipboards = loadAllFromDirectory(f);
if (clipboards.length < 1) {
if (message) {
player.print(Caption.of("fawe.error.schematic.not.found", input));
}
return null;
}
return new MultiClipboardHolder(f.toURI(), clipboards);
}
use of com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.
the class SchematicCommands method unload.
@Command(name = "unload", desc = "Remove a clipboard from your multi-clipboard")
@CommandPermissions({ "worldedit.clipboard.clear", "worldedit.schematic.clear" })
public void unload(Actor actor, LocalSession session, @Arg(desc = "File name, requires extension.") 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.getWorkingDirectoryPath(config.saveDir).toFile();
File root = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
uri = new File(root, fileName).toURI();
}
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);
}
actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.cleared"));
return;
}
}
actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.uri.not.found", fileName));
}
use of com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.
the class SchematicCommands method getFiles.
private List<File> getFiles(ClipboardHolder clipboard) {
Collection<URI> uris = Collections.emptyList();
if (clipboard instanceof URIClipboardHolder) {
uris = ((URIClipboardHolder) clipboard).getURIs();
}
List<File> files = new ArrayList<>();
for (URI uri : uris) {
File file = new File(uri);
if (file.exists()) {
files.add(file);
}
}
return files;
}
use of com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder in project FastAsyncWorldEdit by IntellectualSites.
the class ClipboardCommands method download.
// FAWE start
@Command(name = "download", aliases = { "/download" }, desc = "Downloads your clipboard through the configured web interface")
@Deprecated
@CommandPermissions({ "worldedit.clipboard.download" })
public void download(final Actor actor, final LocalSession session, @Arg(name = "format", desc = "String", def = "fast") final String formatName) throws WorldEditException {
final ClipboardFormat format = ClipboardFormats.findByAlias(formatName);
if (format == null) {
actor.print(Caption.of("fawe.worldedit.clipboard.clipboard.invalid.format", formatName));
return;
}
actor.print(Caption.of("fawe.web.generating.link", formatName));
ClipboardHolder holder = session.getClipboard();
URL url;
if (holder instanceof MultiClipboardHolder) {
MultiClipboardHolder multi = (MultiClipboardHolder) holder;
Set<File> files = new HashSet<>();
Set<URI> invalid = new HashSet<>();
for (ClipboardHolder cur : multi.getHolders()) {
if (cur instanceof URIClipboardHolder) {
URIClipboardHolder uriHolder = (URIClipboardHolder) cur;
URI uri = uriHolder.getUri();
File file = new File(uri.getPath());
if (file.exists() && file.isFile()) {
files.add(file.getAbsoluteFile());
} else if (!uri.getPath().isEmpty()) {
invalid.add(uri);
}
}
}
final LocalConfiguration config = WorldEdit.getInstance().getConfiguration();
final File working = WorldEdit.getInstance().getWorkingDirectoryFile(config.saveDir).getAbsoluteFile();
url = MainUtil.upload(null, null, "zip", new RunnableVal<>() {
@Override
public void run(OutputStream out) {
try (ZipOutputStream zos = new ZipOutputStream(out)) {
for (File file : files) {
String fileName = file.getName();
if (MainUtil.isInSubDirectory(working, file)) {
fileName = working.toURI().relativize(file.toURI()).getPath();
}
ZipEntry ze = new ZipEntry(fileName);
zos.putNextEntry(ze);
Files.copy(file.toPath(), zos);
zos.closeEntry();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} else {
Clipboard clipboard = holder.getClipboard();
final Transform transform = holder.getTransform();
final Clipboard target;
// If we have a transform, bake it into the copy
if (!transform.isIdentity()) {
final FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
target = new BlockArrayClipboard(result.getTransformedRegion(), actor.getUniqueId());
target.setOrigin(clipboard.getOrigin());
Operations.completeLegacy(result.copyTo(target));
} else {
target = clipboard;
}
if (format == BuiltInClipboardFormat.PNG) {
try {
FastByteArrayOutputStream baos = new FastByteArrayOutputStream(Short.MAX_VALUE);
ClipboardWriter writer = format.getWriter(baos);
writer.write(target);
baos.flush();
url = ImgurUtility.uploadImage(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
url = null;
}
} else {
if (Settings.settings().WEB.URL.isEmpty()) {
actor.print(Caption.of("fawe.error.setting.disable", "web.url"));
return;
}
url = FaweAPI.upload(target, format);
}
}
if (url == null) {
actor.print(Caption.of("fawe.web.generating.link.failed"));
} else {
String urlText = url.toString();
actor.print(Caption.of("fawe.web.download.link", urlText).clickEvent(ClickEvent.openUrl(urlText)));
}
}
Aggregations