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 18:53:58 +02:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
2021-04-18 18:37:32 +02:00
|
|
|
import de.steamwar.bausystem.region.flags.Flag;
|
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 18:53:58 +02:00
|
|
|
import de.steamwar.bausystem.shared.SizedStack;
|
2021-04-18 15:48:15 +02:00
|
|
|
import lombok.Getter;
|
2021-04-18 18:37:32 +02:00
|
|
|
import lombok.NonNull;
|
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 18:37:32 +02:00
|
|
|
import yapion.serializing.YAPIONSerializer;
|
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 19:00:19 +02:00
|
|
|
import java.util.function.Consumer;
|
2021-04-18 15:34:31 +02:00
|
|
|
|
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 18:37:32 +02:00
|
|
|
private YAPIONObject regionData;
|
|
|
|
|
2021-04-18 19:00:19 +02:00
|
|
|
private String name;
|
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;
|
|
|
|
|
2021-04-18 19:00:19 +02:00
|
|
|
private String linkedRegionName = null; // Nullable
|
2021-04-18 15:34:31 +02:00
|
|
|
private Region linkedRegion = null; // Nullable
|
|
|
|
|
|
|
|
private FlagStorage flagStorage;
|
|
|
|
|
2021-04-18 18:53:58 +02:00
|
|
|
private SizedStack<EditSession> undoSessions;
|
|
|
|
private SizedStack<EditSession> redoSessions;
|
|
|
|
|
2021-04-18 19:00:19 +02:00
|
|
|
public Region(String name, Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage, YAPIONObject regionData) {
|
|
|
|
this.name = name;
|
2021-04-18 18:37:32 +02:00
|
|
|
this.regionData = regionData;
|
2021-04-18 17:34:02 +02:00
|
|
|
if (prototype != null) {
|
|
|
|
REGION_LIST.add(this);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:00:19 +02:00
|
|
|
if (regionConfig.containsKey("linkedWith")) {
|
|
|
|
linkedRegionName = regionConfig.getPlainValue("linkedWith");
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:34:02 +02:00
|
|
|
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;
|
2021-04-18 18:27:54 +02:00
|
|
|
Point point = null;
|
|
|
|
if (regionConfig.containsKey("minX", Integer.class) && regionConfig.containsKey("minY", Integer.class) && regionConfig.containsKey("minZ", Integer.class)) {
|
|
|
|
point = new Point(regionConfig.getPlainValue("minX"), regionConfig.getPlainValue("minY"), regionConfig.getPlainValue("minZ"));
|
|
|
|
}
|
|
|
|
generatePrototypeData(prototype, point);
|
2021-04-18 17:34:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 18:20:27 +02:00
|
|
|
private void generatePrototypeData(Prototype prototype, Point point) {
|
|
|
|
if (prototype == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-18 18:21:34 +02:00
|
|
|
if (this.prototype != null && !prototypes.contains(prototype)) {
|
2021-04-18 17:34:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prototype = prototype;
|
|
|
|
|
2021-04-18 18:20:27 +02:00
|
|
|
this.minPoint = point;
|
|
|
|
this.maxPoint = point.add(prototype.getSizeX(), prototype.getSizeY(), prototype.getSizeZ());
|
|
|
|
|
2021-04-18 18:27:54 +02:00
|
|
|
if (prototype.getTestblock() != null) {
|
|
|
|
this.minPointTestblock = point.add(prototype.getTestblock().getOffsetX(), prototype.getTestblock().getOffsetY(), prototype.getTestblock().getOffsetZ());
|
|
|
|
this.maxPointTestblock = this.minPointTestblock.add(prototype.getTestblock().getSizeX(), prototype.getTestblock().getSizeY(), prototype.getTestblock().getSizeZ());
|
2021-04-18 18:20:27 +02:00
|
|
|
|
2021-04-18 18:27:54 +02:00
|
|
|
this.minPointTestblockExtension = this.minPointTestblock.substract(prototype.getTestblock().getExtensionNegativeX(), prototype.getTestblock().getExtensionNegativeY(), prototype.getTestblock().getExtensionNegativeZ());
|
|
|
|
this.maxPointTestblockExtension = this.maxPointTestblock.add(prototype.getTestblock().getExtensionPositiveX(), prototype.getTestblock().getExtensionPositiveY(), prototype.getTestblock().getExtensionPositiveZ());
|
|
|
|
}
|
2021-04-18 18:20:27 +02:00
|
|
|
|
2021-04-18 18:27:54 +02:00
|
|
|
if (prototype.getBuild() != null) {
|
|
|
|
this.minPointBuild = point.add(prototype.getBuild().getOffsetX(), prototype.getBuild().getOffsetY(), prototype.getBuild().getOffsetZ());
|
|
|
|
this.maxPointBuild = this.minPointBuild.add(prototype.getBuild().getSizeX(), prototype.getBuild().getSizeY(), prototype.getBuild().getSizeZ());
|
2021-04-18 18:20:27 +02:00
|
|
|
|
2021-04-18 18:27:54 +02:00
|
|
|
this.minPointBuildExtension = this.minPointBuild.substract(prototype.getBuild().getExtensionNegativeX(), prototype.getBuild().getExtensionNegativeY(), prototype.getBuild().getExtensionNegativeZ());
|
|
|
|
this.maxPointBuildExtension = this.maxPointBuild.add(prototype.getBuild().getExtensionPositiveX(), prototype.getBuild().getExtensionPositiveY(), prototype.getBuild().getExtensionPositiveZ());
|
|
|
|
}
|
2021-04-18 17:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:37:32 +02:00
|
|
|
|
2021-04-18 19:00:19 +02:00
|
|
|
private void setLinkedRegion(Consumer<Region> regionConsumer) {
|
|
|
|
if (linkedRegionName == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (linkedRegion != null) {
|
|
|
|
regionConsumer.accept(linkedRegion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (Region region : REGION_LIST) {
|
|
|
|
if (region.name.equals(name)) {
|
|
|
|
linkedRegion = region;
|
|
|
|
regionConsumer.accept(linkedRegion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 18:37:32 +02:00
|
|
|
public void setPrototype(@NonNull Prototype prototype) {
|
|
|
|
if (this.prototype == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
regionData.add("prototype", prototype.getName());
|
|
|
|
generatePrototypeData(prototype, minPoint);
|
2021-04-18 19:00:19 +02:00
|
|
|
setLinkedRegion(region -> {
|
|
|
|
region.regionData.add("prototype", prototype.getName());
|
|
|
|
region.generatePrototypeData(prototype, region.minPoint);
|
|
|
|
});
|
2021-04-18 18:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void set(Flag flagType, Flag.Value<?> value) {
|
|
|
|
if (flagStorage.set(flagType, value)) {
|
|
|
|
regionData.add("flagStorage", YAPIONSerializer.serialize(flagStorage));
|
|
|
|
}
|
2021-04-18 19:00:19 +02:00
|
|
|
setLinkedRegion(region -> region.set(flagType, value));
|
2021-04-18 18:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public <T extends Enum<T> & Flag.Value<T>> Flag.Value<T> get(Flag flagType) {
|
|
|
|
return flagStorage.get(flagType);
|
|
|
|
}
|
2021-04-18 18:53:58 +02:00
|
|
|
|
2021-04-19 17:22:15 +02:00
|
|
|
public <T extends Enum<T> & Flag.Value<T>> T getPlain(Flag flagType) {
|
|
|
|
return (T) flagStorage.get(flagType).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T extends Enum<T> & Flag.Value<T>> T getPlain(Flag flagType, Class<T> type) {
|
|
|
|
return (T) flagStorage.get(flagType).getValue();
|
|
|
|
}
|
|
|
|
|
2021-04-18 18:53:58 +02:00
|
|
|
public void reset(RegionType regionType) {
|
|
|
|
if (!hasType(regionType)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (regionType) {
|
|
|
|
case BUILD:
|
|
|
|
|
|
|
|
case TESTBLOCK:
|
|
|
|
default:
|
|
|
|
case NORMAL:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 18:20:27 +02:00
|
|
|
}
|