diff --git a/src/main/java/net/minestom/server/advancements/AdvancementAction.java b/src/main/java/net/minestom/server/advancements/AdvancementAction.java new file mode 100644 index 000000000..06beb4a28 --- /dev/null +++ b/src/main/java/net/minestom/server/advancements/AdvancementAction.java @@ -0,0 +1,7 @@ +package net.minestom.server.advancements; + +public enum AdvancementAction { + + OPENED_TAB, CLOSED_SCREEN + +} diff --git a/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java b/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java new file mode 100644 index 000000000..324843723 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/AdvancementTabEvent.java @@ -0,0 +1,51 @@ +package net.minestom.server.event.player; + +import net.minestom.server.advancements.AdvancementAction; +import net.minestom.server.entity.Player; +import net.minestom.server.event.Event; + +/** + * Called when the players open the advancement screens or switch the tab + * and when he closes the screen + */ +public class AdvancementTabEvent extends Event { + + private final Player player; + private final AdvancementAction action; + private final String tabId; + + public AdvancementTabEvent(Player player, AdvancementAction action, String tabId) { + this.player = player; + this.action = action; + this.tabId = tabId; + } + + /** + * Get the player responsive for the event + * + * @return the player + */ + public Player getPlayer() { + return player; + } + + /** + * Get the action + * + * @return the action + */ + public AdvancementAction getAction() { + return action; + } + + /** + * Get the tab id + *
+ * Not null ony if {@link #getAction()} is equal to {@link AdvancementAction#OPENED_TAB} + * + * @return the tab id + */ + public String getTabId() { + return tabId; + } +} diff --git a/src/main/java/net/minestom/server/listener/AdvancementTabListener.java b/src/main/java/net/minestom/server/listener/AdvancementTabListener.java index fddfd31ec..1802f6732 100644 --- a/src/main/java/net/minestom/server/listener/AdvancementTabListener.java +++ b/src/main/java/net/minestom/server/listener/AdvancementTabListener.java @@ -1,12 +1,17 @@ package net.minestom.server.listener; +import net.minestom.server.advancements.AdvancementAction; import net.minestom.server.entity.Player; +import net.minestom.server.event.player.AdvancementTabEvent; import net.minestom.server.network.packet.client.play.ClientAdvancementTabPacket; public class AdvancementTabListener { public static void listener(ClientAdvancementTabPacket packet, Player player) { - // Currentely unused and don't see much usage for an API - // TODO: Create an Event? + final AdvancementAction action = packet.action; + final String tabId = packet.tabIdentifier; + AdvancementTabEvent advancementTabEvent = new AdvancementTabEvent(player, action, tabId); + + player.callEvent(AdvancementTabEvent.class, advancementTabEvent); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java index c40f62c44..02c2123a0 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java @@ -1,25 +1,21 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.advancements.AdvancementAction; import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.client.ClientPlayPacket; public class ClientAdvancementTabPacket extends ClientPlayPacket { - public Action action; + public AdvancementAction action; public String tabIdentifier; @Override public void read(PacketReader reader) { - this.action = Action.values()[reader.readVarInt()]; + this.action = AdvancementAction.values()[reader.readVarInt()]; - if (action == Action.OPENED_TAB) { + if (action == AdvancementAction.OPENED_TAB) { this.tabIdentifier = reader.readSizedString(); } } - public enum Action { - OPENED_TAB, - CLOSED_SCREEN - } - }