mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 14:07:43 +01:00
parent
bb3c232256
commit
3821d204cf
@ -285,13 +285,14 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean changeHeld(@NotNull Player player, int slot, int key) {
|
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 PlayerInventory playerInventory = player.getInventory();
|
||||||
final boolean isInWindow = isClickInWindow(slot);
|
final boolean isInWindow = isClickInWindow(slot);
|
||||||
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
final int clickSlot = isInWindow ? slot : PlayerInventoryUtils.convertSlot(slot, offset);
|
||||||
final ItemStack clicked = isInWindow ? getItemStack(slot) : playerInventory.getItemStack(clickSlot);
|
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,
|
final InventoryClickResult clickResult = clickProcessor.changeHeld(player,
|
||||||
isInWindow ? this : playerInventory, clickSlot, key, clicked, heldItem);
|
isInWindow ? this : playerInventory, clickSlot, convertedKey, clicked, heldItem);
|
||||||
if (clickResult.isCancel()) {
|
if (clickResult.isCancel()) {
|
||||||
updateAll(player);
|
updateAll(player);
|
||||||
return false;
|
return false;
|
||||||
@ -301,7 +302,7 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
|
|||||||
} else {
|
} else {
|
||||||
playerInventory.setItemStack(clickSlot, clickResult.getClicked());
|
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));
|
callClickEvent(player, isInWindow ? this : null, slot, ClickType.CHANGE_HELD, clicked, getCursorItem(player));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -266,18 +266,19 @@ public non-sealed class PlayerInventory extends AbstractInventory implements Equ
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean changeHeld(@NotNull Player player, int slot, int key) {
|
public boolean changeHeld(@NotNull Player player, int slot, int key) {
|
||||||
|
final int convertedKey = key == 40 ? OFFHAND_SLOT : key;
|
||||||
final ItemStack cursorItem = getCursorItem();
|
final ItemStack cursorItem = getCursorItem();
|
||||||
if (!cursorItem.isAir()) return false;
|
if (!cursorItem.isAir()) return false;
|
||||||
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
|
final int convertedSlot = convertPlayerInventorySlot(slot, OFFSET);
|
||||||
final ItemStack heldItem = getItemStack(key);
|
final ItemStack heldItem = getItemStack(convertedKey);
|
||||||
final ItemStack clicked = getItemStack(convertedSlot);
|
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()) {
|
if (clickResult.isCancel()) {
|
||||||
update();
|
update();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
setItemStack(convertedSlot, clickResult.getClicked());
|
setItemStack(convertedSlot, clickResult.getClicked());
|
||||||
setItemStack(key, clickResult.getCursor());
|
setItemStack(convertedKey, clickResult.getCursor());
|
||||||
callClickEvent(player, null, convertedSlot, ClickType.CHANGE_HELD, clicked, cursorItem);
|
callClickEvent(player, null, convertedSlot, ClickType.CHANGE_HELD, clicked, cursorItem);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
|
|
||||||
@EnvTest
|
@EnvTest
|
||||||
public class HeldClickIntegrationTest {
|
public class HeldClickIntegrationTest {
|
||||||
@ -30,12 +29,13 @@ public class HeldClickIntegrationTest {
|
|||||||
var listener = env.listen(InventoryPreClickEvent.class);
|
var listener = env.listen(InventoryPreClickEvent.class);
|
||||||
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
|
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
|
||||||
inventory.setItemStack(2, ItemStack.of(Material.GOLD_INGOT));
|
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
|
// Empty
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertNull(event.getInventory()); // Player inventory
|
assertNull(event.getInventory()); // Player inventory
|
||||||
assertEquals(3, event.getSlot());
|
assertTrue(event.getSlot() == 4 || event.getSlot() == 5);
|
||||||
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
||||||
|
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
||||||
@ -43,13 +43,13 @@ public class HeldClickIntegrationTest {
|
|||||||
|
|
||||||
assertEquals(ItemStack.AIR, event.getClickedItem());
|
assertEquals(ItemStack.AIR, event.getClickedItem());
|
||||||
});
|
});
|
||||||
heldClick(player, 3, 4);
|
heldClick(player, 4, 5);
|
||||||
}
|
}
|
||||||
// Swap air
|
// Swap air
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertNull(event.getInventory()); // Player inventory
|
assertNull(event.getInventory()); // Player inventory
|
||||||
assertEquals(1, event.getSlot());
|
assertTrue(event.getSlot() == 1 || event.getSlot() == 0);
|
||||||
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
||||||
|
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
||||||
@ -65,7 +65,7 @@ public class HeldClickIntegrationTest {
|
|||||||
// Swap items
|
// Swap items
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertEquals(0, event.getSlot());
|
assertTrue(event.getSlot() == 0 || event.getSlot() == 2);
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
assertEquals(ItemStack.AIR, inventory.getCursorItem());
|
||||||
assertEquals(ItemStack.AIR, inventory.getItemStack(1));
|
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.DIAMOND), inventory.getItemStack(2));
|
||||||
assertEquals(ItemStack.of(Material.GOLD_INGOT), inventory.getItemStack(0));
|
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
|
// Cancel event
|
||||||
{
|
{
|
||||||
listener.followup(event -> event.setCancelled(true));
|
listener.followup(event -> event.setCancelled(true));
|
||||||
@ -96,11 +105,12 @@ public class HeldClickIntegrationTest {
|
|||||||
var listener = env.listen(InventoryPreClickEvent.class);
|
var listener = env.listen(InventoryPreClickEvent.class);
|
||||||
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
|
inventory.setItemStack(1, ItemStack.of(Material.DIAMOND));
|
||||||
inventory.setItemStack(2, ItemStack.of(Material.GOLD_INGOT));
|
inventory.setItemStack(2, ItemStack.of(Material.GOLD_INGOT));
|
||||||
|
inventory.setItemStack(3, ItemStack.of(Material.EGG));
|
||||||
inventory.setItemStack(4, ItemStack.of(Material.DIAMOND));
|
inventory.setItemStack(4, ItemStack.of(Material.DIAMOND));
|
||||||
// Empty
|
// Empty
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertEquals(inventory, event.getInventory());
|
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
|
||||||
assertEquals(0, event.getSlot());
|
assertEquals(0, event.getSlot());
|
||||||
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
assertEquals(ClickType.CHANGE_HELD, event.getClickType());
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
||||||
@ -112,8 +122,8 @@ public class HeldClickIntegrationTest {
|
|||||||
// Swap empty
|
// Swap empty
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertEquals(inventory, event.getInventory());
|
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
|
||||||
assertEquals(1, event.getSlot());
|
assertTrue(event.getSlot() == 1 || event.getSlot() == 0);
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
||||||
});
|
});
|
||||||
heldClickOpenInventory(player, 1, 0);
|
heldClickOpenInventory(player, 1, 0);
|
||||||
@ -124,8 +134,8 @@ public class HeldClickIntegrationTest {
|
|||||||
// Swap items
|
// Swap items
|
||||||
{
|
{
|
||||||
listener.followup(event -> {
|
listener.followup(event -> {
|
||||||
assertEquals(inventory, event.getInventory());
|
if (event.getInventory() != null) assertEquals(inventory, event.getInventory());
|
||||||
assertEquals(2, event.getSlot());
|
assertTrue(event.getSlot() == 2 || event.getSlot() == 0);
|
||||||
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
assertEquals(ItemStack.AIR, inventory.getCursorItem(player));
|
||||||
});
|
});
|
||||||
heldClickOpenInventory(player, 2, 0);
|
heldClickOpenInventory(player, 2, 0);
|
||||||
@ -133,6 +143,16 @@ public class HeldClickIntegrationTest {
|
|||||||
assertEquals(ItemStack.of(Material.DIAMOND), inventory.getItemStack(2));
|
assertEquals(ItemStack.of(Material.DIAMOND), inventory.getItemStack(2));
|
||||||
assertEquals(ItemStack.of(Material.GOLD_INGOT), playerInv.getItemStack(0));
|
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
|
// Cancel event
|
||||||
{
|
{
|
||||||
listener.followup(event -> event.setCancelled(true));
|
listener.followup(event -> event.setCancelled(true));
|
||||||
|
Loading…
Reference in New Issue
Block a user