Add SulfurCubeSwallowEvent (#14061)

This commit is contained in:
Amine Kacimi 2026-08-05 16:09:21 +01:00 committed by GitHub
parent 3c6ac856f8
commit 71fa8decd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,101 @@
package io.papermc.paper.event.entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.SulfurCube;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
/**
* Called when a SulfurCube swallows an item.
* <p>
* If the ItemStack is modified, the SulfurCube will swallow the new item
* and not remove the original one from the player's inventory.
* <p>
* If the event is cancelled, the SulfurCube will not swallow the item, and
* it will not be removed from the player's inventory.
*/
@NullMarked
public class SulfurCubeSwallowItemEvent extends EntityEvent implements Cancellable {
private static final HandlerList HANDLER_LIST = new HandlerList();
private final Player player;
private final ItemStack oldItem;
private ItemStack newItem;
private boolean cancelled;
@ApiStatus.Internal
public SulfurCubeSwallowItemEvent(final SulfurCube sulfurCube, final Player player, final ItemStack oldItem, final ItemStack newItem) {
super(sulfurCube);
this.player = player;
this.oldItem = oldItem;
this.newItem = newItem;
}
/**
* Gets the player interacting with the SulfurCube.
*
* @return the player that interacted with the SulfurCube
*/
public Player getPlayer() {
return this.player;
}
/**
* Gets the item that is currently swallowed by the SulfurCube.
*
* @return an ItemStack for the item currently swallowed
*/
public ItemStack getOldItem() {
return this.oldItem.clone();
}
/**
* Gets the item that is being swallowed. Modifying the returned item will
* have no effect, you must use {@link
* #setNewItem(org.bukkit.inventory.ItemStack)} instead.
*
* @return an ItemStack for the item being swallowed
*/
public ItemStack getNewItem() {
return this.newItem.clone();
}
/**
* Set the item being swallowed.
*
* @param newItem the item being swallowed
*/
public void setNewItem(ItemStack newItem) {
this.newItem = newItem;
}
@Override
public SulfurCube getEntity() {
return (SulfurCube) super.getEntity();
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(final boolean cancel) {
this.cancelled = cancel;
}
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}

View file

@ -91,6 +91,35 @@
this.gameEvent(GameEvent.SHEAR, player);
heldItem.hurtAndBreak(1, player, hand.asEquipmentSlot());
CriteriaTriggers.PLAYER_SHEARED_EQUIPMENT.trigger((ServerPlayer)player, itemStackToShear, this);
@@ -463,12 +_,26 @@
return InteractionResult.SUCCESS;
} else if (isSwallowableItem(heldItem)) {
- boolean itWorked = this.equipItem(heldItem);
+ // Paper start - SulfurCubeSwallowItemEvent
+ org.bukkit.inventory.ItemStack craftSwallowedItem = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(this.getItemBySlot(EquipmentSlot.BODY));
+ org.bukkit.inventory.ItemStack craftHeldItem = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(heldItem);
+ io.papermc.paper.event.entity.SulfurCubeSwallowItemEvent event = new io.papermc.paper.event.entity.SulfurCubeSwallowItemEvent(
+ (org.bukkit.entity.SulfurCube) this.getBukkitLivingEntity(), (org.bukkit.entity.Player) player.getBukkitLivingEntity(), craftSwallowedItem, craftHeldItem
+ );
+ if (!event.callEvent()) {
+ if (player instanceof final ServerPlayer serverPlayer) serverPlayer.containerMenu.forceHeldSlot(hand);
+ return InteractionResult.PASS;
+ }
+ boolean result = craftHeldItem.equals(event.getNewItem());
+ boolean itWorked = this.equipItem(result ? heldItem : org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(event.getNewItem())); // Paper - SulfurCubeSwallowItemEvent
+ // Paper end - SulfurCubeSwallowItemEvent
if (itWorked) {
- heldItem.consume(1, player);
+ if (result) heldItem.consume(1, player); // Paper - SulfurCubeSwallowItemEvent
this.gameEvent(GameEvent.ENTITY_INTERACT);
}
+ if (player instanceof final ServerPlayer serverPlayer) serverPlayer.containerMenu.forceHeldSlot(hand); // Paper - SulfurCubeSwallowItemEvent
+
return itWorked ? InteractionResult.SUCCESS_SERVER : InteractionResult.PASS;
} else {
return Bucketable.bucketMobPickup(player, hand, this).orElse(super.mobInteract(player, hand));
@@ -525,7 +_,7 @@
if (this.level() instanceof ServerLevel serverLevel) {
for (SulfurCubeArchetype.ContactDamage damage : this.contactDamages) {