Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlayerUseBowWithoutProjectileEvent #11774

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.papermc.paper.event.player;

import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;

/**
* Called when a player tries to draw a bow or load a crossbow without having a suitable projectile item in their inventory
*/
@NullMarked
public class PlayerUseBowWithoutProjectileEvent extends PlayerEvent {

private static final HandlerList HANDLER_LIST = new HandlerList();

private final ItemStack weapon;
private ItemStack projectileItem;

@ApiStatus.Internal
public PlayerUseBowWithoutProjectileEvent(final Player player, final ItemStack weapon) {
super(player);
this.weapon = weapon;
this.projectileItem = ItemStack.empty();
}

/**
* @return The weapon wich the player tries to use
*/
public ItemStack getWeapon() {
return weapon.clone();
}

/**
* @return The projectile that should be used
*/
public ItemStack getProjectileItem() {
return projectileItem.clone();
}

/**
* Sets the projectile that should be used
* <p>
* Note: setting this to {@link ItemStack#empty()} will prevent the player from using the bow/crossbow
*
* @param projectile the projectile
*/
public void setProjectileItem(ItemStack projectile) {
this.projectileItem = projectile.clone();
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,20 @@

for (int i = 0; i < this.inventory.getContainerSize(); i++) {
ItemStack item = this.inventory.getItem(i);
@@ -2007,7 +_,15 @@
}
}

- return this.abilities.instabuild ? new ItemStack(Items.ARROW) : ItemStack.EMPTY;
+ // Paper start - PlayerUseBowWithoutProjectileEvent
+ if (this.abilities.instabuild) {
+ return new ItemStack(Items.ARROW);
+ } else {
+ io.papermc.paper.event.player.PlayerUseBowWithoutProjectileEvent event = new io.papermc.paper.event.player.PlayerUseBowWithoutProjectileEvent((org.bukkit.entity.Player) getBukkitEntity(), org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(shootable));
+ event.callEvent();
+ return org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(event.getProjectileItem());
+ }
+ // Paper end - PlayerUseBowWithoutProjectileEvent
}
}
}