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