mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Changed the packet listener api, allowing for listener override
This commit is contained in:
parent
883e7eb80f
commit
b8f2afa4c3
@ -106,10 +106,9 @@ public class PlayerInit {
|
||||
}
|
||||
}, new UpdateOption(5, TimeUnit.TICK));
|
||||
|
||||
connectionManager.addPacketConsumer((player, packet) -> {
|
||||
connectionManager.addPacketConsumer((player, packetController, packet) -> {
|
||||
// Listen to all received packet
|
||||
// Returning true means cancelling the packet
|
||||
return false;
|
||||
packetController.setCancel(false);
|
||||
});
|
||||
|
||||
connectionManager.addPlayerInitialization(player -> {
|
||||
|
@ -12,7 +12,8 @@ public class AnimationListener {
|
||||
player.callCancellableEvent(AnimationEvent.class, animationEvent, () -> {
|
||||
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
|
||||
entityAnimationPacket.entityId = player.getEntityId();
|
||||
entityAnimationPacket.animation = animationEvent.getHand() == Player.Hand.MAIN ? EntityAnimationPacket.Animation.SWING_MAIN_ARM : EntityAnimationPacket.Animation.SWING_OFF_HAND;
|
||||
entityAnimationPacket.animation = animationEvent.getHand() == Player.Hand.MAIN ?
|
||||
EntityAnimationPacket.Animation.SWING_MAIN_ARM : EntityAnimationPacket.Animation.SWING_OFF_HAND;
|
||||
player.sendPacketToViewers(entityAnimationPacket);
|
||||
});
|
||||
}
|
||||
|
@ -16,5 +16,4 @@ public class KeepAliveListener {
|
||||
int latency = (int) (System.currentTimeMillis() - packet.id);
|
||||
player.refreshLatency(latency);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -115,8 +115,10 @@ public class PlayerDiggingListener {
|
||||
case SWAP_ITEM_HAND:
|
||||
PlayerSwapItemEvent swapItemEvent = new PlayerSwapItemEvent(offHand.clone(), mainHand.clone());
|
||||
player.callCancellableEvent(PlayerSwapItemEvent.class, swapItemEvent, () -> {
|
||||
playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem());
|
||||
playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem());
|
||||
synchronized (playerInventory) {
|
||||
playerInventory.setItemInMainHand(swapItemEvent.getMainHandItem());
|
||||
playerInventory.setItemInOffHand(swapItemEvent.getOffHandItem());
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
@ -14,6 +14,11 @@ public class UseEntityListener {
|
||||
if (entity == null)
|
||||
return;
|
||||
ClientInteractEntityPacket.Type type = packet.type;
|
||||
|
||||
// Player cannot interact entities that he cannot see
|
||||
if (!entity.isViewer(player))
|
||||
return;
|
||||
|
||||
if (type == ClientInteractEntityPacket.Type.ATTACK) {
|
||||
if (entity instanceof LivingEntity && ((LivingEntity) entity).isDead()) // Can't attack dead entities
|
||||
return;
|
||||
|
@ -21,6 +21,7 @@ public class UseItemListener {
|
||||
|
||||
Material material = Material.fromId(itemStack.getMaterialId());
|
||||
|
||||
// Equip armor with right click
|
||||
if (material.isArmor()) {
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
if (useItemEvent.isCancelled()) {
|
||||
|
@ -1,10 +1,10 @@
|
||||
package net.minestom.server.listener.manager;
|
||||
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.network.packet.client.ClientPacket;
|
||||
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PacketConsumer {
|
||||
// Cancel the packet if return true
|
||||
boolean accept(Player player, ClientPacket packet);
|
||||
void accept(Player player, PacketController packetController, ClientPlayPacket packet);
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
package net.minestom.server.listener.manager;
|
||||
|
||||
public class PacketController {
|
||||
|
||||
private boolean cancel;
|
||||
private PacketListenerConsumer packetListenerConsumer;
|
||||
|
||||
protected PacketController(PacketListenerConsumer packetListenerConsumer) {
|
||||
this.packetListenerConsumer = packetListenerConsumer;
|
||||
}
|
||||
|
||||
public boolean isCancel() {
|
||||
return cancel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cancel true if the packet should be cancelled
|
||||
*/
|
||||
public void setCancel(boolean cancel) {
|
||||
this.cancel = cancel;
|
||||
}
|
||||
|
||||
public PacketListenerConsumer getPacketListenerConsumer() {
|
||||
return packetListenerConsumer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param packetListenerConsumer the new listener (do not override the default listener)
|
||||
*/
|
||||
public void setPacketListenerConsumer(PacketListenerConsumer packetListenerConsumer) {
|
||||
this.packetListenerConsumer = packetListenerConsumer;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package net.minestom.server.listener.manager;
|
||||
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PacketListenerConsumer<T extends ClientPlayPacket> {
|
||||
void accept(T packet, Player player);
|
||||
}
|
@ -9,13 +9,12 @@ import net.minestom.server.network.packet.client.play.*;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class PacketListenerManager {
|
||||
|
||||
private static ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
|
||||
|
||||
private Map<Class<? extends ClientPlayPacket>, BiConsumer<? extends ClientPlayPacket, Player>> listeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<? extends ClientPlayPacket>, PacketListenerConsumer> listeners = new ConcurrentHashMap<>();
|
||||
|
||||
public PacketListenerManager() {
|
||||
addListener(ClientKeepAlivePacket.class, KeepAliveListener::listener);
|
||||
@ -46,22 +45,30 @@ public class PacketListenerManager {
|
||||
|
||||
public <T extends ClientPlayPacket> void process(T packet, Player player) {
|
||||
|
||||
boolean cancel = false;
|
||||
for (PacketConsumer packetConsumer : connectionManager.getPacketConsumers()) {
|
||||
cancel = packetConsumer.accept(player, packet);
|
||||
PacketListenerConsumer<T> packetListenerConsumer = listeners.get(packet.getClass());
|
||||
|
||||
// Listener can be null if none has been set before, call PacketConsumer anyway
|
||||
if (packetListenerConsumer == null) {
|
||||
System.err.println("Packet " + packet.getClass() + " does not have any listener!");
|
||||
}
|
||||
if (cancel)
|
||||
|
||||
|
||||
PacketController packetController = new PacketController(packetListenerConsumer);
|
||||
for (PacketConsumer packetConsumer : connectionManager.getPacketConsumers()) {
|
||||
packetConsumer.accept(player, packetController, packet);
|
||||
}
|
||||
|
||||
if (packetController.isCancel())
|
||||
return;
|
||||
|
||||
BiConsumer<T, Player> biConsumer = (BiConsumer<T, Player>) listeners.get(packet.getClass());
|
||||
if (biConsumer == null) {
|
||||
System.err.println("Packet " + packet.getClass() + " does not have any listener!");
|
||||
return;
|
||||
// Call the listener if not null
|
||||
// (can be null because no listener is set, or because it has been changed by the controller)
|
||||
if (packetListenerConsumer != null) {
|
||||
packetListenerConsumer.accept(packet, player);
|
||||
}
|
||||
biConsumer.accept(packet, player);
|
||||
}
|
||||
|
||||
public <T extends ClientPlayPacket> void addListener(Class<T> packetClass, BiConsumer<T, Player> consumer) {
|
||||
public <T extends ClientPlayPacket> void addListener(Class<T> packetClass, PacketListenerConsumer<T> consumer) {
|
||||
this.listeners.put(packetClass, consumer);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user