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