Skip to content

Instantly share code, notes, and snippets.

@Phoenix616
Last active May 4, 2016 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phoenix616/5b20112312de6afbe2741ab5314b9669 to your computer and use it in GitHub Desktop.
Save Phoenix616/5b20112312de6afbe2741ab5314b9669 to your computer and use it in GitHub Desktop.
Fix the usage of lingering potions in PvP with WG6
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.RegionContainer;
import com.sk89q.worldguard.bukkit.RegionQuery;
import com.sk89q.worldguard.bukkit.WGBukkit;
import com.sk89q.worldguard.bukkit.util.Materials;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.potion.PotionEffect;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Copyright (C) 2016 Max Lee (https://github.com/Phoenix616/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License as published by
* the Mozilla Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v2.0 for more details.
*
* You should have received a copy of the Mozilla Public License v2.0
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public class LingeringPotionPvpFix extends JavaPlugin implements Listener {
public void onEnable() {
if(getServer().getPluginManager().getPlugin("WorldGuard") != null && Integer.parseInt(WGBukkit.getPlugin().getDescription().getVersion().split("\\.")[0]) >= 6)
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onAreaEffect(AreaEffectCloudApplyEvent event) {
if(!(event.getEntity().getSource() instanceof Player))
return;
boolean affectsPlayer = false;
for(LivingEntity e : event.getAffectedEntities()) {
if(e instanceof Player) {
affectsPlayer = true;
break;
}
}
if(!affectsPlayer)
return;
Set<PotionEffect> effects = new HashSet<PotionEffect>(event.getEntity().getCustomEffects());
effects.add(new PotionEffect(event.getEntity().getBasePotionData().getType().getEffectType(), event.getEntity().getDuration(), 1));
if(!Materials.hasDamageEffect(effects))
return;
RegionContainer container = WGBukkit.getPlugin().getRegionContainer();
RegionQuery query = container.createQuery();
Iterator<LivingEntity> it = event.getAffectedEntities().iterator();
while(it.hasNext()) {
LivingEntity e = it.next();
if(e instanceof Player && e != event.getEntity().getSource()) {
boolean pvp = e.getWorld().getPVP() && query.queryState(e.getLocation(), WGBukkit.getPlugin().wrapPlayer((Player) e), DefaultFlag.PVP) != StateFlag.State.DENY;
if(!pvp) {
it.remove();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment