From c683b414ba4b6f034933f2d8f2b5367f0f3cc47d Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 11 Aug 2018 15:03:43 +1000 Subject: [PATCH] Add PlayerSaveClipboardEvent --- .../worldedit/command/SchematicCommands.java | 20 +++++---- .../worldedit/event/extent/FaweEvent.java | 16 +++++++ .../worldedit/event/extent/PasteEvent.java | 6 +-- .../extent/PlayerSaveClipboardEvent.java | 44 +++++++++++++++++++ 4 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/com/sk89q/worldedit/event/extent/FaweEvent.java create mode 100644 core/src/main/java/com/sk89q/worldedit/event/extent/PlayerSaveClipboardEvent.java diff --git a/core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index f490605b..7dfb98b8 100644 --- a/core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -41,6 +41,7 @@ import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.event.extent.PlayerSaveClipboardEvent; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; @@ -337,15 +338,18 @@ public class SchematicCommands extends MethodCommands { } else { target = clipboard; } - - try (ClipboardWriter writer = format.getWriter(fos)) { - if (writer instanceof StructureFormat) { - ((StructureFormat) writer).write(target, holder.getWorldData(), player.getName()); - } else { - writer.write(target, holder.getWorldData()); + if (new PlayerSaveClipboardEvent(player, clipboard, f.toURI()).call()) { + try (ClipboardWriter writer = format.getWriter(fos)) { + if (writer instanceof StructureFormat) { + ((StructureFormat) writer).write(target, holder.getWorldData(), player.getName()); + } else { + writer.write(target, holder.getWorldData()); + } + log.info(player.getName() + " saved " + f.getCanonicalPath()); + BBC.SCHEMATIC_SAVED.send(player, filename); } - log.info(player.getName() + " saved " + f.getCanonicalPath()); - BBC.SCHEMATIC_SAVED.send(player, filename); + } else { + BBC.WORLDEDIT_CANCEL_REASON_MANUAL.send(player); } } } catch (IllegalArgumentException e) { diff --git a/core/src/main/java/com/sk89q/worldedit/event/extent/FaweEvent.java b/core/src/main/java/com/sk89q/worldedit/event/extent/FaweEvent.java new file mode 100644 index 00000000..58d63975 --- /dev/null +++ b/core/src/main/java/com/sk89q/worldedit/event/extent/FaweEvent.java @@ -0,0 +1,16 @@ +package com.sk89q.worldedit.event.extent; + +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.event.Cancellable; +import com.sk89q.worldedit.event.Event; + +public abstract class FaweEvent extends Event implements Cancellable { + /** + * Returns true if this event was called and not cancelled + * @return !isCancelled + */ + public boolean call() { + WorldEdit.getInstance().getEventBus().post(this); + return !this.isCancelled(); + } +} diff --git a/core/src/main/java/com/sk89q/worldedit/event/extent/PasteEvent.java b/core/src/main/java/com/sk89q/worldedit/event/extent/PasteEvent.java index f004f862..1d63c347 100644 --- a/core/src/main/java/com/sk89q/worldedit/event/extent/PasteEvent.java +++ b/core/src/main/java/com/sk89q/worldedit/event/extent/PasteEvent.java @@ -21,16 +21,14 @@ package com.sk89q.worldedit.event.extent; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.entity.Player; -import com.sk89q.worldedit.event.Cancellable; -import com.sk89q.worldedit.event.Event; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.clipboard.Clipboard; -import java.net.URI; +import java.net.URI; import static com.sk89q.worldedit.EditSession.Stage; -public class PasteEvent extends Event implements Cancellable { +public class PasteEvent extends FaweEvent { private final Player player; private final Clipboard clipboard; diff --git a/core/src/main/java/com/sk89q/worldedit/event/extent/PlayerSaveClipboardEvent.java b/core/src/main/java/com/sk89q/worldedit/event/extent/PlayerSaveClipboardEvent.java new file mode 100644 index 00000000..20c993b0 --- /dev/null +++ b/core/src/main/java/com/sk89q/worldedit/event/extent/PlayerSaveClipboardEvent.java @@ -0,0 +1,44 @@ +package com.sk89q.worldedit.event.extent; + +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.event.Cancellable; +import com.sk89q.worldedit.event.Event; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.session.ClipboardHolder; + +import java.net.URI; + +public class PlayerSaveClipboardEvent extends FaweEvent { + private final Player player; + private final Clipboard clipboard; + private final URI uri; + private boolean cancelled; + + public PlayerSaveClipboardEvent(Player player, Clipboard clipboard, URI destination) { + this.player = player; + this.clipboard = clipboard; + this.uri = destination; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + public URI getUri() { + return uri; + } + + public Clipboard getClipboard() { + return clipboard; + } + + public Player getPlayer() { + return player; + } +}