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.util.BukkitEntityUtil;
023import com.plotsquared.bukkit.util.BukkitUtil;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.location.Location;
026import com.plotsquared.core.permissions.Permission;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.plot.PlotArea;
030import com.plotsquared.core.plot.PlotHandler;
031import com.plotsquared.core.plot.flag.implementations.ProjectilesFlag;
032import com.plotsquared.core.plot.world.PlotAreaManager;
033import com.plotsquared.core.util.PlotFlagUtil;
034import net.kyori.adventure.text.minimessage.tag.Tag;
035import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
036import org.bukkit.entity.Entity;
037import org.bukkit.entity.LivingEntity;
038import org.bukkit.entity.Player;
039import org.bukkit.entity.Projectile;
040import org.bukkit.entity.ThrownPotion;
041import org.bukkit.event.EventHandler;
042import org.bukkit.event.EventPriority;
043import org.bukkit.event.Listener;
044import org.bukkit.event.entity.LingeringPotionSplashEvent;
045import org.bukkit.event.entity.PotionSplashEvent;
046import org.bukkit.event.entity.ProjectileHitEvent;
047import org.bukkit.event.entity.ProjectileLaunchEvent;
048import org.bukkit.projectiles.BlockProjectileSource;
049import org.bukkit.projectiles.ProjectileSource;
050import org.checkerframework.checker.nullness.qual.NonNull;
051
052@SuppressWarnings("unused")
053public class ProjectileEventListener implements Listener {
054
055    private final PlotAreaManager plotAreaManager;
056
057    @Inject
058    public ProjectileEventListener(final @NonNull PlotAreaManager plotAreaManager) {
059        this.plotAreaManager = plotAreaManager;
060    }
061
062    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
063    public void onLingeringPotionSplash(LingeringPotionSplashEvent event) {
064        // Cancelling projectile hit events still results in area effect clouds.
065        // We need to cancel the splash events to get rid of those.
066        onProjectileHit(event);
067    }
068
069    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
070    public void onPotionSplash(PotionSplashEvent event) {
071        ThrownPotion damager = event.getPotion();
072        Location location = BukkitUtil.adapt(damager.getLocation());
073        if (!this.plotAreaManager.hasPlotArea(location.getWorldName())) {
074            return;
075        }
076        int count = 0;
077        for (LivingEntity victim : event.getAffectedEntities()) {
078            if (!BukkitEntityUtil.entityDamage(damager, victim)) {
079                event.setIntensity(victim, 0);
080                count++;
081            }
082        }
083        if (count > 0 && count == event.getAffectedEntities().size()) {
084            event.setCancelled(true);
085        } else {
086            // Cancelling projectile hit events still results in potions
087            // splashing in the world. We need to cancel the splash events to
088            // avoid that.
089            onProjectileHit(event);
090        }
091    }
092
093    @EventHandler(ignoreCancelled = true)
094    public void onProjectileLaunch(ProjectileLaunchEvent event) {
095        Projectile entity = event.getEntity();
096        ProjectileSource shooter = entity.getShooter();
097        if (!(shooter instanceof Player)) {
098            return;
099        }
100        Location location = BukkitUtil.adapt(entity.getLocation());
101        PlotArea area = location.getPlotArea();
102        if (area == null) {
103            return;
104        }
105        PlotPlayer<Player> pp = BukkitUtil.adapt((Player) shooter);
106        Plot plot = location.getOwnedPlot();
107
108        if (plot == null) {
109            if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true) && !pp.hasPermission(
110                    Permission.PERMISSION_ADMIN_PROJECTILE_ROAD
111            )) {
112                pp.sendMessage(
113                        TranslatableCaption.of("permission.no_permission_event"),
114                        TagResolver.resolver(
115                                "node",
116                                Tag.inserting(Permission.PERMISSION_ADMIN_PROJECTILE_ROAD)
117                        )
118                );
119                entity.remove();
120                event.setCancelled(true);
121            }
122        } else if (!plot.hasOwner()) {
123            if (!pp.hasPermission(Permission.PERMISSION_ADMIN_PROJECTILE_UNOWNED)) {
124                pp.sendMessage(
125                        TranslatableCaption.of("permission.no_permission_event"),
126                        TagResolver.resolver(
127                                "node",
128                                Tag.inserting(Permission.PERMISSION_ADMIN_PROJECTILE_UNOWNED)
129                        )
130                );
131                entity.remove();
132                event.setCancelled(true);
133            }
134        } else if (!plot.isAdded(pp.getUUID())) {
135            if (!plot.getFlag(ProjectilesFlag.class)) {
136                if (!pp.hasPermission(Permission.PERMISSION_ADMIN_PROJECTILE_OTHER)) {
137                    pp.sendMessage(
138                            TranslatableCaption.of("permission.no_permission_event"),
139                            TagResolver.resolver(
140                                    "node",
141                                    Tag.inserting(Permission.PERMISSION_ADMIN_PROJECTILE_OTHER)
142                            )
143                    );
144                    entity.remove();
145                    event.setCancelled(true);
146                }
147            }
148        }
149    }
150
151    @EventHandler
152    public void onProjectileHit(ProjectileHitEvent event) {
153        Projectile entity = event.getEntity();
154        Location location = BukkitUtil.adapt(entity.getLocation());
155        if (!this.plotAreaManager.hasPlotArea(location.getWorldName())) {
156            return;
157        }
158        PlotArea area = location.getPlotArea();
159        if (area == null) {
160            return;
161        }
162        Plot plot = area.getPlot(location);
163        ProjectileSource shooter = entity.getShooter();
164        if (shooter instanceof Player) {
165            if (!((Player) shooter).isOnline()) {
166                if (plot != null) {
167                    if (plot.isAdded(((Player) shooter).getUniqueId()) || plot.getFlag(ProjectilesFlag.class)) {
168                        return;
169                    }
170                } else if (PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true)) {
171                    return;
172                }
173
174                entity.remove();
175                event.setCancelled(true);
176                return;
177            }
178
179            PlotPlayer<?> pp = BukkitUtil.adapt((Player) shooter);
180            if (plot == null) {
181                if (!PlotFlagUtil.isAreaRoadFlagsAndFlagEquals(area, ProjectilesFlag.class, true) && !pp.hasPermission(
182                        Permission.PERMISSION_ADMIN_PROJECTILE_UNOWNED
183                )) {
184                    entity.remove();
185                    event.setCancelled(true);
186                }
187                return;
188            }
189            if (plot.isAdded(pp.getUUID()) || pp.hasPermission(Permission.PERMISSION_ADMIN_PROJECTILE_OTHER) || plot.getFlag(
190                    ProjectilesFlag.class)) {
191                return;
192            }
193            entity.remove();
194            event.setCancelled(true);
195            return;
196        }
197        if (!(shooter instanceof Entity) && shooter != null) {
198            if (plot == null) {
199                entity.remove();
200                event.setCancelled(true);
201                return;
202            }
203            Location sLoc =
204                    BukkitUtil.adapt(((BlockProjectileSource) shooter).getBlock().getLocation());
205            if (!area.contains(sLoc.getX(), sLoc.getZ())) {
206                entity.remove();
207                event.setCancelled(true);
208                return;
209            }
210            Plot sPlot = area.getOwnedPlotAbs(sLoc);
211            if (sPlot == null || !PlotHandler.sameOwners(plot, sPlot)) {
212                entity.remove();
213                event.setCancelled(true);
214            }
215        }
216    }
217
218}