From 06d716248e669e84c8b637a2eb859c13ec660a08 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Mon, 13 Sep 2021 15:08:03 +0100 Subject: [PATCH] Flush clipboard rather than close on player disconnect Addresses #1291 --- .../core/extent/clipboard/LazyClipboardHolder.java | 7 +++++++ .../core/extent/clipboard/MultiClipboardHolder.java | 12 ++++++++++++ .../main/java/com/sk89q/worldedit/LocalSession.java | 10 +++++++--- .../java/com/sk89q/worldedit/entity/Player.java | 2 +- .../sk89q/worldedit/session/ClipboardHolder.java | 13 ++++++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/LazyClipboardHolder.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/LazyClipboardHolder.java index 4934fc126..fe784c971 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/LazyClipboardHolder.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/LazyClipboardHolder.java @@ -61,4 +61,11 @@ public class LazyClipboardHolder extends URIClipboardHolder { clipboard = null; } + @Override + public synchronized void flush() { + if (clipboard instanceof BlockArrayClipboard) { + clipboard.flush(); + } + } + } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/MultiClipboardHolder.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/MultiClipboardHolder.java index c2402d3ee..de739adf8 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/MultiClipboardHolder.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/clipboard/MultiClipboardHolder.java @@ -3,6 +3,7 @@ package com.fastasyncworldedit.core.extent.clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.session.ClipboardHolder; +import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.HashSet; @@ -183,4 +184,15 @@ public class MultiClipboardHolder extends URIClipboardHolder { } } + @Override + public void flush() { + for (ClipboardHolder holder : holders) { + try { + holder.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } 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 9932993ae..ebacbb0f5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -840,12 +840,16 @@ public class LocalSession implements TextureHolder { } /** - * Ensure the player's clipboard is closed. (will only do something with clipboard-on-disk) + * Ensure the player's clipboard is flushed. (will only do something with clipboard-on-disk) */ - public void closeClipboard() { + public void flushClipboard() { synchronized (clipboardLock) { if (this.clipboard != null) { - this.clipboard.close(); + try { + this.clipboard.flush(); + } catch (IOException e) { + e.printStackTrace(); + } } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java index 87e1f518b..9ef866628 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java @@ -391,7 +391,7 @@ public interface Player extends Entity, Actor { */ default void unregister() { cancel(true); - getSession().closeClipboard(); + getSession().flushClipboard(); if (Settings.IMP.HISTORY.DELETE_ON_LOGOUT) { getSession().clearHistory(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/ClipboardHolder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/ClipboardHolder.java index 8c7af5a04..87d53c3d3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/ClipboardHolder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/ClipboardHolder.java @@ -24,6 +24,9 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.math.transform.Identity; import com.sk89q.worldedit.math.transform.Transform; +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; import java.util.Collections; import java.util.List; @@ -32,7 +35,8 @@ import static com.google.common.base.Preconditions.checkNotNull; /** * Holds the clipboard and the current transform on the clipboard. */ -public class ClipboardHolder { +//FAWE start - closeable and flushable +public class ClipboardHolder implements Closeable, Flushable { private Clipboard clipboard; private Transform transform = new Identity(); @@ -118,6 +122,7 @@ public class ClipboardHolder { return new PasteBuilder(this, targetExtent); } + @Override public void close() { if (clipboard != null) { clipboard.close(); @@ -125,5 +130,11 @@ public class ClipboardHolder { clipboard = null; } + @Override + public void flush() throws IOException { + if (clipboard != null) { + clipboard.flush(); + } + } }