Add PlayerPacketEvent

This commit is contained in:
TheMode 2021-06-05 01:04:51 +02:00
parent 25f1183a1b
commit 3dfd4678d5
2 changed files with 54 additions and 4 deletions

View File

@ -0,0 +1,38 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.network.packet.client.ClientPacket;
import org.jetbrains.annotations.NotNull;
public class PlayerPacketEvent implements PlayerEvent, CancellableEvent {
private final Player player;
private final ClientPacket packet;
private boolean cancelled;
public PlayerPacketEvent(Player player, ClientPacket packet) {
this.player = player;
this.packet = packet;
}
@Override
public @NotNull Player getPlayer() {
return player;
}
public @NotNull ClientPacket getPacket() {
return packet;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,8 @@ package net.minestom.server.listener.manager;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.player.PlayerPacketEvent;
import net.minestom.server.listener.*;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.client.ClientPlayPacket;
@ -74,13 +76,23 @@ public final class PacketListenerManager {
LOGGER.warn("Packet " + clazz + " does not have any default listener! (The issue comes from Minestom)");
}
final PacketController packetController = new PacketController();
for (ClientPacketConsumer clientPacketConsumer : CONNECTION_MANAGER.getReceivePacketConsumers()) {
clientPacketConsumer.accept(player, packetController, packet);
// TODO remove legacy
{
final PacketController packetController = new PacketController();
for (ClientPacketConsumer clientPacketConsumer : CONNECTION_MANAGER.getReceivePacketConsumers()) {
clientPacketConsumer.accept(player, packetController, packet);
}
if (packetController.isCancel())
return;
}
if (packetController.isCancel())
// Event
PlayerPacketEvent playerPacketEvent = new PlayerPacketEvent(player, packet);
EventDispatcher.call(playerPacketEvent);
if (playerPacketEvent.isCancelled()) {
return;
}
// Finally execute the listener
if (packetListenerConsumer != null) {