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