Clean up some events and utility classes. Try to use as general type

declarations as possible. Add configurable override for the unsafe block checker.
This commit is contained in:
Sauilitired 2018-12-28 07:39:39 +01:00
parent 660754511b
commit 12b8ae3eed
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
16 changed files with 226 additions and 290 deletions

View File

@ -2,18 +2,26 @@ package com.github.intellectualsites.plotsquared.bukkit.events;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import java.util.ArrayList;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
public class PlotMergeEvent extends PlotEvent implements Cancellable {
/**
* Event called when several plots are merged
* {@inheritDoc}
*/
public final class PlotMergeEvent extends PlotEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ArrayList<PlotId> plots;
private final World world;
private boolean cancelled;
private final List<PlotId> plots;
@Getter private final World world;
@Getter @Setter private boolean cancelled;
/**
* PlotMergeEvent: Called when plots are merged
@ -22,7 +30,8 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable {
* @param plot Plot that was merged
* @param plots A list of plots involved in the event
*/
public PlotMergeEvent(World world, Plot plot, ArrayList<PlotId> plots) {
public PlotMergeEvent(@Nonnull final World world, @Nonnull final Plot plot,
@Nonnull final List<PlotId> plots) {
super(plot);
this.world = world;
this.plots = plots;
@ -35,25 +44,13 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable {
/**
* Get the plots being added.
*
* @return Plot
* @return Unmodifiable list containing the merging plots
*/
public ArrayList<PlotId> getPlots() {
return this.plots;
}
public World getWorld() {
return this.world;
public List<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}
@Override public boolean isCancelled() {
return this.cancelled;
}
@Override public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View File

@ -2,20 +2,28 @@ package com.github.intellectualsites.plotsquared.bukkit.events;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.ArrayList;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
public class PlotUnlinkEvent extends Event implements Cancellable {
/**
* Event called when several merged plots are unlinked
* {@inheritDoc}
*/
public final class PlotUnlinkEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ArrayList<PlotId> plots;
private final World world;
private final PlotArea area;
private boolean cancelled;
private final List<PlotId> plots;
@Getter private final World world;
@Getter private final PlotArea area;
@Getter @Setter private boolean cancelled;
/**
* Called when a mega-plot is unlinked.
@ -23,7 +31,8 @@ public class PlotUnlinkEvent extends Event implements Cancellable {
* @param world World in which the event occurred
* @param plots Plots that are involved in the event
*/
public PlotUnlinkEvent(World world, PlotArea area, ArrayList<PlotId> plots) {
public PlotUnlinkEvent(@Nonnull final World world, @Nonnull final PlotArea area,
@Nonnull final List<PlotId> plots) {
this.plots = plots;
this.world = world;
this.area = area;
@ -36,29 +45,13 @@ public class PlotUnlinkEvent extends Event implements Cancellable {
/**
* Get the plots involved.
*
* @return The {@link PlotId}'s of the plots involved
* @return Unmodifiable list containing {@link PlotId PlotIds} of the plots involved
*/
public ArrayList<PlotId> getPlots() {
return this.plots;
}
public World getWorld() {
return this.world;
}
public PlotArea getArea() {
return this.area;
public List<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}
@Override public boolean isCancelled() {
return this.cancelled;
}
@Override public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View File

@ -10,20 +10,24 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class BukkitEventUtil extends EventUtil {
/**
* Utility class for handling Bukkit {@link Event events}
*/
public final class BukkitEventUtil extends EventUtil {
public Player getPlayer(PlotPlayer player) {
@Nullable public Player getPlayer(final PlotPlayer player) {
if (player instanceof BukkitPlayer) {
return ((BukkitPlayer) player).player;
}
return null;
}
public boolean callEvent(Event event) {
private boolean callEvent(@Nonnull final Event event) {
Bukkit.getServer().getPluginManager().callEvent(event);
return !(event instanceof Cancellable) || !((Cancellable) event).isCancelled();
}
@ -56,11 +60,11 @@ public class BukkitEventUtil extends EventUtil {
return callEvent(new PlotFlagRemoveEvent(flag, plot));
}
@Override public boolean callMerge(Plot plot, ArrayList<PlotId> plots) {
@Override public boolean callMerge(Plot plot, List<PlotId> plots) {
return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots));
}
@Override public boolean callUnlink(PlotArea area, ArrayList<PlotId> plots) {
@Override public boolean callUnlink(PlotArea area, List<PlotId> plots) {
return callEvent(new PlotUnlinkEvent(BukkitUtil.getWorld(area.worldname), area, plots));
}

View File

@ -244,7 +244,7 @@ public class Auto extends SubCommand {
}
}
if (!plotarea
.mergePlots(MainUtil.getPlotSelectionIds(start, end), true, true)) {
.mergePlots(MainUtil.getPlotSelectionIds(start, end), true)) {
return false;
}
break;

View File

@ -244,10 +244,12 @@ import java.util.UUID;
MainUtil.sendMessage(player, C.CLUSTER_INTERSECTION, intersect.getName());
return false;
}
HashSet<Plot> existing =
Set<Plot> existing =
area.getPlotSelectionOwned(cluster.getP1(), cluster.getP2());
HashSet<Plot> newPlots = area.getPlotSelectionOwned(pos1, pos2);
HashSet<Plot> removed = (HashSet<Plot>) existing.clone();
Set<Plot> newPlots = area.getPlotSelectionOwned(pos1, pos2);
// Set<Plot> removed = (HashSet<Plot>) existing.clone();
Set<Plot> removed = new HashSet<>(existing);
removed.removeAll(newPlots);
// Check expand / shrink
if (!removed.isEmpty()) {

View File

@ -12,25 +12,6 @@ import lombok.NonNull;
*/
public class Configuration {
public static final SettingValue<String> STRING = new SettingValue<String>("STRING") {
@Override public boolean validateValue(String string) {
return true;
}
@Override public String parseString(String string) {
return string;
}
};
public static final SettingValue<String[]> STRINGLIST =
new SettingValue<String[]>("STRINGLIST") {
@Override public boolean validateValue(String string) {
return true;
}
@Override public String[] parseString(String string) {
return string.split(",");
}
};
public static final SettingValue<Integer> INTEGER = new SettingValue<Integer>("INTEGER") {
@Override public boolean validateValue(String string) {
try {
@ -47,6 +28,7 @@ public class Configuration {
};
public static final SettingValue<Boolean> BOOLEAN = new SettingValue<Boolean>("BOOLEAN") {
@Override public boolean validateValue(String string) {
//noinspection ResultOfMethodCallIgnored
Boolean.parseBoolean(string);
return true;
}
@ -55,20 +37,6 @@ public class Configuration {
return Boolean.parseBoolean(string);
}
};
public static final SettingValue<Double> DOUBLE = new SettingValue<Double>("DOUBLE") {
@Override public boolean validateValue(String string) {
try {
Double.parseDouble(string);
return true;
} catch (NumberFormatException ignored) {
return false;
}
}
@Override public Double parseString(String string) {
return Double.parseDouble(string);
}
};
public static final SettingValue<String> BIOME = new SettingValue<String>("BIOME") {
@Override public boolean validateValue(String string) {
try {
@ -112,7 +80,8 @@ public class Configuration {
WorldUtil.IMP.getClosestBlock(block);
if (value == null) {
throw new UnknownBlockException(block);
} else if (!value.best.isAir() && !WorldUtil.IMP.isBlockSolid(value.best)) {
} else if (Settings.Enabled_Components.PREVENT_UNSAFE && !value.best.isAir() &&
!WorldUtil.IMP.isBlockSolid(value.best)) {
throw new UnsafeBlockException(value.best);
}
blockBucket.addBlock(value.best, chance);
@ -145,7 +114,7 @@ public class Configuration {
WorldUtil.IMP.getClosestBlock(block);
if (value == null || value.match > 1) {
return false;
} else if (!value.best.isAir() && !WorldUtil.IMP.isBlockSolid(value.best)) {
} else if (Settings.Enabled_Components.PREVENT_UNSAFE && !value.best.isAir() && !WorldUtil.IMP.isBlockSolid(value.best)) {
throw new UnsafeBlockException(value.best);
}
}
@ -156,27 +125,12 @@ public class Configuration {
}
};
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static int gcd(int[] a) {
int result = a[0];
for (int i = 1; i < a.length; i++) {
result = gcd(result, a[i]);
}
return result;
}
public static final class UnknownBlockException extends IllegalArgumentException {
@Getter private final String unknownValue;
public UnknownBlockException(@NonNull final String unknownValue) {
UnknownBlockException(@NonNull final String unknownValue) {
super(String.format("\"%s\" is not a valid block", unknownValue));
this.unknownValue = unknownValue;
}
@ -191,7 +145,7 @@ public class Configuration {
private final String type;
public SettingValue(String type) {
SettingValue(String type) {
this.type = type;
}
@ -209,7 +163,7 @@ public class Configuration {
@Getter private final PlotBlock unsafeBlock;
public UnsafeBlockException(@NonNull final PlotBlock unsafeBlock) {
UnsafeBlockException(@NonNull final PlotBlock unsafeBlock) {
super(String.format("%s is not a valid block", unsafeBlock));
this.unsafeBlock = unsafeBlock;
}

View File

@ -354,5 +354,7 @@ public class Settings extends Config {
@Comment("Actively purge invalid database entries") public static boolean DATABASE_PURGER =
false;
@Comment("Delete plots when a player is banned") public static boolean BAN_DELETER = false;
@Comment("Prevent possibly unsafe blocks from being used in plot components") public static
boolean PREVENT_UNSAFE = true;
}
}

View File

@ -5,7 +5,7 @@ import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue;
import java.util.ArrayList;
import java.util.List;
/**
* A plot manager with square plots which tessellate on a square grid with the following sections: ROAD, WALL, BORDER (wall), PLOT, FLOOR (plot).
@ -424,23 +424,23 @@ public class ClassicPlotManager extends SquarePlotManager {
/**
* Finishing off plot merging by adding in the walls surrounding the plot (OPTIONAL)(UNFINISHED).
*/
@Override public boolean finishPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean finishPlotMerge(PlotArea plotArea, List<PlotId> plotIds) {
final BlockBucket block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK;
plotIds.forEach(id -> setWall(plotArea, id, block));
return true;
}
@Override public boolean finishPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean finishPlotUnlink(PlotArea plotArea, List<PlotId> plotIds) {
final BlockBucket block = ((ClassicPlotWorld) plotArea).CLAIMED_WALL_BLOCK;
plotIds.forEach(id -> setWall(plotArea, id, block));
return true;
}
@Override public boolean startPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean startPlotMerge(PlotArea plotArea, List<PlotId> plotIds) {
return true;
}
@Override public boolean startPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean startPlotUnlink(PlotArea plotArea, List<PlotId> plotIds) {
return true;
}

View File

@ -1,12 +1,19 @@
package com.github.intellectualsites.plotsquared.plot.generator;
import com.github.intellectualsites.plotsquared.configuration.ConfigurationSection;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Configuration;
import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.object.BlockBucket;
import com.github.intellectualsites.plotsquared.plot.object.PlotBlock;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import javax.annotation.Nonnull;
import java.lang.reflect.Field;
import java.util.Locale;
@SuppressWarnings("WeakerAccess")
public abstract class ClassicPlotWorld extends SquarePlotWorld {
public int ROAD_HEIGHT = 62;
@ -38,7 +45,7 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld {
* command - this may be useful if a config value can be changed at a later date, and has no impact on the actual
* world generation</p>
*/
@Override public ConfigurationNode[] getSettingNodes() {
@Nonnull @Override public ConfigurationNode[] getSettingNodes() {
return new ConfigurationNode[] {
new ConfigurationNode("plot.height", this.PLOT_HEIGHT, "Plot height",
Configuration.INTEGER),
@ -84,5 +91,24 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld {
this.WALL_HEIGHT = Math.min(254, config.getInt("wall.height"));
this.CLAIMED_WALL_BLOCK =
Configuration.BLOCK_BUCKET.parseString(config.getString("wall.block_claimed"));
// Dump world settings
if (Settings.DEBUG) {
PlotSquared.debug(String.format("- Dumping settings for ClassicPlotWorld with name %s", this.worldname));
final Field[] fields = this.getClass().getFields();
for (final Field field : fields) {
final String name = field.getName().toLowerCase(Locale.ENGLISH);
Object value;
try {
final boolean accessible = field.isAccessible();
field.setAccessible(true);
value = field.get(this);
field.setAccessible(accessible);
} catch (final IllegalAccessException e) {
value = String.format("Failed to parse: %s", e.getMessage());
}
PlotSquared.debug(String.format("-- %s = %s", name, value));
}
}
}
}

View File

@ -21,6 +21,7 @@ import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableSet;
import com.sk89q.jnbt.CompoundTag;
import javax.annotation.Nullable;
import java.awt.geom.Area;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
@ -2052,7 +2053,7 @@ public class Plot {
* @param player the claiming player
* @return
*/
public boolean canClaim(PlotPlayer player) {
public boolean canClaim(@Nullable PlotPlayer player) {
PlotCluster cluster = this.getCluster();
if (cluster != null && player != null) {
if (!cluster.isAdded(player.getUUID()) && !Permissions

View File

@ -15,13 +15,14 @@ import com.github.intellectualsites.plotsquared.plot.util.area.QuadMap;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Jesse Boyd
* @author Jesse Boyd, Alexander Söderberg
*/
public abstract class PlotArea {
@ -65,8 +66,8 @@ public abstract class PlotArea {
private ConcurrentHashMap<String, Object> meta;
private QuadMap<PlotCluster> clusters;
public PlotArea(String worldName, String id, IndependentPlotGenerator generator, PlotId min,
PlotId max) {
public PlotArea(@Nonnull final String worldName, @Nullable final String id, @Nullable IndependentPlotGenerator generator,
@Nullable final PlotId min, @Nullable final PlotId max) {
this.worldname = worldName;
this.id = id;
this.manager = generator != null ? generator.getNewPlotManager() : null;
@ -88,11 +89,8 @@ public abstract class PlotArea {
/**
* Create a new PlotArea object with no functionality/information.
* - Mainly used during startup before worlds are created as a temporary object
*
* @param world
* @return
*/
public static PlotArea createGeneric(String world) {
public static PlotArea createGeneric(@Nonnull final String world) {
return new PlotArea(world, null, null, null, null) {
@Override public void loadConfiguration(ConfigurationSection config) {
}
@ -103,7 +101,7 @@ public abstract class PlotArea {
};
}
public LocalBlockQueue getQueue(boolean autoQueue) {
public LocalBlockQueue getQueue(final boolean autoQueue) {
return GlobalBlockQueue.IMP.getNewQueue(worldname, autoQueue);
}
@ -127,7 +125,7 @@ public abstract class PlotArea {
*
* @return RegionWrapper or null if no applicable region
*/
public RegionWrapper getRegionAbs() {
private RegionWrapper getRegionAbs() {
if (this.region == null) {
if (this.min != null) {
Location bot = getPlotManager().getPlotBottomLocAbs(this, this.min);
@ -179,7 +177,7 @@ public abstract class PlotArea {
}
public Set<PlotCluster> getClusters() {
return this.clusters == null ? new HashSet<PlotCluster>() : this.clusters.getAll();
return this.clusters == null ? new HashSet<>() : this.clusters.getAll();
}
/**
@ -377,7 +375,7 @@ public abstract class PlotArea {
}
}
@Override public String toString() {
@Nonnull @Override public String toString() {
if (this.id == null) {
return this.worldname;
} else {
@ -405,8 +403,8 @@ public abstract class PlotArea {
* @param location the location
* @return the {@code Plot} or null if none exists
*/
public Plot getPlotAbs(Location location) {
PlotId pid =
@Nullable public Plot getPlotAbs(@Nonnull final Location location) {
final PlotId pid =
this.manager.getPlotId(this, location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
@ -420,8 +418,8 @@ public abstract class PlotArea {
* @param location the location
* @return base Plot
*/
public Plot getPlot(Location location) {
PlotId pid =
@Nullable public Plot getPlot(@Nonnull final Location location) {
final PlotId pid =
this.manager.getPlotId(this, location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
@ -435,8 +433,8 @@ public abstract class PlotArea {
* @param location the location
* @return the base plot or null
*/
public Plot getOwnedPlot(Location location) {
PlotId pid =
@Nullable public Plot getOwnedPlot(@Nonnull final Location location) {
final PlotId pid =
this.manager.getPlotId(this, location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
@ -451,8 +449,8 @@ public abstract class PlotArea {
* @param location the location
* @return Plot or null
*/
public Plot getOwnedPlotAbs(Location location) {
PlotId pid =
@Nullable public Plot getOwnedPlotAbs(@Nonnull final Location location) {
final PlotId pid =
this.manager.getPlotId(this, location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
@ -466,30 +464,30 @@ public abstract class PlotArea {
* @param id the {@code PlotId}
* @return the plot or null
*/
public Plot getOwnedPlotAbs(PlotId id) {
@Nullable public Plot getOwnedPlotAbs(@Nonnull final PlotId id) {
return this.plots.get(id);
}
public Plot getOwnedPlot(PlotId id) {
@Nullable public Plot getOwnedPlot(@Nonnull final PlotId id) {
Plot plot = this.plots.get(id);
return plot == null ? null : plot.getBasePlot(false);
}
public boolean contains(int x, int z) {
public boolean contains(final int x, final int z) {
return this.TYPE != 2 || getRegionAbs().isIn(x, z);
}
public boolean contains(PlotId id) {
public boolean contains(@Nonnull final PlotId id) {
return this.min == null || (id.x >= this.min.x && id.x <= this.max.x && id.y >= this.min.y
&& id.y <= this.max.y);
}
public boolean contains(Location location) {
public boolean contains(@Nonnull final Location location) {
return StringMan.isEqual(location.getWorld(), this.worldname) && (getRegionAbs() == null
|| this.region.isIn(location.getX(), location.getZ()));
}
public Set<Plot> getPlotsAbs(final UUID uuid) {
@Nonnull Set<Plot> getPlotsAbs(final UUID uuid) {
if (uuid == null) {
return Collections.emptySet();
}
@ -504,19 +502,17 @@ public abstract class PlotArea {
return myPlots;
}
public Set<Plot> getPlots(UUID uuid) {
HashSet<Plot> myplots = new HashSet<>();
for (Plot plot : getPlots()) {
if (plot.isBasePlot()) {
if (plot.isOwner(uuid)) {
@Nonnull public Set<Plot> getPlots(@Nonnull final UUID uuid) {
final Set<Plot> myplots = new HashSet<>();
for (final Plot plot : getPlots()) {
if (plot.isBasePlot() && plot.isOwner(uuid)) {
myplots.add(plot);
}
}
}
return myplots;
}
public Set<Plot> getPlots(PlotPlayer player) {
public Set<Plot> getPlots(@Nonnull final PlotPlayer player) {
return getPlots(player.getUUID());
}
@ -529,11 +525,7 @@ public abstract class PlotArea {
return this.plots.values();
}
public Set<Plot> getPlotsAbs(PlotPlayer player) {
return player != null ? getPlotsAbs(player.getUUID()) : new HashSet<Plot>();
}
public int getPlotCount(UUID uuid) {
public int getPlotCount(@Nonnull final UUID uuid) {
if (!Settings.Done.COUNTS_TOWARDS_LIMIT) {
int count = 0;
for (Plot plot : getPlotsAbs(uuid)) {
@ -546,7 +538,7 @@ public abstract class PlotArea {
return getPlotsAbs(uuid).size();
}
public boolean hasPlot(UUID uuid) {
public boolean hasPlot(@Nonnull final UUID uuid) {
for (Entry<PlotId, Plot> entry : this.plots.entrySet()) {
if (entry.getValue().isOwner(uuid))
return true;
@ -554,11 +546,11 @@ public abstract class PlotArea {
return false;
}
public int getPlotCount(PlotPlayer player) {
public int getPlotCount(@Nullable final PlotPlayer player) {
return player != null ? getPlotCount(player.getUUID()) : 0;
}
public Plot getPlotAbs(PlotId id) {
@Nullable public Plot getPlotAbs(@Nonnull final PlotId id) {
Plot plot = getOwnedPlotAbs(id);
if (plot == null) {
if (this.min != null && (id.x < this.min.x || id.x > this.max.x || id.y < this.min.y
@ -570,8 +562,8 @@ public abstract class PlotArea {
return plot;
}
public Plot getPlot(PlotId id) {
Plot plot = getOwnedPlotAbs(id);
@Nullable public Plot getPlot(@Nonnull final PlotId id) {
final Plot plot = getOwnedPlotAbs(id);
if (plot == null) {
if (this.min != null && (id.x < this.min.x || id.x > this.max.x || id.y < this.min.y
|| id.y > this.max.y)) {
@ -591,15 +583,15 @@ public abstract class PlotArea {
return this.plots.size();
}
public PlotCluster getCluster(Location location) {
Plot plot = getPlot(location);
@Nullable public PlotCluster getCluster(@Nonnull final Location location) {
final Plot plot = getPlot(location);
if (plot == null) {
return null;
}
return this.clusters != null ? this.clusters.get(plot.getId().x, plot.getId().y) : null;
}
public PlotCluster getFirstIntersectingCluster(PlotId pos1, PlotId pos2) {
@Nullable public PlotCluster getFirstIntersectingCluster(@Nonnull final PlotId pos1, @Nonnull final PlotId pos2) {
if (this.clusters == null) {
return null;
}
@ -611,7 +603,7 @@ public abstract class PlotArea {
return null;
}
public PlotCluster getCluster(PlotId id) {
@Nullable PlotCluster getCluster(@Nonnull final PlotId id) {
return this.clusters != null ? this.clusters.get(id.x, id.y) : null;
}
@ -623,20 +615,17 @@ public abstract class PlotArea {
* Session only plot metadata (session is until the server stops).
* <br>
* For persistent metadata use the flag system
*
* @param key
* @param value
* @see FlagManager
*/
public void setMeta(String key, Object value) {
public void setMeta(@Nonnull final String key, @Nullable final Object value) {
if (this.meta == null) {
this.meta = new ConcurrentHashMap<>();
}
this.meta.put(key, value);
}
public <T> T getMeta(String key, T def) {
Object v = getMeta(key);
@Nullable public <T> T getMeta(@Nullable final String key, @Nullable final T def) {
final Object v = getMeta(key);
return v == null ? def : (T) v;
}
@ -644,59 +633,50 @@ public abstract class PlotArea {
* Get the metadata for a key<br>
* <br>
* For persistent metadata use the flag system
*
* @param key
* @return
*/
public Object getMeta(String key) {
@Nullable public Object getMeta(@Nonnull final String key) {
if (this.meta != null) {
return this.meta.get(key);
}
return null;
}
public Set<Plot> getBasePlots() {
HashSet<Plot> myPlots = new HashSet<>(getPlots());
Iterator<Plot> iterator = myPlots.iterator();
while (iterator.hasNext()) {
if (!iterator.next().isBasePlot()) {
iterator.remove();
}
}
@SuppressWarnings("unused") @Nonnull public Set<Plot> getBasePlots() {
final HashSet<Plot> myPlots = new HashSet<>(getPlots());
myPlots.removeIf(plot -> !plot.isBasePlot());
return myPlots;
}
public void foreachPlotAbs(RunnableVal<Plot> run) {
for (Entry<PlotId, Plot> entry : this.plots.entrySet()) {
private void foreachPlotAbs(@Nonnull final RunnableVal<Plot> run) {
for (final Entry<PlotId, Plot> entry : this.plots.entrySet()) {
run.run(entry.getValue());
}
}
public void foreachBasePlot(RunnableVal<Plot> run) {
for (Plot plot : getPlots()) {
public void foreachBasePlot(@Nonnull final RunnableVal<Plot> run) {
for (final Plot plot : getPlots()) {
if (plot.isBasePlot()) {
run.run(plot);
}
}
}
public Map<PlotId, Plot> getPlotsRaw() {
@Nonnull public Map<PlotId, Plot> getPlotsRaw() {
return this.plots;
}
public Set<Entry<PlotId, Plot>> getPlotEntries() {
@Nonnull public Set<Entry<PlotId, Plot>> getPlotEntries() {
return this.plots.entrySet();
}
public boolean addPlot(Plot plot) {
public boolean addPlot(@Nonnull final Plot plot) {
for (PlotPlayer pp : plot.getPlayersInPlot()) {
pp.setMeta(PlotPlayer.META_LAST_PLOT, plot);
}
return this.plots.put(plot.getId(), plot) == null;
}
public Plot getNextFreePlot(PlotPlayer player, @Nullable PlotId start) {
public Plot getNextFreePlot(final PlotPlayer player, @Nullable PlotId start) {
int plots;
PlotId center;
PlotId min = getMin();
@ -710,7 +690,7 @@ public abstract class PlotArea {
center = new PlotId(0, 0);
plots = Integer.MAX_VALUE;
}
PlotId currentId = new PlotId(0, 0);
PlotId currentId;
for (int i = 0; i < plots; i++) {
if (start == null) {
start = getMeta("lastPlot", new PlotId(0, 0));
@ -727,7 +707,7 @@ public abstract class PlotArea {
return null;
}
public boolean addPlotIfAbsent(Plot plot) {
public boolean addPlotIfAbsent(@Nonnull final Plot plot) {
if (this.plots.putIfAbsent(plot.getId(), plot) == null) {
for (PlotPlayer pp : plot.getPlayersInPlot()) {
pp.setMeta(PlotPlayer.META_LAST_PLOT, plot);
@ -737,46 +717,17 @@ public abstract class PlotArea {
return false;
}
public boolean addPlotAbs(Plot plot) {
public boolean addPlotAbs(@Nonnull final Plot plot) {
return this.plots.put(plot.getId(), plot) == null;
}
/**
* Check if the plots in a selection are unowned.
*
* @param pos1 first corner of selection
* @param pos2 second corner of selection
* @return are plots in selection unowned
*/
public boolean isUnowned(PlotId pos1, PlotId pos2) {
int area = (pos2.x - pos1.x + 1) * (pos2.y - pos1.y + 1);
if (area > getPlotCount()) {
for (Plot plot : getPlots()) {
if (plot.getId().x >= pos1.x && plot.getId().x <= pos2.x && plot.getId().y >= pos1.y
&& plot.getId().y <= pos2.y) {
return false;
}
}
} else {
for (int x = pos1.x; x <= pos2.x; x++) {
for (int y = pos1.y; y <= pos2.y; y++) {
PlotId id = new PlotId(x, y);
if (this.plots.get(id) != null) {
return false;
}
}
}
}
return true;
}
/**
* Get the plot border distance for a world<br>
*
* @return The border distance or Integer.MAX_VALUE if no border is set
*/
public int getBorder() {
Integer meta = (Integer) getMeta("worldBorder");
final Integer meta = (Integer) getMeta("worldBorder");
if (meta != null) {
int border = meta + 1;
if (border == 0) {
@ -795,11 +746,11 @@ public abstract class PlotArea {
if (!this.WORLD_BORDER) {
return;
}
Integer meta = (Integer) getMeta("worldBorder");
final Integer meta = (Integer) getMeta("worldBorder");
if (meta == null) {
setMeta("worldBorder", 1);
}
for (Plot plot : getPlots()) {
for (final Plot plot : getPlots()) {
plot.updateWorldBorder();
}
}
@ -809,30 +760,33 @@ public abstract class PlotArea {
* - metadata is session only
* - deleting other plugin's metadata may cause issues
*
* @param key
* @param key Meta data key
*/
public void deleteMeta(String key) {
public void deleteMeta(@Nonnull final String key) {
if (this.meta != null) {
this.meta.remove(key);
}
}
public boolean canClaim(PlotPlayer player, PlotId pos1, PlotId pos2) {
public boolean canClaim(@Nullable final PlotPlayer player, @Nonnull final PlotId pos1,
@Nonnull final PlotId pos2) {
if (pos1.x == pos2.x && pos1.y == pos2.y) {
if (getOwnedPlot(pos1) != null) {
return false;
}
Plot plot = getPlotAbs(pos1);
if (plot == null)
final Plot plot = getPlotAbs(pos1);
if (plot == null) {
return false;
}
return plot.canClaim(player);
}
for (int x = pos1.x; x <= pos2.x; x++) {
for (int y = pos1.y; y <= pos2.y; y++) {
PlotId id = new PlotId(x, y);
Plot plot = getPlotAbs(id);
if (plot == null)
final PlotId id = new PlotId(x, y);
final Plot plot = getPlotAbs(id);
if (plot == null) {
return false;
}
if (!plot.canClaim(player)) {
return false;
}
@ -841,27 +795,27 @@ public abstract class PlotArea {
return true;
}
public boolean removePlot(PlotId id) {
public boolean removePlot(@Nonnull final PlotId id) {
return this.plots.remove(id) != null;
}
public boolean mergePlots(ArrayList<PlotId> plotIds, boolean removeRoads,
boolean updateDatabase) {
public boolean mergePlots(@Nonnull final List<PlotId> plotIds, final boolean removeRoads) {
if (plotIds.size() < 2) {
return false;
}
PlotId pos1 = plotIds.get(0);
PlotId pos2 = plotIds.get(plotIds.size() - 1);
PlotManager manager = getPlotManager();
boolean result = EventUtil.manager.callMerge(getPlotAbs(pos1), plotIds);
final PlotId pos1 = plotIds.get(0);
final PlotId pos2 = plotIds.get(plotIds.size() - 1);
final PlotManager manager = getPlotManager();
final boolean result = EventUtil.manager.callMerge(getPlotAbs(pos1), plotIds);
if (!result) {
return false;
}
HashSet<UUID> trusted = new HashSet<>();
HashSet<UUID> members = new HashSet<>();
HashSet<UUID> denied = new HashSet<>();
final Set<UUID> trusted = new HashSet<>();
final Set<UUID> members = new HashSet<>();
final Set<UUID> denied = new HashSet<>();
manager.startPlotMerge(this, plotIds);
for (int x = pos1.x; x <= pos2.x; x++) {
@ -881,13 +835,15 @@ public abstract class PlotArea {
denied.removeAll(members);
for (int x = pos1.x; x <= pos2.x; x++) {
for (int y = pos1.y; y <= pos2.y; y++) {
boolean lx = x < pos2.x;
boolean ly = y < pos2.y;
PlotId id = new PlotId(x, y);
Plot plot = getPlotAbs(id);
final boolean lx = x < pos2.x;
final boolean ly = y < pos2.y;
final PlotId id = new PlotId(x, y);
final Plot plot = getPlotAbs(id);
plot.setTrusted(trusted);
plot.setMembers(members);
plot.setDenied(denied);
Plot plot2;
if (lx) {
if (ly) {
@ -922,12 +878,12 @@ public abstract class PlotArea {
* @param pos2 second corner of selection
* @return the plots in the selection which are owned
*/
public HashSet<Plot> getPlotSelectionOwned(PlotId pos1, PlotId pos2) {
int size = (1 + pos2.x - pos1.x) * (1 + pos2.y - pos1.y);
HashSet<Plot> result = new HashSet<>();
public Set<Plot> getPlotSelectionOwned(@Nonnull final PlotId pos1, @Nonnull final PlotId pos2) {
final int size = (1 + pos2.x - pos1.x) * (1 + pos2.y - pos1.y);
final Set<Plot> result = new HashSet<>();
if (size < 16 || size < getPlotCount()) {
for (PlotId pid : MainUtil.getPlotSelectionIds(pos1, pos2)) {
Plot plot = getPlotAbs(pid);
for (final PlotId pid : MainUtil.getPlotSelectionIds(pos1, pos2)) {
final Plot plot = getPlotAbs(pid);
if (plot.hasOwner()) {
if (plot.getId().x > pos1.x || plot.getId().y > pos1.y
|| plot.getId().x < pos2.x || plot.getId().y < pos2.y) {
@ -936,7 +892,7 @@ public abstract class PlotArea {
}
}
} else {
for (Plot plot : getPlots()) {
for (final Plot plot : getPlots()) {
if (plot.getId().x > pos1.x || plot.getId().y > pos1.y || plot.getId().x < pos2.x
|| plot.getId().y < pos2.y) {
result.add(plot);
@ -946,14 +902,15 @@ public abstract class PlotArea {
return result;
}
public void removeCluster(PlotCluster plotCluster) {
@SuppressWarnings("WeakerAccess")
public void removeCluster(@Nullable final PlotCluster plotCluster) {
if (this.clusters == null) {
throw new IllegalAccessError("Clusters not enabled!");
}
this.clusters.remove(plotCluster);
}
public void addCluster(PlotCluster plotCluster) {
public void addCluster(@Nullable final PlotCluster plotCluster) {
if (this.clusters == null) {
this.clusters = new QuadMap<PlotCluster>(Integer.MAX_VALUE, 0, 0, 62) {
@Override public RegionWrapper getRegion(PlotCluster value) {
@ -965,7 +922,7 @@ public abstract class PlotArea {
this.clusters.add(plotCluster);
}
public PlotCluster getCluster(String string) {
@Nullable public PlotCluster getCluster(final String string) {
for (PlotCluster cluster : getClusters()) {
if (cluster.getName().equalsIgnoreCase(string)) {
return cluster;
@ -973,4 +930,5 @@ public abstract class PlotArea {
}
return null;
}
}

View File

@ -4,9 +4,9 @@ import com.github.intellectualsites.plotsquared.plot.commands.Template;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public abstract class PlotManager {
@ -60,13 +60,13 @@ public abstract class PlotManager {
public abstract boolean removeRoadSouthEast(PlotArea plotArea, Plot plot);
public abstract boolean startPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds);
public abstract boolean startPlotMerge(PlotArea plotArea, List<PlotId> plotIds);
public abstract boolean startPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds);
public abstract boolean startPlotUnlink(PlotArea plotArea, List<PlotId> plotIds);
public abstract boolean finishPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds);
public abstract boolean finishPlotMerge(PlotArea plotArea, List<PlotId> plotIds);
public abstract boolean finishPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds);
public abstract boolean finishPlotUnlink(PlotArea plotArea, List<PlotId> plotIds);
public void exportTemplate(PlotArea plotArea) throws IOException {
HashSet<FileBytes> files = new HashSet<>(Collections.singletonList(

View File

@ -9,6 +9,9 @@ import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class SinglePlotArea extends GridPlotWorld {
public boolean VOID = false;
@ -58,7 +61,7 @@ public class SinglePlotArea extends GridPlotWorld {
new ConfigurationNode("void", this.VOID, "Void world", Configuration.BOOLEAN)};
}
@Override public Plot getOwnedPlot(Location location) {
@Nullable @Override public Plot getOwnedPlot(@Nonnull final Location location) {
PlotId pid = PlotId.fromString(location.getWorld());
if (pid == null) {
return null;
@ -67,7 +70,7 @@ public class SinglePlotArea extends GridPlotWorld {
return plot == null ? null : plot.getBasePlot(false);
}
@Override public Plot getOwnedPlotAbs(Location location) {
@Nullable @Override public Plot getOwnedPlotAbs(@Nonnull Location location) {
PlotId pid = PlotId.fromString(location.getWorld());
if (pid == null) {
return null;
@ -75,7 +78,7 @@ public class SinglePlotArea extends GridPlotWorld {
return plots.get(pid);
}
@Override public Plot getPlot(Location location) {
@Nullable @Override public Plot getPlot(@Nonnull final Location location) {
PlotId pid = PlotId.fromString(location.getWorld());
if (pid == null) {
return null;
@ -83,30 +86,30 @@ public class SinglePlotArea extends GridPlotWorld {
return getPlot(pid);
}
@Override public Plot getPlotAbs(Location location) {
PlotId pid = PlotId.fromString(location.getWorld());
@Nullable @Override public Plot getPlotAbs(@Nonnull final Location location) {
final PlotId pid = PlotId.fromString(location.getWorld());
if (pid == null) {
return null;
}
return getPlotAbs(pid);
}
public boolean addPlot(Plot plot) {
public boolean addPlot(@Nonnull Plot plot) {
plot = adapt(plot);
return super.addPlot(plot);
}
@Override public boolean addPlotAbs(Plot plot) {
@Override public boolean addPlotAbs(@Nonnull Plot plot) {
plot = adapt(plot);
return super.addPlotAbs(plot);
}
@Override public boolean addPlotIfAbsent(Plot plot) {
@Override public boolean addPlotIfAbsent(@Nonnull Plot plot) {
plot = adapt(plot);
return super.addPlotIfAbsent(plot);
}
protected Plot adapt(Plot p) {
private Plot adapt(Plot p) {
if (p instanceof SinglePlot) {
return p;
}
@ -117,7 +120,7 @@ public class SinglePlotArea extends GridPlotWorld {
return p;
}
public Plot getPlotAbs(PlotId id) {
@Nullable public Plot getPlotAbs(@Nonnull final PlotId id) {
Plot plot = getOwnedPlotAbs(id);
if (plot == null) {
return new SinglePlot(this, id);
@ -125,7 +128,7 @@ public class SinglePlotArea extends GridPlotWorld {
return plot;
}
public Plot getPlot(PlotId id) {
@Nullable public Plot getPlot(@Nonnull PlotId id) {
// TODO
Plot plot = getOwnedPlotAbs(id);
if (plot == null) {

View File

@ -7,7 +7,7 @@ import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class SinglePlotManager extends PlotManager {
@Override public PlotId getPlotIdAbs(PlotArea plotArea, int x, int y, int z) {
@ -88,19 +88,19 @@ public class SinglePlotManager extends PlotManager {
return false;
}
@Override public boolean startPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean startPlotMerge(PlotArea plotArea, List<PlotId> plotIds) {
return false;
}
@Override public boolean startPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean startPlotUnlink(PlotArea plotArea, List<PlotId> plotIds) {
return false;
}
@Override public boolean finishPlotMerge(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean finishPlotMerge(PlotArea plotArea, List<PlotId> plotIds) {
return false;
}
@Override public boolean finishPlotUnlink(PlotArea plotArea, ArrayList<PlotId> plotIds) {
@Override public boolean finishPlotUnlink(PlotArea plotArea, List<PlotId> plotIds) {
return false;
}
}

View File

@ -11,8 +11,8 @@ import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Optional;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
public abstract class EventUtil {
@ -45,9 +45,9 @@ public abstract class EventUtil {
public abstract boolean callFlagRemove(Flag<?> flag, Object value, PlotCluster cluster);
public abstract boolean callMerge(Plot plot, ArrayList<PlotId> plots);
public abstract boolean callMerge(Plot plot, List<PlotId> plots);
public abstract boolean callUnlink(PlotArea area, ArrayList<PlotId> plots);
public abstract boolean callUnlink(PlotArea area, List<PlotId> plots);
public abstract void callEntry(PlotPlayer player, Plot plot);
@ -90,11 +90,7 @@ public abstract class EventUtil {
public void doRespawnTask(final PlotPlayer player) {
final Plot plot = player.getCurrentPlot();
if (Settings.Teleport.ON_DEATH && plot != null) {
TaskManager.runTask(new Runnable() {
@Override public void run() {
plot.teleportPlayer(player);
}
});
TaskManager.runTask(() -> plot.teleportPlayer(player));
MainUtil.sendMessage(player, C.TELEPORTED_TO_ROAD);
}
}

View File

@ -3,7 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.util;
import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.object.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class EventUtilTest extends EventUtil {
@ -43,11 +43,11 @@ public class EventUtilTest extends EventUtil {
return true;
}
@Override public boolean callMerge(Plot plot, ArrayList<PlotId> plots) {
@Override public boolean callMerge(Plot plot, List<PlotId> plots) {
return false;
}
@Override public boolean callUnlink(PlotArea area, ArrayList<PlotId> plots) {
@Override public boolean callUnlink(PlotArea area, List<PlotId> plots) {
return false;
}