Skip to content

Commit 3909eae

Browse files
committed
Address reviews
1 parent de27412 commit 3909eae

6 files changed

Lines changed: 56 additions & 47 deletions

File tree

api/src/main/java/org/geysermc/geyser/api/entity/type/GeyserEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public interface GeyserEntity {
104104
* If the new value is null, the property is reset to the default value.
105105
*
106106
* @param dataType an entity data type, such as from {@link GeyserEntityDataTypes}
107-
* @param value the new property value
107+
* @param value the new property value or null
108108
* @param <T> the type of the value
109109
*/
110110
<T> void update(GeyserEntityDataType<T> dataType, @Nullable T value);
@@ -114,7 +114,7 @@ public interface GeyserEntity {
114114
* If the new value is null, the property is reset to the default value.
115115
*
116116
* @param property a {@link GeyserEntityProperty} registered for this type in the {@link GeyserDefineEntityPropertiesEvent}
117-
* @param value the new property value
117+
* @param value the new property value or null
118118
* @param <T> the type of the value
119119
* @since 2.9.0
120120
*/

core/src/main/java/org/geysermc/geyser/entity/VanillaEntities.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@
164164
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.BooleanEntityMetadata;
165165
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.FloatEntityMetadata;
166166
import org.geysermc.mcprotocollib.protocol.data.game.entity.type.BuiltinEntityType;
167-
import org.geysermc.mcprotocollib.protocol.data.game.entity.type.EntityType;
168167

169168
public final class VanillaEntities {
170169
public static final VanillaEntityType<BoatEntity> ACACIA_BOAT;

core/src/main/java/org/geysermc/geyser/entity/spawn/EntitySpawnContext.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,17 @@ public boolean callServerSpawnEvent() {
118118
return true;
119119
}
120120

121-
GeyserImpl.getInstance().getEventBus().fire(new ServerSpawnEntityEvent(session) {
121+
ServerSpawnEntityEvent event = new ServerSpawnEntityEvent(session) {
122+
private boolean cancelled = false;
122123

123124
@Override
124125
public boolean isCancelled() {
125-
return bedrockEntityDefinition == null;
126+
return cancelled || bedrockEntityDefinition == null;
126127
}
127128

128129
@Override
129130
public void setCancelled(boolean cancelled) {
130-
bedrockEntityDefinition = null;
131+
this.cancelled = cancelled;
131132
}
132133

133134
@Override
@@ -154,6 +155,7 @@ public int entityId() {
154155
public void definition(@Nullable GeyserEntityDefinition entityDefinition) {
155156
if (entityDefinition == null) {
156157
bedrockEntityDefinition = null;
158+
cancelled = true;
157159
return;
158160
}
159161

@@ -175,9 +177,10 @@ public void preSpawnConsumer(Consumer<@NonNull GeyserEntity> consumer) {
175177
}
176178
consumers.add(consumer);
177179
}
178-
});
180+
};
179181

180-
return bedrockEntityDefinition != null;
182+
GeyserImpl.getInstance().eventBus().fire(event);
183+
return bedrockEntityDefinition != null && !event.isCancelled();
181184
}
182185

183186
/**
@@ -188,7 +191,9 @@ public boolean callParrotEvent(PlayerEntity player, int variant, boolean right)
188191
return true;
189192
}
190193

191-
GeyserImpl.getInstance().eventBus().fire(new ServerAttachParrotsEvent(session) {
194+
ServerAttachParrotsEvent event = new ServerAttachParrotsEvent(session) {
195+
private boolean cancelled = false;
196+
192197
@Override
193198
public GeyserPlayerEntity player() {
194199
return player;
@@ -229,12 +234,12 @@ public void definition(@Nullable GeyserEntityDefinition entityDefinition) {
229234

230235
@Override
231236
public boolean isCancelled() {
232-
return bedrockEntityDefinition == null;
237+
return cancelled || bedrockEntityDefinition == null;
233238
}
234239

235240
@Override
236241
public void setCancelled(boolean cancelled) {
237-
bedrockEntityDefinition = null;
242+
this.cancelled = cancelled;
238243
}
239244

240245
@Override
@@ -244,9 +249,10 @@ public void preSpawnConsumer(Consumer<@NonNull GeyserEntity> consumer) {
244249
}
245250
consumers.add(consumer);
246251
}
247-
});
252+
};
248253

249-
return bedrockEntityDefinition != null;
254+
GeyserImpl.getInstance().eventBus().fire(event);
255+
return bedrockEntityDefinition != null && !event.isCancelled();
250256
}
251257

252258
// Not assigned by default - preparation for cancellable entity spawning

core/src/main/java/org/geysermc/geyser/entity/type/Entity.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -876,41 +876,43 @@ public void updatePropertiesBatched(Consumer<BatchPropertyUpdater> consumer, boo
876876
if (this.propertyManager == null) {
877877
throw new IllegalArgumentException("Given entity has no registered properties!");
878878
}
879-
880879
Objects.requireNonNull(consumer);
881-
GeyserEntityProperties propertyDefinitions = bedrockDefinition.registeredProperties();
882-
consumer.accept(new BatchPropertyUpdater() {
883-
@Override
884-
public <T> void update(@NonNull GeyserEntityProperty<T> property, @Nullable T value) {
885-
Objects.requireNonNull(property, "property must not be null!");
886-
if (!(property instanceof PropertyType<T, ?> propertyType)) {
887-
throw new IllegalArgumentException("Invalid property implementation! Got: " + property.getClass().getSimpleName());
888-
}
889-
int index = propertyDefinitions.getPropertyIndex(property.identifier().toString());
890-
if (index < 0) {
891-
throw new IllegalArgumentException("No property with the name " + property.identifier() + " has been registered.");
892-
}
893880

894-
var expectedProperty = propertyDefinitions.getProperties().get(index);
895-
if (!expectedProperty.equals(propertyType)) {
896-
throw new IllegalArgumentException("The supplied property was not registered with this entity type!");
881+
session.ensureInEventLoop(() -> {
882+
GeyserEntityProperties propertyDefinitions = bedrockDefinition.registeredProperties();
883+
consumer.accept(new BatchPropertyUpdater() {
884+
@Override
885+
public <T> void update(@NonNull GeyserEntityProperty<T> property, @Nullable T value) {
886+
Objects.requireNonNull(property, "property must not be null!");
887+
if (!(property instanceof PropertyType<T, ?> propertyType)) {
888+
throw new IllegalArgumentException("Invalid property implementation! Got: " + property.getClass().getSimpleName());
889+
}
890+
int index = propertyDefinitions.getPropertyIndex(property.identifier().toString());
891+
if (index < 0) {
892+
throw new IllegalArgumentException("No property with the name " + property.identifier() + " has been registered.");
893+
}
894+
895+
var expectedProperty = propertyDefinitions.getProperties().get(index);
896+
if (!expectedProperty.equals(propertyType)) {
897+
throw new IllegalArgumentException("The supplied property was not registered with this entity type!");
898+
}
899+
900+
propertyType.apply(propertyManager, value);
901+
}
902+
});
903+
904+
if (propertyManager.hasProperties()) {
905+
if (immediate) {
906+
SetEntityDataPacket packet = new SetEntityDataPacket();
907+
packet.setRuntimeEntityId(geyserId());
908+
propertyManager.applyFloatProperties(packet.getProperties().getFloatProperties());
909+
propertyManager.applyIntProperties(packet.getProperties().getIntProperties());
910+
session.sendUpstreamPacketImmediately(packet);
911+
} else {
912+
session.getEntityCache().markDirty(this);
897913
}
898-
899-
propertyType.apply(propertyManager, value);
900914
}
901915
});
902-
903-
if (propertyManager.hasProperties()) {
904-
if (immediate) {
905-
SetEntityDataPacket packet = new SetEntityDataPacket();
906-
packet.setRuntimeEntityId(geyserId());
907-
propertyManager.applyFloatProperties(packet.getProperties().getFloatProperties());
908-
propertyManager.applyIntProperties(packet.getProperties().getIntProperties());
909-
session.sendUpstreamPacketImmediately(packet);
910-
} else {
911-
session.getEntityCache().markDirty(this);
912-
}
913-
}
914916
}
915917

916918
public void offset(float offset, boolean teleport) {
@@ -934,8 +936,10 @@ public void offset(float offset, boolean teleport) {
934936
@Override
935937
public <T> void update(@NonNull GeyserEntityDataType<T> dataType, @Nullable T value) {
936938
if (dataType instanceof GeyserEntityDataImpl<T> geyserEntityDataImpl) {
937-
geyserEntityDataImpl.update(this, value);
938-
session.getEntityCache().markDirty(this);
939+
session.ensureInEventLoop(() -> {
940+
geyserEntityDataImpl.update(this, value);
941+
session.getEntityCache().markDirty(this);
942+
});
939943
} else {
940944
throw new IllegalArgumentException("Invalid data type: " + dataType.getClass().getSimpleName());
941945
}

core/src/main/java/org/geysermc/geyser/entity/type/player/PlayerEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected void setParrot(OptionalInt variant, boolean isLeft) {
198198
// The parrot is a separate entity in Bedrock, but part of the player entity in Java
199199
EntitySpawnContext context = EntitySpawnContext.inherited(session, VanillaEntities.PARROT, this, position());
200200
if (!context.callParrotEvent(this, variant.getAsInt(), !isLeft)) {
201-
GeyserImpl.getInstance().getLogger().debug(session, "Cancelled parrot spawn event as definition is null!");
201+
GeyserImpl.getInstance().getLogger().debug(session, "Cancelled parrot spawn event!");
202202
return;
203203
}
204204
ParrotEntity parrot = new ParrotEntity(context);

core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaAddEntityTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void translate(GeyserSession session, ClientboundAddEntityPacket packet)
101101
}
102102

103103
if (!context.callServerSpawnEvent()) {
104-
GeyserImpl.getInstance().getLogger().debug(session, "Cancelled entity spawn (%s) at (%s)".formatted(type.identifier(), context.position()));
104+
GeyserImpl.getInstance().getLogger().debug(session, "Cancelled entity spawn (%s) at (%s)", type.identifier(), context.position());
105105
return;
106106
}
107107

0 commit comments

Comments
 (0)