mirror of
https://github.com/Auxilor/EcoMobs.git
synced 2025-12-22 00:19:30 +00:00
Java 16 migration, more null checks, record conversion
This commit is contained in:
@@ -500,11 +500,10 @@ public class EcoBoss extends PluginDependent {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof EcoBoss)) {
|
||||
if (!(o instanceof EcoBoss boss)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EcoBoss boss = (EcoBoss) o;
|
||||
return this.getName().equals(boss.getName());
|
||||
}
|
||||
|
||||
|
||||
@@ -83,8 +83,8 @@ public class LivingEcoBoss extends PluginDependent {
|
||||
Bukkit.getServer().createBossBar(
|
||||
plugin.getNamespacedKeyFactory().create("boss_" + NumberUtils.randInt(0, 1000000)),
|
||||
entity.getCustomName(),
|
||||
boss.getBossbarProperties().getColor(),
|
||||
boss.getBossbarProperties().getStyle(),
|
||||
boss.getBossbarProperties().color(),
|
||||
boss.getBossbarProperties().style(),
|
||||
(BarFlag) null
|
||||
),
|
||||
this.getPlugin().getConfigYml().getInt("bossbar-radius")
|
||||
@@ -127,7 +127,7 @@ public class LivingEcoBoss extends PluginDependent {
|
||||
attackDamage.setBaseValue(boss.getAttackDamage());
|
||||
|
||||
for (OptionedSound sound : boss.getSpawnSounds()) {
|
||||
entity.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch());
|
||||
entity.getWorld().playSound(entity.getLocation(), sound.sound(), sound.volume(), sound.pitch());
|
||||
}
|
||||
|
||||
for (String spawnMessage : boss.getSpawnMessages()) {
|
||||
@@ -170,7 +170,7 @@ public class LivingEcoBoss extends PluginDependent {
|
||||
*/
|
||||
public void handleAttack(@NotNull final Player player) {
|
||||
for (OptionedSound sound : boss.getInjureSounds()) {
|
||||
player.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch());
|
||||
player.getWorld().playSound(entity.getLocation(), sound.sound(), sound.volume(), sound.pitch());
|
||||
}
|
||||
|
||||
for (Effect effect : effects) {
|
||||
|
||||
@@ -91,10 +91,9 @@ public abstract class Effect implements BossTicker {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof Effect)) {
|
||||
if (!(o instanceof Effect effect)) {
|
||||
return false;
|
||||
}
|
||||
Effect effect = (Effect) o;
|
||||
return Objects.equals(getArgs(), effect.getArgs());
|
||||
}
|
||||
|
||||
|
||||
@@ -38,21 +38,11 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onAttackBoss(@NotNull final EntityDamageByEntityEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity entity = (LivingEntity) event.getEntity();
|
||||
|
||||
Player player = null;
|
||||
|
||||
if (event.getDamager() instanceof Player) {
|
||||
player = (Player) event.getDamager();
|
||||
} else if (event.getDamager() instanceof Projectile) {
|
||||
if (((Projectile) event.getDamager()).getShooter() instanceof Player) {
|
||||
player = (Player) ((Projectile) event.getDamager()).getShooter();
|
||||
}
|
||||
}
|
||||
Player player = BossUtils.getPlayerFromEntity(event.getDamager());
|
||||
|
||||
if (player == null) {
|
||||
return;
|
||||
@@ -66,6 +56,8 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
|
||||
LivingEcoBoss livingEcoBoss = boss.getLivingBoss(entity);
|
||||
|
||||
BossUtils.warnIfNull(livingEcoBoss);
|
||||
|
||||
if (boss.isAttackOnInjure()) {
|
||||
livingEcoBoss.handleAttack(player);
|
||||
}
|
||||
@@ -78,28 +70,16 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
|
||||
public void topDamageTracker(@NotNull final EntityDamageByEntityEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity entity = (LivingEntity) event.getEntity();
|
||||
Player player = BossUtils.getPlayerFromEntity(event.getDamager());
|
||||
|
||||
Player temp = null;
|
||||
|
||||
if (event.getDamager() instanceof Player) {
|
||||
temp = (Player) event.getDamager();
|
||||
} else if (event.getDamager() instanceof Projectile) {
|
||||
if (((Projectile) event.getDamager()).getShooter() instanceof Player) {
|
||||
temp = (Player) ((Projectile) event.getDamager()).getShooter();
|
||||
}
|
||||
}
|
||||
|
||||
if (temp == null) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player player = temp;
|
||||
|
||||
EcoBoss boss = BossUtils.getBoss(entity);
|
||||
|
||||
if (boss == null) {
|
||||
@@ -110,11 +90,11 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
|
||||
double playerDamage;
|
||||
|
||||
Optional<DamagerProperty> damager = topDamagers.stream().filter(damagerProperty -> damagerProperty.getPlayerUUID().equals(player.getUniqueId())).findFirst();
|
||||
playerDamage = damager.map(DamagerProperty::getDamage).orElse(0.0);
|
||||
Optional<DamagerProperty> damager = topDamagers.stream().filter(damagerProperty -> damagerProperty.playerUUID().equals(player.getUniqueId())).findFirst();
|
||||
playerDamage = damager.map(DamagerProperty::damage).orElse(0.0);
|
||||
|
||||
playerDamage += event.getFinalDamage();
|
||||
topDamagers.removeIf(damagerProperty -> damagerProperty.getPlayerUUID().equals(player.getUniqueId()));
|
||||
topDamagers.removeIf(damagerProperty -> damagerProperty.playerUUID().equals(player.getUniqueId()));
|
||||
topDamagers.add(new DamagerProperty(player.getUniqueId(), playerDamage));
|
||||
|
||||
entity.removeMetadata("ecobosses-top-damagers", this.getPlugin());
|
||||
@@ -128,18 +108,14 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onAttackPlayer(@NotNull final EntityDamageByEntityEvent event) {
|
||||
if (!(event.getEntity() instanceof Player)) {
|
||||
if (!(event.getEntity() instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(event.getDamager() instanceof LivingEntity)) {
|
||||
if (!(event.getDamager() instanceof LivingEntity entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity entity = (LivingEntity) event.getDamager();
|
||||
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
EcoBoss boss = BossUtils.getBoss(entity);
|
||||
|
||||
if (boss == null) {
|
||||
@@ -148,9 +124,7 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
|
||||
LivingEcoBoss livingEcoBoss = boss.getLivingBoss(entity);
|
||||
|
||||
if (livingEcoBoss == null) {
|
||||
return;
|
||||
}
|
||||
BossUtils.warnIfNull(livingEcoBoss);
|
||||
|
||||
livingEcoBoss.handleAttack(player);
|
||||
}
|
||||
@@ -162,12 +136,10 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
|
||||
public void defenceListener(@NotNull final EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity entity = (LivingEntity) event.getEntity();
|
||||
|
||||
EcoBoss boss = BossUtils.getBoss(entity);
|
||||
|
||||
if (boss == null) {
|
||||
@@ -176,22 +148,22 @@ public class AttackListeners extends PluginDependent implements Listener {
|
||||
|
||||
ImmunityOptions immunities = boss.getImmunityOptions();
|
||||
|
||||
if (immunities.isImmuneToFire()
|
||||
if (immunities.immuneToFire()
|
||||
&& (event.getCause() == EntityDamageEvent.DamageCause.FIRE
|
||||
|| event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK
|
||||
|| event.getCause() == EntityDamageEvent.DamageCause.HOT_FLOOR)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
if (immunities.isImmuneToSuffocation()&& event.getCause() == EntityDamageEvent.DamageCause.SUFFOCATION) {
|
||||
if (immunities.immuneToSuffocation()&& event.getCause() == EntityDamageEvent.DamageCause.SUFFOCATION) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
if (immunities.isImmuneToDrowning() && event.getCause() == EntityDamageEvent.DamageCause.DROWNING) {
|
||||
if (immunities.immuneToDrowning() && event.getCause() == EntityDamageEvent.DamageCause.DROWNING) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
if (immunities.isImmuneToExplosions() && (event.getCause() == EntityDamageEvent.DamageCause.ENTITY_EXPLOSION || event.getCause() == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
|
||||
if (immunities.immuneToExplosions() && (event.getCause() == EntityDamageEvent.DamageCause.ENTITY_EXPLOSION || event.getCause() == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
if (immunities.isImmuneToProjectiles() && (event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE)) {
|
||||
if (immunities.immuneToProjectiles() && (event.getCause() == EntityDamageEvent.DamageCause.PROJECTILE)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ public class DeathListeners extends PluginDependent implements Listener {
|
||||
}
|
||||
|
||||
for (OptionedSound sound : boss.getDeathSounds()) {
|
||||
entity.getWorld().playSound(entity.getLocation(), sound.getSound(), sound.getVolume(), sound.getPitch());
|
||||
entity.getWorld().playSound(entity.getLocation(), sound.sound(), sound.volume(), sound.pitch());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,14 +89,14 @@ public class DeathListeners extends PluginDependent implements Listener {
|
||||
|
||||
String na = this.getPlugin().getLangYml().getString("na");
|
||||
|
||||
String topDamager = top == null ? na : Bukkit.getPlayer(top.getPlayerUUID()).getDisplayName();
|
||||
String topDamage = top == null ? na : StringUtils.internalToString(top.getDamage());
|
||||
String topDamager = top == null ? na : Bukkit.getPlayer(top.playerUUID()).getDisplayName();
|
||||
String topDamage = top == null ? na : StringUtils.internalToString(top.damage());
|
||||
|
||||
String secondDamager = second == null ? na : Bukkit.getPlayer(second.getPlayerUUID()).getDisplayName();
|
||||
String secondDamage = second == null ? na : StringUtils.internalToString(second.getDamage());
|
||||
String secondDamager = second == null ? na : Bukkit.getPlayer(second.playerUUID()).getDisplayName();
|
||||
String secondDamage = second == null ? na : StringUtils.internalToString(second.damage());
|
||||
|
||||
String thirdDamager = third == null ? na : Bukkit.getPlayer(third.getPlayerUUID()).getDisplayName();
|
||||
String thirdDamage = third == null ? na : StringUtils.internalToString(third.getDamage());
|
||||
String thirdDamager = third == null ? na : Bukkit.getPlayer(third.playerUUID()).getDisplayName();
|
||||
String thirdDamage = third == null ? na : StringUtils.internalToString(third.damage());
|
||||
|
||||
for (String spawnMessage : boss.getDeathMessages()) {
|
||||
Bukkit.broadcastMessage(spawnMessage
|
||||
@@ -114,17 +114,17 @@ public class DeathListeners extends PluginDependent implements Listener {
|
||||
for (Pair<Double, String> pair : topDamagerCommands) {
|
||||
if (top != null && i == 1) {
|
||||
if (NumberUtils.randFloat(0, 100) < pair.getFirst()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(top.getPlayerUUID()).getName()));
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(top.playerUUID()).getName()));
|
||||
}
|
||||
}
|
||||
if (second != null && i == 2) {
|
||||
if (NumberUtils.randFloat(0, 100) < pair.getFirst()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(second.getPlayerUUID()).getName()));
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(second.playerUUID()).getName()));
|
||||
}
|
||||
}
|
||||
if (third != null && i == 3) {
|
||||
if (NumberUtils.randFloat(0, 100) < pair.getFirst()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(third.getPlayerUUID()).getName()));
|
||||
Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), pair.getSecond().replace("%player%", Bukkit.getOfflinePlayer(third.playerUUID()).getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class PassiveListeners extends PluginDependent implements Listener {
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onAttackBoss(@NotNull final EntityMountEvent event) {
|
||||
if (!(event.getEntity() instanceof LivingEntity)) {
|
||||
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -37,8 +37,6 @@ public class PassiveListeners extends PluginDependent implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
LivingEntity entity = (LivingEntity) event.getEntity();
|
||||
|
||||
EcoBoss boss = BossUtils.getBoss(entity);
|
||||
|
||||
if (boss == null) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.ecobosses.bosses.util;
|
||||
import com.willfp.ecobosses.EcoBossesPlugin;
|
||||
import com.willfp.ecobosses.bosses.EcoBoss;
|
||||
import com.willfp.ecobosses.bosses.EcoBosses;
|
||||
import com.willfp.ecobosses.bosses.LivingEcoBoss;
|
||||
import com.willfp.ecobosses.bosses.util.obj.DamagerProperty;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -11,6 +12,8 @@ import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.boss.KeyedBossBar;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -69,7 +72,7 @@ public class BossUtils {
|
||||
}
|
||||
assert topDamagers != null;
|
||||
|
||||
topDamagers.sort(Comparator.comparingDouble(DamagerProperty::getDamage));
|
||||
topDamagers.sort(Comparator.comparingDouble(DamagerProperty::damage));
|
||||
Collections.reverse(topDamagers);
|
||||
|
||||
return topDamagers;
|
||||
@@ -131,4 +134,39 @@ public class BossUtils {
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get player from entity if player or projectile.
|
||||
*
|
||||
* @param entity The entity.
|
||||
* @return The player, or null if not a player.
|
||||
*/
|
||||
@Nullable
|
||||
public Player getPlayerFromEntity(@NotNull final Entity entity) {
|
||||
Player player = null;
|
||||
|
||||
if (entity instanceof Player) {
|
||||
player = (Player) entity;
|
||||
} else if (entity instanceof Projectile) {
|
||||
if (((Projectile) entity).getShooter() instanceof Player) {
|
||||
player = (Player) ((Projectile) entity).getShooter();
|
||||
}
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Warn if a boss is null.
|
||||
*
|
||||
* @param boss The boss.
|
||||
*/
|
||||
public void warnIfNull(@Nullable final LivingEcoBoss boss) {
|
||||
if (boss != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
PLUGIN.getLogger().severe("Not-Null boss is null! Generating stacktrace.");
|
||||
throw new NullPointerException("Not-Null boss is null!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,6 @@ import lombok.Data;
|
||||
import org.bukkit.boss.BarColor;
|
||||
import org.bukkit.boss.BarStyle;
|
||||
|
||||
@Data
|
||||
public class BossbarProperties {
|
||||
/**
|
||||
* The BossBar color.
|
||||
*/
|
||||
private final BarColor color;
|
||||
public record BossbarProperties(BarColor color, BarStyle style) {
|
||||
|
||||
/**
|
||||
* The BossBar style.
|
||||
*/
|
||||
private final BarStyle style;
|
||||
}
|
||||
|
||||
@@ -4,15 +4,6 @@ import lombok.Data;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
public class DamagerProperty {
|
||||
/**
|
||||
* The player.
|
||||
*/
|
||||
private final UUID playerUUID;
|
||||
public record DamagerProperty(UUID playerUUID, double damage) {
|
||||
|
||||
/**
|
||||
* The damage.
|
||||
*/
|
||||
private final double damage;
|
||||
}
|
||||
|
||||
@@ -2,29 +2,7 @@ package com.willfp.ecobosses.bosses.util.obj;
|
||||
|
||||
import com.willfp.eco.util.NumberUtils;
|
||||
|
||||
public class ExperienceOptions {
|
||||
/**
|
||||
* The minimum xp to drop.
|
||||
*/
|
||||
private final int minimum;
|
||||
|
||||
/**
|
||||
* The maximum xp to drop.
|
||||
*/
|
||||
private final int maximum;
|
||||
|
||||
/**
|
||||
* Create new experience options.
|
||||
*
|
||||
* @param minimum Minimum xp.
|
||||
* @param maximum Maximum xp.
|
||||
*/
|
||||
public ExperienceOptions(final int minimum,
|
||||
final int maximum) {
|
||||
this.minimum = minimum;
|
||||
this.maximum = maximum;
|
||||
}
|
||||
|
||||
public record ExperienceOptions(int minimum, int maximum) {
|
||||
/**
|
||||
* Generate an exp amount.
|
||||
*
|
||||
|
||||
@@ -2,30 +2,9 @@ package com.willfp.ecobosses.bosses.util.obj;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ImmunityOptions {
|
||||
/**
|
||||
* If is immune to fire.
|
||||
*/
|
||||
private final boolean immuneToFire;
|
||||
|
||||
/**
|
||||
* If is immune to suffocation.
|
||||
*/
|
||||
private final boolean immuneToSuffocation;
|
||||
|
||||
/**
|
||||
* If is immune to drowning.
|
||||
*/
|
||||
private final boolean immuneToDrowning;
|
||||
|
||||
/**
|
||||
* If is immune to projectiles.
|
||||
*/
|
||||
private final boolean immuneToProjectiles;
|
||||
|
||||
/**
|
||||
* If is immune to explosions.
|
||||
*/
|
||||
private final boolean immuneToExplosions;
|
||||
public record ImmunityOptions(boolean immuneToFire,
|
||||
boolean immuneToSuffocation,
|
||||
boolean immuneToDrowning,
|
||||
boolean immuneToProjectiles,
|
||||
boolean immuneToExplosions) {
|
||||
}
|
||||
|
||||
@@ -4,37 +4,8 @@ import lombok.Getter;
|
||||
import org.bukkit.Sound;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class OptionedSound {
|
||||
/**
|
||||
* The sound.
|
||||
*/
|
||||
@Getter
|
||||
private final Sound sound;
|
||||
public record OptionedSound(Sound sound,
|
||||
float volume,
|
||||
float pitch) {
|
||||
|
||||
/**
|
||||
* The volume.
|
||||
*/
|
||||
@Getter
|
||||
private final float volume;
|
||||
|
||||
/**
|
||||
* The pitch, from 0.5 to 2.
|
||||
*/
|
||||
@Getter
|
||||
private final float pitch;
|
||||
|
||||
/**
|
||||
* Create a new optioned sound.
|
||||
*
|
||||
* @param sound The sound.
|
||||
* @param volume The volume.
|
||||
* @param pitch The pitch, from 0.5 to 2.
|
||||
*/
|
||||
public OptionedSound(@NotNull final Sound sound,
|
||||
final float volume,
|
||||
final float pitch) {
|
||||
this.sound = sound;
|
||||
this.volume = volume;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,20 +3,7 @@ package com.willfp.ecobosses.bosses.util.obj;
|
||||
import lombok.Data;
|
||||
import org.bukkit.Material;
|
||||
|
||||
@Data
|
||||
public class SpawnTotem {
|
||||
/**
|
||||
* The bottom block.
|
||||
*/
|
||||
private final Material bottom;
|
||||
|
||||
/**
|
||||
* The middle block.
|
||||
*/
|
||||
private final Material middle;
|
||||
|
||||
/**
|
||||
* The top block.
|
||||
*/
|
||||
private final Material top;
|
||||
public record SpawnTotem(Material bottom,
|
||||
Material middle,
|
||||
Material top) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user