Added AdvancementTabEvent

This commit is contained in:
Felix Cravic 2020-08-05 11:34:49 +02:00
parent 87fcb83837
commit 2ab7427b94
4 changed files with 69 additions and 10 deletions

View File

@ -0,0 +1,7 @@
package net.minestom.server.advancements;
public enum AdvancementAction {
OPENED_TAB, CLOSED_SCREEN
}

View File

@ -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
* <p>
* Not null ony if {@link #getAction()} is equal to {@link AdvancementAction#OPENED_TAB}
*
* @return the tab id
*/
public String getTabId() {
return tabId;
}
}

View File

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

View File

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