Added AntigriefManager support to Instability enchantment

This commit is contained in:
_OfTeN_ 2022-01-24 23:09:34 +03:00
parent c538a007da
commit 84e57fb2d6

View File

@ -5,20 +5,40 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class Instability extends EcoEnchant {
private static final List<Entity> toProtect = new ArrayList<>();
public Instability() {
super(
"instability", EnchantmentType.SPECIAL
);
}
@EventHandler
public void onInstabilityExplode(@NotNull final EntityDamageEvent event) {
if (!toProtect.contains(event.getEntity())) {
return;
}
if (event.getCause() != EntityDamageEvent.DamageCause.ENTITY_EXPLOSION && event.getCause()
!= EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) {
return;
}
event.setCancelled(true);
toProtect.remove(event.getEntity());
}
@EventHandler
public void onInstabilityLand(@NotNull final ProjectileHitEvent event) {
if (event.getEntityType() != EntityType.ARROW) {
@ -60,6 +80,16 @@ public class Instability extends EcoEnchant {
breakblocks = AntigriefManager.canBreakBlock(player, event.getEntity().getLocation().getWorld().getBlockAt(event.getEntity().getLocation()));
}
List<Entity> toAdd = event.getEntity().getNearbyEntities(power, power, power)
.stream().filter(entity -> entity instanceof LivingEntity && !AntigriefManager.canInjure(player, (LivingEntity) entity))
.toList();
toProtect.addAll(
toAdd
);
this.getPlugin().getScheduler().runLater(() -> toProtect.removeAll(toAdd), 20);
event.getEntity().getWorld().createExplosion(event.getEntity().getLocation().getX(), event.getEntity().getLocation().getY(), event.getEntity().getLocation().getZ(), power, fire, breakblocks);
event.getEntity().remove();