From 5280d27852d0b0c93f8ed2f03fef2c17a0e1c009 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 17 Nov 2026 07:31:06 -0800 Subject: [PATCH 1/2] feat: MythicMobs mobs as phase finale bosses A finale can name a MythicMobs mob with `mythic-boss` (and `mythic-level`). It is spawned through BentoBox's MythicMobs hook on the same tick, then handled like any other boss: leash, boss bar, player-only damage, player scaling and mercy. It keeps its own health and size unless the phase file sets them. `boss` becomes an optional fallback used when MythicMobs is missing or does not know the mob; with no fallback the finale is waived so an island is never sealed behind a boss that cannot appear. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HBXcjarmPKNsWM5LHDzSw2 --- .../chunkblock/listeners/FinaleListener.java | 106 +++++++++++++++-- .../oneblocks/OneBlocksManager.java | 38 ++++-- .../chunkblock/oneblocks/PhaseFinale.java | 42 ++++++- src/main/resources/phases/0_plains.yml | 17 ++- .../listeners/FinaleListenerTest.java | 109 ++++++++++++++++++ .../oneblocks/OneBlocksManagerTest3.java | 47 ++++++++ 6 files changed, 332 insertions(+), 27 deletions(-) diff --git a/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java b/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java index 7c538b5..9e95959 100644 --- a/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java +++ b/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java @@ -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; @@ -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; /** @@ -234,14 +237,13 @@ private boolean start(Island island, OneBlockIslands is, String phaseName, Phase 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 fighters = fighters(island, world); - Component name = Util.parseMiniMessageOrLegacy(finale.name()); ActiveFinale f = new ActiveFinale(island, is, phaseName, finale, block, boss, name); // Dress the boss @@ -249,10 +251,12 @@ private boolean start(Island island, OneBlockIslands is, String phaseName, Phase 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()); @@ -296,6 +300,92 @@ private boolean start(Island island, OneBlockIslands is, String phaseName, Phase 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()); + if (!(entity instanceof LivingEntity boss) || !entity.isValid()) { + entity.remove(); + addon.logWarning("Could not spawn finale boss " + finale.boss() + " for phase " + phaseName); + return null; + } + return boss; + } + + @Nullable + private LivingEntity spawnMythicBoss(PhaseFinale finale, Location home, String phaseName, String plainName) { + Entity entity; + try { + entity = spawnMythicEntity(finale, home, phaseName, plainName); + } 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. + * + * @return the spawned entity, or null if MythicMobs is missing or does not know the mob + */ + @Nullable + Entity spawnMythicEntity(PhaseFinale finale, Location home, String phaseName, String plainName) { + Optional hook = addon.getPlugin().getHooks().getHook("MythicMobs") + .filter(MythicMobsHook.class::isInstance).map(MythicMobsHook.class::cast); + if (hook.isEmpty()) { + addon.logWarning("Phase " + phaseName + " has MythicMobs boss " + finale.mythicBoss() + + " but MythicMobs is not installed"); + return null; + } + AtomicReference spawned = new AtomicReference<>(); + // A delay of 0 spawns on this tick and runs the callback before returning + if (!hook.get().spawnMythicMob( + new MythicMobRecord(finale.mythicBoss(), plainName, finale.mythicLevel(), 0F, ""), home, + spawned::set, 0L)) { + addon.logWarning("Phase " + phaseName + " finale: MythicMobs has no mob called " + finale.mythicBoss()); + 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); diff --git a/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java b/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java index 1c5081b..60986b5 100644 --- a/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java +++ b/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java @@ -1063,20 +1063,34 @@ 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 minions = readFinaleMinions(sec.getConfigurationSection("minions"), phaseName); Map 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)))); } private Map readFinaleMinions(ConfigurationSection minionSec, String phaseName) { @@ -1117,7 +1131,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()); diff --git a/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java b/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java index 7387a16..42e812f 100644 --- a/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java +++ b/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java @@ -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 @@ -23,11 +28,15 @@ * @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 * @author tastybento */ -public record PhaseFinale(EntityType boss, String name, double health, double scale, double damage, double speed, - boolean voidRespawn, int warning, Map minions, Map drops, - List commands, List firstTimeCommands) { +public record PhaseFinale(@Nullable EntityType boss, String name, double health, double scale, double damage, + double speed, boolean voidRespawn, int warning, Map minions, + Map drops, List commands, List firstTimeCommands, String mythicBoss, + double mythicLevel) { /** * Default number of blocks of forewarning. @@ -42,5 +51,26 @@ 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(); + 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 minions, Map drops, + List commands, List firstTimeCommands) { + this(boss, name, health, scale, damage, speed, voidRespawn, warning, minions, drops, commands, + firstTimeCommands, "", 1D); + } + + /** + * @return true if the boss is a MythicMobs mob + */ + public boolean isMythic() { + return !mythicBoss.isEmpty(); } } diff --git a/src/main/resources/phases/0_plains.yml b/src/main/resources/phases/0_plains.yml index 748cf02..8ae49ae 100644 --- a/src/main/resources/phases/0_plains.yml +++ b/src/main/resources/phases/0_plains.yml @@ -160,10 +160,19 @@ # # 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. 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 diff --git a/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java b/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java index 0a2281a..bab8905 100644 --- a/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java +++ b/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java @@ -2,12 +2,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -26,6 +30,7 @@ import org.bukkit.attribute.AttributeInstance; import org.bukkit.block.Block; import org.bukkit.entity.Arrow; +import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Item; import org.bukkit.entity.Zombie; @@ -452,4 +457,108 @@ void testOnDisable() { // Still pending for after the restart assertEquals(PLAINS, is.getFinalePending()); } + + // --------------------------------------------------------------------- + // MythicMobs bosses + // --------------------------------------------------------------------- + + private PhaseFinale mythicFinale(EntityType fallback) { + return new PhaseFinale(fallback, "Skeleton King", 0, 0, 0, 0, true, 20, Map.of(), + Map.of(Material.IRON_INGOT, 8), List.of(), List.of(), "SkeletonKing", 3); + } + + /** + * MythicMobs is not on the test classpath, so stub the one method that talks to its hook. + */ + private void mythicSpawns(Entity result) { + fl = spy(fl); + doReturn(result).when(fl).spawnMythicEntity(any(), any(), anyString(), anyString()); + } + + @Test + void testMythicBossSummoned() { + plains.setFinale(mythicFinale(null)); + mythicSpawns(boss); + when(attribute.getBaseValue()).thenReturn(200D); + summon(); + verify(fl).spawnMythicEntity(eq(plains.getFinale()), any(), eq(PLAINS), eq("Skeleton King")); + verify(world, never()).spawnEntity(any(), any(EntityType.class)); + // Keeps its own size and health, scaled for one player + verify(boss, never()).getAttribute(Attribute.SCALE); + verify(attribute).setBaseValue(200D); + verify(boss).setPersistent(false); + verify(boss).setTarget(mockPlayer); + assertTrue(fl.isActive(island)); + } + + @Test + void testMythicBossWinIsTheKill() { + plains.setFinale(mythicFinale(null)); + mythicSpawns(boss); + summon(); + EntityDeathEvent e = mock(EntityDeathEvent.class); + when(e.getEntity()).thenReturn(boss); + fl.onDeath(e); + assertFalse(fl.isActive(island)); + assertEquals("", is.getFinalePending()); + assertTrue(is.getDefeatedFinales().contains(PLAINS)); + } + + @Test + void testMythicBossUnknownUsesFallback() { + plains.setFinale(mythicFinale(EntityType.ZOMBIE)); + mythicSpawns(null); + summon(); + verify(world).spawnEntity(any(), eq(EntityType.ZOMBIE)); + assertTrue(fl.isActive(island)); + } + + @Test + void testMythicBossNoPluginUsesFallback() { + plains.setFinale(mythicFinale(EntityType.ZOMBIE)); + summon(); + verify(world).spawnEntity(any(), eq(EntityType.ZOMBIE)); + assertTrue(fl.isActive(island)); + } + + @Test + void testMythicBossNoPluginNoFallbackWaives() { + plains.setFinale(mythicFinale(null)); + is.setFinalePending(PLAINS); + assertFalse(fl.gate(island, is, mockPlayer, block)); + verify(world, never()).spawnEntity(any(), any(EntityType.class)); + assertEquals("", is.getFinalePending()); + assertFalse(fl.isActive(island)); + } + + @Test + void testMythicBossHookThrowsWaives() { + plains.setFinale(mythicFinale(null)); + fl = spy(fl); + doThrow(new NoClassDefFoundError("io/lumine/mythic/bukkit/MythicBukkit")).when(fl) + .spawnMythicEntity(any(), any(), anyString(), anyString()); + is.setFinalePending(PLAINS); + assertFalse(fl.gate(island, is, mockPlayer, block)); + assertEquals("", is.getFinalePending()); + } + + @Test + void testMythicBossNotLivingWaives() { + plains.setFinale(mythicFinale(null)); + Item item = mock(Item.class); + mythicSpawns(item); + is.setFinalePending(PLAINS); + assertFalse(fl.gate(island, is, mockPlayer, block)); + verify(item).remove(); + assertEquals("", is.getFinalePending()); + } + + @Test + void testFinaleNeedsABoss() { + Map none = Map.of(); + Map drops = Map.of(); + List cmds = List.of(); + assertThrows(IllegalArgumentException.class, () -> new PhaseFinale(null, "x", 1, 1, 0, 0, true, 0, none, + drops, cmds, cmds, "", 1)); + } } diff --git a/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java b/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java index fa2964d..acc23da 100644 --- a/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java +++ b/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java @@ -1370,6 +1370,53 @@ void testAddFinale() throws Exception { assertTrue(f.firstTimeCommands().isEmpty()); } + /** + * A MythicMobs boss keeps its own health and size unless they are set, and the + * vanilla boss is an optional fallback. + */ + @Test + void testAddFinaleMythic() throws Exception { + YamlConfiguration cfg = new YamlConfiguration(); + cfg.loadFromString(""" + finale: + mythic-boss: SkeletonKing + mythic-level: 5 + """); + OneBlockPhase phase = new OneBlockPhase("0"); + phase.setPhaseName("Plains"); + obm.addFinale(phase, cfg); + PhaseFinale f = phase.getFinale(); + assertTrue(f.isMythic()); + assertEquals("SkeletonKing", f.mythicBoss()); + assertEquals(5D, f.mythicLevel()); + assertNull(f.boss()); + assertEquals("SkeletonKing", f.name()); + assertEquals(0D, f.health()); + assertEquals(0D, f.scale()); + + cfg.loadFromString(""" + finale: + mythic-boss: SkeletonKing + boss: WITHER_SKELETON + health: 300 + """); + obm.addFinale(phase, cfg); + f = phase.getFinale(); + assertEquals(EntityType.WITHER_SKELETON, f.boss()); + assertEquals(300D, f.health()); + assertEquals(1D, f.mythicLevel()); + + // A bad fallback does not disable a mythic boss + cfg.loadFromString(""" + finale: + mythic-boss: SkeletonKing + boss: ARROW + """); + obm.addFinale(phase, cfg); + assertNull(phase.getFinale().boss()); + assertTrue(phase.getFinale().isMythic()); + } + /** * A phase without a finale section, or with a bad boss, has no finale. */ From 7c9a8551dfc077b1090a2fd750a8ab671bac1b46 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 17 Nov 2026 08:41:32 -0800 Subject: [PATCH 2/2] feat: MythicMobs minions for phase finales A finale's `mythic-minions` section (MythicMobs type: count) spawns MythicMobs mobs alongside the boss, at `mythic-level`. They are finale minions like the vanilla ones: no block changes, cleared when the fight ends. They work with a vanilla or a MythicMobs boss. If MythicMobs is missing or does not know a type, that type is tried once, logged and skipped; the fight goes on. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HBXcjarmPKNsWM5LHDzSw2 --- .../chunkblock/listeners/FinaleListener.java | 54 +++++++++---- .../oneblocks/OneBlocksManager.java | 17 ++++- .../chunkblock/oneblocks/PhaseFinale.java | 9 ++- src/main/resources/phases/0_plains.yml | 5 +- .../listeners/FinaleListenerTest.java | 76 +++++++++++++++++-- .../oneblocks/OneBlocksManagerTest3.java | 4 + 6 files changed, 140 insertions(+), 25 deletions(-) diff --git a/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java b/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java index 9e95959..a8dfdb2 100644 --- a/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java +++ b/src/main/java/world/bentobox/chunkblock/listeners/FinaleListener.java @@ -282,6 +282,7 @@ private boolean start(Island island, OneBlockIslands is, String phaseName, Phase // 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); @@ -342,7 +343,7 @@ private LivingEntity spawnBoss(PhaseFinale finale, Location home, String phaseNa private LivingEntity spawnMythicBoss(PhaseFinale finale, Location home, String phaseName, String plainName) { Entity entity; try { - entity = spawnMythicEntity(finale, home, phaseName, plainName); + 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()); @@ -364,23 +365,27 @@ private LivingEntity spawnMythicBoss(PhaseFinale finale, Location home, String p * 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(PhaseFinale finale, Location home, String phaseName, String plainName) { + Entity spawnMythicEntity(String type, double level, Location home, String displayName, String phaseName) { Optional hook = addon.getPlugin().getHooks().getHook("MythicMobs") .filter(MythicMobsHook.class::isInstance).map(MythicMobsHook.class::cast); if (hook.isEmpty()) { - addon.logWarning("Phase " + phaseName + " has MythicMobs boss " + finale.mythicBoss() + addon.logWarning("Phase " + phaseName + " finale uses MythicMobs mob " + type + " but MythicMobs is not installed"); return null; } AtomicReference spawned = new AtomicReference<>(); // A delay of 0 spawns on this tick and runs the callback before returning - if (!hook.get().spawnMythicMob( - new MythicMobRecord(finale.mythicBoss(), plainName, finale.mythicLevel(), 0F, ""), home, - spawned::set, 0L)) { - addon.logWarning("Phase " + phaseName + " finale: MythicMobs has no mob called " + finale.mythicBoss()); + 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(); @@ -388,14 +393,35 @@ Entity spawnMythicEntity(PhaseFinale finale, Location home, String phaseName, St 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); } } diff --git a/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java b/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java index 60986b5..8558e57 100644 --- a/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java +++ b/src/main/java/world/bentobox/chunkblock/oneblocks/OneBlocksManager.java @@ -1090,7 +1090,8 @@ void addFinale(OneBlockPhase obPhase, ConfigurationSection phase) { 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"), mythicBoss, - Math.max(1D, sec.getDouble("mythic-level", 1D)))); + Math.max(1D, sec.getDouble("mythic-level", 1D)), + readFinaleMythicMinions(sec.getConfigurationSection("mythic-minions")))); } private Map readFinaleMinions(ConfigurationSection minionSec, String phaseName) { @@ -1109,6 +1110,19 @@ private Map 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 readFinaleMythicMinions(ConfigurationSection minionSec) { + Map minions = new LinkedHashMap<>(); + if (minionSec != null) { + minionSec.getKeys(false).forEach(key -> minions.put(key, Math.max(0, minionSec.getInt(key)))); + } + return minions; + } + private Map readFinaleDrops(ConfigurationSection dropSec, String phaseName) { Map drops = new EnumMap<>(Material.class); if (dropSec == null) { @@ -1146,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()); diff --git a/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java b/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java index 42e812f..9cd91f5 100644 --- a/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java +++ b/src/main/java/world/bentobox/chunkblock/oneblocks/PhaseFinale.java @@ -30,13 +30,15 @@ * @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 + * @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(@Nullable EntityType boss, String name, double health, double scale, double damage, double speed, boolean voidRespawn, int warning, Map minions, Map drops, List commands, List firstTimeCommands, String mythicBoss, - double mythicLevel) { + double mythicLevel, Map mythicMinions) { /** * Default number of blocks of forewarning. @@ -52,6 +54,7 @@ public record PhaseFinale(@Nullable EntityType boss, String name, double health, 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"); } @@ -64,7 +67,7 @@ public PhaseFinale(EntityType boss, String name, double health, double scale, do boolean voidRespawn, int warning, Map minions, Map drops, List commands, List firstTimeCommands) { this(boss, name, health, scale, damage, speed, voidRespawn, warning, minions, drops, commands, - firstTimeCommands, "", 1D); + firstTimeCommands, "", 1D, Map.of()); } /** diff --git a/src/main/resources/phases/0_plains.yml b/src/main/resources/phases/0_plains.yml index 8ae49ae..1794f1e 100644 --- a/src/main/resources/phases/0_plains.yml +++ b/src/main/resources/phases/0_plains.yml @@ -165,7 +165,8 @@ # 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. Default 1. + # 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 @@ -181,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. diff --git a/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java b/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java index bab8905..4509a82 100644 --- a/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java +++ b/src/test/java/world/bentobox/chunkblock/listeners/FinaleListenerTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; @@ -463,16 +464,31 @@ void testOnDisable() { // --------------------------------------------------------------------- private PhaseFinale mythicFinale(EntityType fallback) { + return mythicFinale(fallback, Map.of()); + } + + private PhaseFinale mythicFinale(EntityType fallback, Map mythicMinions) { return new PhaseFinale(fallback, "Skeleton King", 0, 0, 0, 0, true, 20, Map.of(), - Map.of(Material.IRON_INGOT, 8), List.of(), List.of(), "SkeletonKing", 3); + Map.of(Material.IRON_INGOT, 8), List.of(), List.of(), "SkeletonKing", 3, + mythicMinions); } /** * MythicMobs is not on the test classpath, so stub the one method that talks to its hook. */ private void mythicSpawns(Entity result) { - fl = spy(fl); - doReturn(result).when(fl).spawnMythicEntity(any(), any(), anyString(), anyString()); + mythicSpawns(null, result); + } + + /** + * @param type mob type to stub, or null for any + */ + private void mythicSpawns(String type, Entity result) { + if (!org.mockito.Mockito.mockingDetails(fl).isSpy()) { + fl = spy(fl); + } + doReturn(result).when(fl).spawnMythicEntity(type == null ? anyString() : eq(type), anyDouble(), any(), + anyString(), anyString()); } @Test @@ -481,7 +497,7 @@ void testMythicBossSummoned() { mythicSpawns(boss); when(attribute.getBaseValue()).thenReturn(200D); summon(); - verify(fl).spawnMythicEntity(eq(plains.getFinale()), any(), eq(PLAINS), eq("Skeleton King")); + verify(fl).spawnMythicEntity(eq("SkeletonKing"), eq(3D), any(), eq("Skeleton King"), eq(PLAINS)); verify(world, never()).spawnEntity(any(), any(EntityType.class)); // Keeps its own size and health, scaled for one player verify(boss, never()).getAttribute(Attribute.SCALE); @@ -536,7 +552,7 @@ void testMythicBossHookThrowsWaives() { plains.setFinale(mythicFinale(null)); fl = spy(fl); doThrow(new NoClassDefFoundError("io/lumine/mythic/bukkit/MythicBukkit")).when(fl) - .spawnMythicEntity(any(), any(), anyString(), anyString()); + .spawnMythicEntity(anyString(), anyDouble(), any(), anyString(), anyString()); is.setFinalePending(PLAINS); assertFalse(fl.gate(island, is, mockPlayer, block)); assertEquals("", is.getFinalePending()); @@ -553,12 +569,60 @@ void testMythicBossNotLivingWaives() { assertEquals("", is.getFinalePending()); } + @Test + void testMythicMinions() { + Zombie imp = mock(Zombie.class); + UUID impId = UUID.randomUUID(); + when(imp.getUniqueId()).thenReturn(impId); + when(imp.isValid()).thenReturn(true); + when(imp.getWorld()).thenReturn(world); + when(imp.getLocation()).thenReturn(new Location(world, 0, 65, 0)); + mockedBukkit.when(() -> Bukkit.getEntity(impId)).thenReturn(imp); + plains.setFinale(mythicFinale(null, Map.of("Imp", 3))); + mythicSpawns("SkeletonKing", boss); + mythicSpawns("Imp", imp); + summon(); + verify(fl, times(3)).spawnMythicEntity(eq("Imp"), eq(3D), any(), eq("Imp"), eq(PLAINS)); + verify(imp, times(3)).setPersistent(false); + // Minions are finale mobs: they cannot change blocks, and leave with the boss + EntityChangeBlockEvent e = mock(EntityChangeBlockEvent.class); + when(e.getEntity()).thenReturn(imp); + fl.onChangeBlock(e); + verify(e).setCancelled(true); + fl.clear(island, is); + verify(imp).remove(); + } + + @Test + void testMythicMinionsWithVanillaBoss() { + PhaseFinale f = new PhaseFinale(EntityType.ZOMBIE, "The Colossus", 100, 2, 4, 0, true, 20, Map.of(), + Map.of(), List.of(), List.of(), "", 2, Map.of("Imp", 2)); + assertFalse(f.isMythic()); + plains.setFinale(f); + mythicSpawns(null); + summon(); + verify(world).spawnEntity(any(), eq(EntityType.ZOMBIE)); + // Unknown or missing: tried once, not once per minion + verify(fl, times(1)).spawnMythicEntity(eq("Imp"), eq(2D), any(), anyString(), eq(PLAINS)); + assertTrue(fl.isActive(island)); + } + + @Test + void testMythicMinionThrowsBossStillFights() { + plains.setFinale(mythicFinale(null, Map.of("Imp", 2))); + mythicSpawns("SkeletonKing", boss); + doThrow(new NoClassDefFoundError("io/lumine/mythic/bukkit/MythicBukkit")).when(fl) + .spawnMythicEntity(eq("Imp"), anyDouble(), any(), anyString(), anyString()); + summon(); + assertTrue(fl.isActive(island)); + } + @Test void testFinaleNeedsABoss() { Map none = Map.of(); Map drops = Map.of(); List cmds = List.of(); assertThrows(IllegalArgumentException.class, () -> new PhaseFinale(null, "x", 1, 1, 0, 0, true, 0, none, - drops, cmds, cmds, "", 1)); + drops, cmds, cmds, "", 1, Map.of())); } } diff --git a/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java b/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java index acc23da..d1a485d 100644 --- a/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java +++ b/src/test/java/world/bentobox/chunkblock/oneblocks/OneBlocksManagerTest3.java @@ -1381,6 +1381,9 @@ void testAddFinaleMythic() throws Exception { finale: mythic-boss: SkeletonKing mythic-level: 5 + mythic-minions: + Imp: 3 + BoneGuard: 1 """); OneBlockPhase phase = new OneBlockPhase("0"); phase.setPhaseName("Plains"); @@ -1393,6 +1396,7 @@ void testAddFinaleMythic() throws Exception { assertEquals("SkeletonKing", f.name()); assertEquals(0D, f.health()); assertEquals(0D, f.scale()); + assertEquals(java.util.Map.of("Imp", 3, "BoneGuard", 1), f.mythicMinions()); cfg.loadFromString(""" finale: