Skip to content

Commit

Permalink
huh
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 6, 2024
1 parent 60a6a42 commit be3d2c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ private Entity findEntity(NmsRecord record, EntityType<?> nmsType, double expand

var boundingBox = nmsType.getDimensions().makeBoundingBox(player.position());

Class<? extends Entity> classType = EntityTypeUtils.getNmsClass(bukkitType, record.nmsPlayer().getBukkitEntity().getWorld());
Class<? extends Entity> classType = EntityTypeUtils.getNmsClass(bukkitType,
record.nmsPlayer().getBukkitEntity().getWorld(),
record.nmsPlayer().getBukkitEntity().getLocation());

if (classType == null) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/nifeather/morph/misc/SoundHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/xyz/nifeather/morph/utilities/EntityTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,23 +63,33 @@ public record SoundInfo(@Nullable SoundEvent sound, SoundSource source, int inte

// Hope this will work with Folia
// I guess it will...
public static <T extends Entity> T createEntityThenDispose(net.minecraft.world.entity.EntityType<T> 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 extends Entity> T createEntityThenDispose(net.minecraft.world.entity.EntityType<T> 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);

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)
{
Expand Down Expand Up @@ -113,7 +125,7 @@ public static net.minecraft.world.entity.EntityType<?> getNmsType(@NotNull Entit
}

@Nullable
public static Class<? extends Entity> getNmsClass(@NotNull EntityType type, World tickingWorld)
public static Class<? extends Entity> getNmsClass(@NotNull EntityType type, World tickingWorld, Location tickingLocation)
{
var cache = nmsClassMap.getOrDefault(type, null);
if (cache != null) return cache;
Expand All @@ -127,7 +139,7 @@ public static Class<? extends Entity> getNmsClass(@NotNull EntityType type, Worl
return null;
}

var entity = createEntityThenDispose(nmsType, tickingWorld);
var entity = createEntityThenDispose(nmsType, tickingWorld, tickingLocation);

if (entity == null)
{
Expand Down

0 comments on commit be3d2c8

Please sign in to comment.