1212import net .minecraft .nbt .Tag ;
1313import net .minecraft .nbt .TagParser ;
1414import org .bukkit .*;
15+ import org .bukkit .block .Block ;
1516import org .bukkit .block .BlockFace ;
1617import org .bukkit .craftbukkit .v1_19_R2 .entity .CraftLivingEntity ;
1718import org .bukkit .entity .*;
1819import org .bukkit .event .EventHandler ;
1920import org .bukkit .event .EventPriority ;
2021import org .bukkit .event .Listener ;
22+ import org .bukkit .event .block .Action ;
2123import org .bukkit .event .entity .CreatureSpawnEvent ;
2224import org .bukkit .event .player .PlayerBucketFillEvent ;
2325import org .bukkit .event .player .PlayerInteractEntityEvent ;
@@ -40,7 +42,9 @@ public void bucketMob(PlayerInteractEntityEvent event) {
4042 if (event .getHand () != EquipmentSlot .HAND ) return ;
4143 Player player = event .getPlayer ();
4244 if (!player .isSneaking ()) return ;
43- if (!(event .getRightClicked () instanceof LivingEntity entity )) return ;
45+ Entity clicked = event .getRightClicked ();
46+ if (clicked instanceof ComplexEntityPart part ) clicked = part .getParent ();
47+ if (!(clicked instanceof LivingEntity entity )) return ;
4448 if (entity .getType () == EntityType .PLAYER ) return ;
4549 if (Config .getInstance ().isNoHostileTargeting ()
4650 && entity instanceof Monster monster
@@ -99,8 +103,10 @@ public void bucketMob(PlayerInteractEntityEvent event) {
99103 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
100104 public void unbucketMob (PlayerInteractEvent event ) {
101105 if (event .getHand () != EquipmentSlot .HAND ) return ;
102- Location interactLoc = event .getInteractionPoint ();
103- if (interactLoc == null ) return ;
106+ if (event .getAction () != Action .RIGHT_CLICK_BLOCK ) return ;
107+ Block block = event .getClickedBlock ();
108+ if (block == null ) return ;
109+ Location location = block .getRelative (event .getBlockFace ()).getLocation ().add (0.5 , 0.0 , 0.5 );
104110
105111 Player player = event .getPlayer ();
106112 ItemStack bucket = player .getEquipment ().getItemInMainHand ();
@@ -109,7 +115,7 @@ public void unbucketMob(PlayerInteractEvent event) {
109115
110116 String serializedNbt = bucket .getItemMeta ().getPersistentDataContainer ().get (mobNBTKey , PersistentDataType .STRING );
111117
112- try { if (serializedNbt != null ) applyNBT (interactLoc , serializedNbt , event .getBlockFace ()); }
118+ try { if (serializedNbt != null ) applyNBT (location , serializedNbt , event .getBlockFace ()); }
113119 catch (IOException | CommandSyntaxException e ) {
114120 player .sendMessage (Message .ERROR_FAILED_DESERIALIZATION .getParsedMessage ());
115121 e .printStackTrace ();
@@ -169,15 +175,16 @@ private void applyNBT(Location location, String serializedNbt, BlockFace face) t
169175 // Special cases where minecraft:id != Bukkit Name.
170176 if (id .equals ("MOOSHROOM" )) mobType = EntityType .MUSHROOM_COW ;
171177 else mobType = EntityType .valueOf (id );
172- Entity entity = location .getWorld ().spawnEntity (location , mobType , CreatureSpawnEvent .SpawnReason .CUSTOM );
173- entity .teleport (adjustLoc (location , face , entity .getBoundingBox ()));
174- CompoundTag newLoc = new CompoundTag ();
175- ((CraftLivingEntity ) entity ).getHandle ().save (newLoc );
176- tag .put ("Motion" , newLoc .get ("Motion" ));
177- tag .put ("Pos" , newLoc .get ("Pos" ));
178- tag .put ("Rotation" , newLoc .get ("Rotation" ));
179- tag .put ("UUID" , newLoc .get ("UUID" ));
180- ((CraftLivingEntity ) entity ).getHandle ().load (tag );
178+ Entity entity = location .getWorld ().spawnEntity (location , mobType , CreatureSpawnEvent .SpawnReason .CUSTOM , spawned -> {
179+ CompoundTag newLoc = new CompoundTag ();
180+ ((CraftLivingEntity ) spawned ).getHandle ().save (newLoc );
181+ tag .put ("Motion" , newLoc .get ("Motion" ));
182+ tag .put ("Pos" , newLoc .get ("Pos" ));
183+ tag .put ("Rotation" , newLoc .get ("Rotation" ));
184+ tag .put ("UUID" , newLoc .get ("UUID" ));
185+ ((CraftLivingEntity ) spawned ).getHandle ().load (tag );
186+ });
187+ // TODO: Adjust Entity on Bounding Box -- entity.teleport(adjustLoc(location, face, entity.getBoundingBox()));
181188 }
182189
183190 /**
0 commit comments