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