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.bukkit.listener;
020
021import com.google.inject.Inject;
022import com.plotsquared.bukkit.player.BukkitPlayer;
023import com.plotsquared.bukkit.util.BukkitUtil;
024import com.plotsquared.core.location.Location;
025import com.plotsquared.core.plot.Plot;
026import com.plotsquared.core.plot.PlotArea;
027import com.plotsquared.core.plot.flag.implementations.CopperOxideFlag;
028import com.plotsquared.core.plot.flag.implementations.MiscInteractFlag;
029import com.plotsquared.core.plot.flag.implementations.SculkSensorInteractFlag;
030import com.plotsquared.core.util.PlotFlagUtil;
031import org.bukkit.block.Block;
032import org.bukkit.entity.Entity;
033import org.bukkit.entity.Item;
034import org.bukkit.entity.Player;
035import org.bukkit.event.EventHandler;
036import org.bukkit.event.EventPriority;
037import org.bukkit.event.Listener;
038import org.bukkit.event.block.BlockFertilizeEvent;
039import org.bukkit.event.block.BlockFormEvent;
040import org.bukkit.event.block.BlockReceiveGameEvent;
041
042import java.util.List;
043import java.util.Objects;
044import java.util.UUID;
045
046@SuppressWarnings("unused")
047public class BlockEventListener117 implements Listener {
048
049    @Inject
050    public BlockEventListener117() {
051    }
052
053    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
054    public void onBlockReceiveGame(BlockReceiveGameEvent event) {
055        Block block = event.getBlock();
056        Location location = BukkitUtil.adapt(block.getLocation());
057        Entity entity = event.getEntity();
058
059        PlotArea area = location.getPlotArea();
060        if (area == null) {
061            return;
062        }
063
064        BukkitPlayer plotPlayer = null;
065
066        if (entity instanceof Player player) {
067            plotPlayer = BukkitUtil.adapt(player);
068            if (area.notifyIfOutsideBuildArea(plotPlayer, location.getY())) {
069                event.setCancelled(true);
070                return;
071            }
072        }
073
074        Plot plot = location.getOwnedPlot();
075        if (plot == null && !PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(
076                area,
077                MiscInteractFlag.class,
078                true
079        ) || plot != null && (!plot.getFlag(MiscInteractFlag.class) || !plot.getFlag(SculkSensorInteractFlag.class))) {
080            if (plotPlayer != null) {
081                if (plot != null) {
082                    if (!plot.isAdded(plotPlayer.getUUID())) {
083                        plot.debug(plotPlayer.getName() + " couldn't trigger sculk sensors because both " +
084                                "sculk-sensor-interact and misc-interact = false");
085                        event.setCancelled(true);
086                    }
087                }
088                return;
089            }
090            if (entity instanceof Item item) {
091                UUID itemThrower = item.getThrower();
092                if (plot != null) {
093                    if (itemThrower == null && (itemThrower = item.getOwner()) == null) {
094                        plot.debug(
095                                "A thrown item couldn't trigger sculk sensors because both sculk-sensor-interact and " +
096                                        "misc-interact = false and the item's owner could not be resolved.");
097                        event.setCancelled(true);
098                        return;
099                    }
100                    if (!plot.isAdded(itemThrower)) {
101                        if (!plot.isAdded(itemThrower)) {
102                            plot.debug("A thrown item couldn't trigger sculk sensors because both sculk-sensor-interact and " +
103                                    "misc-interact = false");
104                            event.setCancelled(true);
105                        }
106                    }
107                }
108            }
109        }
110    }
111
112    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
113    public void onBlockFertilize(BlockFertilizeEvent event) {
114        Block block = event.getBlock();
115        List<org.bukkit.block.BlockState> blocks = event.getBlocks();
116        Location location = BukkitUtil.adapt(block.getLocation());
117
118        PlotArea area = location.getPlotArea();
119        if (area == null) {
120            for (int i = blocks.size() - 1; i >= 0; i--) {
121                Location blockLocation = BukkitUtil.adapt(blocks.get(i).getLocation());
122                if (blockLocation.isPlotArea()) {
123                    blocks.remove(i);
124                }
125            }
126        } else {
127            Plot origin = area.getOwnedPlot(location);
128            if (origin == null) {
129                event.setCancelled(true);
130                return;
131            }
132            for (int i = blocks.size() - 1; i >= 0; i--) {
133                Location blockLocation = BukkitUtil.adapt(blocks.get(i).getLocation());
134                if (!area.contains(blockLocation.getX(), blockLocation.getZ())) {
135                    blocks.remove(i);
136                    continue;
137                }
138                Plot plot = area.getOwnedPlot(blockLocation);
139                if (!Objects.equals(plot, origin)) {
140                    event.getBlocks().remove(i);
141                    continue;
142                }
143                if (!area.buildRangeContainsY(location.getY())) {
144                    event.getBlocks().remove(i);
145                }
146            }
147        }
148    }
149
150    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
151    public void onBlockForm(BlockFormEvent event) {
152        Block block = event.getBlock();
153        Location location = BukkitUtil.adapt(block.getLocation());
154        if (location.isPlotRoad()) {
155            event.setCancelled(true);
156            return;
157        }
158        PlotArea area = location.getPlotArea();
159        if (area == null) {
160            return;
161        }
162        Plot plot = area.getOwnedPlot(location);
163        if (plot == null) {
164            return;
165        }
166        if (event.getNewState().getType().name().contains("COPPER")) {
167            if (!plot.getFlag(CopperOxideFlag.class)) {
168                plot.debug("Copper could not oxide because copper-oxide = false");
169                event.setCancelled(true);
170            }
171        }
172    }
173
174}