001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import com.plotsquared.core.PlotSquared;
022import com.plotsquared.core.configuration.Settings;
023import com.plotsquared.core.location.Location;
024import com.plotsquared.core.player.MetaDataAccess;
025import com.plotsquared.core.player.PlayerMetaDataKeys;
026import com.plotsquared.core.player.PlotPlayer;
027import com.plotsquared.core.plot.Plot;
028import com.plotsquared.core.plot.PlotArea;
029import com.plotsquared.core.plot.flag.implementations.DoneFlag;
030import com.plotsquared.core.plot.flag.implementations.NoWorldeditFlag;
031import com.sk89q.worldedit.math.BlockVector3;
032import com.sk89q.worldedit.regions.CuboidRegion;
033
034import java.util.HashSet;
035import java.util.Set;
036import java.util.UUID;
037
038public class WEManager {
039
040    private static final BlockVector3 MIN = BlockVector3.at(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
041    private static final BlockVector3 MAX = BlockVector3.at(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
042
043    public static boolean maskContains(Set<CuboidRegion> mask, int x, int y, int z) {
044        for (CuboidRegion region : mask) {
045            if (RegionUtil.contains(region, x, y, z)) {
046                return true;
047            }
048        }
049        return false;
050    }
051
052    public static boolean maskContains(Set<CuboidRegion> mask, int x, int z) {
053        for (CuboidRegion region : mask) {
054            if (RegionUtil.contains(region, x, z)) {
055                return true;
056            }
057        }
058        return false;
059    }
060
061    public static HashSet<CuboidRegion> getMask(PlotPlayer<?> player) {
062        HashSet<CuboidRegion> regions = new HashSet<>();
063        UUID uuid = player.getUUID();
064        Location location = player.getLocation();
065        String world = location.getWorldName();
066        if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(world)) {
067            regions.add(new CuboidRegion(MIN, MAX));
068            return regions;
069        }
070        PlotArea area = player.getApplicablePlotArea();
071        if (area == null) {
072            return regions;
073        }
074        boolean allowMember = player.hasPermission("plots.worldedit.member");
075        Plot plot = player.getCurrentPlot();
076        try (final MetaDataAccess<Plot> metaDataAccess =
077                     player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_WORLD_EDIT_REGION_PLOT)) {
078            if (plot == null) {
079                plot = metaDataAccess.get().orElse(null);
080            }
081            if (plot != null && (!Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot)) && (
082                    (allowMember && plot.isAdded(uuid)) || (!allowMember && plot.isOwner(uuid) || plot
083                            .getTrusted().contains(uuid))) && !plot.getFlag(NoWorldeditFlag.class)) {
084                for (CuboidRegion region : plot.getRegions()) {
085                    BlockVector3 pos1 = region.getMinimumPoint().withY(area.getMinBuildHeight());
086                    BlockVector3 pos2 = region.getMaximumPoint().withY(area.getMaxBuildHeight() - 1);
087                    CuboidRegion copy = new CuboidRegion(pos1, pos2);
088                    regions.add(copy);
089                }
090                metaDataAccess.set(plot);
091            }
092        }
093        return regions;
094    }
095
096}