12
12
import net .minecraft .nbt .Tag ;
13
13
import net .minecraft .nbt .TagParser ;
14
14
import org .bukkit .*;
15
+ import org .bukkit .block .Block ;
15
16
import org .bukkit .block .BlockFace ;
16
17
import org .bukkit .craftbukkit .v1_19_R2 .entity .CraftLivingEntity ;
17
18
import org .bukkit .entity .*;
18
19
import org .bukkit .event .EventHandler ;
19
20
import org .bukkit .event .EventPriority ;
20
21
import org .bukkit .event .Listener ;
22
+ import org .bukkit .event .block .Action ;
21
23
import org .bukkit .event .entity .CreatureSpawnEvent ;
22
24
import org .bukkit .event .player .PlayerBucketFillEvent ;
23
25
import org .bukkit .event .player .PlayerInteractEntityEvent ;
@@ -40,7 +42,9 @@ public void bucketMob(PlayerInteractEntityEvent event) {
40
42
if (event .getHand () != EquipmentSlot .HAND ) return ;
41
43
Player player = event .getPlayer ();
42
44
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 ;
44
48
if (entity .getType () == EntityType .PLAYER ) return ;
45
49
if (Config .getInstance ().isNoHostileTargeting ()
46
50
&& entity instanceof Monster monster
@@ -99,8 +103,10 @@ public void bucketMob(PlayerInteractEntityEvent event) {
99
103
@ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
100
104
public void unbucketMob (PlayerInteractEvent event ) {
101
105
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 );
104
110
105
111
Player player = event .getPlayer ();
106
112
ItemStack bucket = player .getEquipment ().getItemInMainHand ();
@@ -109,7 +115,7 @@ public void unbucketMob(PlayerInteractEvent event) {
109
115
110
116
String serializedNbt = bucket .getItemMeta ().getPersistentDataContainer ().get (mobNBTKey , PersistentDataType .STRING );
111
117
112
- try { if (serializedNbt != null ) applyNBT (interactLoc , serializedNbt , event .getBlockFace ()); }
118
+ try { if (serializedNbt != null ) applyNBT (location , serializedNbt , event .getBlockFace ()); }
113
119
catch (IOException | CommandSyntaxException e ) {
114
120
player .sendMessage (Message .ERROR_FAILED_DESERIALIZATION .getParsedMessage ());
115
121
e .printStackTrace ();
@@ -169,15 +175,16 @@ private void applyNBT(Location location, String serializedNbt, BlockFace face) t
169
175
// Special cases where minecraft:id != Bukkit Name.
170
176
if (id .equals ("MOOSHROOM" )) mobType = EntityType .MUSHROOM_COW ;
171
177
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()));
181
188
}
182
189
183
190
/**
0 commit comments