mirror of
https://github.com/Minestom/Minestom.git
synced 2024-06-26 06:24:54 +02:00
Calling an entity event will now also trigger it for the instance (useful for instance-specific listeners)
This commit is contained in:
parent
3f1b2fb84d
commit
8c623072f3
|
@ -16,14 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
*/
|
*/
|
||||||
public class SerializableDataImpl extends DataImpl implements SerializableData {
|
public class SerializableDataImpl extends DataImpl implements SerializableData {
|
||||||
|
|
||||||
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class name -> Class
|
* Class name -> Class
|
||||||
* Used to cache data so we don't load class by name each time
|
* Used to cache data so we don't load class by name each time
|
||||||
*/
|
*/
|
||||||
private static ConcurrentHashMap<String, Class> nameToClassMap = new ConcurrentHashMap<>();
|
private static ConcurrentHashMap<String, Class> nameToClassMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data key -> Class
|
||||||
|
* Used to know the type of an element of this data object (for serialization purpose)
|
||||||
|
*/
|
||||||
|
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a value to a specific key
|
* Set a value to a specific key
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -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
|
* 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 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
|
* @param reader the reader
|
||||||
*/
|
*/
|
||||||
private static void readIndexedData(SerializableData data, Object2ShortMap<String> typeToIndexMap, BinaryReader reader) {
|
private static void readIndexedData(SerializableData data, Object2ShortMap<String> typeToIndexMap, BinaryReader reader) {
|
||||||
|
@ -167,8 +171,10 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
|
||||||
|
|
||||||
final Class type;
|
final Class type;
|
||||||
{
|
{
|
||||||
|
// Retrieve the class type
|
||||||
final String className = indexToTypeMap.get(typeIndex);
|
final String className = indexToTypeMap.get(typeIndex);
|
||||||
type = nameToClassMap.computeIfAbsent(className, s -> {
|
type = nameToClassMap.computeIfAbsent(className, s -> {
|
||||||
|
// First time that this type is retrieved
|
||||||
try {
|
try {
|
||||||
return Class.forName(className);
|
return Class.forName(className);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
|
|
@ -64,6 +64,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
||||||
protected float lastYaw, lastPitch;
|
protected float lastYaw, lastPitch;
|
||||||
protected float cacheYaw, cachePitch;
|
protected float cacheYaw, cachePitch;
|
||||||
|
protected boolean onGround;
|
||||||
|
|
||||||
private BoundingBox boundingBox;
|
private BoundingBox boundingBox;
|
||||||
|
|
||||||
|
@ -72,6 +73,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
// Velocity
|
// Velocity
|
||||||
protected Vector velocity = new Vector(); // Movement in block per second
|
protected Vector velocity = new Vector(); // Movement in block per second
|
||||||
protected long lastVelocityUpdateTime; // Reset velocity to 0 after countdown
|
protected long lastVelocityUpdateTime; // Reset velocity to 0 after countdown
|
||||||
|
private long velocityUpdatePeriod;
|
||||||
|
|
||||||
protected float gravityDragPerTick;
|
protected float gravityDragPerTick;
|
||||||
protected float eyeHeight;
|
protected float eyeHeight;
|
||||||
|
@ -91,10 +93,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
private long lastUpdate;
|
private long lastUpdate;
|
||||||
private final EntityType entityType;
|
private final EntityType entityType;
|
||||||
|
|
||||||
// Synchronization
|
// Network synchronization
|
||||||
private static final long SYNCHRONIZATION_DELAY = 1500; // In ms
|
private static final long SYNCHRONIZATION_DELAY = 1500; // In ms
|
||||||
private long lastSynchronizationTime;
|
private long lastSynchronizationTime;
|
||||||
|
|
||||||
|
// Events
|
||||||
private final Map<Class<? extends Event>, List<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
|
private final Map<Class<? extends Event>, List<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
// Metadata
|
// Metadata
|
||||||
|
@ -115,9 +118,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
|
|
||||||
protected final List<Consumer<Entity>> nextTick = Collections.synchronizedList(new ArrayList<>());
|
protected final List<Consumer<Entity>> nextTick = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
private long velocityUpdatePeriod;
|
|
||||||
protected boolean onGround;
|
|
||||||
|
|
||||||
// Tick related
|
// Tick related
|
||||||
private long ticks;
|
private long ticks;
|
||||||
private final EntityTickEvent tickEvent = new EntityTickEvent(this);
|
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);
|
return eventCallbacks.values().stream().flatMap(Collection::stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends Event> void callEvent(Class<E> 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
|
* Each entity has an unique id which will change after a restart
|
||||||
* All entities can be retrieved by calling {@link Entity#getEntity(int)}
|
* All entities can be retrieved by calling {@link Entity#getEntity(int)}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user