1
0
mirror of https://github.com/GeyserMC/Geyser.git synced 2025-12-31 04:36:33 +00:00

Fixes for Custom entity properties API (#5949)

* Fixes

* Further fixes

* let's not talk about this one

---------

Co-authored-by: onebeastchris <github@onechris.mozmail.com>
This commit is contained in:
dima_dencep
2025-11-01 19:40:11 +07:00
committed by GitHub
parent 36a5980e54
commit a5db5104b7
7 changed files with 22 additions and 15 deletions

View File

@@ -161,7 +161,7 @@ public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, Entit
if (identifier == null && type != null) {
identifier = "minecraft:" + type.name().toLowerCase(Locale.ROOT);
}
GeyserEntityProperties registeredProperties = propertiesBuilder == null ? null : propertiesBuilder.build();
GeyserEntityProperties registeredProperties = propertiesBuilder == null ? new GeyserEntityProperties() : propertiesBuilder.build();
EntityDefinition<T> definition = new EntityDefinition<>(factory, type, identifier, width, height, offset, registeredProperties, translators);
if (register && definition.entityType() != null) {
Registries.ENTITY_DEFINITIONS.get().putIfAbsent(definition.entityType(), definition);

View File

@@ -1339,7 +1339,7 @@ public final class EntityDefinitions {
});
for (var definition : Registries.ENTITY_DEFINITIONS.get().values()) {
if (definition.registeredProperties() != null) {
if (!definition.registeredProperties().isEmpty()) {
Registries.BEDROCK_ENTITY_PROPERTIES.get().add(definition.registeredProperties().toNbtMap(definition.identifier()));
}
}

View File

@@ -50,13 +50,8 @@ public class GeyserEntityProperties {
private final static Pattern ENTITY_PROPERTY_PATTERN = Pattern.compile("^[a-z0-9_.:-]*:[a-z0-9_.:-]*$");
private final ObjectArrayList<PropertyType<?, ?>> properties;
private final Object2IntMap<String> propertyIndices;
private GeyserEntityProperties() {
this.properties = new ObjectArrayList<>();
this.propertyIndices = new Object2IntOpenHashMap<>();
}
private ObjectArrayList<PropertyType<?, ?>> properties;
private Object2IntMap<String> propertyIndices;
public NbtMap toNbtMap(String entityType) {
NbtMapBuilder mapBuilder = NbtMap.builder();
@@ -75,6 +70,11 @@ public class GeyserEntityProperties {
throw new IllegalStateException("Cannot add properties outside the GeyserDefineEntityProperties event!");
}
if (properties == null || propertyIndices == null) {
this.properties = new ObjectArrayList<>(0);
this.propertyIndices = new Object2IntOpenHashMap<>(0);
}
if (this.properties.size() > 32) {
throw new IllegalArgumentException("Cannot register more than 32 properties for entity type " + entityType);
}
@@ -97,6 +97,10 @@ public class GeyserEntityProperties {
return properties;
}
public boolean isEmpty() {
return properties == null || properties.isEmpty();
}
public int getPropertyIndex(String name) {
return propertyIndices.getOrDefault(name, -1);
}

View File

@@ -25,6 +25,7 @@
package org.geysermc.geyser.entity.properties.type;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.entity.property.type.GeyserEnumEntityProperty;
import org.geysermc.geyser.api.util.Identifier;
@@ -35,7 +36,7 @@ import java.util.Locale;
public record EnumProperty<E extends Enum<E>>(
Identifier identifier,
Class<E> enumClass,
E defaultValue
@NonNull E defaultValue
) implements AbstractEnumProperty<E>, GeyserEnumEntityProperty<E> {
public EnumProperty {

View File

@@ -25,6 +25,7 @@
package org.geysermc.geyser.entity.properties.type;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.entity.FloatEntityProperty;
import org.geysermc.geyser.api.entity.property.type.GeyserFloatEntityProperty;
@@ -34,7 +35,7 @@ public record FloatProperty(
Identifier identifier,
float max,
float min,
Float defaultValue
@Nullable Float defaultValue
) implements PropertyType<Float, FloatEntityProperty>, GeyserFloatEntityProperty {
public FloatProperty {
@@ -42,7 +43,7 @@ public record FloatProperty(
throw new IllegalArgumentException("Cannot create float entity property (%s) with a minimum value (%s) greater than maximum (%s)!"
.formatted(identifier, min, max));
}
if (defaultValue < min || defaultValue > max) {
if (defaultValue != null && (defaultValue < min || defaultValue > max)) {
throw new IllegalArgumentException("Cannot create float entity property (%s) with a default value (%s) outside of the range (%s - %s)!"
.formatted(identifier, defaultValue, min, max));
}

View File

@@ -25,6 +25,7 @@
package org.geysermc.geyser.entity.properties.type;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.entity.IntEntityProperty;
import org.geysermc.geyser.GeyserImpl;
@@ -35,7 +36,7 @@ public record IntProperty(
Identifier identifier,
int max,
int min,
Integer defaultValue
@Nullable Integer defaultValue
) implements PropertyType<Integer, IntEntityProperty>, GeyserIntEntityProperty {
public IntProperty {
@@ -43,7 +44,7 @@ public record IntProperty(
throw new IllegalArgumentException("Cannot create int entity property (%s) with a minimum value (%s) greater than maximum (%s)!"
.formatted(identifier, min, max));
}
if (defaultValue < min || defaultValue > max) {
if (defaultValue != null && (defaultValue < min || defaultValue > max)) {
throw new IllegalArgumentException("Cannot create int entity property (%s) with a default value (%s) outside of the range (%s - %s)!"
.formatted(identifier, defaultValue, min, max));
}

View File

@@ -167,7 +167,7 @@ public class Entity implements GeyserEntity {
this.valid = false;
this.propertyManager = definition.registeredProperties() == null ? null : new GeyserEntityPropertyManager(definition.registeredProperties());
this.propertyManager = definition.registeredProperties().isEmpty() ? null : new GeyserEntityPropertyManager(definition.registeredProperties());
setPosition(position);
setAirSupply(getMaxAir());