mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-25 02:27:38 +01:00
We can now have multiple listeners
This commit is contained in:
parent
38abe55161
commit
48c4941813
@ -87,14 +87,14 @@ public class PlayerInit {
|
||||
responseData.setFavicon("data:image/png;base64,<data>");
|
||||
});
|
||||
|
||||
connectionManager.setPacketConsumer((player, packet) -> {
|
||||
connectionManager.addPacketConsumer((player, packet) -> {
|
||||
// Listen to all received packet
|
||||
// Returning true means cancelling the packet
|
||||
return false;
|
||||
});
|
||||
|
||||
connectionManager.setPlayerInitialization(player -> {
|
||||
player.setEventCallback(AttackEvent.class, event -> {
|
||||
connectionManager.addPlayerInitialization(player -> {
|
||||
player.addEventCallback(AttackEvent.class, event -> {
|
||||
Entity entity = event.getTarget();
|
||||
if (entity instanceof EntityCreature) {
|
||||
EntityCreature creature = (EntityCreature) entity;
|
||||
@ -113,7 +113,7 @@ public class PlayerInit {
|
||||
}
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerBlockPlaceEvent.class, event -> {
|
||||
player.addEventCallback(PlayerBlockPlaceEvent.class, event -> {
|
||||
if (event.getHand() != Player.Hand.MAIN)
|
||||
return;
|
||||
|
||||
@ -127,11 +127,11 @@ public class PlayerInit {
|
||||
|
||||
});
|
||||
|
||||
player.setEventCallback(PickupItemEvent.class, event -> {
|
||||
player.addEventCallback(PickupItemEvent.class, event -> {
|
||||
event.setCancelled(!player.getInventory().addItemStack(event.getItemStack())); // Cancel event if player does not have enough inventory space
|
||||
});
|
||||
|
||||
player.setEventCallback(ItemDropEvent.class, event -> {
|
||||
player.addEventCallback(ItemDropEvent.class, event -> {
|
||||
ItemStack droppedItem = event.getItemStack();
|
||||
|
||||
ItemEntity itemEntity = new ItemEntity(droppedItem);
|
||||
@ -142,15 +142,15 @@ public class PlayerInit {
|
||||
itemEntity.setVelocity(velocity, 500);
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerDisconnectEvent.class, event -> {
|
||||
player.addEventCallback(PlayerDisconnectEvent.class, event -> {
|
||||
System.out.println("DISCONNECTION " + player.getUsername());
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerLoginEvent.class, event -> {
|
||||
player.addEventCallback(PlayerLoginEvent.class, event -> {
|
||||
event.setSpawningInstance(instanceContainer);
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerSpawnEvent.class, event -> {
|
||||
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.teleport(new Position(0, 75, 0));
|
||||
|
||||
|
@ -6,21 +6,20 @@ import net.minestom.server.collision.BoundingBox;
|
||||
import net.minestom.server.collision.CollisionUtils;
|
||||
import net.minestom.server.data.Data;
|
||||
import net.minestom.server.data.DataContainer;
|
||||
import net.minestom.server.event.Callback;
|
||||
import net.minestom.server.event.CancellableEvent;
|
||||
import net.minestom.server.event.Event;
|
||||
import net.minestom.server.event.EventCallback;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.network.packet.PacketWriter;
|
||||
import net.minestom.server.network.packet.server.play.*;
|
||||
import net.minestom.server.network.player.PlayerConnection;
|
||||
import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
@ -65,7 +64,7 @@ public abstract class Entity implements Viewable, DataContainer {
|
||||
private long scheduledRemoveTime;
|
||||
private int entityType;
|
||||
private long lastUpdate;
|
||||
private Map<Class<? extends Event>, Callback> eventCallbacks = new ConcurrentHashMap<>();
|
||||
private Map<Class<? extends Event>, List<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
|
||||
protected long velocityTime; // Reset velocity to 0 after countdown
|
||||
|
||||
// Synchronization
|
||||
@ -263,7 +262,7 @@ public abstract class Entity implements Viewable, DataContainer {
|
||||
float newY = position.getY() - strength;
|
||||
//System.out.println("REFRESH Y " + newY);
|
||||
// allow entities to go below the world
|
||||
if(foundBlock) {
|
||||
if (foundBlock) {
|
||||
newY = Math.max(newY, firstBlock);
|
||||
}
|
||||
refreshPosition(position.getX(), newY, position.getZ());
|
||||
@ -303,23 +302,26 @@ public abstract class Entity implements Viewable, DataContainer {
|
||||
*/
|
||||
protected void handleVoid() {
|
||||
// Kill if in void
|
||||
if(getInstance().isInVoid(this.position)) {
|
||||
if (getInstance().isInVoid(this.position)) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
public <E extends Event> void setEventCallback(Class<E> eventClass, Callback<E> callback) {
|
||||
this.eventCallbacks.put(eventClass, callback);
|
||||
public <E extends Event> void addEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
|
||||
List<EventCallback> callbacks = getEventCallbacks(eventClass);
|
||||
callbacks.add(eventCallback);
|
||||
this.eventCallbacks.put(eventClass, callbacks);
|
||||
}
|
||||
|
||||
public <E extends Event> Callback<E> getEventCallback(Class<E> eventClass) {
|
||||
return this.eventCallbacks.get(eventClass);
|
||||
public <E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass) {
|
||||
return eventCallbacks.getOrDefault(eventClass, new CopyOnWriteArrayList<>());
|
||||
}
|
||||
|
||||
public <E extends Event> void callEvent(Class<E> eventClass, E event) {
|
||||
Callback<E> callback = getEventCallback(eventClass);
|
||||
if (callback != null)
|
||||
getEventCallback(eventClass).run(event);
|
||||
List<EventCallback> eventCallbacks = getEventCallbacks(eventClass);
|
||||
for (EventCallback<E> eventCallback : eventCallbacks) {
|
||||
eventCallback.run(event);
|
||||
}
|
||||
}
|
||||
|
||||
public <E extends CancellableEvent> void callCancellableEvent(Class<E> eventClass, E event, Runnable runnable) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
public interface Callback<E extends Event> {
|
||||
public interface EventCallback<E extends Event> {
|
||||
|
||||
void run(E event);
|
||||
|
@ -43,12 +43,13 @@ public class PacketListenerManager {
|
||||
}
|
||||
|
||||
public <T extends ClientPlayPacket> void process(T packet, Player player) {
|
||||
PacketConsumer packetConsumer = connectionManager.getPacketConsumer();
|
||||
if (packetConsumer != null) {
|
||||
boolean cancel = packetConsumer.accept(player, packet);
|
||||
if (cancel)
|
||||
return; // Listening cancelled
|
||||
|
||||
boolean cancel = false;
|
||||
for (PacketConsumer packetConsumer : connectionManager.getPacketConsumers()) {
|
||||
cancel = packetConsumer.accept(player, packet);
|
||||
}
|
||||
if (cancel)
|
||||
return;
|
||||
|
||||
BiConsumer<T, Player> biConsumer = (BiConsumer<T, Player>) listeners.get(packet.getClass());
|
||||
if (biConsumer == null) {
|
||||
|
@ -6,6 +6,7 @@ import net.minestom.server.network.player.PlayerConnection;
|
||||
import net.minestom.server.ping.ResponseDataConsumer;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
@ -16,8 +17,8 @@ public class ConnectionManager {
|
||||
private Map<PlayerConnection, Player> connectionPlayerMap = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
private ResponseDataConsumer responseDataConsumer;
|
||||
private PacketConsumer packetConsumer;
|
||||
private Consumer<Player> playerInitialization;
|
||||
private List<PacketConsumer> packetConsumers = new CopyOnWriteArrayList<>();
|
||||
private List<Consumer<Player>> playerInitializations = new CopyOnWriteArrayList<>();
|
||||
|
||||
public Player getPlayer(PlayerConnection connection) {
|
||||
return connectionPlayerMap.get(connection);
|
||||
@ -59,20 +60,20 @@ public class ConnectionManager {
|
||||
this.responseDataConsumer = responseDataConsumer;
|
||||
}
|
||||
|
||||
public PacketConsumer getPacketConsumer() {
|
||||
return packetConsumer;
|
||||
public List<PacketConsumer> getPacketConsumers() {
|
||||
return Collections.unmodifiableList(packetConsumers);
|
||||
}
|
||||
|
||||
public void setPacketConsumer(PacketConsumer packetConsumer) {
|
||||
this.packetConsumer = packetConsumer;
|
||||
public void addPacketConsumer(PacketConsumer packetConsumer) {
|
||||
this.packetConsumers.add(packetConsumer);
|
||||
}
|
||||
|
||||
public Consumer<Player> getPlayerInitialization() {
|
||||
return playerInitialization;
|
||||
public List<Consumer<Player>> getPlayerInitializations() {
|
||||
return Collections.unmodifiableList(playerInitializations);
|
||||
}
|
||||
|
||||
public void setPlayerInitialization(Consumer<Player> playerInitialization) {
|
||||
this.playerInitialization = playerInitialization;
|
||||
public void addPlayerInitialization(Consumer<Player> playerInitialization) {
|
||||
this.playerInitializations.add(playerInitialization);
|
||||
}
|
||||
|
||||
// Is only used at LoginStartPacket#process
|
||||
|
@ -92,9 +92,9 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
||||
playerInfoPacket.playerInfos.add(addPlayer);
|
||||
connection.sendPacket(playerInfoPacket);
|
||||
|
||||
Consumer<Player> playerInitialization = MinecraftServer.getConnectionManager().getPlayerInitialization();
|
||||
if (playerInitialization != null)
|
||||
for (Consumer<Player> playerInitialization : connectionManager.getPlayerInitializations()) {
|
||||
playerInitialization.accept(player);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user