Files
src
build.gradle
Code
5 229
src/main/java/com/dragoneggguard/DragonEggGuard.java
package com.dragoneggguard;
import com.cryptomorin.xseries.XMaterial;
import com.dragoneggguard.commands.RemoveDragonEggCommand;
import com.dragoneggguard.listeners.EggListener;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
public class DragonEggGuard extends JavaPlugin {
private Material eggMaterial;
@Override
public void onEnable() {
eggMaterial = XMaterial.matchXMaterial("DRAGON_EGG").map(XMaterial::parseMaterial).orElse(Material.AIR);
getServer().getPluginManager().registerEvents(new EggListener(eggMaterial, this), this);
getCommand("removedragonegg").setExecutor(new RemoveDragonEggCommand(eggMaterial, this));
startEffectTask();
getLogger().info("DragonEggGuard enabled.");
}
@Override
public void onDisable() {
getLogger().info("DragonEggGuard disabled.");
}
private void startEffectTask() {
new BukkitRunnable() {
@Override
public void run() {
for (Player player : getServer().getOnlinePlayers()) {
if (player.getInventory().contains(eggMaterial)) {
applyEffects(player);
}
}
}
}.runTaskTimer(this, 0L, 40L);
}
private void applyEffects(Player player) {
XPotion.matchXPotion("ABSORPTION").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 3)));
XPotion.matchXPotion("REGENERATION").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 1)));
XPotion.matchXPotion("DAMAGE_RESISTANCE").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 0)));
XPotion.matchXPotion("FIRE_RESISTANCE").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 0)));
}
public void clearEggEffects(Player player) {
XPotion.matchXPotion("ABSORPTION").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
XPotion.matchXPotion("REGENERATION").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
XPotion.matchXPotion("DAMAGE_RESISTANCE").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
XPotion.matchXPotion("FIRE_RESISTANCE").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
}
}