mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-12-20 15:29:27 +00:00
Allow updating properties immediately (#5961)
This commit is contained in:
@@ -62,7 +62,23 @@ public interface GeyserEntity {
|
|||||||
/**
|
/**
|
||||||
* Updates multiple properties with just one update packet.
|
* Updates multiple properties with just one update packet.
|
||||||
* @see BatchPropertyUpdater
|
* @see BatchPropertyUpdater
|
||||||
|
*
|
||||||
|
* @param consumer a batch updater
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
*/
|
*/
|
||||||
void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer);
|
default void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer) {
|
||||||
|
this.updatePropertiesBatched(consumer, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates multiple properties with just one update packet, which can be sent immediately to the client.
|
||||||
|
* Usually, sending updates immediately is not required except for specific situations where packet batching
|
||||||
|
* would result in update order issues.
|
||||||
|
* @see BatchPropertyUpdater
|
||||||
|
*
|
||||||
|
* @param consumer a batch updater
|
||||||
|
* @param immediate whether this update should be sent immediately
|
||||||
|
* @since 2.9.1
|
||||||
|
*/
|
||||||
|
void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer, boolean immediate);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -778,40 +778,44 @@ public class Entity implements GeyserEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer) {
|
public void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer, boolean immediate) {
|
||||||
if (this.propertyManager != null) {
|
if (this.propertyManager == null) {
|
||||||
Objects.requireNonNull(consumer);
|
throw new IllegalArgumentException("Given entity has no registered properties!");
|
||||||
GeyserEntityProperties propertyDefinitions = definition.registeredProperties();
|
}
|
||||||
consumer.accept(new BatchPropertyUpdater() {
|
|
||||||
@Override
|
|
||||||
public <T> void update(@NonNull GeyserEntityProperty<T> property, @Nullable T value) {
|
|
||||||
Objects.requireNonNull(property, "property must not be null!");
|
|
||||||
if (!(property instanceof PropertyType<T, ? extends EntityProperty> propertyType)) {
|
|
||||||
throw new IllegalArgumentException("Invalid property implementation! Got: " + property.getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
int index = propertyDefinitions.getPropertyIndex(property.identifier().toString());
|
|
||||||
if (index < 0) {
|
|
||||||
throw new IllegalArgumentException("No property with the name " + property.identifier() + " has been registered.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedProperty = propertyDefinitions.getProperties().get(index);
|
Objects.requireNonNull(consumer);
|
||||||
if (!expectedProperty.equals(propertyType)) {
|
GeyserEntityProperties propertyDefinitions = definition.registeredProperties();
|
||||||
throw new IllegalArgumentException("The supplied property was not registered with this entity type!");
|
consumer.accept(new BatchPropertyUpdater() {
|
||||||
}
|
@Override
|
||||||
|
public <T> void update(@NonNull GeyserEntityProperty<T> property, @Nullable T value) {
|
||||||
propertyType.apply(propertyManager, value);
|
Objects.requireNonNull(property, "property must not be null!");
|
||||||
|
if (!(property instanceof PropertyType<T, ? extends EntityProperty> propertyType)) {
|
||||||
|
throw new IllegalArgumentException("Invalid property implementation! Got: " + property.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
int index = propertyDefinitions.getPropertyIndex(property.identifier().toString());
|
||||||
|
if (index < 0) {
|
||||||
|
throw new IllegalArgumentException("No property with the name " + property.identifier() + " has been registered.");
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if (propertyManager.hasProperties()) {
|
var expectedProperty = propertyDefinitions.getProperties().get(index);
|
||||||
SetEntityDataPacket packet = new SetEntityDataPacket();
|
if (!expectedProperty.equals(propertyType)) {
|
||||||
packet.setRuntimeEntityId(getGeyserId());
|
throw new IllegalArgumentException("The supplied property was not registered with this entity type!");
|
||||||
propertyManager.applyFloatProperties(packet.getProperties().getFloatProperties());
|
}
|
||||||
propertyManager.applyIntProperties(packet.getProperties().getIntProperties());
|
|
||||||
|
propertyType.apply(propertyManager, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (propertyManager.hasProperties()) {
|
||||||
|
SetEntityDataPacket packet = new SetEntityDataPacket();
|
||||||
|
packet.setRuntimeEntityId(getGeyserId());
|
||||||
|
propertyManager.applyFloatProperties(packet.getProperties().getFloatProperties());
|
||||||
|
propertyManager.applyIntProperties(packet.getProperties().getIntProperties());
|
||||||
|
if (immediate) {
|
||||||
|
session.sendUpstreamPacketImmediately(packet);
|
||||||
|
} else {
|
||||||
session.sendUpstreamPacket(packet);
|
session.sendUpstreamPacket(packet);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Given entity has no registered properties!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user