mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-03 23:17:48 +01:00
Added PlayerSetItemStackEvent#setSlot + some checks
This commit is contained in:
parent
633d02222a
commit
ea034701f8
@ -10,10 +10,12 @@ import net.minestom.server.item.ItemStack;
|
||||
public class PlayerSetItemStackEvent extends CancellableEvent {
|
||||
|
||||
private Player player;
|
||||
private int slot;
|
||||
private ItemStack itemStack;
|
||||
|
||||
public PlayerSetItemStackEvent(Player player, ItemStack itemStack) {
|
||||
public PlayerSetItemStackEvent(Player player, int slot, ItemStack itemStack) {
|
||||
this.player = player;
|
||||
this.slot = slot;
|
||||
this.itemStack = itemStack;
|
||||
}
|
||||
|
||||
@ -26,6 +28,24 @@ public class PlayerSetItemStackEvent extends CancellableEvent {
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the slot where the item will be set
|
||||
*
|
||||
* @return the slot
|
||||
*/
|
||||
public int getSlot() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the slot where the item will be set
|
||||
*
|
||||
* @param slot the new slot
|
||||
*/
|
||||
public void setSlot(int slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the item stack which will be set
|
||||
*
|
||||
|
@ -18,6 +18,7 @@ import net.minestom.server.network.packet.server.play.WindowItemsPacket;
|
||||
import net.minestom.server.utils.ArrayUtils;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.item.ItemStackUtils;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -71,11 +72,14 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
public void setItemStack(int slot, ItemStack itemStack) {
|
||||
itemStack = ItemStackUtils.notNull(itemStack);
|
||||
|
||||
PlayerSetItemStackEvent setItemStackEvent = new PlayerSetItemStackEvent(player, itemStack);
|
||||
player.callCancellableEvent(PlayerSetItemStackEvent.class, setItemStackEvent, () -> {
|
||||
ItemStack item = setItemStackEvent.getItemStack();
|
||||
safeItemInsert(slot, item);
|
||||
});
|
||||
PlayerSetItemStackEvent setItemStackEvent = new PlayerSetItemStackEvent(player, slot, itemStack);
|
||||
player.callEvent(PlayerSetItemStackEvent.class, setItemStackEvent);
|
||||
if (setItemStackEvent.isCancelled())
|
||||
return;
|
||||
slot = setItemStackEvent.getSlot();
|
||||
itemStack = setItemStackEvent.getItemStack();
|
||||
|
||||
safeItemInsert(slot, itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -222,8 +226,14 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
*
|
||||
* @param slot an internal slot
|
||||
* @param itemStack the item to insert at the slot
|
||||
* @throws IllegalArgumentException if the slot {@code slot} does not exist
|
||||
* @throws NullPointerException if {@code itemStack} is null
|
||||
*/
|
||||
private synchronized void safeItemInsert(int slot, ItemStack itemStack) {
|
||||
Check.argCondition(!MathUtils.isBetween(slot, 0, getSize()),
|
||||
"The slot " + slot + " does not exist for player");
|
||||
Check.notNull(itemStack, "The ItemStack cannot be null, you can set air instead");
|
||||
|
||||
EntityEquipmentPacket.Slot equipmentSlot;
|
||||
|
||||
if (slot == player.getHeldSlot()) {
|
||||
|
Loading…
Reference in New Issue
Block a user