public void spawnBoss2() {
this.damageList = null;
String configBossId = getRandomBossConfigId();
Boss boss = Configs.MAIN_CONFIG.getBossById(configBossId);
String randomBossId = boss.getId() + random.nextInt(1000000);
this.getLogger().warning(randomBossId);
LivingEntity bossEntity = this.createBossEntity(boss, configBossId);
this.displayBossEntity(boss, bossEntity);
this.applyBossSpheres(boss, bossEntity, configBossId, randomBossId);
bossEntity.getPersistentDataContainer().set(key, PersistentDataType.STRING, randomBossId);
bossEntities.put(bossEntity, boss);
get2.add(bossEntity.getCustomName());
get3.add(configBossId);
BossBar bossBar = this.createBossBar(boss, bossEntity);
bossBars.put(randomBossId, bossBar);
BukkitTask task = this.getServer().getScheduler().runTaskTimer(this, this::broadcastBossStatus, 0L, 40L);
tasks.put(randomBossId, task);
}
private String getRandomBossConfigId() {
return this.options[this.random.nextInt(this.options.length)].replace("\"", "");
}
private LivingEntity createBossEntity(Boss boss, String configBossId) {
LivingEntity bossEntity = (LivingEntity) boss.getLocation().getWorld().spawnEntity(boss.getLocation(), boss.getType());
this.applyAttributes(bossEntity, boss, configBossId);
bossEntity.setCanPickupItems(false);
bossEntity.setCustomName(boss.getName());
bossEntity.setRemoveWhenFarAway(false);
bossEntity.setCustomNameVisible(true);
bossEntity.setGlowing(true);
bossEntity.setPersistent(true);
if (bossEntity instanceof Zombie zombie) {
zombie.setAdult();
}
return bossEntity;
}
private void applyAttributes(LivingEntity bossEntity, Boss boss, String configBossId) {
YamlConfiguration configuration = ConfigManager.of("settings.yml").getYamlConfiguration();
AttributeInstance speed = bossEntity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
if (speed != null) {
speed.addModifier(new AttributeModifier(
"GENERIC_MOVEMENT_SPEED",
speed.getBaseValue() * configuration.getInt("bosses." + configBossId + ".move"),
AttributeModifier.Operation.ADD_NUMBER
));
}
AttributeInstance maxHealth = bossEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (maxHealth != null) {
double health = boss.getHealth();
maxHealth.setBaseValue(health);
bossEntity.setHealth(health);
}
AttributeInstance damage = bossEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
if (damage != null) {
damage.setBaseValue(damage.getBaseValue() * configuration.getInt("bosses." + configBossId + ".damage"));
}
}
private void applyBossSpheres(Boss boss, LivingEntity bossEntity, String configBossId, String randomBossId) {
if (configuration.getBoolean("bosses." + configBossId + ".spheres") == true) {
Spheres(bossEntity, boss.getName(), randomBossId);
}
}
private BossBar createBossBar(Boss boss, LivingEntity bossEntity) {
BossBar bar = Bukkit.createBossBar(ColorHelper.colorize(
Configs.MAIN_CONFIG.getBossbar()
.replace("%name%", boss.getName())
.replace("%health%", String.valueOf(Math.round(bossEntity.getHealth())))
), boss.getColor(), BarStyle.SEGMENTED_12, new BarFlag[0]);
bar.setVisible(true);
bar.setProgress(1.0D);
Bukkit.getOnlinePlayers().forEach(bar::addPlayer);
return bar;
}
private void displayBossEntity(Boss boss, LivingEntity bossEntity) {
Location location = bossEntity.getLocation();
World world = location.getWorld();
world.spawnParticle(Particle.CRIT,
location, 50, 1, 1, 1, 0.1);
world.spawnParticle(Particle.ENCHANTMENT_TABLE,
location, 30, 1, 1, 1, 0.1);
world.spawnParticle(Particle.FIREWORKS_SPARK,
location, 20, 1, 1, 1, 0.1);
for (Player player : Bukkit.getOnlinePlayers()) {
Lang.SPAWNED.send(player,
new Pair("%id%", boss.getId()),
new Pair("%name%", boss.getName())
);
}
}
private void broadcastBossStatus() {
Server server = Bukkit.getServer();
List<Map.Entry<String, Integer>> damageList = Handler.getSortedDamageList();
for (int index = 0; index < damageList.size(); index++) {
Map.Entry<String, Integer> entry = damageList.get(index);
String playerName = entry.getKey();
Player player = server.getPlayer(playerName);
if (player == null) continue;
int damage = entry.getValue();
Component actionBar = Component.text(
Configs.MAIN_CONFIG.getActionBar()
.replace("%index%", String.valueOf(index + 1))
.replace("%max%", String.valueOf(damageList.size()))
.replace("%damage%", String.valueOf(damage))
);
player.sendActionBar(actionBar);
}
}