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 2 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,65 @@
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 in their inventory
*/
@NullMarked
public class PlayerUseBowWithoutProjectileEvent extends PlayerEvent {

private static final HandlerList HANDLER_LIST = new HandlerList();

private final ItemStack item;
private ItemStack projectile;

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

/**
* Gets the item which the player tries to use
*
* @return the item
*/
public ItemStack getItem() {
return item.clone();
}
Copy link
Member

@Owen1212055 Owen1212055 Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this event is only specific to bows, I think that this should be getBowItem() or something more specific

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tries to draw a bow or load a crossbow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to find a better name I guess, but certainly not related to bows xD

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant bows, which applies to both crossbows/bow. But not sure, maybe leave as is then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess something like weapon item?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant you use anything as "bow" now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just getWeapon()?


/**
* Gets the projectile that should be used
*
* @return the projectile
*/
public ItemStack getProjectile() {
return projectile.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 setProjectile(ItemStack projectile) {
this.projectile = projectile.clone();
}
Chaosdave34 marked this conversation as resolved.
Show resolved Hide resolved

@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(stack));
+ event.callEvent();
+ return org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(event.getProjectile());
+ }
+ // Paper end - PlayerUseBowWithoutProjectileEvent
}
}
}