Add EditBookEvent (#1120)

This commit is contained in:
Michael 2022-05-24 00:08:00 -04:00 committed by GitHub
parent f5dc048185
commit 7b8445d0e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 0 deletions

View File

@ -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;
}
}

View 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()));
}
}

View File

@ -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);
}
/**

View File

@ -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;
}
}