Commit WIP flag work.

This commit is contained in:
Alexander Söderberg 2020-02-07 21:24:49 +01:00
parent d0b6badf09
commit 364c693b8c
13 changed files with 312 additions and 76 deletions

View File

@ -2,13 +2,13 @@ package com.github.intellectualsites.plotsquared.bukkit.listeners;
import com.destroystokyo.paper.MaterialTags;
import com.github.intellectualsites.plotsquared.bukkit.BukkitMain;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitBlockUtil;
import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag;
import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.Location;
@ -31,13 +31,11 @@ import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.UpdateUtility;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -145,7 +143,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
@ -1146,7 +1143,7 @@ import java.util.regex.Pattern;
}
Plot plot = area.getOwnedPlot(location);
if (plot != null) {
if (Flags.EXPLOSION.isTrue(plot)) {
if (plot.getFlag(ExplosionFlag.class)) {
List<MetadataValue> meta = event.getEntity().getMetadata("plot");
Plot origin;
if (meta.isEmpty()) {
@ -1858,7 +1855,7 @@ import java.util.regex.Pattern;
return;
}
Plot plot = area.getOwnedPlot(location);
if (plot == null || !plot.getFlag(Flags.EXPLOSION).orElse(false)) {
if (plot == null || !plot.getFlag(ExplosionFlag.class)) {
event.setCancelled(true);
}
event.blockList().removeIf(

View File

@ -18,6 +18,7 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:4.2.2")
implementation("com.squareup.okio:okio:2.4.1")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.61")
implementation("org.jetbrains:annotations:18.0.0")
}
sourceCompatibility = 1.8

View File

@ -2,6 +2,7 @@ package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.flags.FlagContainer;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
@ -219,7 +220,7 @@ public class FlagManager {
return true;
}
public static void setPlotFlags(Plot origin, HashMap<Flag<?>, Object> flags) {
public static void setPlotFlags(Plot origin, FlagContainer flagContainer) {
for (Plot plot : origin.getConnectedPlots()) {
if (flags != null && !flags.isEmpty()) {
plot.getFlags().clear();

View File

@ -43,7 +43,6 @@ public final class Flags {
return Captions.FLAG_ERROR_PRICE.getTranslated();
}
};
public static final BooleanFlag EXPLOSION = new BooleanFlag("explosion");
public static final BooleanFlag GRASS_GROW = new BooleanFlag("grass-grow");
public static final BooleanFlag VINE_GROW = new BooleanFlag("vine-grow");
public static final BooleanFlag MYCEL_GROW = new BooleanFlag("mycel-grow");

View File

@ -0,0 +1,65 @@
package com.github.intellectualsites.plotsquared.plot.flags;
import com.google.common.collect.ImmutableMap;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@EqualsAndHashCode(of = "flagMap") public class FlagContainer {
private final FlagContainer parentContainer;
private final Map<Class<?>, PlotFlag<?>> flagMap = new HashMap<>();
public FlagContainer(@Nullable final FlagContainer parentContainer) {
this.parentContainer = parentContainer;
}
public FlagContainer getParentContainer() {
return this.parentContainer;
}
protected Map<Class<?>, PlotFlag<?>> getInternalPlotFlagMap() {
return this.flagMap;
}
public Map<Class<?>, PlotFlag<?>> getFlagMap() {
return ImmutableMap.<Class<?>, PlotFlag<?>>builder().putAll(this.flagMap).build();
}
public void addFlag(final PlotFlag<?> flag) {
this.flagMap.put(flag.getClass(), flag);
}
public void addAll(final Collection<PlotFlag<?>> flags) {
for (final PlotFlag<?> flag : flags) {
this.addFlag(flag);
}
}
public Collection<PlotFlag<?>> getRecognizedPlotFlags() {
return this.getHighestClassContainer().getFlagMap().values();
}
public final FlagContainer getHighestClassContainer() {
if (this.getParentContainer() != null) {
return this.getParentContainer();
}
return this;
}
public <T> PlotFlag<T> getFlag(final Class<? extends PlotFlag<T>> flagClass) {
final PlotFlag<?> flag = this.flagMap.get(flagClass);
if (flag != null) {
return (PlotFlag<T>) flag;
} else {
if (getParentContainer() != null) {
return getParentContainer().getFlag(flagClass);
}
}
return null;
}
}

View File

@ -0,0 +1,33 @@
package com.github.intellectualsites.plotsquared.plot.flags;
public class FlagParseException extends Exception {
private final PlotFlag<?> flag;
private final String value;
public FlagParseException(final PlotFlag<?> flag, final String value) {
super(String.format("Failed to parse flag of type '%s'. Value '%s' was not accepted.",
flag.getName(), value));
this.flag = flag;
this.value = value;
}
/**
* Returns the value that caused the parse exception
*
* @return Value that failed to parse
*/
public String getValue() {
return this.value;
}
/**
* Returns the class that threw the exception
*
* @return Flag that threw the exception
*/
public PlotFlag<?> getFlag() {
return this.flag;
}
}

View File

@ -0,0 +1,28 @@
package com.github.intellectualsites.plotsquared.plot.flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.ExplosionFlag;
import lombok.Getter;
import javax.annotation.Nonnull;
public final class GlobalFlagContainer extends FlagContainer {
@Getter private static final GlobalFlagContainer instance = new GlobalFlagContainer();
@Nonnull @Override public <T> PlotFlag<T> getFlag(Class<? extends PlotFlag<T>> flagClass) {
final PlotFlag<?> flag = super.getFlag(flagClass);
if (flag != null) {
return (PlotFlag<T>) flag;
} else {
throw new IllegalStateException(String.format("Unrecognized flag '%s'. All flag types"
+ " must be present in the global flag container.", flagClass.getSimpleName()));
}
}
private GlobalFlagContainer() {
super(null);
// Register all default flags here
this.addFlag(new ExplosionFlag());
}
}

View File

@ -0,0 +1,78 @@
package com.github.intellectualsites.plotsquared.plot.flags;
import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
/**
* A plot flag is any property that can be assigned
* to a plot, that will alter its functionality in some way.
* These are user assignable in-game, or via configuration files.
*
* @param <T> Value contained in the flag.
*/
@EqualsAndHashCode(of = "value") public abstract class PlotFlag<T> {
private T value;
/**
* Construct a new flag instance.
*
* @param value Flag value
*/
protected PlotFlag(@NotNull final T value) {
this.value = Preconditions.checkNotNull(value, "flag values may not be null");
}
/**
* Get the flag value
*
* @return Non-nullable flag value
*/
@NotNull public final T getValue() {
return this.value;
}
/**
* Update the flag value.
*
* @param newValue New flag value.
*/
public T setFlagValue(@NotNull final T newValue) {
return (this.value = Preconditions.checkNotNull(newValue, "flag values may not be null"));
}
/**
* Parse a string into a flag value, and throw an exception in the case that the
* string does not represent a valid flag value. The flag value will be updated to
* the newly parsed value.
*
* @param input String to parse.
* @return Parsed value, if valid.
* @throws FlagParseException If the value could not be parsed.
*/
public abstract T parse(@NotNull final String input) throws FlagParseException;
/**
* Merge two flag values into one and updates the flag value.
*
* @param oldValue Existing flag value.
* @param newValue New flag value.
* @return Flag containing parsed flag value.
*/
public abstract T merge(@NotNull final T oldValue, @NotNull final T newValue);
/**
* Returns a string representation of the flag instance, that when
* passed through {@link #parse(String)} will result in an equivalent
* instance of the flag.
*
* @return String representation of the flag
*/
public abstract String toString();
public final String getName() {
return this.getClass().getSimpleName();
}
}

View File

@ -0,0 +1,6 @@
package com.github.intellectualsites.plotsquared.plot.flags.implementations;
import com.github.intellectualsites.plotsquared.plot.flags.types.BooleanFlag;
public class ExplosionFlag extends BooleanFlag {
}

View File

@ -0,0 +1,51 @@
package com.github.intellectualsites.plotsquared.plot.flags.types;
import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException;
import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
public abstract class BooleanFlag extends PlotFlag<Boolean> {
private static final Collection<String> positiveValues = Arrays.asList("1", "yes", "allow", "true");
private static final Collection<String> negativeValues = Arrays.asList("0", "no", "deny", "disallow", "false");
/**
* Construct a new flag instance.
*
* @param value Flag value
*/
protected BooleanFlag(boolean value) {
super(value);
}
/**
* Construct a new boolean flag, with
* {@code false} as the default value.
*/
protected BooleanFlag() {
this(false);
}
@Override public Boolean parse(@NotNull String input) throws FlagParseException {
if (positiveValues.contains(input.toLowerCase(Locale.ENGLISH))) {
return this.setFlagValue(true);
} else if (negativeValues.contains(input.toLowerCase(Locale.ENGLISH))) {
return this.setFlagValue(false);
} else {
throw new FlagParseException(this, input);
}
}
@Override public Boolean merge(@NotNull Boolean oldValue, @NotNull Boolean newValue) {
return this.setFlagValue(oldValue || newValue);
}
@Override public String toString() {
return this.getValue().toString();
}
}

View File

@ -1,8 +1,5 @@
package com.github.intellectualsites.plotsquared.plot.object;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.config.Configuration;
@ -11,6 +8,8 @@ import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.flag.FlagManager;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.FlagContainer;
import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag;
import com.github.intellectualsites.plotsquared.plot.generator.SquarePlotWorld;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment;
@ -39,7 +38,8 @@ 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 java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -62,6 +62,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@ -152,6 +153,11 @@ public class Plot {
*/
private Plot origin;
/**
* Plot flag container
*/
@Getter private FlagContainer flagContainer;
/**
* Constructor for a new plot.
* (Only changes after plot.create() will be properly set in the database)
@ -162,9 +168,7 @@ public class Plot {
* @see Plot#getPlot(Location) for existing plots
*/
public Plot(PlotArea area, @NotNull PlotId id, UUID owner) {
this.area = area;
this.id = id;
this.owner = owner;
this(area, id, owner, 0);
}
/**
@ -176,8 +180,7 @@ public class Plot {
* @see Plot#getPlot(Location) for existing plots
*/
public Plot(PlotArea area, @NotNull PlotId id) {
this.area = area;
this.id = id;
this(area, id, null, 0);
}
/**
@ -196,6 +199,7 @@ public class Plot {
this.id = id;
this.owner = owner;
this.temp = temp;
this.flagContainer = new FlagContainer(area.getFlagContainer());
}
/**
@ -209,7 +213,7 @@ public class Plot {
* @see Plot#getPlot(Location) for existing plots
*/
public Plot(@NotNull PlotId id, UUID owner, HashSet<UUID> trusted, HashSet<UUID> members,
HashSet<UUID> denied, String alias, BlockLoc position, Collection<Flag> flags,
HashSet<UUID> denied, String alias, BlockLoc position, Collection<PlotFlag<?>> flags,
PlotArea area, boolean[] merged, long timestamp, int temp) {
this.id = id;
this.area = area;
@ -221,13 +225,14 @@ public class Plot {
this.settings.setAlias(alias);
this.settings.setPosition(position);
this.settings.setMerged(merged);
if (flags != null) {
for (Flag flag : flags) {
this.settings.flags.put(flag, flag);
}
}
this.timestamp = timestamp;
this.temp = temp;
this.flagContainer = new FlagContainer(area.getFlagContainer());
if (flags != null) {
for (PlotFlag<?> flag : flags) {
this.flagContainer.addFlag(flag);
}
}
}
/**
@ -1101,30 +1106,6 @@ public class Plot {
return FlagManager.removePlotFlag(this, flag);
}
/**
* Gets the flag for a given key
*
* @param key Flag to get value for
*/
public <V> Optional<V> getFlag(Flag<V> key) {
return FlagManager.getPlotFlag(this, key);
}
/**
* Gets the flag for a given key
*
* @param key the flag
* @param defaultValue if the key is null, the value to return
*/
public <V> V getFlag(Flag<V> key, V defaultValue) {
V value = FlagManager.getPlotFlagRaw(this, key);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
/**
* Delete a plot (use null for the runnable if you don't need to be notified on completion)
*
@ -2015,16 +1996,6 @@ public class Plot {
return this.id.hashCode();
}
/**
* Gets the flags specific to this plot<br>
* - Does not take default flags into account<br>
*
* @return
*/
public HashMap<Flag<?>, Object> getFlags() {
return this.getSettings().flags;
}
/**
* Sets a flag for this plot.
*
@ -2034,6 +2005,10 @@ public class Plot {
FlagManager.setPlotFlags(this, flags);
}
public void setFlagContainer(final FlagContainer flagContainer) {
this.flagContainer = flagContainer;
}
/**
* Gets the plot alias.
* - Returns an empty string if no alias is set
@ -2374,18 +2349,17 @@ public class Plot {
* @param plot
*/
public void mergeData(Plot plot) {
HashMap<Flag<?>, Object> flags1 = this.getFlags();
HashMap<Flag<?>, Object> flags2 = plot.getFlags();
if ((!flags1.isEmpty() || !flags2.isEmpty()) && !flags1.equals(flags2)) {
boolean greater = flags1.size() > flags2.size();
final FlagContainer flagContainer1 = this.getFlagContainer();
final FlagContainer flagContainer2 = plot.getFlagContainer();
if (!flagContainer1.equals(flagContainer2)) {
boolean greater = flagContainer1.getFlagMap().size() > flagContainer2.getFlagMap().size();
if (greater) {
flags1.putAll(flags2);
flagContainer1.addAll(flagContainer2.getFlagMap().values());
} else {
flags2.putAll(flags1);
flagContainer2.addAll(flagContainer1.getFlagMap().values());
}
HashMap<Flag<?>, Object> net = (greater ? flags1 : flags2);
this.setFlags(net);
plot.setFlags(net);
this.setFlagContainer(greater ? flagContainer1 : flagContainer2);
plot.setFlagContainer(this.getFlagContainer());
}
if (!this.getAlias().isEmpty()) {
plot.setAlias(this.getAlias());
@ -3150,10 +3124,6 @@ public class Plot {
return true;
}
public boolean hasFlag(Flag<?> flag) {
return getFlags().containsKey(flag);
}
public boolean removeComment(PlotComment comment) {
return getSettings().removeComment(comment);
}
@ -3177,4 +3147,9 @@ public class Plot {
public boolean getMerged(Direction direction) {
return getMerged(direction.getIndex());
}
public <T> T getFlag(final Class<? extends PlotFlag<T>> flagClass) {
return this.flagContainer.getFlag(flagClass).getValue();
}
}

View File

@ -8,6 +8,8 @@ import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.flag.FlagManager;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.FlagContainer;
import com.github.intellectualsites.plotsquared.plot.flags.GlobalFlagContainer;
import com.github.intellectualsites.plotsquared.plot.generator.GridPlotWorld;
import com.github.intellectualsites.plotsquared.plot.generator.IndependentPlotGenerator;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
@ -28,6 +30,7 @@ import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -88,6 +91,10 @@ public abstract class PlotArea {
private CuboidRegion region;
private ConcurrentHashMap<String, Object> meta;
private QuadMap<PlotCluster> clusters;
/**
* Area flag container
*/
@Getter private FlagContainer flagContainer = new FlagContainer(GlobalFlagContainer.getInstance());
public PlotArea(@NotNull final String worldName, @Nullable final String id,
@NotNull IndependentPlotGenerator generator, @Nullable final PlotId min,

View File

@ -2,6 +2,7 @@ package com.github.intellectualsites.plotsquared.plot.object;
import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.FlagContainer;
import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment;
import com.google.common.collect.ImmutableList;
@ -37,12 +38,6 @@ public class PlotSettings {
* @deprecated Raw access
*/
@Deprecated public HashMap<UUID, Integer> ratings;
/**
* Flags.
*
* @deprecated Raw access
*/
@Deprecated public HashMap<Flag<?>, Object> flags = new HashMap<>();
private List<PlotComment> comments = null;
/**
* Home Position.