Skip to content

Commit

Permalink
fix: World mismatch for getNmsClass() and getAmbientSound() in Entity…
Browse files Browse the repository at this point in the history
…TypeUtils
  • Loading branch information
MATRIX-feather committed Mar 3, 2024
1 parent 5577ee1 commit eb4ed26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private Entity findEntity(NmsRecord record, EntityType<?> nmsType, double expand

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

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

if (classType == null) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xiamomc/morph/misc/DisguiseState.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,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);
var soundEvent = EntityTypeUtils.getAmbientSound(entityType, bindingPlayer.getWorld());

var sound = soundEvent.sound();
if (sound == null) return;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/xiamomc/morph/utilities/EntityTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftEntity;
import org.bukkit.entity.Enemy;
import org.bukkit.entity.EntityType;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPlugin;
Expand Down Expand Up @@ -62,14 +64,15 @@ public record SoundInfo(@Nullable SoundEvent sound, SoundSource source, int inte
}

@NotNull
public static SoundInfo getAmbientSound(EntityType bukkitType)
public static SoundInfo getAmbientSound(EntityType bukkitType, @Nullable World world)
{
var cache = typeSoundMap.getOrDefault(bukkitType, null);
if (cache != null) return cache;

var nmsType = getNmsType(bukkitType);

var serverWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
if (world == null) world = Bukkit.getWorlds().get(0);
var serverWorld = ((CraftWorld) world).getHandle();
var entity = nmsType.create(serverWorld, null, EntityTypeUtils::scheduleEntityDiscard, BlockPos.ZERO, MobSpawnType.COMMAND, false, false);

if (entity instanceof Mob mob)
Expand All @@ -95,7 +98,7 @@ public static net.minecraft.world.entity.EntityType<?> getNmsType(@NotNull Entit
}

@Nullable
public static Class<? extends Entity> getNmsClass(@NotNull EntityType type)
public static Class<? extends Entity> getNmsClass(@NotNull EntityType type, @Nullable World world)
{
var cache = nmsClassMap.getOrDefault(type, null);
if (cache != null) return cache;
Expand All @@ -109,7 +112,8 @@ public static Class<? extends Entity> getNmsClass(@NotNull EntityType type)
return null;
}

var serverWorld = ((CraftWorld) Bukkit.getWorlds().get(0)).getHandle();
if (world == null) world = Bukkit.getWorlds().get(0);
var serverWorld = ((CraftWorld) world).getHandle();
var entity = nmsType.create(serverWorld, null, EntityTypeUtils::scheduleEntityDiscard, BlockPos.ZERO, MobSpawnType.COMMAND, false, false);

if (entity == null)
Expand Down

0 comments on commit eb4ed26

Please sign in to comment.