mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Add PlayerPacketEvent
This commit is contained in:
parent
25f1183a1b
commit
3dfd4678d5
@ -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;
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user