2020-04-16 04:52:39 +02:00
|
|
|
/*
|
2022-06-15 20:17:17 +02:00
|
|
|
* PlotSquared, a land and world management plugin for Minecraft.
|
|
|
|
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
|
|
|
* Copyright (C) IntellectualSites team and contributors
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-16 04:52:39 +02:00
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.core.plot;
|
2015-01-26 05:16:10 +01:00
|
|
|
|
2020-07-19 14:23:55 +02:00
|
|
|
import com.plotsquared.core.PlotSquared;
|
2020-04-30 12:01:52 +02:00
|
|
|
import com.plotsquared.core.database.DBFunc;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.location.BlockLoc;
|
|
|
|
import com.plotsquared.core.location.Location;
|
|
|
|
import com.plotsquared.core.util.RegionUtil;
|
2019-11-04 22:08:33 +01:00
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
2021-01-09 22:28:41 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2020-07-19 14:23:55 +02:00
|
|
|
|
2016-03-14 07:18:04 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.UUID;
|
2020-04-07 20:19:39 +02:00
|
|
|
import java.util.function.Consumer;
|
2016-03-14 07:18:04 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class PlotCluster {
|
2021-01-09 22:28:41 +01:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public PlotArea area;
|
2019-02-04 16:18:50 +01:00
|
|
|
public PlotSettings settings;
|
2015-02-20 07:34:19 +01:00
|
|
|
public UUID owner;
|
2016-03-14 07:18:04 +01:00
|
|
|
public HashSet<UUID> helpers = new HashSet<>();
|
|
|
|
public HashSet<UUID> invited = new HashSet<>();
|
|
|
|
public int temp;
|
2019-02-04 16:18:50 +01:00
|
|
|
private PlotId pos1;
|
|
|
|
private PlotId pos2;
|
2019-11-04 22:08:33 +01:00
|
|
|
private CuboidRegion region;
|
2015-10-17 13:50:52 +02:00
|
|
|
|
2019-02-04 16:18:50 +01:00
|
|
|
public PlotCluster(PlotArea area, PlotId pos1, PlotId pos2, UUID owner) {
|
2016-02-10 19:59:51 +01:00
|
|
|
this.area = area;
|
2015-02-20 07:34:19 +01:00
|
|
|
this.pos1 = pos1;
|
|
|
|
this.pos2 = pos2;
|
|
|
|
this.owner = owner;
|
2016-03-29 06:56:44 +02:00
|
|
|
this.settings = new PlotSettings();
|
2015-10-17 13:50:52 +02:00
|
|
|
this.temp = -1;
|
2016-02-10 19:59:51 +01:00
|
|
|
setRegion();
|
2015-01-29 07:02:13 +01:00
|
|
|
}
|
2016-03-29 06:56:44 +02:00
|
|
|
|
2019-02-04 16:18:50 +01:00
|
|
|
public PlotCluster(PlotArea area, PlotId pos1, PlotId pos2, UUID owner, int temp) {
|
2016-02-10 19:59:51 +01:00
|
|
|
this.area = area;
|
2015-10-17 13:50:52 +02:00
|
|
|
this.pos1 = pos1;
|
|
|
|
this.pos2 = pos2;
|
|
|
|
this.owner = owner;
|
2016-03-29 06:56:44 +02:00
|
|
|
this.settings = new PlotSettings();
|
2015-10-17 13:50:52 +02:00
|
|
|
this.temp = temp;
|
2016-02-10 19:59:51 +01:00
|
|
|
setRegion();
|
|
|
|
}
|
2016-03-14 07:18:04 +01:00
|
|
|
|
|
|
|
public PlotId getP1() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.pos1;
|
2016-03-14 07:18:04 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:56:44 +02:00
|
|
|
public void setP1(PlotId id) {
|
|
|
|
this.pos1 = id;
|
2016-03-14 07:18:04 +01:00
|
|
|
setRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlotId getP2() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.pos2;
|
2016-03-14 07:18:04 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:56:44 +02:00
|
|
|
public void setP2(PlotId id) {
|
|
|
|
this.pos2 = id;
|
2016-03-14 07:18:04 +01:00
|
|
|
setRegion();
|
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
private void setRegion() {
|
2022-03-05 19:03:39 +01:00
|
|
|
this.region = RegionUtil.createRegion(this.pos1.getX(), this.pos2.getX(), 0, 0,
|
2021-01-09 22:28:41 +01:00
|
|
|
this.pos1.getY(), this.pos2.getY()
|
|
|
|
);
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2022-03-05 19:03:39 +01:00
|
|
|
/**
|
|
|
|
* Returns a region of PlotIDs
|
|
|
|
*
|
|
|
|
* @deprecated - returns region of IDs, not of actual blocks.
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2019-11-04 22:08:33 +01:00
|
|
|
public CuboidRegion getRegion() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.region;
|
2015-10-17 13:50:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 10:42:05 +02:00
|
|
|
public boolean isOwner(UUID uuid) {
|
|
|
|
return uuid.equals(owner);
|
|
|
|
}
|
|
|
|
|
2016-03-29 06:56:44 +02:00
|
|
|
public boolean isAdded(UUID uuid) {
|
2018-08-10 17:01:10 +02:00
|
|
|
return this.owner.equals(uuid) || this.invited.contains(uuid) || this.invited
|
2021-01-09 22:28:41 +01:00
|
|
|
.contains(DBFunc.EVERYONE) || this.helpers.contains(uuid) || this.helpers
|
|
|
|
.contains(DBFunc.EVERYONE);
|
2015-01-29 07:02:13 +01:00
|
|
|
}
|
2016-03-29 06:56:44 +02:00
|
|
|
|
|
|
|
public boolean hasHelperRights(UUID uuid) {
|
2018-08-10 17:01:10 +02:00
|
|
|
return this.owner.equals(uuid) || this.helpers.contains(uuid) || this.helpers
|
2021-01-09 22:28:41 +01:00
|
|
|
.contains(DBFunc.EVERYONE);
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public String getName() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.settings.getAlias();
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2015-08-28 08:28:55 +02:00
|
|
|
/**
|
2016-03-29 06:56:44 +02:00
|
|
|
* Get the area (in plots).
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @return area of plots
|
2015-08-28 08:28:55 +02:00
|
|
|
*/
|
2015-09-13 06:04:31 +02:00
|
|
|
public int getArea() {
|
2020-07-18 11:05:16 +02:00
|
|
|
return (1 + this.pos2.getX() - this.pos1.getX()) * (1 + this.pos2.getY() - this.pos1.getY());
|
2015-08-28 08:28:55 +02:00
|
|
|
}
|
2016-03-14 07:18:04 +01:00
|
|
|
|
2016-06-02 17:38:47 +02:00
|
|
|
public void setArea(PlotArea plotArea) {
|
2016-03-14 07:18:04 +01:00
|
|
|
if (this.area != null) {
|
|
|
|
this.area.removeCluster(this);
|
|
|
|
}
|
2016-06-02 17:38:47 +02:00
|
|
|
this.area = plotArea;
|
|
|
|
plotArea.addCluster(this);
|
2016-03-14 07:18:04 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.pos1.hashCode();
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 06:56:44 +02:00
|
|
|
PlotCluster other = (PlotCluster) obj;
|
2018-08-10 17:01:10 +02:00
|
|
|
return this.pos1.equals(other.pos1) && this.pos2.equals(other.pos2) && this.area
|
2021-01-09 22:28:41 +01:00
|
|
|
.equals(other.area);
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2020-07-18 11:05:16 +02:00
|
|
|
return this.area + ";" + this.pos1.toString() + ";" + this.pos2.toString();
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
public void getHome(final @NonNull Consumer<Location> result) {
|
2020-07-10 12:21:29 +02:00
|
|
|
final BlockLoc home = this.settings.getPosition();
|
|
|
|
Consumer<Location> locationConsumer = toReturn ->
|
2021-01-09 22:28:41 +01:00
|
|
|
PlotSquared.platform().worldUtil().getHighestBlock(this.area.getWorldName(), toReturn.getX(), toReturn.getZ(),
|
|
|
|
highest -> {
|
2022-03-05 19:03:39 +01:00
|
|
|
if (highest <= area.getMinBuildHeight()) {
|
2021-01-09 22:28:41 +01:00
|
|
|
highest = 63;
|
|
|
|
}
|
|
|
|
if (highest > toReturn.getY()) {
|
|
|
|
result.accept(toReturn.withY(1 + highest));
|
|
|
|
} else {
|
|
|
|
result.accept(toReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-03-05 19:03:39 +01:00
|
|
|
if (home.getY() == Integer.MIN_VALUE) {
|
2016-02-10 19:59:51 +01:00
|
|
|
// default pos
|
2016-03-29 06:56:44 +02:00
|
|
|
Plot center = getCenterPlot();
|
2020-04-07 20:19:39 +02:00
|
|
|
center.getHome(location -> {
|
|
|
|
Location toReturn = location;
|
2022-03-05 19:03:39 +01:00
|
|
|
if (toReturn.getY() <= area.getMinBuildHeight()) {
|
2020-04-07 20:19:39 +02:00
|
|
|
PlotManager manager = this.area.getPlotManager();
|
|
|
|
Location locationSign = manager.getSignLoc(center);
|
2020-07-10 12:21:29 +02:00
|
|
|
toReturn = toReturn.withY(locationSign.getY());
|
2020-04-07 20:19:39 +02:00
|
|
|
}
|
|
|
|
locationConsumer.accept(toReturn);
|
|
|
|
});
|
2016-02-10 19:59:51 +01:00
|
|
|
} else {
|
2020-04-07 20:19:39 +02:00
|
|
|
locationConsumer.accept(getClusterBottom().add(home.getX(), home.getY(), home.getZ()));
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull PlotId getCenterPlotId() {
|
2020-07-18 11:05:16 +02:00
|
|
|
final PlotId bot = getP1();
|
|
|
|
final PlotId top = getP2();
|
|
|
|
return PlotId.of((bot.getX() + top.getX()) / 2, (bot.getY() + top.getY()) / 2);
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-03-14 07:18:04 +01:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
public @Nullable Plot getCenterPlot() {
|
2016-03-29 06:56:44 +02:00
|
|
|
return this.area.getPlotAbs(getCenterPlotId());
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Location getClusterBottom() {
|
2016-03-29 06:56:44 +02:00
|
|
|
PlotManager manager = this.area.getPlotManager();
|
2019-05-31 15:05:37 +02:00
|
|
|
return manager.getPlotBottomLocAbs(getP1());
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public Location getClusterTop() {
|
2016-03-29 06:56:44 +02:00
|
|
|
PlotManager manager = this.area.getPlotManager();
|
2019-05-31 15:05:37 +02:00
|
|
|
return manager.getPlotTopLocAbs(getP2());
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2016-02-10 19:59:51 +01:00
|
|
|
public boolean intersects(PlotId pos1, PlotId pos2) {
|
2020-07-18 11:05:16 +02:00
|
|
|
return pos1.getX() <= this.pos2.getX() && pos2.getX() >= this.pos1.getX() &&
|
2021-01-09 22:28:41 +01:00
|
|
|
pos1.getY() <= this.pos2.getY() && pos2.getY() >= this.pos1.getY();
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2016-03-29 06:56:44 +02:00
|
|
|
|
|
|
|
public boolean contains(PlotId id) {
|
2020-07-18 11:05:16 +02:00
|
|
|
return this.pos1.getX() <= id.getX() && this.pos1.getY() <= id.getY() &&
|
2021-01-09 22:28:41 +01:00
|
|
|
this.pos2.getX() >= id.getX() && this.pos2.getY() >= id.getY();
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2021-01-09 22:28:41 +01:00
|
|
|
|
2015-01-26 05:16:10 +01:00
|
|
|
}
|