2021-04-18 09:10:30 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of the SteamWar software.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 SteamWar.de-Serverteam
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.steamwar.bausystem.region;
|
|
|
|
|
2021-04-18 17:34:02 +02:00
|
|
|
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
|
|
|
import de.steamwar.bausystem.region.utils.RegionType;
|
2021-04-18 15:48:15 +02:00
|
|
|
import lombok.Getter;
|
2021-04-18 17:34:02 +02:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import yapion.hierarchy.types.YAPIONObject;
|
|
|
|
import yapion.hierarchy.types.YAPIONType;
|
|
|
|
import yapion.hierarchy.types.YAPIONValue;
|
2021-04-18 15:48:15 +02:00
|
|
|
|
2021-04-18 17:34:02 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2021-04-18 15:34:31 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
2021-04-18 15:48:15 +02:00
|
|
|
@Getter
|
2021-04-18 09:10:30 +02:00
|
|
|
public class Region {
|
2021-04-18 15:34:31 +02:00
|
|
|
|
2021-04-18 17:34:02 +02:00
|
|
|
private static final List<Region> REGION_LIST = new ArrayList<>();
|
|
|
|
|
|
|
|
public static Region getRegion(Location location) {
|
|
|
|
for (Region region : REGION_LIST) {
|
|
|
|
if (region.inRegion(location, RegionType.NORMAL, RegionExtensionType.NORMAL)) {
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GlobalRegion.instance;
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:34:31 +02:00
|
|
|
private Prototype prototype;
|
2021-04-18 17:34:02 +02:00
|
|
|
private Set<Prototype> prototypes;
|
2021-04-18 15:34:31 +02:00
|
|
|
|
|
|
|
private Point minPoint;
|
|
|
|
private Point maxPoint;
|
|
|
|
|
|
|
|
private Point minPointTestblock;
|
|
|
|
private Point maxPointTestblock;
|
|
|
|
|
|
|
|
private Point minPointTestblockExtension;
|
|
|
|
private Point maxPointTestblockExtension;
|
|
|
|
|
|
|
|
private Point minPointBuild;
|
|
|
|
private Point maxPointBuild;
|
|
|
|
|
|
|
|
private Point minPointBuildExtension;
|
|
|
|
private Point maxPointBuildExtension;
|
|
|
|
|
|
|
|
private Region linkedRegion = null; // Nullable
|
|
|
|
|
|
|
|
private FlagStorage flagStorage;
|
|
|
|
|
2021-04-18 17:34:02 +02:00
|
|
|
public Region(Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage) {
|
|
|
|
if (prototype != null) {
|
|
|
|
REGION_LIST.add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
prototypes = new HashSet<>();
|
|
|
|
if (regionConfig.containsKey("prototypes", YAPIONType.ARRAY)) {
|
|
|
|
regionConfig.getArray("prototypes").forEach(yapionAnyType -> {
|
|
|
|
if (yapionAnyType instanceof YAPIONValue) {
|
|
|
|
prototypes.add(Prototype.PROTOTYPE_MAP.get(((YAPIONValue<String>) yapionAnyType).get()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.flagStorage = flagStorage;
|
|
|
|
generatePrototypeData(prototype);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void generatePrototypeData(Prototype prototype) {
|
|
|
|
if (prototype != null && !prototypes.contains(prototype)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prototype = prototype;
|
|
|
|
|
|
|
|
// TODO: implement generation
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean inRegion(Location location, RegionType regionType, RegionExtensionType regionExtensionType) {
|
2021-04-18 17:49:31 +02:00
|
|
|
if (!hasType(regionType)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-18 17:42:46 +02:00
|
|
|
switch (regionType) {
|
|
|
|
case BUILD:
|
|
|
|
Point minBPoint = regionExtensionType == RegionExtensionType.EXTENSION ? minPointBuildExtension : minPointBuild;
|
|
|
|
Point maxBPoint = regionExtensionType == RegionExtensionType.EXTENSION ? maxPointBuildExtension : maxPointBuild;
|
|
|
|
return inRegion(location, minBPoint, maxBPoint);
|
|
|
|
case TESTBLOCK:
|
|
|
|
Point minTBPoint = regionExtensionType == RegionExtensionType.EXTENSION ? minPointTestblockExtension : minPointTestblock;
|
|
|
|
Point maxTBPoint = regionExtensionType == RegionExtensionType.EXTENSION ? maxPointTestblockExtension : maxPointTestblock;
|
|
|
|
return inRegion(location, minTBPoint, maxTBPoint);
|
|
|
|
default:
|
|
|
|
case NORMAL:
|
|
|
|
return inRegion(location, minPoint, maxPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean inRegion(Location location, Point minPoint, Point maxPoint) {
|
|
|
|
return location.getBlockX() >= minPoint.getX() && location.getBlockX() < maxPoint.getX() &&
|
|
|
|
location.getBlockY() >= minPoint.getY() && location.getBlockY() < maxPoint.getY() &&
|
|
|
|
location.getBlockZ() >= minPoint.getZ() && location.getBlockZ() < maxPoint.getZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasType(RegionType regionType) {
|
|
|
|
switch (regionType) {
|
|
|
|
case BUILD:
|
|
|
|
return prototype != null && prototype.getBuild() != null;
|
|
|
|
case TESTBLOCK:
|
|
|
|
return prototype != null && prototype.getTestblock() != null;
|
|
|
|
default:
|
|
|
|
case NORMAL:
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-18 17:34:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:49:31 +02:00
|
|
|
public String getDisplayName() {
|
|
|
|
return prototype != null ? prototype.getDisplayName() : "";
|
|
|
|
}
|
|
|
|
|
2021-04-18 09:10:30 +02:00
|
|
|
}
|