Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potion splash event edge case #6296

Merged
merged 2 commits into from
Nov 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 33 additions & 52 deletions src/com/palmergames/bukkit/towny/listeners/TownyEntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,42 +255,44 @@ public void onLingeringPotionSplashEvent(LingeringPotionSplashEvent event) {
return;

ThrownPotion potion = event.getEntity();
Location loc = potion.getLocation();
TownyWorld townyWorld = TownyAPI.getInstance().getTownyWorld(loc.getWorld().getName());
float radius = event.getAreaEffectCloud().getRadius();
List<Block> blocks = new ArrayList<>();

for(double x = loc.getX() - radius; x < loc.getX() + radius; x++ ) {
for(double z = loc.getZ() - radius; z < loc.getZ() + radius; z++ ) {
Location loc2 = new Location(potion.getWorld(), x, loc.getY(), z);
Block b = loc2.getBlock();
if (b.getType().equals(Material.AIR)) blocks.add(b);
}
}

List<PotionEffect> effects = (List<PotionEffect>) potion.getEffects();
boolean detrimental = false;

/*
* List of potion effects blocked from PvP.
*/
List<String> prots = TownySettings.getPotionTypes();
for (PotionEffect effect : effects) {
List<String> detrimentalPotions = TownySettings.getPotionTypes();

for (PotionEffect effect : potion.getEffects()) {

/*
* Check to see if any of the potion effects are protected.
*/
if (prots.contains(effect.getType().getName())) {
if (detrimentalPotions.contains(effect.getType().getName())) {
detrimental = true;
break;
}
}

if (!detrimental)
return;

Location loc = potion.getLocation();
TownyWorld townyWorld = TownyAPI.getInstance().getTownyWorld(loc.getWorld().getName());
float radius = event.getAreaEffectCloud().getRadius();
List<Block> blocks = new ArrayList<>();

for(double x = loc.getX() - radius; x < loc.getX() + radius; x++ ) {
for(double z = loc.getZ() - radius; z < loc.getZ() + radius; z++ ) {
Location loc2 = new Location(potion.getWorld(), x, loc.getY(), z);
Block b = loc2.getBlock();
if (b.getType().equals(Material.AIR)) blocks.add(b);
}
}

for (Block block : blocks) {

if (!TownyAPI.getInstance().isWilderness(block.getLocation())
&& CombatUtil.preventPvP(townyWorld, TownyAPI.getInstance().getTownBlock(block.getLocation()))
&& detrimental) {
&& CombatUtil.preventPvP(townyWorld, TownyAPI.getInstance().getTownBlock(block.getLocation()))) {
event.setCancelled(true);
break;
}
Expand All @@ -313,53 +315,32 @@ public void onPotionSplashEvent(PotionSplashEvent event) {
if (!TownyAPI.getInstance().isTownyWorld(event.getEntity().getWorld()))
return;

List<LivingEntity> affectedEntities = (List<LivingEntity>) event.getAffectedEntities();
ThrownPotion potion = event.getPotion();
Entity attacker = null;

List<PotionEffect> effects = (List<PotionEffect>) potion.getEffects();
boolean detrimental = false;

/*
* List of potion effects blocked from PvP.
*/
List<String> prots = TownySettings.getPotionTypes();
List<String> detrimentalPotions = TownySettings.getPotionTypes();


for (PotionEffect effect : effects) {
for (PotionEffect effect : event.getPotion().getEffects()) {

/*
* Check to see if any of the potion effects are protected.
*/

if (prots.contains(effect.getType().getName())) {
if (detrimentalPotions.contains(effect.getType().getName())) {
detrimental = true;
break;
}

}

Object source = potion.getShooter();
Block dispenser = null;

if (source instanceof BlockProjectileSource blockProjectileSource) {
dispenser = blockProjectileSource.getBlock();
} else {
attacker = (Entity) source;
}

for (LivingEntity defender : affectedEntities) {
if (dispenser != null) {
if (CombatUtil.preventDispenserDamage(dispenser, defender, DamageCause.MAGIC) && detrimental)
event.setIntensity(defender, -1.0);
} else
/*
* Don't block potion use on ourselves
* yet allow the use of beneficial potions on all.
*/
if (attacker != defender)
if (CombatUtil.preventDamageCall(attacker, defender, DamageCause.MAGIC) && detrimental) {
event.setIntensity(defender, -1.0);
}
if (!detrimental)
return;

for (LivingEntity defender : event.getAffectedEntities()) {
if (CombatUtil.preventDamageCall(event.getPotion(), defender, DamageCause.MAGIC)) {
event.setIntensity(defender, -1.0);
}
}
}

Expand Down