From 1fd4d7c27e80eaab2c268a16d9dea145fb4357b0 Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Sun, 22 Mar 2020 15:46:02 -0400 Subject: [PATCH] Update draw.js --- contrib/craftscripts/draw.js | 8 +- .../com/sk89q/worldedit/LocalSession.java | 31 +- .../worldedit/command/SnapshotCommands.java | 282 +++++++++++------- 3 files changed, 206 insertions(+), 115 deletions(-) diff --git a/contrib/craftscripts/draw.js b/contrib/craftscripts/draw.js index bc78843b9..fb7e4f050 100644 --- a/contrib/craftscripts/draw.js +++ b/contrib/craftscripts/draw.js @@ -133,12 +133,12 @@ context.checkArgs(1, 3, " "); var f = context.getSafeOpenFile("drawings", argv[1], "png", ["png", "jpg", "jpeg", "bmp"]); var sess = context.remember(); -var upright = argv[2] === "v"; +var upright = argv[2] == "v"; var colors = clothColors; -if(argv[3] === "opt") { +if(argv[3] == "opt") { colors = clothColorsOpt; player.print("Using optimized palette"); -} else if(argv[3] === "optHD") { +} else if(argv[3] == "optHD") { colors = clothColorsOptHD; player.print("Using optimized HD palette"); } @@ -165,4 +165,4 @@ if (!f.exists()) { } } } -} \ No newline at end of file +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index fed1b0416..c4227c9be 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -139,8 +139,8 @@ public class LocalSession implements TextureHolder { private transient int maxBlocksChanged = -1; private transient int maxTimeoutTime; private transient boolean useInventory; - private transient Snapshot snapshot; - private transient boolean hasCUISupport = false; + private transient com.sk89q.worldedit.world.snapshot.Snapshot snapshot; + private transient com.sk89q.worldedit.world.snapshot.experimental.Snapshot snapshotExperimental; private transient boolean hasCUISupport = false; private transient int cuiVersion = -1; private transient boolean fastMode = false; private transient Mask mask; @@ -959,17 +959,36 @@ public class LocalSession implements TextureHolder { * @return the legacy snapshot */ @Nullable - public Snapshot getSnapshot() { + public com.sk89q.worldedit.world.snapshot.Snapshot getSnapshot() { return snapshot; } + /** + * Select a legacy snapshot. + * + * @param snapshot a legacy snapshot + */ + public void setSnapshot(@Nullable com.sk89q.worldedit.world.snapshot.Snapshot snapshot) { + this.snapshot = snapshot; + } + + /** + * Get the snapshot that has been selected. + * + * @return the snapshot + */ + public @Nullable + com.sk89q.worldedit.world.snapshot.experimental.Snapshot getSnapshotExperimental() { + return snapshotExperimental; + } + /** * Select a snapshot. * - * @param snapshot a snapshot + * @param snapshotExperimental a snapshot */ - public void setSnapshot(@Nullable Snapshot snapshot) { - this.snapshot = snapshot; + public void setSnapshotExperimental(@Nullable com.sk89q.worldedit.world.snapshot.experimental.Snapshot snapshotExperimental) { + this.snapshotExperimental = snapshotExperimental; } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotCommands.java index 9bda92014..22d758287 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotCommands.java @@ -21,7 +21,6 @@ package com.sk89q.worldedit.command; -import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; @@ -32,22 +31,30 @@ import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.util.formatting.component.PaginationBox; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; import com.sk89q.worldedit.world.World; -import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException; -import com.sk89q.worldedit.world.snapshot.Snapshot; -import com.sk89q.worldedit.world.storage.MissingWorldException; -import java.io.File; -import java.io.IOException; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.List; +import com.sk89q.worldedit.world.snapshot.experimental.Snapshot; +import com.sk89q.worldedit.world.snapshot.experimental.fs.FileSystemSnapshotDatabase; import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; import org.enginehub.piston.annotation.param.Arg; import org.enginehub.piston.annotation.param.ArgFlag; +import org.enginehub.piston.exception.StopExecutionException; + +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; /** * Snapshot commands. @@ -58,49 +65,77 @@ public class SnapshotCommands { private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); private final WorldEdit we; + private final LegacySnapshotCommands legacy; public SnapshotCommands(WorldEdit we) { this.we = we; + this.legacy = new LegacySnapshotCommands(we); } + static void checkSnapshotsConfigured(LocalConfiguration localConfiguration) { + if (!localConfiguration.snapshotsConfigured) { + throw new StopExecutionException(TranslatableComponent.of( + "worldedit.restore.not-configured" + )); + } + } + + static URI resolveSnapshotName(LocalConfiguration config, String name) { + if (!name.contains("://")) { + if (config.snapshotDatabase instanceof FileSystemSnapshotDatabase) { + String newName = name; + while (newName.startsWith("/")) { + newName = newName.substring(1); + } + return FileSystemSnapshotDatabase.createUri(newName); + } + } + return URI.create(name); + } + + @Command( name = "list", desc = "List snapshots" ) @CommandPermissions("worldedit.snapshots.list") - public void list(Actor actor, World world, + void list(Actor actor, World world, @ArgFlag(name = 'p', desc = "Page of results to return", def = "1") - int page) throws WorldEditException { - + int page) throws WorldEditException, IOException { LocalConfiguration config = we.getConfiguration(); + checkSnapshotsConfigured(config); - if (config.snapshotRepo == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-configured")); + if (config.snapshotRepo != null) { + legacy.list(actor, world, page); return; } - try { - List snapshots = config.snapshotRepo.getSnapshots(true, world.getName()); + List snapshots; + try (Stream snapshotStream = + config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) { + snapshots = snapshotStream + .collect(toList()); + } - if (!snapshots.isEmpty()) { - actor.print(new SnapshotListBox(world.getName(), snapshots).create(page)); - } else { - actor.printError(TranslatableComponent.of("worldedit.restore.none-found-console")); + if (!snapshots.isEmpty()) { + actor.print(new SnapshotListBox(world.getName(), snapshots).create(page)); + } else { + actor.printError(TranslatableComponent.of( + "worldedit.restore.none-for-specific-world", + TextComponent.of(world.getName()) + )); - // Okay, let's toss some debugging information! - File dir = config.snapshotRepo.getDirectory(); - - try { - WorldEdit.logger.info("WorldEdit found no snapshots: looked in: " - + dir.getCanonicalPath()); - } catch (IOException e) { - WorldEdit.logger.info("WorldEdit found no snapshots: looked in " - + "(NON-RESOLVABLE PATH - does it exist?): " - + dir.getPath()); + if (config.snapshotDatabase instanceof FileSystemSnapshotDatabase) { + FileSystemSnapshotDatabase db = (FileSystemSnapshotDatabase) config.snapshotDatabase; + Path root = db.getRoot(); + if (Files.isDirectory(root)) { + WorldEdit.logger.info("No snapshots were found for world '" + + world.getName() + "'; looked in " + root.toRealPath()); + } else { + WorldEdit.logger.info("No snapshots were found for world '" + + world.getName() + "'; " + root.toRealPath() + " is not a directory"); } } - } catch (MissingWorldException ex) { - actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world")); } } @@ -109,36 +144,47 @@ public class SnapshotCommands { desc = "Choose a snapshot to use" ) @CommandPermissions("worldedit.snapshots.restore") - public void use(Actor actor, World world, LocalSession session, + void use(Actor actor, World world, LocalSession session, @Arg(desc = "Snapshot to use") - String name) throws WorldEditException { - + String name) throws IOException { LocalConfiguration config = we.getConfiguration(); + checkSnapshotsConfigured(config); - if (config.snapshotRepo == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-configured")); + if (config.snapshotRepo != null) { + legacy.use(actor, world, session, name); return; } // Want the latest snapshot? if (name.equalsIgnoreCase("latest")) { - try { - Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(world.getName()); + Snapshot snapshot; + try (Stream snapshotStream = + config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) { + snapshot = snapshotStream + .findFirst().orElse(null); + } - if (snapshot != null) { - session.setSnapshot(null); - actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use.newest")); - } else { - actor.printError(TranslatableComponent.of("worldedit.restore.none-found")); + if (snapshot != null) { + if (session.getSnapshotExperimental() != null) { + session.getSnapshotExperimental().close(); } - } catch (MissingWorldException ex) { + session.setSnapshot(null); + actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use.newest")); + } else { actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world")); } } else { - try { - session.setSnapshot(config.snapshotRepo.getSnapshot(name)); - actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use", TextComponent.of(name))); - } catch (InvalidSnapshotException e) { + URI uri = resolveSnapshotName(config, name); + Optional snapshot = config.snapshotDatabase.getSnapshot(uri); + if (snapshot.isPresent()) { + if (session.getSnapshotExperimental() != null) { + session.getSnapshotExperimental().close(); + } + session.setSnapshotExperimental(snapshot.get()); + actor.printInfo(TranslatableComponent.of( + "worldedit.snapshot.use", TextComponent.of(name) + )); + } else { actor.printError(TranslatableComponent.of("worldedit.restore.not-available")); } } @@ -149,13 +195,14 @@ public class SnapshotCommands { desc = "Choose the snapshot based on the list id" ) @CommandPermissions("worldedit.snapshots.restore") - public void sel(Actor actor, World world, LocalSession session, + void sel(Actor actor, World world, LocalSession session, @Arg(desc = "The list ID to select") - int index) throws WorldEditException { + int index) throws IOException { LocalConfiguration config = we.getConfiguration(); + checkSnapshotsConfigured(config); - if (config.snapshotRepo == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-configured")); + if (config.snapshotRepo != null) { + legacy.sel(actor, world, session, index); return; } @@ -164,22 +211,32 @@ public class SnapshotCommands { return; } - try { - List snapshots = config.snapshotRepo.getSnapshots(true, world.getName()); - if (snapshots.size() < index) { - actor.printError(TranslatableComponent.of("worldedit.snapshot.index-oob", TextComponent.of(snapshots.size()))); - return; - } - Snapshot snapshot = snapshots.get(index - 1); - if (snapshot == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-available")); - return; - } - session.setSnapshot(snapshot); - actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use", TextComponent.of(snapshot.getName()))); - } catch (MissingWorldException e) { - actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world")); + List snapshots; + try (Stream snapshotStream = + config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) { + snapshots = snapshotStream + .collect(toList()); } + if (snapshots.size() < index) { + actor.printError(TranslatableComponent.of( + "worldedit.snapshot.index-oob", + TextComponent.of(snapshots.size()) + )); + return; + } + Snapshot snapshot = snapshots.get(index - 1); + if (snapshot == null) { + actor.printError(TranslatableComponent.of("worldedit.restore.not-available")); + return; + } + if (session.getSnapshotExperimental() != null) { + session.getSnapshotExperimental().close(); + } + session.setSnapshotExperimental(snapshot); + actor.printInfo(TranslatableComponent.of( + "worldedit.snapshot.use", + TextComponent.of(snapshot.getInfo().getDisplayName()) + )); } @Command( @@ -187,31 +244,38 @@ public class SnapshotCommands { desc = "Choose the nearest snapshot before a date" ) @CommandPermissions("worldedit.snapshots.restore") - public void before(Actor actor, World world, LocalSession session, + void before(Actor actor, World world, LocalSession session, @Arg(desc = "The soonest date that may be used") - ZonedDateTime date) throws WorldEditException { - + ZonedDateTime date) throws IOException { LocalConfiguration config = we.getConfiguration(); + checkSnapshotsConfigured(config); - if (config.snapshotRepo == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-configured")); + if (config.snapshotRepo != null) { + legacy.before(actor, world, session, date); return; } - try { - Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, world.getName()); + Snapshot snapshot; + try (Stream snapshotStream = + config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) { + snapshot = snapshotStream + .findFirst().orElse(null); + } - if (snapshot == null) { - actor.printError(TranslatableComponent.of( - "worldedit.snapshot.none-before", - TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))) - ); - } else { - session.setSnapshot(snapshot); - actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use", TextComponent.of(snapshot.getName()))); + if (snapshot == null) { + actor.printError(TranslatableComponent.of( + "worldedit.snapshot.none-before", + TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))) + ); + } else { + if (session.getSnapshotExperimental() != null) { + session.getSnapshotExperimental().close(); } - } catch (MissingWorldException ex) { - actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world")); + session.setSnapshotExperimental(snapshot); + actor.printInfo(TranslatableComponent.of( + "worldedit.snapshot.use", + TextComponent.of(snapshot.getInfo().getDisplayName()) + )); } } @@ -220,30 +284,37 @@ public class SnapshotCommands { desc = "Choose the nearest snapshot after a date" ) @CommandPermissions("worldedit.snapshots.restore") - public void after(Actor actor, World world, LocalSession session, + void after(Actor actor, World world, LocalSession session, @Arg(desc = "The soonest date that may be used") - ZonedDateTime date) throws WorldEditException { - + ZonedDateTime date) throws IOException { LocalConfiguration config = we.getConfiguration(); + checkSnapshotsConfigured(config); - if (config.snapshotRepo == null) { - actor.printError(TranslatableComponent.of("worldedit.restore.not-configured")); + if (config.snapshotRepo != null) { + legacy.after(actor, world, session, date); return; } - try { - Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, world.getName()); - if (snapshot == null) { - actor.printError(TranslatableComponent.of( - "worldedit.snapshot.none-after", - TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))) - ); - } else { - session.setSnapshot(snapshot); - actor.printInfo(TranslatableComponent.of("worldedit.snapshot.use", TextComponent.of(snapshot.getName()))); + Snapshot snapshot; + try (Stream snapshotStream = + config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) { + snapshot = snapshotStream + .findFirst().orElse(null); + } + if (snapshot == null) { + actor.printError(TranslatableComponent.of( + "worldedit.snapshot.none-after", + TextComponent.of(dateFormat.withZone(session.getTimeZone()).format(date))) + ); + } else { + if (session.getSnapshotExperimental() != null) { + session.getSnapshotExperimental().close(); } - } catch (MissingWorldException ex) { - actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world")); + session.setSnapshotExperimental(snapshot); + actor.printInfo(TranslatableComponent.of( + "worldedit.snapshot.use", + TextComponent.of(snapshot.getInfo().getDisplayName()) + )); } } @@ -258,10 +329,11 @@ public class SnapshotCommands { @Override public Component getComponent(int number) { final Snapshot snapshot = snapshots.get(number); + String displayName = snapshot.getInfo().getDisplayName(); return TextComponent.of(number + 1 + ". ", TextColor.GOLD) - .append(TextComponent.of(snapshot.getName(), TextColor.LIGHT_PURPLE) - .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to use"))) - .clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, "/snap use " + snapshot.getName()))); + .append(TextComponent.builder(displayName, TextColor.LIGHT_PURPLE) + .hoverEvent(HoverEvent.showText(TextComponent.of("Click to use"))) + .clickEvent(ClickEvent.runCommand("/snap use " + displayName))); } @Override