Calling an entity event will now also trigger it for the instance (useful for instance-specific listeners)

This commit is contained in:
themode 2020-09-23 22:01:47 +02:00
parent 3f1b2fb84d
commit 8c623072f3
2 changed files with 23 additions and 7 deletions

View File

@ -16,14 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class SerializableDataImpl extends DataImpl implements SerializableData {
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
/**
* Class name -> Class
* Used to cache data so we don't load class by name each time
*/
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
* <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
*
* @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-&gt;classIndex)
* @param reader the 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;
{
// 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) {

View File

@ -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<Class<? extends Event>, List<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
// Metadata
@ -115,9 +118,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
protected final List<Consumer<Entity>> 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 <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
* All entities can be retrieved by calling {@link Entity#getEntity(int)}