77import io .lumine .mythic .bukkit .MythicBukkit ;
88import io .lumine .mythic .core .mobs .ActiveMob ;
99import org .bukkit .Location ;
10+ import org .bukkit .entity .Entity ;
1011
1112import java .util .Optional ;
1213
@@ -20,22 +21,59 @@ public MythicMobCrateDisplay(Location location, String value, float yaw) {
2021
2122 @ Override
2223 public void spawn () {
24+ // Create centered location while preserving yaw and pitch from original location
25+ Location originalLocation = getLocation ();
26+ Location centeredLocation = originalLocation .clone ().add (0.5 , 0 , 0.5 );
27+ centeredLocation .setYaw (originalLocation .getYaw ());
28+ centeredLocation .setPitch (originalLocation .getPitch ());
29+
30+ // Remove any existing entity at this location (from previous server session)
31+ // This ensures ModelEngine properly applies the model to a fresh spawn
32+ Entity existingEntity = findExistingMythicEntity (centeredLocation );
33+ if (existingEntity != null ) {
34+ // Check if it's an ActiveMob and remove it properly
35+ Optional <ActiveMob > existingActiveMob = MythicBukkit .inst ().getMobManager ().getActiveMob (existingEntity .getUniqueId ());
36+ if (existingActiveMob .isPresent ()) {
37+ existingActiveMob .get ().remove ();
38+ } else {
39+ existingEntity .remove ();
40+ }
41+ }
42+
43+ // Always spawn a fresh MythicMob to ensure ModelEngine applies the model correctly
2344 Optional <MythicMob > mythicMobOpt = MythicBukkit .inst ().getMobManager ().getMythicMob (this .entityType );
2445 if (mythicMobOpt .isEmpty ()) {
2546 throw new IllegalArgumentException ("MythicMob type not found: " + this .entityType );
2647 }
2748
28- this .activeMob = mythicMobOpt .get ().spawn (BukkitAdapter .adapt (getLocation ()), 1 );
49+ // Spawn with the centered location that includes the correct yaw
50+ this .activeMob = mythicMobOpt .get ().spawn (BukkitAdapter .adapt (centeredLocation ), 1 );
2951 this .entity = activeMob .getEntity ().getBukkitEntity ();
3052
31- this .entity .setRotation (getLocation ().getYaw (), 0 );
53+ // Apply rotation immediately
54+ this .entity .setRotation (centeredLocation .getYaw (), 0 );
3255 this .entity .setInvulnerable (true );
3356 this .entity .setPersistent (true );
3457 this .entity .setGravity (false );
3558
3659 Keys .PLACED_CRATE_ENTITY .set (this .entity .getPersistentDataContainer (), true );
3760 }
3861
62+ private Entity findExistingMythicEntity (Location centeredLocation ) {
63+ // Search for entities with the PDC marker in the vicinity
64+ return centeredLocation .getWorld ().getNearbyEntities (centeredLocation , 1 , 1 , 1 ).stream ()
65+ .filter (e -> {
66+ // Check if entity has our marker
67+ if (!Keys .PLACED_CRATE_ENTITY .get (e .getPersistentDataContainer (), false )) {
68+ return false ;
69+ }
70+ // Check if this is a MythicMob entity
71+ return MythicBukkit .inst ().getMobManager ().isMythicMob (e );
72+ })
73+ .findFirst ()
74+ .orElse (null );
75+ }
76+
3977 @ Override
4078 public void remove () {
4179 if (activeMob != null ) {
0 commit comments