diff --git a/src/main/java/xiamomc/morph/utilities/EntityTypeUtils.java b/src/main/java/xiamomc/morph/utilities/EntityTypeUtils.java index 219ac175..9acb0549 100644 --- a/src/main/java/xiamomc/morph/utilities/EntityTypeUtils.java +++ b/src/main/java/xiamomc/morph/utilities/EntityTypeUtils.java @@ -19,11 +19,14 @@ import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import xiamomc.morph.MorphPlugin; import xiamomc.morph.misc.DisguiseTypes; +import xiamomc.pluginbase.Exceptions.NullDependencyException; import java.util.Arrays; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; public class EntityTypeUtils { @@ -64,21 +67,34 @@ public static SoundInfo getSoundEvent(EntityType bukkitType) var cache = typeSoundMap.getOrDefault(bukkitType, null); if (cache != null) return cache; - var nmsType = getNmsType(bukkitType); + try + { + var nmsType = getNmsType(bukkitType); + if (nmsType == null) + throw new NullPointerException("Null NMSType for BukkitType " + bukkitType); - var serverWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - var entity = nmsType.create(serverWorld, null, e -> e.remove(Entity.RemovalReason.DISCARDED), BlockPos.ZERO, MobSpawnType.COMMAND, false, false); + var serverWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle(); - if (entity instanceof Mob mob) - { - var source = mob.getSoundSource(); - var sound = mob.getAmbientSound0(); - var interval = mob.getAmbientSoundInterval(); + AtomicReference entity = new AtomicReference<>(); + Bukkit.getRegionScheduler().execute(MorphPlugin.getInstance(), Bukkit.getWorlds().get(0), 0, 0, () -> + entity.set(nmsType.create(serverWorld, null, e -> e.remove(Entity.RemovalReason.DISCARDED), BlockPos.ZERO, MobSpawnType.COMMAND, false, false))); - var rec = new SoundInfo(sound, source, interval, mob.getSoundVolume()); - typeSoundMap.put(bukkitType, rec); + if (entity.get() instanceof Mob mob) + { + var source = mob.getSoundSource(); + var sound = mob.getAmbientSound0(); + var interval = mob.getAmbientSoundInterval(); - return rec; + var rec = new SoundInfo(sound, source, interval, mob.getSoundVolume()); + typeSoundMap.put(bukkitType, rec); + + return rec; + } + } + catch (Throwable t) + { + MorphPlugin.getInstance().getSLF4JLogger().error("Unable to get SoundInfo for " + bukkitType); + t.printStackTrace(); } return new SoundInfo(null, SoundSource.PLAYERS, Integer.MAX_VALUE, 1);