Fix NPE when clicking outside of the inventory to drop

This commit is contained in:
themode 2020-12-18 04:05:29 +01:00
parent 213599a649
commit c7876d648c
3 changed files with 11 additions and 9 deletions

View File

@ -534,8 +534,9 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
public boolean drop(@NotNull Player player, int mode, int slot, int button) {
final PlayerInventory playerInventory = player.getInventory();
final boolean isInWindow = isClickInWindow(slot);
final ItemStack clicked = slot == -999 ?
null : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset));
final boolean outsideDrop = slot == -999;
final ItemStack clicked = outsideDrop ?
ItemStack.getAirItem() : (isInWindow ? getItemStack(slot) : playerInventory.getItemStack(slot, offset));
final ItemStack cursor = getCursorItem(player);
final InventoryClickResult clickResult = clickProcessor.drop(this, player,
@ -546,7 +547,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
}
ItemStack resultClicked = clickResult.getClicked();
if (isInWindow) {
if (isInWindow && !outsideDrop) {
if (resultClicked != null)
setItemStack(slot, resultClicked);
} else {

View File

@ -414,7 +414,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
@Override
public boolean drop(@NotNull Player player, int mode, int slot, int button) {
final ItemStack cursor = getCursorItem();
final ItemStack clicked = slot == -999 ? null : getItemStack(slot, OFFSET);
final boolean outsideDrop = slot == -999;
final ItemStack clicked = outsideDrop ? ItemStack.getAirItem() : getItemStack(slot, OFFSET);
final InventoryClickResult clickResult = clickProcessor.drop(null, player,
mode, slot, button, clicked, cursor);
@ -423,7 +424,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
sendSlotRefresh((short) slot, clicked);
ItemStack resultClicked = clickResult.getClicked();
if (resultClicked != null)
if (resultClicked != null && !outsideDrop)
setItemStack(slot, OFFSET, resultClicked);
setCursorItem(clickResult.getCursor());

View File

@ -425,17 +425,17 @@ public class InventoryClickProcessor {
public InventoryClickResult drop(Inventory inventory, Player player,
int mode, int slot, int button,
ItemStack clicked, ItemStack cursor) {
@NotNull ItemStack clicked, @NotNull ItemStack cursor) {
final InventoryClickResult clickResult = startCondition(inventory, player, slot, ClickType.DROP, clicked, cursor);
if (clickResult.isCancel()) {
return clickResult;
}
final StackingRule clickedRule = clicked == null ? null : clicked.getStackingRule();
final StackingRule clickedRule = clicked.getStackingRule();
final StackingRule cursorRule = cursor.getStackingRule();
ItemStack resultClicked = clicked == null ? null : clicked.clone();
ItemStack resultClicked = clicked.clone();
ItemStack resultCursor = cursor.clone();
@ -494,7 +494,7 @@ public class InventoryClickProcessor {
@NotNull
private InventoryClickResult startCondition(@NotNull InventoryClickResult clickResult, @Nullable Inventory inventory,
@NotNull Player player, int slot, @NotNull ClickType clickType,
ItemStack clicked, ItemStack cursor) {
@NotNull ItemStack clicked, ItemStack cursor) {
boolean isPlayerInventory = inventory == null;
final int inventorySlot = isPlayerInventory ? 0 : inventory.getSize();
isPlayerInventory = isPlayerInventory ? isPlayerInventory : slot >= inventorySlot;