Skip to content
Merged
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
40 changes: 31 additions & 9 deletions src/main/java/world/bentobox/bentobox/hooks/MythicMobsHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ public boolean spawnMythicMob(MythicMobRecord mmr, Location spawnLocation) {
/**
* Spawn a MythicMob and run a callback once the entity has actually been spawned.
* <p>
* Spawning is delayed by 40 ticks inside this hook (to give NMS-pasted blocks time
* to settle), which means the caller cannot act on the spawned entity synchronously.
* This overload accepts a {@link Consumer} that will be invoked with the live Bukkit
* {@link Entity} once the mob has been spawned, so that addons (e.g. AOneBlock) can
* perform follow-up work such as clearing space for large hitboxes.
* Delegates to {@link #spawnMythicMob(MythicMobRecord, Location, Consumer, long)}
* with a 40-tick delay — the historical behaviour, required by blueprint-paste
* callers so blocks settle before mobs land on them.
*
* @param mmr MythicMobRecord
* @param spawnLocation location
Expand All @@ -76,13 +74,32 @@ public boolean spawnMythicMob(MythicMobRecord mmr, Location spawnLocation) {
* @since 3.14.0
*/
public boolean spawnMythicMob(MythicMobRecord mmr, Location spawnLocation, Consumer<Entity> onSpawn) {
return spawnMythicMob(mmr, spawnLocation, onSpawn, 40L);
}

/**
* Spawn a MythicMob with an explicit scheduler delay.
* <p>
* Blueprint-paste callers need a short delay so NMS-pasted blocks settle
* before mobs land on them; synchronous callers (e.g. AOneBlock's
* {@code MythicMobCustomBlock}) can pass {@code 0} to spawn immediately on
* the current tick. When {@code delayTicks <= 0} the spawn runs inline and
* the {@code onSpawn} callback is invoked synchronously.
*
* @param mmr MythicMobRecord
* @param spawnLocation location
* @param onSpawn callback invoked with the spawned Bukkit entity; may be {@code null}
* @param delayTicks ticks to wait before spawning; {@code <= 0} = spawn immediately
* @return true if the mob type exists and a spawn was scheduled (or ran)
* @since 3.15.0
*/
public boolean spawnMythicMob(MythicMobRecord mmr, Location spawnLocation,
Consumer<Entity> onSpawn, long delayTicks) {
if (!this.isPluginAvailable()) {
return false;
}
return MythicBukkit.inst().getMobManager().getMythicMob(mmr.type()).map(mob -> {
// A delay is required before spawning, I assume because the blocks are pasted using NMS
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> {
// spawns mob
Runnable spawn = () -> {
ActiveMob activeMob = mob.spawn(BukkitAdapter.adapt(spawnLocation), mmr.level());
activeMob.setDisplayName(mmr.displayName());
activeMob.setPower(mmr.power());
Expand All @@ -93,7 +110,12 @@ public boolean spawnMythicMob(MythicMobRecord mmr, Location spawnLocation, Consu
onSpawn.accept(bukkitEntity);
}
}
}, 40L);
};
if (delayTicks <= 0L) {
spawn.run();
} else {
Bukkit.getScheduler().runTaskLater(getPlugin(), spawn, delayTicks);
}
return true;
}).orElse(false);
}
Expand Down
Loading