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.listener;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.configuration.Settings;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.plot.Plot;
027import com.plotsquared.core.plot.world.PlotAreaManager;
028import com.plotsquared.core.util.WEManager;
029import com.plotsquared.core.util.WorldUtil;
030import com.sk89q.worldedit.EditSession;
031import com.sk89q.worldedit.WorldEdit;
032import com.sk89q.worldedit.entity.Player;
033import com.sk89q.worldedit.event.extent.EditSessionEvent;
034import com.sk89q.worldedit.extension.platform.Actor;
035import com.sk89q.worldedit.extent.NullExtent;
036import com.sk89q.worldedit.regions.CuboidRegion;
037import com.sk89q.worldedit.util.Location;
038import com.sk89q.worldedit.util.eventbus.EventHandler.Priority;
039import com.sk89q.worldedit.util.eventbus.Subscribe;
040import com.sk89q.worldedit.world.World;
041import net.kyori.adventure.text.minimessage.Template;
042import org.checkerframework.checker.nullness.qual.NonNull;
043
044import java.util.Set;
045
046public class WESubscriber {
047
048    private final PlotAreaManager plotAreaManager;
049    private final WorldUtil worldUtil;
050
051    @Inject
052    public WESubscriber(final @NonNull PlotAreaManager plotAreaManager, final @NonNull WorldUtil worldUtil) {
053        this.plotAreaManager = plotAreaManager;
054        this.worldUtil = worldUtil;
055    }
056
057    @Subscribe(priority = Priority.VERY_EARLY)
058    public void onEditSession(EditSessionEvent event) {
059        if (!Settings.Enabled_Components.WORLDEDIT_RESTRICTIONS) {
060            WorldEdit.getInstance().getEventBus().unregister(this);
061            return;
062        }
063        if (event.getStage() != EditSession.Stage.BEFORE_HISTORY) {
064            return;
065        }
066        World worldObj = event.getWorld();
067        if (worldObj == null) {
068            return;
069        }
070        String world = worldObj.getName();
071        Actor actor = event.getActor();
072        if (actor != null && actor.isPlayer()) {
073            String name = actor.getName();
074            final PlotPlayer<?> plotPlayer = PlotSquared.platform().playerManager().getPlayerIfExists(name);
075            Set<CuboidRegion> mask;
076            if (plotPlayer == null) {
077                Player player = (Player) actor;
078                Location location = player.getLocation();
079                com.plotsquared.core.location.Location pLoc = com.plotsquared.core.location.Location.at(
080                        player.getWorld().getName(),
081                        location.toVector().toBlockPoint()
082                );
083                Plot plot = pLoc.getPlot();
084                if (plot == null) {
085                    event.setExtent(new NullExtent());
086                    return;
087                }
088                mask = plot.getRegions();
089            } else if (plotPlayer.getAttribute("worldedit")) {
090                return;
091            } else {
092                mask = WEManager.getMask(plotPlayer);
093                if (mask.isEmpty()) {
094                    if (plotPlayer.hasPermission("plots.worldedit.bypass")) {
095                        plotPlayer.sendMessage(
096                                TranslatableCaption.of("worldedit.worldedit_bypass"),
097                                Template.of("command", "/plot toggle worldedit")
098                        );
099                    }
100                    if (this.plotAreaManager.hasPlotArea(world)) {
101                        event.setExtent(new NullExtent());
102                    }
103                    return;
104                }
105            }
106            if (Settings.Enabled_Components.CHUNK_PROCESSOR) {
107                if (this.plotAreaManager.hasPlotArea(world)) {
108                    event.setExtent(
109                            new ProcessedWEExtent(world, mask, event.getMaxBlocks(), event.getExtent(),
110                                    event.getExtent(), this.worldUtil
111                            ));
112                }
113            } else if (this.plotAreaManager.hasPlotArea(world)) {
114                event.setExtent(new WEExtent(mask, event.getExtent()));
115            }
116        }
117    }
118
119}