Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OIO is 🔥🍞 #1492

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import com.sk89q.worldedit.util.report.Unreported;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* YAMLConfiguration but with setting for no op permissions and plugin root data folder.
Expand Down Expand Up @@ -56,14 +58,15 @@ private void migrateLegacyFolders() {
}

private void migrate(String file, String name) {
File fromDir = new File(".", file);
File toDir = new File(getWorkingDirectory(), file);
if (fromDir.exists() & !toDir.exists()) {
if (fromDir.renameTo(toDir)) {
plugin.getLogger().info("Migrated " + name + " folder '" + file
Path fromDir = Paths.get(".", file);
Path toDir = getWorkingDirectoryPath().resolve(file);
if (!Files.exists(toDir) && Files.exists(fromDir)) {
try {
Files.move(fromDir, toDir);
WorldEditPlugin.log.info("Migrated " + name + " folder '" + file
+ "' from server root to plugin data folder.");
} else {
plugin.getLogger().warning("Error while migrating " + name + " folder!");
} catch (IOException e) {
WorldEditPlugin.log.warn("Error while migrating " + name + " folder!", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
}
}

private static final Logger log = LoggerFactory.getLogger(WorldEditPlugin.class);
protected static final Logger log = LoggerFactory.getLogger(WorldEditPlugin.class);
public static final String CUI_PLUGIN_CHANNEL = "worldedit:cui";
private static WorldEditPlugin INSTANCE;
private static final int BSTATS_PLUGIN_ID = 3328;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.FileDialogUtil;
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldedit.util.collection.SetWithDefault;
import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.serializer.plain.PlainComponentSerializer;
import com.sk89q.worldedit.util.io.file.FileDialogUtil;
import com.sk89q.worldedit.util.io.file.FileSelectionAbortedException;
import com.sk89q.worldedit.util.io.file.FileType;
import com.sk89q.worldedit.util.io.file.PathRequestType;
import org.slf4j.Logger;

import java.io.File;
import java.nio.file.Path;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.swing.SwingUtilities;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down Expand Up @@ -131,13 +137,18 @@ public boolean isPlayer() {
}

@Override
public File openFileOpenDialog(String[] extensions) {
return FileDialogUtil.showOpenDialog(extensions);
}

@Override
public File openFileSaveDialog(String[] extensions) {
return FileDialogUtil.showSaveDialog(extensions);
public CompletableFuture<Path> requestPath(PathRequestType type,
SetWithDefault<FileType> fileTypes) {
return CompletableFuture.supplyAsync(
() -> {
try {
return FileDialogUtil.requestPath(type, fileTypes);
} catch (FileSelectionAbortedException e) {
throw new RuntimeException(e);
}
},
SwingUtilities::invokeLater
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package com.sk89q.worldedit.cli;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.cli.data.FileRegistries;
import com.sk89q.worldedit.cli.schematic.ClipboardWorld;
import com.sk89q.worldedit.event.platform.CommandEvent;
Expand All @@ -32,7 +36,11 @@
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.collection.SetWithDefault;
import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.io.file.FileType;
import com.sk89q.worldedit.util.io.file.PathRequestType;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.block.BlockState;
Expand All @@ -58,9 +66,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.CompletionException;

/**
* The CLI implementation of WorldEdit.
Expand Down Expand Up @@ -269,34 +278,38 @@ public static void main(String[] args) {
CommandLine cmd = new DefaultParser().parse(options, args);

String fileArg = cmd.getOptionValue('f');
File file;
Path file;
if (fileArg == null) {
String[] formats = Arrays.copyOf(ClipboardFormats.getFileExtensionArray(), ClipboardFormats.getFileExtensionArray().length + 1);
formats[formats.length - 1] = "dat";
file = app.commandSender.openFileOpenDialog(formats);
SetWithDefault<FileType> fileTypes = SetWithDefault.of(
null,
Iterables.concat(
ClipboardFormats.getFileTypes(),
ImmutableSet.of(FileType.of("MC data files", "dat"))
)
);
try {
file = app.commandSender.requestPath(PathRequestType.LOAD, fileTypes).join();
} catch (CompletionException e) {
Throwable cause = e.getCause();
Throwables.propagateIfPossible(cause, WorldEditException.class);
throw e;
}
} else {
file = new File(fileArg);
}
if (file == null) {
throw new IllegalArgumentException("A file must be provided!");
file = Paths.get(fileArg);
}
if (file.getName().endsWith("level.dat")) {
if (file.getFileName().toString().endsWith("level.dat")) {
throw new IllegalArgumentException("level.dat file support is unfinished.");
} else {
ClipboardFormat format = ClipboardFormats.findByFile(file);
ClipboardFormat format = ClipboardFormats.findByPath(file);
if (format != null) {
ClipboardReader dataVersionReader = format
.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ));
int dataVersion = dataVersionReader.getDataVersion()
.orElseThrow(() -> new IllegalArgumentException("Failed to obtain data version from schematic."));
dataVersionReader.close();
int dataVersion = getDataVersion(file, format);
app.platform.setDataVersion(dataVersion);
app.onStarted();
try (ClipboardReader clipboardReader = format.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ))) {
try (ClipboardReader clipboardReader = format.getReader(Files.newInputStream(file))) {
ClipboardWorld world = new ClipboardWorld(
file,
clipboardReader.read(),
file.getName()
file,
clipboardReader.read(),
file.getFileName().toString()
);
app.platform.addWorld(world);
WorldEdit.getInstance().getSessionManager().get(app.commandSender).setWorldOverride(world);
Expand All @@ -319,6 +332,11 @@ public static void main(String[] args) {
}

app.run(inputStream);
} catch (WorldEditException e) {
System.err.println(WorldEditText.reduceToText(
e.getRichMessage(), Locale.getDefault()
));
exitCode = 1;
} catch (Exception e) {
e.printStackTrace();
exitCode = 1;
Expand All @@ -333,4 +351,14 @@ public static void main(String[] args) {

System.exit(exitCode);
}

private static int getDataVersion(Path file, ClipboardFormat format) throws IOException {
int dataVersion;
try (ClipboardReader dataVersionReader = format.getReader(
Files.newInputStream(file, StandardOpenOption.READ))) {
dataVersion = dataVersionReader.getDataVersion()
.orElseThrow(() -> new IllegalArgumentException("Failed to obtain data version from schematic."));
}
return dataVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.annotation.Nullable;

public class ClipboardWorld extends AbstractWorld implements Clipboard, CLIWorld {

private final File file;
private final Path path;
private final Clipboard clipboard;
private final String name;

private boolean dirty = false;

public ClipboardWorld(File file, Clipboard clipboard, String name) {
this.file = file;
public ClipboardWorld(Path path, Clipboard clipboard, String name) {
this.path = path;
this.clipboard = clipboard;
this.name = name;
}
Expand Down Expand Up @@ -199,7 +199,8 @@ public BlockVector3 getMinimumPoint() {
@Override
public void save(boolean force) {
if (dirty || force) {
try (ClipboardWriter writer = ClipboardFormats.findByFile(file).getWriter(new FileOutputStream(file))) {
try (ClipboardWriter writer = ClipboardFormats.findByPath(path)
.getWriter(Files.newOutputStream(path))) {
writer.write(this);
dirty = false;
} catch (IOException e) {
Expand Down
Loading