Make FlagContainer instance in Plot final

This commit is contained in:
Alexander Söderberg 2020-02-18 19:53:14 +01:00
parent 207bd9839e
commit 43e94de897
6 changed files with 24 additions and 21 deletions

View File

@ -1895,10 +1895,7 @@ import java.util.concurrent.atomic.AtomicInteger;
PlotSquared.debug("&c" + myflags); PlotSquared.debug("&c" + myflags);
this.setFlags(plot, flags); this.setFlags(plot, flags);
} }
// TODO throws NPE because PlotArea isn't set yet plot.getFlagContainer().addAll(flags);
FlagContainer container = new FlagContainer(plot.getArea().getFlagContainer());
container.addAll(flags);
plot.setFlagContainer(container);
} else if (Settings.Enabled_Components.DATABASE_PURGER) { } else if (Settings.Enabled_Components.DATABASE_PURGER) {
toDelete.add(id); toDelete.add(id);
} else { } else {

View File

@ -2,6 +2,7 @@ package com.github.intellectualsites.plotsquared.plot.flags;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Setter;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
@ -13,7 +14,7 @@ import java.util.Map;
*/ */
@EqualsAndHashCode(of = "flagMap") @SuppressWarnings("unused") public class FlagContainer { @EqualsAndHashCode(of = "flagMap") @SuppressWarnings("unused") public class FlagContainer {
private final FlagContainer parentContainer; @Setter private FlagContainer parentContainer;
private final Map<Class<?>, PlotFlag<?, ?>> flagMap = new HashMap<>(); private final Map<Class<?>, PlotFlag<?, ?>> flagMap = new HashMap<>();
private final PlotFlagUpdateHandler plotFlagUpdateHandler; 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 * Clears the local flag map
*/ */

View File

@ -38,7 +38,6 @@ import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -153,7 +152,7 @@ public class Plot {
/** /**
* Plot flag container * Plot flag container
*/ */
@Getter private FlagContainer flagContainer; @Getter private final FlagContainer flagContainer = new FlagContainer(null);
/** /**
* Constructor for a new plot. * Constructor for a new plot.
@ -196,7 +195,7 @@ public class Plot {
this.id = id; this.id = id;
this.owner = owner; this.owner = owner;
this.temp = temp; 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.timestamp = timestamp;
this.temp = temp; this.temp = temp;
if (area != null) { if (area != null) {
this.flagContainer = new FlagContainer(area.getFlagContainer()); this.flagContainer.setParentContainer(area.getFlagContainer());
if (flags != null) { if (flags != null) {
for (PlotFlag<?, ?> flag : flags) { for (PlotFlag<?, ?> flag : flags) {
this.flagContainer.addFlag(flag); this.flagContainer.addFlag(flag);
@ -526,6 +525,7 @@ public class Plot {
} }
this.area = area; this.area = area;
area.addPlot(this); area.addPlot(this);
this.flagContainer.setParentContainer(area.getFlagContainer());
} }
/** /**
@ -2087,10 +2087,6 @@ public class Plot {
return this.id.hashCode(); return this.id.hashCode();
} }
public void setFlagContainer(final FlagContainer flagContainer) {
this.flagContainer = flagContainer;
}
/** /**
* Gets the plot alias. * Gets the plot alias.
* - Returns an empty string if no alias is set * - Returns an empty string if no alias is set
@ -2441,8 +2437,12 @@ public class Plot {
} else { } else {
flagContainer2.addAll(flagContainer1.getFlagMap().values()); flagContainer2.addAll(flagContainer1.getFlagMap().values());
} }
this.setFlagContainer(greater ? flagContainer1 : flagContainer2); if (!greater) {
plot.setFlagContainer(this.getFlagContainer()); this.flagContainer.clearLocal();
this.flagContainer.addAll(flagContainer2.getFlagMap().values());
}
plot.flagContainer.clearLocal();
plot.flagContainer.addAll(this.flagContainer.getFlagMap().values());
} }
if (!this.getAlias().isEmpty()) { if (!this.getAlias().isEmpty()) {
plot.setAlias(this.getAlias()); plot.setAlias(this.getAlias());

View File

@ -172,7 +172,7 @@ public class SinglePlotArea extends GridPlotWorld {
final FlagContainer oldContainer = p.getFlagContainer(); final FlagContainer oldContainer = p.getFlagContainer();
p = new SinglePlot(p.getId(), p.owner, p.getTrusted(), p.getMembers(), p.getDenied(), 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); s.alias, s.getPosition(), null, this, s.merged, p.getTimestamp(), p.temp);
p.setFlagContainer(oldContainer); p.getFlagContainer().addAll(oldContainer);
return p; return p;
} }

View File

@ -1,6 +1,6 @@
package com.github.intellectualsites.plotsquared.plot.database; 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.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotCluster; 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 com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -90,7 +91,7 @@ public class AbstractDBTest implements AbstractDB {
@Override public void swapPlots(Plot plot1, Plot plot2) { @Override public void swapPlots(Plot plot1, Plot plot2) {
} }
@Override public void setFlags(Plot plot, HashMap<Flag<?>, Object> flags) { @Override public void setFlags(Plot plot, Collection<PlotFlag<?, ?>> flags) {
} }
@Override public void setClusterName(PlotCluster cluster, String name) { @Override public void setClusterName(PlotCluster cluster, String name) {

View File

@ -1,6 +1,6 @@
package com.github.intellectualsites.plotsquared.plot.util; 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.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
@ -37,11 +37,11 @@ public class EventUtilTest extends EventUtil {
return false; return false;
} }
@Override public boolean callFlagAdd(Flag flag, Plot plot) { @Override public boolean callFlagAdd(PlotFlag<?, ?> flag, Plot plot) {
return true; return true;
} }
@Override public boolean callFlagRemove(Flag<?> flag, Plot plot, Object value) { @Override public boolean callFlagRemove(PlotFlag<?, ?> flag, Plot plot, Object value) {
return true; return true;
} }