Allow for 'F' to be pressed in inventories (Fixes #585) (#1164)

This commit is contained in:
Cody 2022-06-13 22:38:44 -07:00 committed by GitHub
parent bb3c232256
commit 3821d204cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 18 deletions

View File

@ -285,13 +285,14 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
@Override
public boolean changeHeld(@NotNull Player player, int slot, int key) {
final int convertedKey = key == 40 ? PlayerInventoryUtils.OFFHAND_SLOT : key;
final PlayerInventory playerInventory = player.getInventory();
final boolean isInWindow = isClickInWindow(slot);
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
final ItemStack heldItem = playerInventory.getItemStack(key);
final ItemStack heldItem = playerInventory.getItemStack(convertedKey);
final InventoryClickResult clickResult = clickProcessor.changeHeld(player,
isInWindow ? this : playerInventory, clickSlot, key, clicked, heldItem);
isInWindow ? this : playerInventory, clickSlot, convertedKey, clicked, heldItem);
if (clickResult.isCancel()) {
updateAll(player);
return false;
@ -301,7 +302,7 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
} else {
playerInventory.setItemStack(clickSlot, clickResult.getClicked());
}
playerInventory.setItemStack(key, clickResult.getCursor());
playerInventory.setItemStack(convertedKey, clickResult.getCursor());
callClickEvent(player, isInWindow ? this : null, slot, ClickType.CHANGE_HELD, clicked, getCursorItem(player));
return true;
}

View File

@ -266,18 +266,19 @@ public non-sealed class PlayerInventory extends AbstractInventory implements Equ
@Override
public boolean changeHeld(@NotNull Player player, int slot, int key) {
final int convertedKey = key == 40 ? OFFHAND_SLOT : key;
final ItemStack cursorItem = getCursorItem();
if (!cursorItem.isAir()) return false;
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
final ItemStack heldItem = getItemStack(key);
final ItemStack heldItem = getItemStack(convertedKey);
final ItemStack clicked = getItemStack(convertedSlot);
final InventoryClickResult clickResult = clickProcessor.changeHeld(player, this, convertedSlot, key, clicked, heldItem);
final InventoryClickResult clickResult = clickProcessor.changeHeld(player, this, convertedSlot, convertedKey, clicked, heldItem);
if (clickResult.isCancel()) {
update();
return false;
}
setItemStack(convertedSlot, clickResult.getClicked());
setItemStack(key, clickResult.getCursor());
setItemStack(convertedKey, clickResult.getCursor());
callClickEvent(player, null, convertedSlot, ClickType.CHANGE_HELD, clicked, cursorItem);
return true;
}

View File

@ -16,8 +16,7 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
@EnvTest
public class HeldClickIntegrationTest {
@ -30,12 +29,13 @@ public class HeldClickIntegrationTest {
var listener = env.listen(InventoryPreClickEvent.class);
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
inventory.setItemStack(2, ItemStack.of(Material.GOLD_INGOT));
inventory.setItemStack(5, ItemStack.of(Material.DIAMOND));
inventory.setItemStack(3, ItemStack.of(Material.EGG));
inventory.setItemStack(6, ItemStack.of(Material.DIAMOND));
// Empty
{
listener.followup(event -> {
assertNull(event.getInventory()); // Player inventory
assertEquals(3, event.getSlot());
assertTrue(event.getSlot() == 4 || event.getSlot() == 5);
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
assertEquals(ItemStack.AIR, inventory.getCursorItem());
@ -43,13 +43,13 @@ public class HeldClickIntegrationTest {
assertEquals(ItemStack.AIR, event.getClickedItem());
});
heldClick(player, 3, 4);
heldClick(player, 4, 5);
}
// Swap air
{
listener.followup(event -> {
assertNull(event.getInventory()); // Player inventory
assertEquals(1, event.getSlot());
assertTrue(event.getSlot() == 1 || event.getSlot() == 0);
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
assertEquals(ItemStack.AIR, inventory.getCursorItem());
@ -65,7 +65,7 @@ public class HeldClickIntegrationTest {
// Swap items
{
listener.followup(event -> {
assertEquals(0, event.getSlot());
assertTrue(event.getSlot() == 0 || event.getSlot() == 2);
assertEquals(ItemStack.AIR, inventory.getCursorItem());
assertEquals(ItemStack.AIR, inventory.getItemStack(1));
});
@ -75,6 +75,15 @@ public class HeldClickIntegrationTest {
assertEquals(ItemStack.of(Material.DIAMOND), inventory.getItemStack(2));
assertEquals(ItemStack.of(Material.GOLD_INGOT), inventory.getItemStack(0));
}
// Swap offhand
{
listener.followup(event -> {
assertTrue(event.getSlot() == 3 || event.getSlot() == 45 /* Vanilla offhand slot is 40, Minestom is 45 */);
});
heldClick(player, 3, 40);
assertEquals(ItemStack.AIR, inventory.getItemStack(3));
assertEquals(ItemStack.of(Material.EGG), inventory.getItemInOffHand());
}
// Cancel event
{
listener.followup(event -> event.setCancelled(true));
@ -96,11 +105,12 @@ public class HeldClickIntegrationTest {
var listener = env.listen(InventoryPreClickEvent.class);
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
inventory.setItemStack(2, ItemStack.of(Material.GOLD_INGOT));
inventory.setItemStack(3, ItemStack.of(Material.EGG));
inventory.setItemStack(4, ItemStack.of(Material.DIAMOND));
// Empty
{
listener.followup(event -> {
assertEquals(inventory, event.getInventory());
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
assertEquals(0, event.getSlot());
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
@ -112,8 +122,8 @@ public class HeldClickIntegrationTest {
// Swap empty
{
listener.followup(event -> {
assertEquals(inventory, event.getInventory());
assertEquals(1, event.getSlot());
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
assertTrue(event.getSlot() == 1 || event.getSlot() == 0);
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
});
heldClickOpenInventory(player, 1, 0);
@ -124,8 +134,8 @@ public class HeldClickIntegrationTest {
// Swap items
{
listener.followup(event -> {
assertEquals(inventory, event.getInventory());
assertEquals(2, event.getSlot());
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
assertTrue(event.getSlot() == 2 || event.getSlot() == 0);
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
});
heldClickOpenInventory(player, 2, 0);
@ -133,6 +143,16 @@ public class HeldClickIntegrationTest {
assertEquals(ItemStack.of(Material.DIAMOND), inventory.getItemStack(2));
assertEquals(ItemStack.of(Material.GOLD_INGOT), playerInv.getItemStack(0));
}
// Swap offhand
{
listener.followup(event -> {
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
assertTrue(event.getSlot() == 3 || event.getSlot() == 45);
});
heldClickOpenInventory(player, 3, 40);
assertEquals(ItemStack.AIR, inventory.getItemStack(3));
assertEquals(ItemStack.of(Material.EGG), playerInv.getItemInOffHand());
}
// Cancel event
{
listener.followup(event -> event.setCancelled(true));