Skip to content
Permalink
Browse files

Improve UX, saving is now automatic and unknown command messages show

  • Loading branch information...
me4502 committed Aug 3, 2019
1 parent d05076e commit c22c41a16711f90736fba82cc7613ee945c56912
@@ -0,0 +1,44 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.cli;

public interface CLIWorld {

/**
* Saves this world back to file.
*
* @param force Force a save
*/
void save(boolean force);

/**
* Gets whether the world is dirty.
*
* @return If it's dirty
*/
boolean isDirty();

/**
* Set the world's dirty status
*
* @param dirty if dirty
*/
void setDirty(boolean dirty);
}
@@ -28,13 +28,10 @@
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.block.BlockState;
@@ -51,7 +48,6 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
@@ -229,48 +225,31 @@ String getInternalVersion() {
return version;
}

public void saveAllWorlds(boolean force) {
platform.getWorlds().stream()
.filter(world -> world instanceof CLIWorld)
.forEach(world -> ((CLIWorld) world).save(force));
}

public void run(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equalsIgnoreCase("stop")) {
commandSender.print("Stopping!");
break;
}
if (line.startsWith("save")) {
String[] bits = line.split(" ");
if (bits.length == 0) {
commandSender.print("Usage: save <filename>");
continue;
try (Scanner scanner = new Scanner(inputStream)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("stop")) {
commandSender.print("Stopping!");
break;
}
World world = platform.getWorlds().get(0);
if (world instanceof ClipboardWorld) {
File file;
if (bits.length >= 2) {
file = new File(bits[1]);
} else {
file = commandSender.openFileSaveDialog(new String[]{"schem"});
}
if (file == null) {
commandSender.printError("Please choose a file.");
continue;
}
try(ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
writer.write((Clipboard) world);
commandSender.print("Saved to file");
} catch (IOException e) {
e.printStackTrace();
}
CommandEvent event = new CommandEvent(commandSender, line);
WorldEdit.getInstance().getEventBus().post(event);
if (!event.isCancelled()) {
commandSender.printError("Unknown command!");
} else {
saveAllWorlds(false);
}

continue;
}
WorldEdit.getInstance().getEventBus().post(new CommandEvent(
commandSender,
line
));
} finally {
saveAllWorlds(false);
}
scanner.close();
}

public static void main(String[] args) {
@@ -310,6 +289,7 @@ public static void main(String[] args) {
app.onStarted();
try (ClipboardReader clipboardReader = format.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ))) {
ClipboardWorld world = new ClipboardWorld(
file,
clipboardReader.read(),
file.getName()
);
@@ -23,9 +23,12 @@
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.cli.CLIWorld;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
@@ -38,17 +41,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.util.List;
import java.util.Locale;

import javax.annotation.Nullable;

public class ClipboardWorld extends AbstractWorld implements Clipboard {
public class ClipboardWorld extends AbstractWorld implements Clipboard, CLIWorld {

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

public ClipboardWorld(Clipboard clipboard, String name) {
private boolean dirty = false;

public ClipboardWorld(File file, Clipboard clipboard, String name) {
this.file = file;
this.clipboard = clipboard;
this.name = name;
}
@@ -66,6 +76,7 @@ public String getId() {
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight)
throws WorldEditException {
dirty = true;
return clipboard.setBlock(position, block);
}

@@ -121,6 +132,7 @@ public BlockVector3 getSpawnPosition() {
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
dirty = true;
return clipboard.createEntity(location, entity);
}

@@ -141,6 +153,7 @@ public BiomeType getBiome(BlockVector2 position) {

@Override
public boolean setBiome(BlockVector2 position, BiomeType biome) {
dirty = true;
return clipboard.setBiome(position, biome);
}

@@ -162,6 +175,7 @@ public BlockVector3 getOrigin() {
@Override
public void setOrigin(BlockVector3 origin) {
clipboard.setOrigin(origin);
dirty = true;
}

@Override
@@ -178,4 +192,26 @@ public BlockVector3 getMaximumPoint() {
public BlockVector3 getMinimumPoint() {
return clipboard.getMinimumPoint();
}

@Override
public void save(boolean force) {
if (dirty || force) {
try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
writer.write(this);
dirty = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public boolean isDirty() {
return this.dirty;
}

@Override
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
}

0 comments on commit c22c41a

Please sign in to comment.
You can’t perform that action at this time.