From b12da22063e727a2c9338242216191cbf17c28fd Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 1 Nov 2023 22:14:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A0=E6=B3=95=E5=9C=A8=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E4=B8=96=E7=95=8C=E8=BF=9B=E8=A1=8C=E4=BC=AA=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../morph/utilities/EntityTypeUtils.java | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) 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);