diff --git a/src/main/java/net/minestom/server/event/book/EditBookEvent.java b/src/main/java/net/minestom/server/event/book/EditBookEvent.java new file mode 100644 index 000000000..50027cecb --- /dev/null +++ b/src/main/java/net/minestom/server/event/book/EditBookEvent.java @@ -0,0 +1,53 @@ +package net.minestom.server.event.book; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.EntityInstanceEvent; +import net.minestom.server.event.trait.ItemEvent; +import net.minestom.server.event.trait.PlayerEvent; +import net.minestom.server.item.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class EditBookEvent implements PlayerEvent, EntityInstanceEvent, ItemEvent { + + private final Player player; + private final ItemStack itemStack; + private final List pages; + private final String title; + + public EditBookEvent( + @NotNull Player player, + @NotNull ItemStack itemStack, + @NotNull List pages, + @Nullable String title + ) { + this.player = player; + this.itemStack = itemStack; + this.pages = pages; + this.title = title; + } + + @Override + public @NotNull Player getPlayer() { + return player; + } + + @Override + public @NotNull ItemStack getItemStack() { + return itemStack; + } + + public @NotNull List getPages() { + return pages; + } + + public @Nullable String getTitle() { + return title; + } + + public boolean isSigned() { + return title != null; + } +} diff --git a/src/main/java/net/minestom/server/listener/BookListener.java b/src/main/java/net/minestom/server/listener/BookListener.java new file mode 100644 index 000000000..936a39448 --- /dev/null +++ b/src/main/java/net/minestom/server/listener/BookListener.java @@ -0,0 +1,18 @@ +package net.minestom.server.listener; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.EventDispatcher; +import net.minestom.server.event.book.EditBookEvent; +import net.minestom.server.item.ItemStack; +import net.minestom.server.network.packet.client.play.ClientEditBookPacket; +import net.minestom.server.utils.inventory.PlayerInventoryUtils; + +public class BookListener { + + public static void listener(ClientEditBookPacket packet, Player player) { + int slot = PlayerInventoryUtils.convertClientInventorySlot(packet.slot()); + ItemStack itemStack = player.getInventory().getItemStack(slot); + EventDispatcher.call(new EditBookEvent(player, itemStack, packet.pages(), packet.title())); + } + +} diff --git a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java index 5aa09ae65..b8c52f069 100644 --- a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java +++ b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java @@ -55,6 +55,7 @@ public final class PacketListenerManager { setListener(ClientResourcePackStatusPacket.class, ResourcePackListener::listener); setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener); setListener(ClientSpectatePacket.class, SpectateListener::listener); + setListener(ClientEditBookPacket.class, BookListener::listener); } /** diff --git a/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java b/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java index 7feffaa3d..5c240b75e 100644 --- a/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java +++ b/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java @@ -76,4 +76,20 @@ public final class PlayerInventoryUtils { } return slot; } + + /** + * Used to convert the clients inventory slot to a Minestom slot. + * The client's inventory does not count the crafting slots. + * + * @param slot the client slot + * @return a slot which can be used internally with Minestom + */ + public static int convertClientInventorySlot(int slot) { + if (slot == 36) return BOOTS_SLOT; + if (slot == 37) return LEGGINGS_SLOT; + if (slot == 38) return CHESTPLATE_SLOT; + if (slot == 39) return HELMET_SLOT; + if (slot == 40) return OFFHAND_SLOT; + return slot; + } }