use of com.sk89q.worldedit.LocalConfiguration 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.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.
the class SchematicCommands method move.
// FAWE start
@Command(name = "move", aliases = { "m" }, desc = "Move your loaded schematic")
@CommandPermissions({ "worldedit.schematic.move", "worldedit.schematic.move.other" })
public void move(Actor actor, LocalSession session, @Arg(desc = "Directory.") String directory) throws WorldEditException, IOException {
LocalConfiguration config = worldEdit.getConfiguration();
File working = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
File dir = Settings.settings().PATHS.PER_PLAYER_SCHEMATICS ? new File(working, actor.getUniqueId().toString()) : working;
File destDir = new File(dir, directory);
if (!MainUtil.isInSubDirectory(working, destDir)) {
actor.print(Caption.of("worldedit.schematic.directory-does-not-exist", TextComponent.of(String.valueOf(destDir))));
return;
}
if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && !MainUtil.isInSubDirectory(dir, destDir) && !actor.hasPermission("worldedit.schematic.move.other")) {
actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.move.other"));
return;
}
ClipboardHolder clipboard = session.getClipboard();
List<File> sources = getFiles(clipboard);
if (sources.isEmpty()) {
actor.print(Caption.of("fawe.worldedit.schematic.schematic.none"));
return;
}
if (!destDir.exists() && !destDir.mkdirs()) {
actor.print(Caption.of("worldedit.schematic.file-perm-fail", TextComponent.of(String.valueOf(destDir))));
return;
}
for (File source : sources) {
File destFile = new File(destDir, source.getName());
if (destFile.exists()) {
actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.exists", destFile));
continue;
}
if (Settings.settings().PATHS.PER_PLAYER_SCHEMATICS && (!MainUtil.isInSubDirectory(dir, destFile) || !MainUtil.isInSubDirectory(dir, source)) && !actor.hasPermission("worldedit.schematic.delete.other")) {
actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.failed", destFile, Caption.of("fawe.error.no-perm", ("worldedit.schematic.move.other"))));
continue;
}
try {
File cached = new File(source.getParentFile(), "." + source.getName() + ".cached");
Files.move(source.toPath(), destFile.toPath());
if (cached.exists()) {
Files.move(cached.toPath(), destFile.toPath());
}
actor.print(Caption.of("fawe.worldedit.schematic.schematic.move.success", source, destFile));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of com.sk89q.worldedit.LocalConfiguration 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.sk89q.worldedit.LocalConfiguration 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)));
}
}
use of com.sk89q.worldedit.LocalConfiguration in project FastAsyncWorldEdit by IntellectualSites.
the class GeneralCommands method timeout.
@Command(name = "/timeout", desc = "Modify evaluation timeout time.")
@CommandPermissions("worldedit.timeout")
public void timeout(Actor actor, LocalSession session, @Arg(desc = "The timeout time to set", def = "") Integer limit) {
LocalConfiguration config = worldEdit.getConfiguration();
boolean mayDisable = actor.hasPermission("worldedit.timeout.unrestricted");
limit = limit == null ? config.calculationTimeout : Math.max(-1, limit);
if (!mayDisable && config.maxCalculationTimeout > -1) {
if (limit > config.maxCalculationTimeout) {
actor.print(Caption.of("worldedit.timeout.too-high", TextComponent.of(config.maxCalculationTimeout)));
return;
}
}
session.setTimeout(limit);
Component component = TextComponent.empty().append(Caption.of("worldedit.timeout.set", TextComponent.of(limit)));
if (limit != config.calculationTimeout) {
component.append(Caption.of("worldedit.timeout.return-to-default"));
}
actor.print(component);
}
Aggregations