diff --git a/src/main/java/net/minestom/server/data/SerializableDataImpl.java b/src/main/java/net/minestom/server/data/SerializableDataImpl.java index 566646b16..34899b0df 100644 --- a/src/main/java/net/minestom/server/data/SerializableDataImpl.java +++ b/src/main/java/net/minestom/server/data/SerializableDataImpl.java @@ -16,14 +16,18 @@ import java.util.concurrent.ConcurrentHashMap; */ public class SerializableDataImpl extends DataImpl implements SerializableData { - private ConcurrentHashMap dataType = new ConcurrentHashMap<>(); - /** * Class name -> Class * Used to cache data so we don't load class by name each time */ private static ConcurrentHashMap nameToClassMap = new ConcurrentHashMap<>(); + /** + * Data key -> Class + * Used to know the type of an element of this data object (for serialization purpose) + */ + private ConcurrentHashMap dataType = new ConcurrentHashMap<>(); + /** * Set a value to a specific key *

@@ -142,7 +146,7 @@ public class SerializableDataImpl extends DataImpl implements SerializableData { * WARNING: the {@link DataManager} needs to have all the required types as the {@link SerializableData} has * * @param data the object to append the data - * @param typeToIndexMap the map which index all the type contained in the data (className->classIndex) + * @param typeToIndexMap the map which index all the type contained in the data (className->classIndex) * @param reader the reader */ private static void readIndexedData(SerializableData data, Object2ShortMap typeToIndexMap, BinaryReader reader) { @@ -167,8 +171,10 @@ public class SerializableDataImpl extends DataImpl implements SerializableData { final Class type; { + // Retrieve the class type final String className = indexToTypeMap.get(typeIndex); type = nameToClassMap.computeIfAbsent(className, s -> { + // First time that this type is retrieved try { return Class.forName(className); } catch (ClassNotFoundException e) { diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 50ce568e7..fb5718293 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -64,6 +64,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition protected float lastYaw, lastPitch; protected float cacheYaw, cachePitch; + protected boolean onGround; private BoundingBox boundingBox; @@ -72,6 +73,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { // Velocity protected Vector velocity = new Vector(); // Movement in block per second protected long lastVelocityUpdateTime; // Reset velocity to 0 after countdown + private long velocityUpdatePeriod; protected float gravityDragPerTick; protected float eyeHeight; @@ -91,10 +93,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { private long lastUpdate; private final EntityType entityType; - // Synchronization + // Network synchronization private static final long SYNCHRONIZATION_DELAY = 1500; // In ms private long lastSynchronizationTime; + // Events private final Map, List> eventCallbacks = new ConcurrentHashMap<>(); // Metadata @@ -115,9 +118,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { protected final List> nextTick = Collections.synchronizedList(new ArrayList<>()); - private long velocityUpdatePeriod; - protected boolean onGround; - // Tick related private long ticks; private final EntityTickEvent tickEvent = new EntityTickEvent(this); @@ -564,6 +564,16 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { return eventCallbacks.values().stream().flatMap(Collection::stream); } + @Override + public void callEvent(Class eventClass, E event) { + EventHandler.super.callEvent(eventClass, event); + + // Call the same event for the current entity instance + if (instance != null) { + instance.callEvent(eventClass, event); + } + } + /** * Each entity has an unique id which will change after a restart * All entities can be retrieved by calling {@link Entity#getEntity(int)}