--- a/net/minecraft/world/item/ItemLeash.java +++ b/net/minecraft/world/item/ItemLeash.java @@ -19,6 +19,12 @@ import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.AxisAlignedBB; +// CraftBukkit start +import org.bukkit.craftbukkit.CraftEquipmentSlot; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.event.hanging.HangingPlaceEvent; +// CraftBukkit end + public class ItemLeash extends Item { public ItemLeash(Item.Info item_info) { @@ -35,7 +41,7 @@ EntityHuman entityhuman = itemactioncontext.getPlayer(); if (!world.isClientSide && entityhuman != null) { - bindPlayerMobs(entityhuman, world, blockposition); + bindPlayerMobs(entityhuman, world, blockposition, itemactioncontext.getHand()); // CraftBukkit - Pass hand } return EnumInteractionResult.sidedSuccess(world.isClientSide); @@ -44,7 +50,7 @@ } } - public static EnumInteractionResult bindPlayerMobs(EntityHuman entityhuman, World world, BlockPosition blockposition) { + public static EnumInteractionResult bindPlayerMobs(EntityHuman entityhuman, World world, BlockPosition blockposition, net.minecraft.world.EnumHand enumhand) { // CraftBukkit - Add EnumHand EntityLeash entityleash = null; List list = leashableInArea(world, blockposition, (leashable) -> { return leashable.getLeashHolder() == entityhuman; @@ -52,22 +58,55 @@ Leashable leashable; - for (Iterator iterator = list.iterator(); iterator.hasNext(); leashable.setLeashedTo(entityleash, true)) { + for (Iterator iterator = list.iterator(); iterator.hasNext();) { // CraftBukkit - handle setLeashedTo at end of loop leashable = (Leashable) iterator.next(); if (entityleash == null) { entityleash = EntityLeash.getOrCreateKnot(world, blockposition); + + // CraftBukkit start - fire HangingPlaceEvent + org.bukkit.inventory.EquipmentSlot hand = CraftEquipmentSlot.getHand(enumhand); + HangingPlaceEvent event = new HangingPlaceEvent((org.bukkit.entity.Hanging) entityleash.getBukkitEntity(), entityhuman != null ? (org.bukkit.entity.Player) entityhuman.getBukkitEntity() : null, CraftBlock.at(world, blockposition), org.bukkit.block.BlockFace.SELF, hand); + world.getCraftServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + entityleash.discard(null); // CraftBukkit - add Bukkit remove cause + return EnumInteractionResult.PASS; + } + // CraftBukkit end entityleash.playPlacementSound(); } + + // CraftBukkit start + if (leashable instanceof Entity leashed) { + if (org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerLeashEntityEvent(leashed, entityleash, entityhuman, enumhand).isCancelled()) { + iterator.remove(); + continue; + } + } + + leashable.setLeashedTo(entityleash, true); + // CraftBukkit end } if (!list.isEmpty()) { world.gameEvent((Holder) GameEvent.BLOCK_ATTACH, blockposition, GameEvent.a.of((Entity) entityhuman)); return EnumInteractionResult.SUCCESS; } else { + // CraftBukkit start- remove leash if we do not leash any entity because of the cancelled event + if (entityleash != null) { + entityleash.discard(null); + } + // CraftBukkit end return EnumInteractionResult.PASS; } } + // CraftBukkit start + public static EnumInteractionResult bindPlayerMobs(EntityHuman entityhuman, World world, BlockPosition blockposition) { + return bindPlayerMobs(entityhuman, world, blockposition, net.minecraft.world.EnumHand.MAIN_HAND); + } + // CraftBukkit end + public static List leashableInArea(World world, BlockPosition blockposition, Predicate predicate) { double d0 = 7.0D; int i = blockposition.getX();