SteamWar/BauSystem2.0
Archiviert
12
0
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
BauSystem2.0/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java

440 Zeilen
16 KiB
Java

/*
* 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;
import com.sk89q.worldedit.EditSession;
2021-04-18 18:37:32 +02:00
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.flagvalues.ColorMode;
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.shared.SizedStack;
2021-04-20 09:24:15 +02:00
import de.steamwar.sql.Schematic;
2021-04-20 11:03:46 +02:00
import lombok.AccessLevel;
2021-04-18 15:48:15 +02:00
import lombok.Getter;
2021-04-18 18:37:32 +02:00
import lombok.NonNull;
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
import java.io.File;
2021-04-20 09:24:15 +02:00
import java.io.IOException;
2021-05-01 18:04:59 +02:00
import java.util.*;
2021-04-19 20:33:15 +02:00
import java.util.function.Predicate;
2021-04-18 15:34:31 +02:00
2021-04-20 13:46:51 +02:00
import static de.steamwar.bausystem.region.RegionUtils.paste;
2021-04-18 15:48:15 +02:00
@Getter
public class Region {
2021-04-18 15:34:31 +02:00
private static final List<Region> REGION_LIST = new ArrayList<>();
public static Region getRegion(Location location) {
2021-05-01 17:33:26 +02:00
String s = Thread.currentThread().getStackTrace()[2].toString();
if (!s.contains("sidebar")) {
2021-05-01 18:04:59 +02:00
System.out.println(s + " " + location);
2021-05-01 17:33:26 +02:00
}
2021-05-01 18:04:59 +02:00
/*for (Region region : REGION_LIST) {
2021-05-01 17:33:26 +02:00
if (region.inRegion(location, region.minPoint, region.maxPoint)) {
return region;
}
}
2021-05-01 18:04:59 +02:00
return GlobalRegion.instance;*/
return REGION_LIST.stream().filter(r -> r.inRegion(location, r.minPoint, r.maxPoint)).findFirst().orElse(GlobalRegion.instance);
}
2021-04-20 08:39:35 +02:00
public static void setGlobal(Flag flagType, Flag.Value<?> value) {
for (Region region : REGION_LIST) {
region.set(flagType, value);
}
}
2021-04-20 13:46:51 +02:00
YAPIONObject regionData;
2021-04-18 18:37:32 +02:00
2021-04-18 19:00:19 +02:00
private String name;
2021-04-18 15:34:31 +02:00
private Prototype prototype;
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-19 17:32:16 +02:00
private int floorLevel;
private int waterLevel;
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-20 11:03:46 +02:00
@Getter(AccessLevel.PRIVATE)
private SizedStack<EditSession> undoSessions;
2021-04-20 11:03:46 +02:00
@Getter(AccessLevel.PRIVATE)
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;
if (prototype != null) {
REGION_LIST.add(this);
}
2021-04-19 20:33:15 +02:00
linkedRegionName = regionConfig.getPlainValueOrDefault("optionsLinkedWith", null);
2021-04-18 19:00:19 +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-19 20:01:38 +02:00
if (!hasType(RegionType.BUILD) || !hasType(RegionType.TESTBLOCK)) {
flagStorage.set(Flag.TNT, TNTMode.DENY);
}
}
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)) {
return;
}
this.prototype = prototype;
2021-04-18 18:20:27 +02:00
this.minPoint = point;
2021-04-21 14:24:49 +02:00
this.maxPoint = point.add(prototype.getSizeX() - 1, prototype.getSizeY() - 1, prototype.getSizeZ() - 1);
2021-04-18 18:20:27 +02:00
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());
2021-04-21 14:24:49 +02:00
this.maxPointTestblock = this.minPointTestblock.add(prototype.getTestblock().getSizeX() - 1, prototype.getTestblock().getSizeY() - 1, prototype.getTestblock().getSizeZ() - 1);
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());
2021-04-21 14:24:49 +02:00
this.maxPointTestblockExtension = this.maxPointTestblock.add(prototype.getTestblock().getExtensionPositiveX() - 1, prototype.getTestblock().getExtensionPositiveY() - 1, prototype.getTestblock().getExtensionPositiveZ() - 1);
2021-04-18 18:27:54 +02:00
}
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());
2021-04-21 14:24:49 +02:00
this.maxPointBuild = this.minPointBuild.add(prototype.getBuild().getSizeX() - 1, prototype.getBuild().getSizeY() - 1, prototype.getBuild().getSizeZ() - 1);
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());
2021-04-21 14:24:49 +02:00
this.maxPointBuildExtension = this.maxPointBuild.add(prototype.getBuild().getExtensionPositiveX() - 1, prototype.getBuild().getExtensionPositiveY() - 1, prototype.getBuild().getExtensionPositiveZ() - 1);
2021-04-18 18:27:54 +02:00
}
2021-04-19 17:32:16 +02:00
if (prototype.getFloorOffset() != 0) {
floorLevel = minPoint.getY() + prototype.getFloorOffset();
} else {
floorLevel = 0;
}
if (prototype.getWaterOffset() != 0) {
waterLevel = minPoint.getY() + prototype.getWaterOffset();
} else {
waterLevel = 0;
}
}
public boolean inRegion(Location location, RegionType regionType, RegionExtensionType regionExtensionType) {
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();
2021-04-18 17:42:46 +02:00
}
public boolean hasType(RegionType regionType) {
2021-04-20 11:15:35 +02:00
if (prototype == null) {
return false;
}
2021-04-20 11:36:39 +02:00
if (regionType == null) {
return false;
}
2021-04-18 17:42:46 +02:00
switch (regionType) {
case BUILD:
2021-04-20 11:15:35 +02:00
return prototype.getBuild() != null;
2021-04-18 17:42:46 +02:00
case TESTBLOCK:
2021-04-20 11:15:35 +02:00
return prototype.getTestblock() != null;
2021-04-18 17:42:46 +02:00
default:
case NORMAL:
2021-04-20 11:15:35 +02:00
return true;
2021-04-18 17:42:46 +02:00
}
}
2021-04-20 08:20:53 +02:00
public boolean hasExtensionType(RegionType regionType) {
2021-04-20 11:18:57 +02:00
if (!hasType(regionType)) {
return false;
}
2021-04-20 08:20:53 +02:00
switch (regionType) {
case BUILD:
2021-04-20 11:18:57 +02:00
return prototype.getBuild().isExtensionRegistered();
2021-04-20 08:20:53 +02:00
case TESTBLOCK:
2021-04-20 11:18:57 +02:00
return prototype.getTestblock().isExtensionRegistered();
2021-04-20 08:20:53 +02:00
default:
case NORMAL:
return false;
}
}
public String getDisplayName() {
return prototype != null ? prototype.getDisplayName() : "";
}
2021-04-18 18:37:32 +02:00
2021-04-19 20:33:15 +02:00
private void setLinkedRegion(Predicate<Region> regionConsumer) {
2021-04-18 19:00:19 +02:00
if (linkedRegionName == null) {
return;
}
if (linkedRegion != null) {
2021-04-19 20:33:15 +02:00
if (regionConsumer.test(linkedRegion)) {
2021-04-20 13:46:51 +02:00
RegionUtils.save(linkedRegion);
2021-04-19 20:33:15 +02:00
}
2021-04-18 19:00:19 +02:00
return;
}
for (Region region : REGION_LIST) {
2021-04-19 20:33:15 +02:00
if (region.name.equals(linkedRegionName)) {
2021-04-18 19:00:19 +02:00
linkedRegion = region;
2021-04-19 20:33:15 +02:00
if (regionConsumer.test(linkedRegion)) {
2021-04-20 13:46:51 +02:00
RegionUtils.save(linkedRegion);
2021-04-19 20:33:15 +02:00
}
2021-04-18 19:00:19 +02:00
return;
}
}
}
2021-04-18 18:37:32 +02:00
public void setPrototype(@NonNull Prototype prototype) {
if (this.prototype == null) {
return;
}
2021-04-19 20:33:15 +02:00
if (!prototypes.contains(prototype)) {
return;
}
2021-04-18 18:37:32 +02:00
generatePrototypeData(prototype, minPoint);
2021-04-20 13:46:51 +02:00
RegionUtils.save(this);
2021-04-18 19:00:19 +02:00
setLinkedRegion(region -> {
region.generatePrototypeData(prototype, region.minPoint);
2021-04-19 20:33:15 +02:00
return true;
2021-04-18 19:00:19 +02:00
});
2021-04-18 18:37:32 +02:00
}
public void set(Flag flagType, Flag.Value<?> value) {
if (flagStorage.set(flagType, value)) {
2021-04-20 13:46:51 +02:00
RegionUtils.save(this);
2021-04-18 18:37:32 +02:00
}
2021-04-19 20:33:15 +02:00
setLinkedRegion(region -> region.flagStorage.set(flagType, value));
2021-04-18 18:37:32 +02:00
}
2021-05-01 17:33:26 +02:00
public Flag.Value<?> get(Flag flagType) {
2021-04-18 18:37:32 +02:00
return flagStorage.get(flagType);
}
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-20 08:20:53 +02:00
public Point getMinPoint(RegionType regionType, RegionExtensionType regionExtensionType) {
switch (regionType) {
case TESTBLOCK:
return (regionExtensionType == null || regionExtensionType == RegionExtensionType.NORMAL) ? minPointTestblock : minPointTestblockExtension;
case BUILD:
return (regionExtensionType == null || regionExtensionType == RegionExtensionType.NORMAL) ? minPointBuild : minPointBuildExtension;
default:
case NORMAL:
return minPoint;
}
}
public Point getMaxPoint(RegionType regionType, RegionExtensionType regionExtensionType) {
switch (regionType) {
case TESTBLOCK:
return (regionExtensionType == null || regionExtensionType == RegionExtensionType.NORMAL) ? maxPointTestblock : maxPointTestblockExtension;
case BUILD:
return (regionExtensionType == null || regionExtensionType == RegionExtensionType.NORMAL) ? maxPointBuild : maxPointBuildExtension;
default:
case NORMAL:
return maxPoint;
}
}
2021-04-20 09:24:15 +02:00
boolean hasReset(RegionType regionType) {
if (!hasType(regionType)) {
2021-04-20 09:24:15 +02:00
return false;
}
switch (regionType) {
case TESTBLOCK:
return prototype.getTestblock().getSchematicFile() != null;
case BUILD:
return prototype.getBuild().getSchematicFile() != null;
default:
case NORMAL:
return prototype.getSchematicFile() != null;
}
}
public void reset(RegionType regionType) throws IOException {
reset(null, regionType);
}
public void reset(Schematic schematic, RegionType regionType) throws IOException {
reset(schematic, regionType, RegionExtensionType.NORMAL, false);
2021-04-20 09:24:15 +02:00
}
public void reset(RegionType regionType, RegionExtensionType regionExtensionType) throws IOException {
reset(null, regionType, regionExtensionType, false);
2021-04-20 09:24:15 +02:00
}
2021-04-20 13:48:17 +02:00
public void reset(Schematic schematic, RegionType regionType, RegionExtensionType regionExtensionType) throws IOException {
reset(schematic, regionType, regionExtensionType, false);
}
public void reset(Schematic schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir) throws IOException {
2021-04-20 09:24:15 +02:00
if (!hasReset(regionType)) {
return;
}
2021-04-20 09:24:15 +02:00
if (regionExtensionType == RegionExtensionType.EXTENSION && !hasExtensionType(regionType)) {
regionExtensionType = RegionExtensionType.NORMAL;
}
PasteOptions pasteOptions = new PasteOptions((schematic != null && (schematic.getSchemType().fightType() || schematic.getSchemType().check())), ignoreAir, getPlain(Flag.COLOR, ColorMode.class).getColor(), regionExtensionType == RegionExtensionType.EXTENSION, getMinPoint(regionType, regionExtensionType), getMaxPoint(regionType, regionExtensionType), waterLevel);
Point pastePoint;
File tempFile = null;
switch (regionType) {
case BUILD:
pastePoint = minPointBuild.add(prototype.getBuild().getSizeX() / 2, 0, prototype.getBuild().getSizeZ() / 2);
if (schematic == null) {
tempFile = prototype.getBuild().getSchematicFile();
}
2021-04-20 11:01:19 +02:00
break;
case TESTBLOCK:
pastePoint = minPointTestblock.add(prototype.getTestblock().getSizeX() / 2, 0, prototype.getTestblock().getSizeZ() / 2);
if (schematic == null) {
tempFile = prototype.getTestblock().getSchematicFile();
}
2021-04-20 11:01:19 +02:00
break;
default:
case NORMAL:
pastePoint = minPoint.add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2);
if (schematic == null) {
tempFile = prototype.getSchematicFile();
}
2021-04-20 11:01:19 +02:00
break;
}
EditSession editSession = null;
if (schematic != null) {
editSession = paste(schematic.load(), pastePoint, pasteOptions);
} else {
editSession = paste(tempFile, pastePoint, pasteOptions);
}
initSessions();
undoSessions.push(editSession);
}
2021-04-20 08:20:53 +02:00
public boolean isGlobal() {
return this == GlobalRegion.getInstance();
}
private void initSessions() {
if (undoSessions == null) {
undoSessions = new SizedStack<>(20);
redoSessions = new SizedStack<>(20);
}
}
public boolean undo() {
initSessions();
EditSession session = null;
try {
session = undoSessions.pop();
if (session == null) {
return false;
}
session.undo(session);
redoSessions.push(session);
return true;
} finally {
if (session != null) {
session.flushSession();
}
}
}
public boolean redo() {
initSessions();
EditSession session = null;
try {
session = redoSessions.pop();
if (session == null) {
return false;
}
session.redo(session);
undoSessions.push(session);
return true;
} finally {
if (session != null) {
session.flushSession();
}
}
}
2021-04-18 18:20:27 +02:00
}