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