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