mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 12:37:42 +01:00
Add EditBookEvent (#1120)
This commit is contained in:
parent
f5dc048185
commit
7b8445d0e2
@ -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<String> pages;
|
||||
private final String title;
|
||||
|
||||
public EditBookEvent(
|
||||
@NotNull Player player,
|
||||
@NotNull ItemStack itemStack,
|
||||
@NotNull List<String> 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<String> getPages() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
public @Nullable String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public boolean isSigned() {
|
||||
return title != null;
|
||||
}
|
||||
}
|
18
src/main/java/net/minestom/server/listener/BookListener.java
Normal file
18
src/main/java/net/minestom/server/listener/BookListener.java
Normal file
@ -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()));
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user