diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java index 1be9984..eb94f98 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java @@ -375,24 +375,6 @@ public class EcoBoss extends PluginDependent { )); } - // Spawn egg - /* - Material eggMaterial = Material.matchMaterial(this.getConfig().getString("spawn-egg.egg-material").toUpperCase()); - assert eggMaterial != null; - this.spawnEgg = new ItemStack(eggMaterial); - SpawnEggMeta meta = (SpawnEggMeta) this.spawnEgg.getItemMeta(); - assert meta != null; - List lore = new ArrayList<>(); - for (String string : this.getConfig().getStrings("spawn-egg.lore")) { - lore.add(StringUtils.translate(string)); - } - meta.setLore(lore); - meta.setDisplayName(this.getConfig().getString("spawn-egg.display-name")); - meta.getPersistentDataContainer().set(this.getPlugin().getNamespacedKeyFactory().create("spawn_egg"), PersistentDataType.STRING, this.getName()); - this.spawnEgg.setItemMeta(meta); - - */ - // Messages this.spawnMessages = new ArrayList<>(); for (String string : this.getConfig().getStrings("broadcast.spawn")) { @@ -453,13 +435,23 @@ public class EcoBoss extends PluginDependent { public void spawn(@NotNull final Location location) { LivingEntity entity = bossType.spawnBossEntity(location); this.livingBosses.put(entity.getUniqueId(), new LivingEcoBoss( - this.getPlugin(), - entity, - this + this.getPlugin(), + entity, + this ) ); } + /** + * Get {@link LivingEcoBoss} from an entity. + * + * @param entity The entity. + * @return The living boss, or null if not a boss. + */ + public LivingEcoBoss getLivingBoss(@NotNull final LivingEntity entity) { + return this.livingBosses.get(entity.getUniqueId()); + } + /** * Remove living boss. * @@ -469,60 +461,6 @@ public class EcoBoss extends PluginDependent { this.livingBosses.remove(uuid); } - /** - * Handle an attack to the player. - * - * @param entity The entity. - * @param player The player. - */ - public void handleAttack(@NotNull final LivingEntity entity, - @NotNull final Player player) { - for (OptionedSound sound : this.getInjureSounds()) { - player.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch()); - } - - for (EffectOption effect : this.getEffects()) { - if (NumberUtils.randFloat(0, 100) > effect.getChance()) { - continue; - } - - player.addPotionEffect(new PotionEffect(effect.getEffectType(), effect.getDuration(), effect.getLevel())); - } - - if (NumberUtils.randFloat(0, 100) < this.getShuffleChance()) { - List hotbar = new ArrayList<>(); - for (int i = 0; i < 9; i++) { - hotbar.add(player.getInventory().getItem(i)); - } - Collections.shuffle(hotbar); - int i2 = 0; - for (ItemStack item : hotbar) { - player.getInventory().setItem(i2, item); - i2++; - } - player.playSound(player.getLocation(), Sound.ENTITY_ENDER_PEARL_THROW, 1, 0.5f); - } - - for (SummonsOption summon : this.getSummons()) { - if (NumberUtils.randFloat(0, 100) > summon.getChance()) { - continue; - } - - Location loc = player.getLocation().add(NumberUtils.randInt(2, 6), 0, NumberUtils.randInt(2, 6)); - while (!loc.getBlock().getType().equals(Material.AIR)) { - loc.add(0, 1, 0); - } - Entity summonedEntity = player.getWorld().spawnEntity(loc, summon.getType()); - if (summonedEntity instanceof Mob) { - ((Mob) summonedEntity).setTarget(player); - } - - for (OptionedSound sound : this.getSummonSounds()) { - player.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch()); - } - } - } - @Override public boolean equals(final Object o) { if (this == o) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java index 6a7345b..39e9a4b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java @@ -1,5 +1,6 @@ package com.willfp.ecobosses.bosses; +import com.willfp.eco.util.NumberUtils; import com.willfp.eco.util.StringUtils; import com.willfp.eco.util.bukkit.scheduling.EcoBukkitRunnable; import com.willfp.eco.util.internal.PluginDependent; @@ -8,7 +9,12 @@ import com.willfp.ecobosses.bosses.tick.BossTicker; import com.willfp.ecobosses.bosses.tick.tickers.BossBarTicker; import com.willfp.ecobosses.bosses.tick.tickers.HealthPlaceholderTicker; import com.willfp.ecobosses.bosses.util.obj.OptionedSound; +import com.willfp.ecobosses.bosses.util.obj.attacks.EffectOption; +import com.willfp.ecobosses.bosses.util.obj.attacks.SummonsOption; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.boss.BarFlag; @@ -16,10 +22,15 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Mob; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; @@ -80,6 +91,58 @@ public class LivingEcoBoss extends PluginDependent { }).runTaskTimer(0, 1); } + /** + * Handle an attack to the player. + * + * @param player The player. + */ + public void handleAttack(@NotNull final Player player) { + for (OptionedSound sound : boss.getInjureSounds()) { + player.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch()); + } + + for (EffectOption effect : boss.getEffects()) { + if (NumberUtils.randFloat(0, 100) > effect.getChance()) { + continue; + } + + player.addPotionEffect(new PotionEffect(effect.getEffectType(), effect.getDuration(), effect.getLevel())); + } + + if (NumberUtils.randFloat(0, 100) < boss.getShuffleChance()) { + List hotbar = new ArrayList<>(); + for (int i = 0; i < 9; i++) { + hotbar.add(player.getInventory().getItem(i)); + } + Collections.shuffle(hotbar); + int i2 = 0; + for (ItemStack item : hotbar) { + player.getInventory().setItem(i2, item); + i2++; + } + player.playSound(player.getLocation(), Sound.ENTITY_ENDER_PEARL_THROW, 1, 0.5f); + } + + for (SummonsOption summon : boss.getSummons()) { + if (NumberUtils.randFloat(0, 100) > summon.getChance()) { + continue; + } + + Location loc = player.getLocation().add(NumberUtils.randInt(2, 6), 0, NumberUtils.randInt(2, 6)); + while (!loc.getBlock().getType().equals(Material.AIR)) { + loc.add(0, 1, 0); + } + Entity summonedEntity = player.getWorld().spawnEntity(loc, summon.getType()); + if (summonedEntity instanceof Mob) { + ((Mob) summonedEntity).setTarget(player); + } + + for (OptionedSound sound : boss.getSummonSounds()) { + player.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch()); + } + } + } + private void onSpawn() { entity.getPersistentDataContainer().set(this.getPlugin().getNamespacedKeyFactory().create("boss"), PersistentDataType.STRING, boss.getName()); entity.setPersistent(true); @@ -130,6 +193,7 @@ public class LivingEcoBoss extends PluginDependent { ticker.onDeath(boss, entity, tick); } runnable.cancel(); + boss.removeLivingBoss(entity.getUniqueId()); } } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AttackListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AttackListeners.java index 1269e5c..12f9819 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AttackListeners.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AttackListeners.java @@ -4,6 +4,7 @@ import com.willfp.eco.util.NumberUtils; import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.LivingEcoBoss; import com.willfp.ecobosses.bosses.util.BossUtils; import com.willfp.ecobosses.bosses.util.obj.DamagerProperty; import com.willfp.ecobosses.bosses.util.obj.ImmunityOptions; @@ -70,8 +71,10 @@ public class AttackListeners extends PluginDependent implements Listener { return; } + LivingEcoBoss livingEcoBoss = boss.getLivingBoss(entity); + if (boss.isAttackOnInjure()) { - boss.handleAttack(entity, player); + livingEcoBoss.handleAttack(player); } } @@ -150,7 +153,9 @@ public class AttackListeners extends PluginDependent implements Listener { return; } - boss.handleAttack(entity, player); + LivingEcoBoss livingEcoBoss = boss.getLivingBoss(entity); + + livingEcoBoss.handleAttack(player); } /**