/* * 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 . */ package de.steamwar.bausystem.region; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.extent.clipboard.Clipboard; 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.tags.Tag; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.bausystem.shared.SizedStack; import de.steamwar.bausystem.utils.FlatteningWrapper; import de.steamwar.sql.SchematicNode; import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.Location; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; import yapion.hierarchy.types.YAPIONValue; import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; import static de.steamwar.bausystem.region.RegionUtils.paste; @Getter public class Region { @Getter private static final Map REGION_MAP = new HashMap<>(); private static final File backupFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "backup"); private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd' 'HH:mm:ss"); public static Region getRegion(Location location) { return REGION_MAP.values().stream() .filter(r -> r.inRegion(location, r.minPoint, r.maxPoint)) .findFirst() .orElse(GlobalRegion.instance); } public static Set getRegion(Prototype prototype) { return REGION_MAP.values().stream() .filter(r -> r.getPrototype() == prototype) .collect(Collectors.toSet()); } public static void setGlobal(Flag flagType, Flag.Value value) { REGION_MAP.values().forEach(region -> region.set(flagType, value)); } YAPIONObject regionData; private String name; private Prototype prototype; private Set prototypes; private String skin; 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 int floorLevel; private int waterLevel; private Point copyPoint; // Nullable private Point testBlockPoint; // Nullable private String linkedRegionName = null; // Nullable private Region linkedRegion = null; // Nullable private FlagStorage flagStorage; @Getter(AccessLevel.PRIVATE) private SizedStack undoSessions; @Getter(AccessLevel.PRIVATE) private SizedStack redoSessions; public Region(String name, Prototype prototype, YAPIONObject regionConfig, FlagStorage flagStorage, YAPIONObject regionData) { this.name = name; this.regionData = regionData; if (prototype != null) { REGION_MAP.put(name, this); } linkedRegionName = regionConfig.getPlainValueOrDefault("optionsLinkedWith", null); prototypes = new HashSet<>(); if (regionConfig.containsKey("prototypes", YAPIONType.ARRAY)) { regionConfig.getArray("prototypes").forEach(yapionAnyType -> { if (yapionAnyType instanceof YAPIONValue) { prototypes.add(((YAPIONValue) yapionAnyType).get()); } }); } if (regionConfig.containsKey("prototype")) { prototypes.add(regionConfig.getPlainValue("prototype")); } this.flagStorage = flagStorage; 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")); } if (prototype != null && prototypes.contains(prototype.getName())) { generatePrototypeData(prototype, point); } else if (regionConfig.containsKey("prototype")) { generatePrototypeData(Prototype.getByName(regionConfig.getPlainValue("prototype")), point); } if (prototype != null) { skin = regionData.getPlainValueOrDefault("skin", prototype.getDefaultSkin()); if (!prototype.getSkinMap().containsKey(skin)) { skin = prototype.getDefaultSkin(); } } regionData.add("skin", skin); if (!hasType(RegionType.BUILD) || !hasType(RegionType.TESTBLOCK)) { flagStorage.set(Flag.TNT, TNTMode.DENY); } } private void generatePrototypeData(Prototype prototype, Point point) { if (prototype == null) { return; } this.prototype = prototype; this.skin = prototype.getDefaultSkin(); this.minPoint = point; this.maxPoint = point.add(prototype.getSizeX() - 1, prototype.getSizeY() - 1, prototype.getSizeZ() - 1); 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() - 1, prototype.getTestblock().getSizeY() - 1, prototype.getTestblock().getSizeZ() - 1); this.minPointTestblockExtension = this.minPointTestblock.subtract(prototype.getTestblock().getExtensionNegativeX(), prototype.getTestblock().getExtensionNegativeY(), prototype.getTestblock().getExtensionNegativeZ()); this.maxPointTestblockExtension = this.maxPointTestblock.add(prototype.getTestblock().getExtensionPositiveX(), prototype.getTestblock().getExtensionPositiveY(), prototype.getTestblock().getExtensionPositiveZ()); if (prototype.getTestblock().getCopyOffsetX() != 0 || prototype.getTestblock().getCopyOffsetY() != 0 || prototype.getTestblock().getCopyOffsetZ() != 0) { this.testBlockPoint = this.minPointTestblock.add(prototype.getTestblock().getCopyOffsetX(), prototype.getTestblock().getCopyOffsetY(), prototype.getTestblock().getCopyOffsetZ()); } else { this.testBlockPoint = this.minPointTestblock.add(prototype.getTestblock().getSizeX() / 2, 0, -1); } } 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() - 1, prototype.getBuild().getSizeY() - 1, prototype.getBuild().getSizeZ() - 1); this.minPointBuildExtension = this.minPointBuild.subtract(prototype.getBuild().getExtensionNegativeX(), prototype.getBuild().getExtensionNegativeY(), prototype.getBuild().getExtensionNegativeZ()); this.maxPointBuildExtension = this.maxPointBuild.add(prototype.getBuild().getExtensionPositiveX(), prototype.getBuild().getExtensionPositiveY(), prototype.getBuild().getExtensionPositiveZ()); if (!prototype.getBuild().isHasCopyPoint() && (prototype.getCopyPointOffsetX() != 0 || prototype.getCopyPointOffsetY() != 0 || prototype.getCopyPointOffsetZ() != 0)) { this.copyPoint = minPoint.add(prototype.getCopyPointOffsetX(), prototype.getCopyPointOffsetY(), prototype.getCopyPointOffsetZ()); } else if (prototype.getBuild().getCopyOffsetX() != 0 || prototype.getBuild().getCopyOffsetY() != 0 || prototype.getBuild().getCopyOffsetZ() != 0) { this.copyPoint = this.minPointBuild.add(prototype.getBuild().getCopyOffsetX(), prototype.getBuild().getCopyOffsetY(), prototype.getBuild().getCopyOffsetZ()); } else { this.copyPoint = this.minPointBuild.add(prototype.getBuild().getSizeX() / 2, 0, prototype.getBuild().getSizeZ()); } } else if (prototype.getCopyPointOffsetX() != 0 || prototype.getCopyPointOffsetY() != 0 || prototype.getCopyPointOffsetZ() != 0) { this.copyPoint = minPoint.add(prototype.getCopyPointOffsetX(), prototype.getCopyPointOffsetY(), prototype.getCopyPointOffsetZ()); } 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; } 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) { int blockX = location.getBlockX(); int blockY = location.getBlockY(); int blockZ = location.getBlockZ(); return blockX >= minPoint.getX() && blockX <= maxPoint.getX() && blockY >= minPoint.getY() && blockY <= maxPoint.getY() && blockZ >= minPoint.getZ() && blockZ <= maxPoint.getZ(); } public boolean hasType(RegionType regionType) { if (prototype == null) { return false; } if (regionType == null) { return false; } switch (regionType) { case BUILD: return prototype.getBuild() != null; case TESTBLOCK: return prototype.getTestblock() != null; default: case NORMAL: return true; } } public boolean hasExtensionType(RegionType regionType) { if (!hasType(regionType)) { return false; } switch (regionType) { case BUILD: return prototype.getBuild().isExtensionRegistered(); case TESTBLOCK: return prototype.getTestblock().isExtensionRegistered(); default: case NORMAL: return false; } } public String getDisplayName() { return prototype != null ? prototype.getSkinMap().get(skin).getName() : ""; } private void setLinkedRegion(Predicate regionConsumer) { if (linkedRegionName == null) { return; } if (linkedRegion != null) { if (regionConsumer.test(linkedRegion)) { RegionUtils.save(linkedRegion); } return; } for (Region region : REGION_MAP.values()) { if (region.name.equals(linkedRegionName)) { linkedRegion = region; if (regionConsumer.test(linkedRegion)) { RegionUtils.save(linkedRegion); } return; } } } public boolean setPrototype(@NonNull Prototype prototype) { if (!prototypes.contains(prototype.getName())) { return false; } return _setPrototype(prototype); } boolean _setPrototype(@NonNull Prototype prototype) { generatePrototypeData(prototype, minPoint); RegionUtils.save(this); return true; } public boolean setSkin(@NonNull String skinName) { if (!prototype.getSkinMap().containsKey(skinName)) { return false; } this.skin = skinName; setLinkedRegion(region -> { region.skin = skinName; return true; }); regionData.add("skin", skin); return true; } public void set(Flag flagType, Flag.Value value) { if (flagStorage.set(flagType, value)) { RegionUtils.save(this); } setLinkedRegion(region -> region.flagStorage.set(flagType, value)); } public void set(Tag tag) { if (flagStorage.set(tag)) { RegionUtils.save(this); } setLinkedRegion(region -> region.flagStorage.set(tag)); } public void remove(Tag tag) { if (flagStorage.remove(tag)) { RegionUtils.save(this); } } public Flag.Value get(Flag flagType) { return flagStorage.get(flagType); } public boolean get(Tag tagType) { return flagStorage.is(tagType); } public & Flag.Value> T getPlain(Flag flagType) { return (T) flagStorage.get(flagType).getValue(); } public & Flag.Value> T getPlain(Flag flagType, Class type) { return (T) flagStorage.get(flagType).getValue(); } 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; } } boolean hasReset(RegionType regionType) { if (!hasType(regionType)) { return false; } switch (regionType) { case TESTBLOCK: return prototype.getSkinMap().get(skin).getTestblockSchematicFile() != null; case BUILD: return prototype.getSkinMap().get(skin).getBuildSchematicFile() != null; default: case NORMAL: return prototype.getSkinMap().get(skin).getSchematicFile() != null; } } public void reset(RegionType regionType) throws IOException { reset(null, regionType); } public void reset(SchematicNode schematic, RegionType regionType) throws IOException { reset(schematic, regionType, RegionExtensionType.NORMAL, false); } public void reset(RegionType regionType, RegionExtensionType regionExtensionType) throws IOException { reset(null, regionType, regionExtensionType, false); } public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType) throws IOException { reset(schematic, regionType, regionExtensionType, false); } public void reset(File file) { EditSession editSession = paste(file, minPoint.add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2), new PasteOptions(false, false, Color.YELLOW, false, false, getMinPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), getMaxPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), waterLevel)); initSessions(); undoSessions.push(editSession); } public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir) throws IOException { reset(schematic, regionType, regionExtensionType, ignoreAir, false); } public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir, boolean onlyColors) throws IOException { if (!hasReset(regionType)) { return; } 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(), onlyColors, regionExtensionType == RegionExtensionType.EXTENSION, getMinPoint(regionType, regionExtensionType), getMaxPoint(regionType, regionExtensionType), waterLevel); Point pastePoint; File tempFile = null; Clipboard clipboard = null; switch (regionType) { case BUILD: pastePoint = minPointBuild.add(prototype.getBuild().getSizeX() / 2, 0, prototype.getBuild().getSizeZ() / 2); if (schematic == null) { tempFile = prototype.getSkinMap().get(skin).getBuildSchematicFile(); } break; case TESTBLOCK: pastePoint = minPointTestblock.add(prototype.getTestblock().getSizeX() / 2, 0, 0); if (schematic == null) { tempFile = prototype.getSkinMap().get(skin).getTestblockSchematicFile(); pastePoint = pastePoint.add(0, 0, prototype.getTestblock().getSizeZ() / 2); } else { clipboard = schematic.load(); int dz = Math.abs(clipboard.getOrigin().getZ() - clipboard.getMinimumPoint().getZ()); if (dz < 2 || dz > prototype.getTestblock().getSizeZ()) { pastePoint = pastePoint.add(0, 0, prototype.getTestblock().getSizeZ() / 2); } else if (clipboard.getDimensions().getZ() != prototype.getTestblock().getSizeZ()) { pastePoint = pastePoint.add(0, 0, clipboard.getDimensions().getZ() / 2 - (clipboard.getOrigin().getZ() - clipboard.getMinimumPoint().getZ()) - 1); } else { pastePoint = pastePoint.add(0, 0, prototype.getTestblock().getSizeZ() / 2); } if (schematic.getSchemtype().getKuerzel().equalsIgnoreCase("wg")) { pastePoint = pastePoint.add(0, 0, 1); } } break; default: case NORMAL: pastePoint = minPoint.add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2); if (schematic == null) { tempFile = prototype.getSkinMap().get(skin).getSchematicFile(); } break; } EditSession editSession = null; if (schematic != null) { editSession = paste(clipboard != null ? clipboard : schematic.load(), pastePoint, pasteOptions); } else { editSession = paste(tempFile, pastePoint, pasteOptions); } initSessions(); undoSessions.push(editSession); } 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(); } } } public boolean backup() { final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name); //noinspection ResultOfMethodCallIgnored definedBackupFolder.mkdirs(); File[] currentBackups = definedBackupFolder.listFiles(); if (currentBackups != null && currentBackups.length >= 20) { List files = new ArrayList<>(Arrays.asList(currentBackups)); files.sort(Comparator.comparingLong(File::lastModified)); while (files.size() >= 20) files.remove(0).delete(); } final File backupFile = new File(definedBackupFolder, LocalDateTime.now().format(formatter) + ".schem"); return FlatteningWrapper.impl.backup(minPoint, maxPoint, backupFile); } public static boolean copy(Point minPoint, Point maxPoint, File file) { return FlatteningWrapper.impl.backup(minPoint, maxPoint, file); } public List listBackup() { final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name); //noinspection ResultOfMethodCallIgnored definedBackupFolder.mkdirs(); File[] currentBackups = definedBackupFolder.listFiles(); List files = new ArrayList<>(Arrays.asList(currentBackups)); files.sort(Comparator.comparingLong(File::lastModified)); return files.stream().map(File::getName).collect(Collectors.toList()); } public File getBackupFile(String backupName) { final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name); //noinspection ResultOfMethodCallIgnored definedBackupFolder.mkdirs(); File[] files = definedBackupFolder.listFiles((dir, name) -> name.equals(backupName + ".schem")); if (files == null || files.length == 0) return null; return files[0]; } }