Better protection for potion effects

This commit is contained in:
destro174 2022-03-05 17:18:00 +01:00
parent 5e8bbe75c3
commit 3436ddcd7e

View File

@ -62,6 +62,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.EntityBlockFormEvent;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityBreakDoorEvent;
@ -80,6 +81,7 @@ import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.entity.ItemMergeEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.entity.LingeringPotionSplashEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
@ -1489,6 +1491,72 @@ public class EntityEventHandler implements Listener
}
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onAreaEffectCloudApplyEvent(AreaEffectCloudApplyEvent event)
{
ProjectileSource projectileSource = event.getEntity().getSource();
if (projectileSource == null) return;
Player thrower = null;
if ((projectileSource instanceof Player))
thrower = (Player) projectileSource;
boolean messagedPlayer = false;
Collection<PotionEffect> effects = event.getEntity().getCustomEffects();
for (PotionEffect effect : effects)
{
PotionEffectType effectType = effect.getType();
// Restrict some potions on claimed villagers and animals.
// Griefers could use potions to kill entities or steal them over fences.
if (PotionEffectType.HARM.equals(effectType)
|| PotionEffectType.POISON.equals(effectType)
|| PotionEffectType.JUMP.equals(effectType)
|| PotionEffectType.WITHER.equals(effectType))
{
Claim cachedClaim = null;
for (LivingEntity affected : event.getAffectedEntities())
{
// Always impact the thrower.
if (affected == thrower) continue;
if (affected.getType() == EntityType.VILLAGER || affected instanceof Animals)
{
Claim claim = this.dataStore.getClaimAt(affected.getLocation(), false, cachedClaim);
if (claim != null)
{
cachedClaim = claim;
if (thrower == null)
{
// Non-player source: Witches, dispensers, etc.
if (!isBlockSourceInClaim(projectileSource, claim))
{
// If the source is not a block in the same claim as the affected entity, disallow.
affected.removePotionEffect(effectType);
}
}
else
{
// Source is a player. Determine if they have permission to access entities in the claim.
Supplier<String> override = () -> instance.dataStore.getMessage(Messages.NoDamageClaimedEntity, claim.getOwnerName());
final Supplier<String> noContainersReason = claim.checkPermission(thrower, ClaimPermission.Inventory, event, override);
if (noContainersReason != null)
{
affected.removePotionEffect(effectType);
if (!messagedPlayer)
{
GriefPrevention.sendMessage(thrower, TextMode.Err, noContainersReason.get());
messagedPlayer = true;
}
}
}
}
}
}
}
}
}
public static final HashSet<PotionEffectType> positiveEffects = new HashSet<>(Arrays.asList
(