From e4158ac08401c293f98fe5ce85b7a64a1d299aef Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 13 Apr 2023 17:34:05 +0100 Subject: [PATCH] fix: invalidate cached FaweMask when a plot is unlinked (#2157) * fix: invalidate cached FaweMask when a plot is unlinked - Fixes IntellectualSites/PlotSquared#3998 * Use a weakly-referenced copy of the copnncted plots set --- .../plotsquared/PlotSquaredFeature.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/regions/plotsquared/PlotSquaredFeature.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/regions/plotsquared/PlotSquaredFeature.java index 34e5aeaaa..cd6f5ae22 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/regions/plotsquared/PlotSquaredFeature.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/regions/plotsquared/PlotSquaredFeature.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.world.World; import org.apache.logging.log4j.Logger; import org.bukkit.Bukkit; +import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; import java.util.Set; @@ -176,15 +177,28 @@ public class PlotSquaredFeature extends FaweMaskManager { maskedRegion = new RegionIntersection(world, weRegions); } - return new FaweMask(maskedRegion) { - @Override - public boolean isValid(Player player, MaskType type) { - if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(finalPlot)) { - return false; - } - return isAllowed(player, finalPlot, type); + return new PlotSquaredMask(maskedRegion, finalPlot); + } + + private final class PlotSquaredMask extends FaweMask { + + private final Plot plot; + private final WeakReference> connectedPlots; + + private PlotSquaredMask(Region region, Plot plot) { + super(region); + this.plot = plot; + connectedPlots = new WeakReference<>(plot.getConnectedPlots()); + } + + @Override + public boolean isValid(Player player, MaskType type) { + if (!connectedPlots.refersTo(plot.getConnectedPlots()) || (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot))) { + return false; } - }; + return isAllowed(player, plot, type); + } + } }