Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 131 additions & 15 deletions src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import org.bukkit.Bukkit;
import org.bukkit.Difficulty;
Expand Down Expand Up @@ -61,7 +62,9 @@
import world.bentobox.bentobox.api.events.island.IslandDeleteEvent;
import world.bentobox.bentobox.api.events.island.IslandResettedEvent;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity.MythicMobRecord;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.hooks.MythicMobsHook;
import world.bentobox.bentobox.util.Util;

/**
Expand Down Expand Up @@ -234,25 +237,26 @@
Player summoner) {
World world = block.getWorld();
Location home = home(block);
Entity entity = world.spawnEntity(home, finale.boss());
if (!(entity instanceof LivingEntity boss) || !entity.isValid()) {
entity.remove();
addon.logWarning("Could not spawn finale boss " + finale.boss() + " for phase " + phaseName);
Component name = Util.parseMiniMessageOrLegacy(finale.name());
LivingEntity boss = spawnBoss(finale, home, phaseName,
PlainTextComponentSerializer.plainText().serialize(name));
if (boss == null) {
return false;
}
List<Player> fighters = fighters(island, world);
Component name = Util.parseMiniMessageOrLegacy(finale.name());
ActiveFinale f = new ActiveFinale(island, is, phaseName, finale, block, boss, name);

// Dress the boss
boss.customName(name);
boss.setCustomNameVisible(true);
boss.setPersistent(false);
boss.setRemoveWhenFarAway(false);
setAttribute(boss, Attribute.SCALE, finale.scale());
double health = scaledHealth(finale.health(), fighters.size(), is.getFinaleFails());
setAttribute(boss, Attribute.MAX_HEALTH, health);
if (finale.scale() > 0) {
setAttribute(boss, Attribute.SCALE, finale.scale());
}
AttributeInstance max = boss.getAttribute(Attribute.MAX_HEALTH);
double health = scaledHealth(baseHealth(finale, boss, max), fighters.size(), is.getFinaleFails());
setAttribute(boss, Attribute.MAX_HEALTH, health);
if (max != null) {
// The server may cap max health, so read it back
boss.setHealth(max.getValue());
Expand All @@ -278,6 +282,7 @@

// Minions
finale.minions().forEach((type, count) -> spawnMinions(f, type, count, home));
finale.mythicMinions().forEach((type, count) -> spawnMythicMinions(f, type, count, home));

// Register
active.put(island.getUniqueId(), f);
Expand All @@ -296,16 +301,127 @@
return true;
}

/**
* @return the boss's health before scaling: the phase file's, or the mob's own
* if that is 0, e.g., from its MythicMobs config
*/
private static double baseHealth(PhaseFinale finale, LivingEntity boss, @Nullable AttributeInstance max) {
if (finale.health() > 0) {
return finale.health();
}
return max != null ? max.getBaseValue() : boss.getHealth();
}

/**
* Spawn the boss: the MythicMob if the finale names one, falling back to the
* vanilla boss type if there is one.
*
* @return the boss, or null if nothing could be spawned
*/
@Nullable
private LivingEntity spawnBoss(PhaseFinale finale, Location home, String phaseName, String plainName) {
if (finale.isMythic()) {
LivingEntity boss = spawnMythicBoss(finale, home, phaseName, plainName);
if (boss != null) {
return boss;
}
if (finale.boss() == null) {
return null;
}
addon.logWarning("Using " + finale.boss() + " as the finale boss for phase " + phaseName + " instead");
}
Entity entity = home.getWorld().spawnEntity(home, finale.boss());

Check warning on line 333 in src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Parameter 2 to this call is marked "@NotNull" but null could be passed.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_ChunkBlock&issues=AaHqI6Cf4B6ImHWFukWC&open=AaHqI6Cf4B6ImHWFukWC&pullRequest=56
if (!(entity instanceof LivingEntity boss) || !entity.isValid()) {
entity.remove();
addon.logWarning("Could not spawn finale boss " + finale.boss() + " for phase " + phaseName);

Check failure on line 336 in src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal " for phase " 4 times.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_ChunkBlock&issues=AaHqI6Cf4B6ImHWFukWD&open=AaHqI6Cf4B6ImHWFukWD&pullRequest=56
return null;
}
return boss;
}

@Nullable
private LivingEntity spawnMythicBoss(PhaseFinale finale, Location home, String phaseName, String plainName) {
Entity entity;
try {
entity = spawnMythicEntity(finale.mythicBoss(), finale.mythicLevel(), home, plainName, phaseName);
} catch (RuntimeException | LinkageError e) {
addon.logError("Could not spawn MythicMobs boss " + finale.mythicBoss() + " for phase " + phaseName
+ ": " + e.getMessage());
return null;
}
if (entity == null) {
return null;
}
if (entity instanceof LivingEntity boss && entity.isValid()) {
return boss;
}
entity.remove();
addon.logWarning("MythicMobs boss " + finale.mythicBoss() + " for phase " + phaseName
+ " is not a living mob or did not spawn");
return null;
}

/**
* Spawn a MythicMob through BentoBox's MythicMobs hook. Kept apart so that it
* is the only code that touches the hook.
*
* @param type MythicMobs mob type id
* @param level mob level
* @param home where to spawn it
* @param displayName name the hook gives the mob
* @param phaseName phase, for log messages
* @return the spawned entity, or null if MythicMobs is missing or does not know the mob
*/
@Nullable
Entity spawnMythicEntity(String type, double level, Location home, String displayName, String phaseName) {
Optional<MythicMobsHook> hook = addon.getPlugin().getHooks().getHook("MythicMobs")
.filter(MythicMobsHook.class::isInstance).map(MythicMobsHook.class::cast);
if (hook.isEmpty()) {
addon.logWarning("Phase " + phaseName + " finale uses MythicMobs mob " + type
+ " but MythicMobs is not installed");
return null;
}
AtomicReference<Entity> spawned = new AtomicReference<>();
// A delay of 0 spawns on this tick and runs the callback before returning
if (!hook.get().spawnMythicMob(new MythicMobRecord(type, displayName, level, 0F, ""), home, spawned::set,
0L)) {
addon.logWarning("Phase " + phaseName + " finale: MythicMobs has no mob called " + type);
return null;
}
return spawned.get();
}

private void spawnMinions(ActiveFinale f, EntityType type, int count, Location home) {
for (int i = 0; i < count; i++) {
Entity e = home.getWorld().spawnEntity(home, type);
if (!e.isValid()) {
continue;
addMinion(f, home.getWorld().spawnEntity(home, type));
}
}

private void addMinion(ActiveFinale f, Entity e) {
if (!e.isValid()) {
return;
}
e.setPersistent(false);
prepare(e);
f.minions.add(e.getUniqueId());
byMinion.put(e.getUniqueId(), f);
}

private void spawnMythicMinions(ActiveFinale f, String type, int count, Location home) {
for (int i = 0; i < count; i++) {
Entity e;
try {
e = spawnMythicEntity(type, f.finale.mythicLevel(), home, type, f.phaseName);
} catch (RuntimeException | LinkageError ex) {
addon.logError("Could not spawn MythicMobs minion " + type + " for phase " + f.phaseName + ": "
+ ex.getMessage());
return;
}
if (e == null) {
// Missing plugin or unknown mob; already logged, so do not try the rest
return;
}
e.setPersistent(false);
prepare(e);
f.minions.add(e.getUniqueId());
byMinion.put(e.getUniqueId(), f);
addMinion(f, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,20 +1063,35 @@ void addFinale(OneBlockPhase obPhase, ConfigurationSection phase) {
return;
}
String phaseName = obPhase.getPhaseName();
EntityType boss = Enums.getIfPresent(EntityType.class, sec.getString("boss", "").toUpperCase(Locale.ENGLISH))
.orNull();
String mythicBoss = sec.getString("mythic-boss", "").trim();
boolean mythic = !mythicBoss.isEmpty();
String bossName = sec.getString("boss", "");
EntityType boss = Enums.getIfPresent(EntityType.class, bossName.toUpperCase(Locale.ENGLISH)).orNull();
if (boss == null || !boss.isAlive() || !boss.isSpawnable()) {
addon.logWarning(FINALE_LOG_PREFIX + phaseName + " finale: boss '" + sec.getString("boss")
+ "' is not a living entity type on this server. The finale is disabled.");
return;
boss = null;
if (!mythic) {
addon.logWarning(FINALE_LOG_PREFIX + phaseName + " finale: boss '" + sec.getString("boss")
+ "' is not a living entity type on this server. The finale is disabled.");
return;
}
if (!bossName.isEmpty()) {
addon.logWarning(FINALE_LOG_PREFIX + phaseName + " finale: fallback boss '" + bossName
+ "' is not a living entity type on this server. There will be no fallback.");
}
}
// MythicMobs bosses keep their own health and size unless the phase file sets them
double health = sec.getDouble("health", mythic ? 0D : 100D);
double scale = sec.getDouble("scale", mythic ? 0D : 1D);
String defaultName = mythic ? mythicBoss : Util.prettifyText(boss.name());
Map<EntityType, Integer> minions = readFinaleMinions(sec.getConfigurationSection("minions"), phaseName);
Map<Material, Integer> drops = readFinaleDrops(sec.getConfigurationSection("drops"), phaseName);
obPhase.setFinale(new PhaseFinale(boss, sec.getString(NAME, Util.prettifyText(boss.name())),
Math.max(1D, sec.getDouble("health", 100D)), Math.max(0.1D, sec.getDouble("scale", 1D)),
obPhase.setFinale(new PhaseFinale(boss, sec.getString(NAME, defaultName),
health > 0D ? Math.max(1D, health) : 0D, scale > 0D ? Math.max(0.1D, scale) : 0D,
sec.getDouble("damage", 0D), sec.getDouble("speed", 0D), sec.getBoolean("void-respawn", true),
Math.max(0, sec.getInt("warning", PhaseFinale.DEFAULT_WARNING)), minions, drops,
sec.getStringList("commands"), sec.getStringList("first-time-commands")));
sec.getStringList("commands"), sec.getStringList("first-time-commands"), mythicBoss,
Math.max(1D, sec.getDouble("mythic-level", 1D)),
readFinaleMythicMinions(sec.getConfigurationSection("mythic-minions"))));
}

private Map<EntityType, Integer> readFinaleMinions(ConfigurationSection minionSec, String phaseName) {
Expand All @@ -1095,6 +1110,19 @@ private Map<EntityType, Integer> readFinaleMinions(ConfigurationSection minionSe
return minions;
}

/**
* MythicMobs minions, by mob type id. The ids cannot be checked here because
* MythicMobs may not have loaded its mobs yet; unknown ones are reported when
* the boss spawns.
*/
private Map<String, Integer> readFinaleMythicMinions(ConfigurationSection minionSec) {
Map<String, Integer> minions = new LinkedHashMap<>();
if (minionSec != null) {
minionSec.getKeys(false).forEach(key -> minions.put(key, Math.max(0, minionSec.getInt(key))));
}
return minions;
}

private Map<Material, Integer> readFinaleDrops(ConfigurationSection dropSec, String phaseName) {
Map<Material, Integer> drops = new EnumMap<>(Material.class);
if (dropSec == null) {
Expand All @@ -1117,7 +1145,13 @@ private void saveFinale(ConfigurationSection phSec, OneBlockPhase p) {
return;
}
ConfigurationSection sec = phSec.createSection(FINALE);
sec.set("boss", f.boss().name());
if (f.isMythic()) {
sec.set("mythic-boss", f.mythicBoss());
sec.set("mythic-level", f.mythicLevel());
}
if (f.boss() != null) {
sec.set("boss", f.boss().name());
}
sec.set(NAME, f.name());
sec.set("health", f.health());
sec.set("scale", f.scale());
Expand All @@ -1126,6 +1160,7 @@ private void saveFinale(ConfigurationSection phSec, OneBlockPhase p) {
sec.set("void-respawn", f.voidRespawn());
sec.set("warning", f.warning());
f.minions().forEach((k, v) -> sec.set("minions." + k.name(), v));
f.mythicMinions().forEach((k, v) -> sec.set("mythic-minions." + k, v));
f.drops().forEach((k, v) -> sec.set("drops." + k.name(), v));
sec.set("commands", f.commands());
sec.set("first-time-commands", f.firstTimeCommands());
Expand Down
45 changes: 39 additions & 6 deletions src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@

import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.Nullable;

/**
* The boss encounter that ends a phase. Defined by the optional {@code finale:}
* section of a phase file.
*
* @param boss entity type of the boss
* @param boss entity type of the boss. If {@code mythicBoss} is set this
* is the fallback used when the MythicMob cannot be spawned,
* and may be null for no fallback
* @param name display name of the boss, MiniMessage or legacy codes
* @param health base maximum health, before scaling for players
* @param scale size multiplier, 1 is normal size
* @param health base maximum health, before scaling for players, or 0 or
* less to keep the mob's own maximum health
* @param scale size multiplier, 1 is normal size, or 0 or less to keep the
* mob's own size
* @param damage attack damage, or 0 or less to keep the mob's default
* @param speed movement speed, or 0 or less to keep the mob's default
* @param voidRespawn if true a boss that falls into the void returns to the
Expand All @@ -23,11 +28,17 @@
* @param drops items dropped at the magic block when the boss is defeated
* @param commands commands run every time the boss is defeated
* @param firstTimeCommands commands run only the first time the island defeats this boss
* @param mythicBoss MythicMobs mob type id to spawn as the boss, or empty for a
* vanilla boss
* @param mythicLevel level of the MythicMobs boss and minions
* @param mythicMinions MythicMobs mobs, by type id, that spawn alongside the boss,
* and how many of each
* @author tastybento
*/
public record PhaseFinale(EntityType boss, String name, double health, double scale, double damage, double speed,
boolean voidRespawn, int warning, Map<EntityType, Integer> minions, Map<Material, Integer> drops,
List<String> commands, List<String> firstTimeCommands) {
public record PhaseFinale(@Nullable EntityType boss, String name, double health, double scale, double damage,
double speed, boolean voidRespawn, int warning, Map<EntityType, Integer> minions,
Map<Material, Integer> drops, List<String> commands, List<String> firstTimeCommands, String mythicBoss,
double mythicLevel, Map<String, Integer> mythicMinions) {

/**
* Default number of blocks of forewarning.
Expand All @@ -42,5 +53,27 @@ public record PhaseFinale(EntityType boss, String name, double health, double sc
drops = Map.copyOf(drops);
commands = List.copyOf(commands);
firstTimeCommands = List.copyOf(firstTimeCommands);
mythicBoss = mythicBoss == null ? "" : mythicBoss.trim();
mythicMinions = mythicMinions == null ? Map.of() : Map.copyOf(mythicMinions);
if (boss == null && mythicBoss.isEmpty()) {
throw new IllegalArgumentException("A finale needs a boss or a mythic-boss");
}
}

/**
* A finale with a vanilla boss.
*/
public PhaseFinale(EntityType boss, String name, double health, double scale, double damage, double speed,
boolean voidRespawn, int warning, Map<EntityType, Integer> minions, Map<Material, Integer> drops,
List<String> commands, List<String> firstTimeCommands) {
this(boss, name, health, scale, damage, speed, voidRespawn, warning, minions, drops, commands,
firstTimeCommands, "", 1D, Map.of());
}

/**
* @return true if the boss is a MythicMobs mob
*/
public boolean isMythic() {
return !mythicBoss.isEmpty();
}
}
20 changes: 16 additions & 4 deletions src/main/resources/phases/0_plains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,20 @@
#
# boss - EntityType of the boss. GIANT has no AI in vanilla;
# it is given one here (needs Paper).
# name - display name, MiniMessage or legacy & codes.
# health - base max health. Server max is 1024 unless raised in
# spigot.yml (settings.attribute.maxHealth).
# scale - size multiplier. 1 is normal. Default 1.
# mythic-boss - optional MythicMobs mob type to use as the boss
# instead, e.g. SkeletonKing. Needs MythicMobs. If it
# cannot be spawned, boss is used as a fallback; leave
# boss out for no fallback (the phase then simply
# moves on). Its own skills, drops and AI all apply.
# mythic-level - level of the MythicMobs boss and mythic-minions.
# Default 1.
# name - display name, MiniMessage or legacy & codes. Set it
# for a mythic-boss too, or the bar shows its type id.
# health - base max health. 0 keeps the mob's own; the default
# is 100, or 0 for a mythic-boss. Server max is 1024
# unless raised in spigot.yml (settings.attribute.maxHealth).
# scale - size multiplier. 1 is normal. Default 1, or the
# mob's own size for a mythic-boss.
# damage - attack damage. Omit or 0 for the mob's default.
# speed - movement speed. Omit or 0 for the mob's default.
# void-respawn - true (default): a boss knocked into the void comes
Expand All @@ -172,6 +182,8 @@
# warning - how many blocks before the end the omen starts.
# Default 20. 0 for no warning.
# minions - mobs that spawn with the boss, TYPE: count.
# mythic-minions - MythicMobs mobs that spawn with the boss, type: count.
# They use mythic-level. Skipped if MythicMobs is missing.
# drops - items dropped when the boss dies, MATERIAL: amount.
# commands - run every time the boss is defeated. Same prefixes
# and placeholders as end-commands.
Expand Down
Loading
Loading