Remove Click's Inventory dependency

This commit is contained in:
themode 2024-04-11 22:00:20 +02:00 committed by mworzala
parent 895248d485
commit 321234688d
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
4 changed files with 35 additions and 31 deletions

View File

@ -97,7 +97,7 @@ public non-sealed class PlayerInventory extends InventoryImpl {
/**
* Sets the cursor item for all viewers of this inventory.
* @param cursorItem the new item (will not update if same as current)
* @param sendPacket whether or not to send a packet
* @param sendPacket whether to send a packet
*/
public void setCursorItem(@NotNull ItemStack cursorItem, boolean sendPacket) {
if (this.cursorItem.equals(cursorItem)) return;

View File

@ -1,7 +1,5 @@
package net.minestom.server.inventory.click;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.play.ClientClickWindowPacket;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
@ -74,9 +72,9 @@ public final class Click {
private final List<Integer> middleDrag = new ArrayList<>();
public void clearCache() {
leftDrag.clear();
rightDrag.clear();
middleDrag.clear();
this.leftDrag.clear();
this.rightDrag.clear();
this.middleDrag.clear();
}
/**
@ -87,23 +85,28 @@ public final class Click {
* @param isCreative whether the player is in creative mode (used for ignoring some actions)
* @return the information about the click, or nothing if there was no immediately usable information
*/
public @Nullable Click.Info process(@NotNull ClientClickWindowPacket packet, @NotNull Inventory inventory, boolean isCreative) {
final int originalSlot = packet.slot();
public @Nullable Click.Info processPlayerClick(@NotNull ClientClickWindowPacket packet, boolean isCreative) {
if (requireCreative(packet) && !isCreative) return null;
final int slot = packet.slot() != -999 ? PlayerInventoryUtils.protocolToMinestom(packet.slot()) : -999;
final int maxSize = PlayerInventoryUtils.INNER_SIZE;
return process(packet.clickType(), slot, packet.button(), slot >= 0 && slot < maxSize);
}
public @Nullable Click.Info processContainerClick(@NotNull ClientClickWindowPacket packet, int inventorySize, boolean isCreative) {
if (requireCreative(packet) && !isCreative) return null;
final int slot = packet.slot();
final int maxSize = inventorySize + PlayerInventoryUtils.INNER_SIZE;
return process(packet.clickType(), slot, packet.button(), slot >= 0 && slot < maxSize);
}
private boolean requireCreative(ClientClickWindowPacket packet) {
final byte button = packet.button();
final ClientClickWindowPacket.ClickType type = packet.clickType();
int slot = inventory instanceof PlayerInventory ? PlayerInventoryUtils.protocolToMinestom(originalSlot) : originalSlot;
if (originalSlot == -999) slot = -999;
final boolean creativeRequired = switch (type) {
return switch (type) {
case CLONE -> true;
case QUICK_CRAFT -> button == 8 || button == 9 || button == 10;
default -> false;
};
if (creativeRequired && !isCreative) return null;
final int maxSize = inventory.getSize() + (inventory instanceof PlayerInventory ? 0 : PlayerInventoryUtils.INNER_SIZE);
return process(type, slot, button, slot >= 0 && slot < maxSize);
}
/**
@ -115,7 +118,7 @@ public final class Click {
* @param valid whether {@code slot} fits within the inventory (may be unused, depending on click)
* @return the information about the click, or nothing if there was no immediately usable information
*/
public @Nullable Click.Info process(@NotNull ClientClickWindowPacket.ClickType type,
private @Nullable Click.Info process(@NotNull ClientClickWindowPacket.ClickType type,
int slot, byte button, boolean valid) {
return switch (type) {
case PICKUP -> {
@ -190,7 +193,6 @@ public final class Click {
case PICKUP_ALL -> valid ? new Info.Double(slot) : null;
};
}
}
public record Getter(@NotNull IntFunction<ItemStack> main, @NotNull IntFunction<ItemStack> player,

View File

@ -5,6 +5,7 @@ import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.inventory.InventoryButtonClickEvent;
import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.click.Click;
import net.minestom.server.network.packet.client.common.ClientPongPacket;
import net.minestom.server.network.packet.client.play.ClientClickWindowButtonPacket;
import net.minestom.server.network.packet.client.play.ClientClickWindowPacket;
@ -15,15 +16,17 @@ public class WindowListener {
public static void clickWindowListener(ClientClickWindowPacket packet, Player player) {
final int windowId = packet.windowId();
final Inventory inventory = windowId == 0 ? player.getInventory() : player.getOpenInventory();
final boolean playerInventory = windowId == 0;
final Inventory inventory = playerInventory ? player.getInventory() : player.getOpenInventory();
// Prevent some invalid packets
if (inventory == null || packet.slot() == -1) return;
var info = player.clickPreprocessor().process(packet, inventory, player.isCreative());
if (info != null) {
inventory.handleClick(player, info);
}
Click.Preprocessor preprocessor = player.clickPreprocessor();
final Click.Info info = playerInventory ?
preprocessor.processPlayerClick(packet, player.isCreative()) :
preprocessor.processContainerClick(packet, inventory.getSize(), player.isCreative());
if (info != null) inventory.handleClick(player, info);
// (Why is the ping packet necessary?)
player.sendPacket(new PingPacket((1 << 30) | (windowId << 16)));
@ -55,5 +58,4 @@ public class WindowListener {
InventoryButtonClickEvent event = new InventoryButtonClickEvent(openInventory, player, packet.buttonId());
EventDispatcher.call(event);
}
}

View File

@ -18,8 +18,7 @@ import java.util.function.UnaryOperator;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ClickUtils {
public final class ClickUtils {
public static final @NotNull InventoryType TYPE = InventoryType.HOPPER;
public static final int SIZE = TYPE.getSize(); // Default hopper size
@ -35,7 +34,8 @@ public class ClickUtils {
public static @NotNull Player createPlayer() {
return new Player(UUID.randomUUID(), "TestPlayer", new PlayerConnection() {
@Override
public void sendPacket(@NotNull SendablePacket packet) {}
public void sendPacket(@NotNull SendablePacket packet) {
}
@Override
public @NotNull SocketAddress getRemoteAddress() {
@ -43,7 +43,8 @@ public class ClickUtils {
}
@Override
public void disconnect() {}
public void disconnect() {
}
});
}
@ -66,7 +67,7 @@ public class ClickUtils {
}
public static void assertProcessed(@NotNull Click.Preprocessor preprocessor, @NotNull Player player, @Nullable Click.Info info, @NotNull ClientClickWindowPacket packet) {
assertEquals(info, preprocessor.process(packet, createInventory(), player.isCreative()));
assertEquals(info, preprocessor.processContainerClick(packet, createInventory().getSize(), player.isCreative()));
}
public static void assertProcessed(@NotNull Player player, @Nullable Click.Info info, @NotNull ClientClickWindowPacket packet) {
@ -80,5 +81,4 @@ public class ClickUtils {
public static @NotNull ClientClickWindowPacket clickPacket(@NotNull ClientClickWindowPacket.ClickType type, int windowId, int button, int slot) {
return new ClientClickWindowPacket((byte) windowId, 0, (short) slot, (byte) button, type, List.of(), ItemStack.AIR);
}
}