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 boolean maskContains(Set<CuboidRegion> mask, double dx, double dy, double dz) {
062        int x = Math.toIntExact(Math.round(dx >= 0 ? dx - 0.5 : dx + 0.5));
063        int y = Math.toIntExact(Math.round(dy - 0.5));
064        int z = Math.toIntExact(Math.round(dz >= 0 ? dz - 0.5 : dz + 0.5));
065        for (CuboidRegion region : mask) {
066            if (RegionUtil.contains(region, x, y, z)) {
067                return true;
068            }
069        }
070        return false;
071    }
072
073    public static boolean maskContains(Set<CuboidRegion> mask, double dx, double dz) {
074        int x = Math.toIntExact(Math.round(dx >= 0 ? dx - 0.5 : dx + 0.5));
075        int z = Math.toIntExact(Math.round(dz >= 0 ? dz - 0.5 : dz + 0.5));
076        for (CuboidRegion region : mask) {
077            if (RegionUtil.contains(region, x, z)) {
078                return true;
079            }
080        }
081        return false;
082    }
083
084    public static HashSet<CuboidRegion> getMask(PlotPlayer<?> player) {
085        HashSet<CuboidRegion> regions = new HashSet<>();
086        UUID uuid = player.getUUID();
087        Location location = player.getLocation();
088        String world = location.getWorldName();
089        if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(world)) {
090            regions.add(new CuboidRegion(MIN, MAX));
091            return regions;
092        }
093        PlotArea area = player.getApplicablePlotArea();
094        if (area == null) {
095            return regions;
096        }
097        boolean allowMember = player.hasPermission("plots.worldedit.member");
098        Plot plot = player.getCurrentPlot();
099        try (final MetaDataAccess<Plot> metaDataAccess =
100                     player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_WORLD_EDIT_REGION_PLOT)) {
101            if (plot == null) {
102                plot = metaDataAccess.get().orElse(null);
103            }
104            if (plot != null && (!Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot)) && (
105                    (allowMember && plot.isAdded(uuid)) || (!allowMember && plot.isOwner(uuid) || plot
106                            .getTrusted().contains(uuid))) && !plot.getFlag(NoWorldeditFlag.class)) {
107                for (CuboidRegion region : plot.getRegions()) {
108                    BlockVector3 pos1 = region.getMinimumPoint().withY(area.getMinBuildHeight());
109                    BlockVector3 pos2 = region.getMaximumPoint().withY(area.getMaxBuildHeight() - 1);
110                    CuboidRegion copy = new CuboidRegion(pos1, pos2);
111                    regions.add(copy);
112                }
113                metaDataAccess.set(plot);
114            }
115        }
116        return regions;
117    }
118
119    public static boolean intersects(CuboidRegion region1, CuboidRegion region2) {
120        return RegionUtil.intersects(region1, region2);
121    }
122
123    public static boolean regionContains(CuboidRegion selection, HashSet<CuboidRegion> mask) {
124        for (CuboidRegion region : mask) {
125            if (intersects(region, selection)) {
126                return true;
127            }
128        }
129        return false;
130    }
131
132}