From 43e94de8979ab29ee6907f63518c92a40a792069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 18 Feb 2020 19:53:14 +0100 Subject: [PATCH] Make FlagContainer instance in Plot final --- .../plotsquared/plot/database/SQLManager.java | 5 +---- .../plotsquared/plot/flags/FlagContainer.java | 7 ++++++- .../plotsquared/plot/object/Plot.java | 20 +++++++++---------- .../plot/object/worlds/SinglePlotArea.java | 2 +- .../plot/database/AbstractDBTest.java | 5 +++-- .../plotsquared/plot/util/EventUtilTest.java | 6 +++--- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java index 903e2892c..96c314050 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java @@ -1895,10 +1895,7 @@ import java.util.concurrent.atomic.AtomicInteger; PlotSquared.debug("&c" + myflags); this.setFlags(plot, flags); } - // TODO throws NPE because PlotArea isn't set yet - FlagContainer container = new FlagContainer(plot.getArea().getFlagContainer()); - container.addAll(flags); - plot.setFlagContainer(container); + plot.getFlagContainer().addAll(flags); } else if (Settings.Enabled_Components.DATABASE_PURGER) { toDelete.add(id); } else { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagContainer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagContainer.java index a8d930425..e5bf23ed2 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagContainer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/FlagContainer.java @@ -2,6 +2,7 @@ package com.github.intellectualsites.plotsquared.plot.flags; import com.google.common.collect.ImmutableMap; import lombok.EqualsAndHashCode; +import lombok.Setter; import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -13,7 +14,7 @@ import java.util.Map; */ @EqualsAndHashCode(of = "flagMap") @SuppressWarnings("unused") public class FlagContainer { - private final FlagContainer parentContainer; + @Setter private FlagContainer parentContainer; private final Map, PlotFlag> flagMap = new HashMap<>(); private final PlotFlagUpdateHandler plotFlagUpdateHandler; @@ -96,6 +97,10 @@ import java.util.Map; } } + public void addAll(final FlagContainer container) { + this.addAll(container.flagMap.values()); + } + /** * Clears the local flag map */ diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index e76f19bde..b0f78192e 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -38,7 +38,6 @@ import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BlockTypes; -import lombok.AccessLevel; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -153,7 +152,7 @@ public class Plot { /** * Plot flag container */ - @Getter private FlagContainer flagContainer; + @Getter private final FlagContainer flagContainer = new FlagContainer(null); /** * Constructor for a new plot. @@ -196,7 +195,7 @@ public class Plot { this.id = id; this.owner = owner; this.temp = temp; - this.flagContainer = new FlagContainer(area.getFlagContainer()); + this.flagContainer.setParentContainer(area.getFlagContainer()); } /** @@ -225,7 +224,7 @@ public class Plot { this.timestamp = timestamp; this.temp = temp; if (area != null) { - this.flagContainer = new FlagContainer(area.getFlagContainer()); + this.flagContainer.setParentContainer(area.getFlagContainer()); if (flags != null) { for (PlotFlag flag : flags) { this.flagContainer.addFlag(flag); @@ -526,6 +525,7 @@ public class Plot { } this.area = area; area.addPlot(this); + this.flagContainer.setParentContainer(area.getFlagContainer()); } /** @@ -2087,10 +2087,6 @@ public class Plot { return this.id.hashCode(); } - public void setFlagContainer(final FlagContainer flagContainer) { - this.flagContainer = flagContainer; - } - /** * Gets the plot alias. * - Returns an empty string if no alias is set @@ -2441,8 +2437,12 @@ public class Plot { } else { flagContainer2.addAll(flagContainer1.getFlagMap().values()); } - this.setFlagContainer(greater ? flagContainer1 : flagContainer2); - plot.setFlagContainer(this.getFlagContainer()); + if (!greater) { + this.flagContainer.clearLocal(); + this.flagContainer.addAll(flagContainer2.getFlagMap().values()); + } + plot.flagContainer.clearLocal(); + plot.flagContainer.addAll(this.flagContainer.getFlagMap().values()); } if (!this.getAlias().isEmpty()) { plot.setAlias(this.getAlias()); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java index f5e620266..c1c6635c5 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java @@ -172,7 +172,7 @@ public class SinglePlotArea extends GridPlotWorld { final FlagContainer oldContainer = p.getFlagContainer(); p = new SinglePlot(p.getId(), p.owner, p.getTrusted(), p.getMembers(), p.getDenied(), s.alias, s.getPosition(), null, this, s.merged, p.getTimestamp(), p.temp); - p.setFlagContainer(oldContainer); + p.getFlagContainer().addAll(oldContainer); return p; } diff --git a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java index a2cc07d1b..2f2a18f5f 100644 --- a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java +++ b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/database/AbstractDBTest.java @@ -1,6 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.database; -import com.github.intellectualsites.plotsquared.plot.flag.Flag; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotCluster; @@ -9,6 +9,7 @@ import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; import org.jetbrains.annotations.NotNull; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -90,7 +91,7 @@ public class AbstractDBTest implements AbstractDB { @Override public void swapPlots(Plot plot1, Plot plot2) { } - @Override public void setFlags(Plot plot, HashMap, Object> flags) { + @Override public void setFlags(Plot plot, Collection> flags) { } @Override public void setClusterName(PlotCluster cluster, String name) { diff --git a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java index fd2a8db15..d4fb9ad31 100644 --- a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java +++ b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java @@ -1,6 +1,6 @@ package com.github.intellectualsites.plotsquared.plot.util; -import com.github.intellectualsites.plotsquared.plot.flag.Flag; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; @@ -37,11 +37,11 @@ public class EventUtilTest extends EventUtil { return false; } - @Override public boolean callFlagAdd(Flag flag, Plot plot) { + @Override public boolean callFlagAdd(PlotFlag flag, Plot plot) { return true; } - @Override public boolean callFlagRemove(Flag flag, Plot plot, Object value) { + @Override public boolean callFlagRemove(PlotFlag flag, Plot plot, Object value) { return true; }