From be3d2c8afdd68f1e41f4e9ecf1e4fa935d87c798 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Thu, 7 Nov 2024 00:15:41 +0800 Subject: [PATCH] huh --- .../impl/HealsFromEntityAbility.java | 4 +++- .../nifeather/morph/misc/SoundHandler.java | 2 +- .../morph/utilities/EntityTypeUtils.java | 24 ++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/xyz/nifeather/morph/abilities/impl/HealsFromEntityAbility.java b/src/main/java/xyz/nifeather/morph/abilities/impl/HealsFromEntityAbility.java index c64e15b3..86494d52 100644 --- a/src/main/java/xyz/nifeather/morph/abilities/impl/HealsFromEntityAbility.java +++ b/src/main/java/xyz/nifeather/morph/abilities/impl/HealsFromEntityAbility.java @@ -186,7 +186,9 @@ private Entity findEntity(NmsRecord record, EntityType nmsType, double expand var boundingBox = nmsType.getDimensions().makeBoundingBox(player.position()); - Class classType = EntityTypeUtils.getNmsClass(bukkitType, record.nmsPlayer().getBukkitEntity().getWorld()); + Class classType = EntityTypeUtils.getNmsClass(bukkitType, + record.nmsPlayer().getBukkitEntity().getWorld(), + record.nmsPlayer().getBukkitEntity().getLocation()); if (classType == null) return null; diff --git a/src/main/java/xyz/nifeather/morph/misc/SoundHandler.java b/src/main/java/xyz/nifeather/morph/misc/SoundHandler.java index dd5e917a..d34bfe72 100644 --- a/src/main/java/xyz/nifeather/morph/misc/SoundHandler.java +++ b/src/main/java/xyz/nifeather/morph/misc/SoundHandler.java @@ -112,7 +112,7 @@ public void refreshSounds(EntityType entityType, boolean isBaby) soundFrequency = MathUtils.clamp(0, 2, config.getBindable(Double.class, ConfigOption.AMBIENT_FREQUENCY).get()); - var soundEvent = EntityTypeUtils.getAmbientSound(entityType, this.bindingPlayer.getWorld()); + var soundEvent = EntityTypeUtils.getAmbientSound(entityType, this.bindingPlayer.getWorld(), this.bindingPlayer.getLocation()); var sound = soundEvent.sound(); if (sound == null) return; diff --git a/src/main/java/xyz/nifeather/morph/utilities/EntityTypeUtils.java b/src/main/java/xyz/nifeather/morph/utilities/EntityTypeUtils.java index 546c0f19..90e82938 100644 --- a/src/main/java/xyz/nifeather/morph/utilities/EntityTypeUtils.java +++ b/src/main/java/xyz/nifeather/morph/utilities/EntityTypeUtils.java @@ -11,8 +11,10 @@ import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.player.Player; +import org.bukkit.Location; import org.bukkit.Sound; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.entity.Enemy; import org.bukkit.entity.EntityType; @@ -61,15 +63,25 @@ public record SoundInfo(@Nullable SoundEvent sound, SoundSource source, int inte // Hope this will work with Folia // I guess it will... - public static T createEntityThenDispose(net.minecraft.world.entity.EntityType nmsType, World world) + // + // Requiring spawn location because Folia has a bug where loading chunks will stuck the whole region thread... + // So we require a specific value that matches the player's current location, hopefully could prevent chunk loading. + @Nullable + public static T createEntityThenDispose(net.minecraft.world.entity.EntityType nmsType, World world, Location spawnLocation) { + spawnLocation = spawnLocation.clone(); + var serverWorld = ((CraftWorld) world).getHandle(); + spawnLocation.setY(-4096); + + var locationBlock = spawnLocation.toBlockLocation(); + var spawnBlockLocation = new BlockPos(locationBlock.getBlockX(), locationBlock.getBlockY(), locationBlock.getBlockZ()); - return nmsType.create(serverWorld, EntityTypeUtils::scheduleEntityDiscard, BlockPos.ZERO, MobSpawnType.COMMAND, false, false); + return nmsType.create(serverWorld, EntityTypeUtils::scheduleEntityDiscard, spawnBlockLocation, MobSpawnType.COMMAND, false, false); } @NotNull - public static SoundInfo getAmbientSound(EntityType bukkitType, World tickingWorld) + public static SoundInfo getAmbientSound(EntityType bukkitType, World tickingWorld, Location tickingLocation) { if (bukkitType == EntityType.UNKNOWN) return new SoundInfo(null, SoundSource.PLAYERS, Integer.MAX_VALUE, 1); @@ -77,7 +89,7 @@ public static SoundInfo getAmbientSound(EntityType bukkitType, World tickingWorl var cache = typeSoundMap.getOrDefault(bukkitType, null); if (cache != null) return cache; - var entity = createEntityThenDispose(getNmsType(bukkitType), tickingWorld); + var entity = createEntityThenDispose(getNmsType(bukkitType), tickingWorld, tickingLocation); if (entity instanceof Mob mob) { @@ -113,7 +125,7 @@ public static net.minecraft.world.entity.EntityType getNmsType(@NotNull Entit } @Nullable - public static Class getNmsClass(@NotNull EntityType type, World tickingWorld) + public static Class getNmsClass(@NotNull EntityType type, World tickingWorld, Location tickingLocation) { var cache = nmsClassMap.getOrDefault(type, null); if (cache != null) return cache; @@ -127,7 +139,7 @@ public static Class getNmsClass(@NotNull EntityType type, Worl return null; } - var entity = createEntityThenDispose(nmsType, tickingWorld); + var entity = createEntityThenDispose(nmsType, tickingWorld, tickingLocation); if (entity == null) {