Skip to content

Instantly share code, notes, and snippets.

@Excel619
Last active September 9, 2024 03:11
Show Gist options
  • Save Excel619/80151feb5fcad1bd740e68c8ff7713c2 to your computer and use it in GitHub Desktop.
Save Excel619/80151feb5fcad1bd740e68c8ff7713c2 to your computer and use it in GitHub Desktop.
Custom PlayerDropItemEvent including the slot
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class PlayerDropItemSlotEvent extends Event implements Cancellable {
private final Player player;
private final ItemStack item;
private final Inventory inventory;
private final int slot;
private boolean cancelled = false;
private static final HandlerList handlers = new HandlerList();
/**
* Similar to PlayerDropItemEvent, but includes the slot of the item dropped.
* If the item dropped was from the player's cursor with their inventory opened,
* getSlot() will return -1 and isCursor() will return true;
*
* @param player The player dropping the item
* @param item The ItemStack being dropped
* @param inventory The inventory that the item was dropped from
* @param slot The slot of the item dropped, -1 if cursor
*/
public PlayerDropItemSlotEvent(Player player, ItemStack item, Inventory inventory, int slot) {
this.player = player;
this.item = item;
this.inventory = inventory;
this.slot = slot;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Get the player that dropped this item
* @return Player
*/
public Player getPlayer() {
return this.player;
}
/**
* Get the ItemStack dropped by the player.
* @return ItemStack
*/
public ItemStack getItemStack() {
return this.item;
}
/**
* Get the inventory that this item was dropped from.
* Is usually the player's inventory, unless the player is dropping the item directly from a chest.
* If the player drops the item from their cursor this will be null.
* @return Inventory
*/
public Inventory getInventory() {
return this.inventory;
}
/**
* Get the slot of the item dropped.
* This returns -1 if the item was dropped from the cursor in the player's inventory.
* @return Slot
*/
public int getSlot() {
return this.slot;
}
/**
* Whether or not this item dropped is from the player's cursor.
* @return Is cursor
*/
public boolean isCursor() {
return this.slot == -1;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean canceled) {
this.cancelled = true;
}
}
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
public class PlayerDropItemSlotHandler implements Listener {
public PlayerDropItemSlotHandler() {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(RunicItems.getInstance(), PacketType.Play.Client.BLOCK_DIG) {
@Override
public void onPacketReceiving(PacketEvent event) {
PacketContainer container = event.getPacket();
if (container.getPlayerDigTypes().getValues().get(0).equals(EnumWrappers.PlayerDigType.DROP_ITEM)
|| container.getPlayerDigTypes().getValues().get(0).equals(EnumWrappers.PlayerDigType.DROP_ALL_ITEMS)) {
Player player = event.getPlayer();
PlayerDropItemSlotEvent customEvent = new PlayerDropItemSlotEvent(
player,
player.getInventory().getItemInMainHand(),
player.getInventory(),
player.getInventory().getHeldItemSlot());
Bukkit.getPluginManager().callEvent(customEvent);
if (customEvent.isCancelled()) event.setCancelled(true);
}
}
});
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return;
Player player = (Player) event.getWhoClicked();
PlayerDropItemSlotEvent customEvent = null;
if (event.getAction() == InventoryAction.DROP_ALL_SLOT
|| event.getAction() == InventoryAction.DROP_ONE_SLOT) {
customEvent = new PlayerDropItemSlotEvent(player, event.getCurrentItem(), event.getClickedInventory(), event.getSlot());
} else if (event.getAction() == InventoryAction.DROP_ALL_CURSOR
|| event.getAction() == InventoryAction.DROP_ONE_CURSOR) {
customEvent = new PlayerDropItemSlotEvent(player, event.getCursor(), event.getClickedInventory(), -1);
}
if (customEvent == null) return;
Bukkit.getPluginManager().callEvent(customEvent);
if (customEvent.isCancelled()) event.setCancelled(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment