mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Entity properties
Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
parent
652f6af784
commit
99e6a2981d
14 changed files with 587 additions and 7 deletions
|
@ -33,6 +33,7 @@ import lombok.Setter;
|
|||
import lombok.experimental.Accessors;
|
||||
import org.geysermc.geyser.GeyserImpl;
|
||||
import org.geysermc.geyser.entity.factory.EntityFactory;
|
||||
import org.geysermc.geyser.entity.properties.GeyserEntityProperties;
|
||||
import org.geysermc.geyser.entity.type.Entity;
|
||||
import org.geysermc.geyser.registry.Registries;
|
||||
import org.geysermc.geyser.translator.entity.EntityMetadataTranslator;
|
||||
|
@ -49,10 +50,10 @@ import java.util.function.BiConsumer;
|
|||
* @param <T> the entity type this definition represents
|
||||
*/
|
||||
public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, EntityType entityType, String identifier,
|
||||
float width, float height, float offset, List<EntityMetadataTranslator<? super T, ?, ?>> translators) {
|
||||
float width, float height, float offset, GeyserEntityProperties registeredProperties, List<EntityMetadataTranslator<? super T, ?, ?>> translators) {
|
||||
|
||||
public static <T extends Entity> Builder<T> inherited(EntityFactory<T> factory, EntityDefinition<? super T> parent) {
|
||||
return new Builder<>(factory, parent.entityType, parent.identifier, parent.width, parent.height, parent.offset, new ObjectArrayList<>(parent.translators));
|
||||
return new Builder<>(factory, parent.entityType, parent.identifier, parent.width, parent.height, parent.offset, parent.registeredProperties, new ObjectArrayList<>(parent.translators));
|
||||
}
|
||||
|
||||
public static <T extends Entity> Builder<T> builder(EntityFactory<T> factory) {
|
||||
|
@ -87,6 +88,7 @@ public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, Entit
|
|||
private float width;
|
||||
private float height;
|
||||
private float offset = 0.00001f;
|
||||
private GeyserEntityProperties registeredProperties;
|
||||
private final List<EntityMetadataTranslator<? super T, ?, ?>> translators;
|
||||
|
||||
private Builder(EntityFactory<T> factory) {
|
||||
|
@ -94,13 +96,14 @@ public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, Entit
|
|||
translators = new ObjectArrayList<>();
|
||||
}
|
||||
|
||||
public Builder(EntityFactory<T> factory, EntityType type, String identifier, float width, float height, float offset, List<EntityMetadataTranslator<? super T, ?, ?>> translators) {
|
||||
public Builder(EntityFactory<T> factory, EntityType type, String identifier, float width, float height, float offset, GeyserEntityProperties registeredProperties, List<EntityMetadataTranslator<? super T, ?, ?>> translators) {
|
||||
this.factory = factory;
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.offset = offset;
|
||||
this.registeredProperties = registeredProperties;
|
||||
this.translators = translators;
|
||||
}
|
||||
|
||||
|
@ -127,6 +130,11 @@ public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, Entit
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder<T> properties(GeyserEntityProperties registeredProperties) {
|
||||
this.registeredProperties = registeredProperties;
|
||||
return this;
|
||||
}
|
||||
|
||||
public <U, EM extends EntityMetadata<U, ? extends MetadataType<U>>> Builder<T> addTranslator(MetadataType<U> type, BiConsumer<T, EM> translateFunction) {
|
||||
translators.add(new EntityMetadataTranslator<>(type, translateFunction));
|
||||
return this;
|
||||
|
@ -149,10 +157,13 @@ public record EntityDefinition<T extends Entity>(EntityFactory<T> factory, Entit
|
|||
if (identifier == null && type != null) {
|
||||
identifier = "minecraft:" + type.name().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
EntityDefinition<T> definition = new EntityDefinition<>(factory, type, identifier, width, height, offset, translators);
|
||||
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);
|
||||
Registries.JAVA_ENTITY_IDENTIFIERS.get().putIfAbsent("minecraft:" + type.name().toLowerCase(Locale.ROOT), definition);
|
||||
if (definition.registeredProperties() != null) {
|
||||
Registries.BEDROCK_ENTITY_PROPERTIES.get().add(definition.registeredProperties().toNbtMap(identifier));
|
||||
}
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.FloatE
|
|||
import org.geysermc.mcprotocollib.protocol.data.game.entity.type.EntityType;
|
||||
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
|
||||
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
|
||||
import org.geysermc.geyser.entity.properties.GeyserEntityProperties;
|
||||
import org.geysermc.geyser.entity.type.*;
|
||||
import org.geysermc.geyser.entity.type.living.*;
|
||||
import org.geysermc.geyser.entity.type.living.animal.*;
|
||||
|
@ -774,7 +775,16 @@ public final class EntityDefinitions {
|
|||
ARMADILLO = EntityDefinition.inherited(ArmadilloEntity::new, ageableEntityBase)
|
||||
.type(EntityType.ARMADILLO)
|
||||
.height(0.65f).width(0.7f)
|
||||
.addTranslator(null)
|
||||
.properties(new GeyserEntityProperties.Builder()
|
||||
.addEnum(
|
||||
"minecraft:armadillo_state",
|
||||
"unrolled",
|
||||
"rolled_up",
|
||||
"rolled_up_peeking",
|
||||
"rolled_up_relaxing",
|
||||
"rolled_up_unrolling")
|
||||
.build())
|
||||
.addTranslator(MetadataType.ARMADILLO_STATE, ArmadilloEntity::setArmadilloState)
|
||||
.build();
|
||||
AXOLOTL = EntityDefinition.inherited(AxolotlEntity::new, ageableEntityBase)
|
||||
.type(EntityType.AXOLOTL)
|
||||
|
@ -786,6 +796,9 @@ public final class EntityDefinitions {
|
|||
BEE = EntityDefinition.inherited(BeeEntity::new, ageableEntityBase)
|
||||
.type(EntityType.BEE)
|
||||
.heightAndWidth(0.6f)
|
||||
.properties(new GeyserEntityProperties.Builder()
|
||||
.addBoolean("minecraft:has_nectar")
|
||||
.build())
|
||||
.addTranslator(MetadataType.BYTE, BeeEntity::setBeeFlags)
|
||||
.addTranslator(MetadataType.INT, BeeEntity::setAngerTime)
|
||||
.build();
|
||||
|
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
import org.cloudburstmc.nbt.NbtMapBuilder;
|
||||
import org.cloudburstmc.nbt.NbtType;
|
||||
import org.geysermc.geyser.entity.properties.type.BooleanProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.EnumProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.FloatProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.IntProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.PropertyType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
public class GeyserEntityProperties {
|
||||
private final ObjectArrayList<PropertyType> properties;
|
||||
private final Object2IntMap<String> propertyIndices;
|
||||
|
||||
private GeyserEntityProperties(ObjectArrayList<PropertyType> properties,
|
||||
Object2IntMap<String> propertyIndices) {
|
||||
this.properties = properties;
|
||||
this.propertyIndices = propertyIndices;
|
||||
}
|
||||
|
||||
public NbtMap toNbtMap(String entityType) {
|
||||
NbtMapBuilder mapBuilder = NbtMap.builder();
|
||||
List<NbtMap> nbtProperties = new ArrayList<>();
|
||||
|
||||
for (PropertyType property : properties) {
|
||||
nbtProperties.add(property.nbtMap());
|
||||
}
|
||||
mapBuilder.putList("properties", NbtType.COMPOUND, nbtProperties);
|
||||
|
||||
return mapBuilder.putString("type", entityType).build();
|
||||
}
|
||||
|
||||
public @NonNull List<PropertyType> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public int getPropertyIndex(String name) {
|
||||
return propertyIndices.getOrDefault(name, -1);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private final ObjectArrayList<PropertyType> properties = new ObjectArrayList<>();
|
||||
private final Object2IntMap<String> propertyIndices = new Object2IntOpenHashMap<>();
|
||||
|
||||
public Builder addInt(@NonNull String name, int min, int max) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new IntProperty(name, min, max);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addInt(@NonNull String name) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new IntProperty(name, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addFloat(@NonNull String name, float min, float max) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new FloatProperty(name, min, max);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addFloat(@NonNull String name) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new FloatProperty(name, Float.MIN_NORMAL, Float.MAX_VALUE);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addBoolean(@NonNull String name) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new BooleanProperty(name);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addEnum(@NonNull String name, List<String> values) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
PropertyType property = new EnumProperty(name, values);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addEnum(@NonNull String name, String... values) {
|
||||
if (propertyIndices.containsKey(name)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Property with name " + name + " already exists on builder!");
|
||||
}
|
||||
List<String> valuesList = Arrays.asList(values); // Convert array to list
|
||||
PropertyType property = new EnumProperty(name, valuesList);
|
||||
this.properties.add(property);
|
||||
propertyIndices.put(name, properties.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GeyserEntityProperties build() {
|
||||
return new GeyserEntityProperties(properties, propertyIndices);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import org.cloudburstmc.protocol.bedrock.data.entity.FloatEntityProperty;
|
||||
import org.cloudburstmc.protocol.bedrock.data.entity.IntEntityProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.EnumProperty;
|
||||
import org.geysermc.geyser.entity.properties.type.PropertyType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GeyserEntityPropertyManager {
|
||||
|
||||
private final GeyserEntityProperties properties;
|
||||
|
||||
private final ObjectArrayList<IntEntityProperty> intEntityProperties = new ObjectArrayList<>();
|
||||
private final ObjectArrayList<FloatEntityProperty> floatEntityProperties = new ObjectArrayList<>();
|
||||
|
||||
public GeyserEntityPropertyManager(GeyserEntityProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public void add(String propertyName, int value) {
|
||||
int index = properties.getPropertyIndex(propertyName);
|
||||
intEntityProperties.add(new IntEntityProperty(index, value));
|
||||
}
|
||||
|
||||
public void add(String propertyName, boolean value) {
|
||||
int index = properties.getPropertyIndex(propertyName);
|
||||
intEntityProperties.add(new IntEntityProperty(index, value ? 1 : 0));
|
||||
}
|
||||
|
||||
public void add(String propertyName, String value) {
|
||||
int index = properties.getPropertyIndex(propertyName);
|
||||
PropertyType property = properties.getProperties().get(index);
|
||||
int enumIndex = ((EnumProperty) property).getIndex(value);
|
||||
intEntityProperties.add(new IntEntityProperty(index, enumIndex));
|
||||
}
|
||||
|
||||
public void add(String propertyName, float value) {
|
||||
int index = properties.getPropertyIndex(propertyName);
|
||||
floatEntityProperties.add(new FloatEntityProperty(index, value));
|
||||
}
|
||||
|
||||
public boolean hasFloatProperties() {
|
||||
return !this.floatEntityProperties.isEmpty();
|
||||
}
|
||||
|
||||
public boolean hasIntProperties() {
|
||||
return !this.intEntityProperties.isEmpty();
|
||||
}
|
||||
|
||||
public boolean hasProperties() {
|
||||
return hasFloatProperties() || hasIntProperties();
|
||||
}
|
||||
|
||||
public ObjectArrayList<IntEntityProperty> intProperties() {
|
||||
return this.intEntityProperties;
|
||||
}
|
||||
|
||||
public void applyIntProperties(List<IntEntityProperty> properties) {
|
||||
properties.addAll(intEntityProperties);
|
||||
intEntityProperties.clear();
|
||||
}
|
||||
|
||||
public ObjectArrayList<FloatEntityProperty> floatProperties() {
|
||||
return this.floatEntityProperties;
|
||||
}
|
||||
|
||||
public void applyFloatProperties(List<FloatEntityProperty> properties) {
|
||||
properties.addAll(floatEntityProperties);
|
||||
floatEntityProperties.clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties.type;
|
||||
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
|
||||
public class BooleanProperty implements PropertyType {
|
||||
private final String name;
|
||||
|
||||
public BooleanProperty(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NbtMap nbtMap() {
|
||||
return NbtMap.builder()
|
||||
.putString("name", name)
|
||||
.putInt("type", 2)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties.type;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
import org.cloudburstmc.nbt.NbtType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EnumProperty implements PropertyType {
|
||||
private final String name;
|
||||
private final List<String> values;
|
||||
private final Object2IntMap<String> valueIndexMap;
|
||||
|
||||
public EnumProperty(String name, List<String> values) {
|
||||
this.name = name;
|
||||
this.values = values;
|
||||
this.valueIndexMap = new Object2IntOpenHashMap<>(values.size());
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
valueIndexMap.put(values.get(i), i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NbtMap nbtMap() {
|
||||
return NbtMap.builder()
|
||||
.putString("name", name)
|
||||
.putList("values", NbtType.STRING, values)
|
||||
.putInt("type", 3)
|
||||
.build();
|
||||
}
|
||||
|
||||
public int getIndex(String value) {
|
||||
return valueIndexMap.getOrDefault(value, -1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties.type;
|
||||
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
|
||||
public class FloatProperty implements PropertyType {
|
||||
private final String name;
|
||||
private final float max;
|
||||
private final float min;
|
||||
|
||||
public FloatProperty(String name, float min, float max) {
|
||||
this.name = name;
|
||||
this.max = max;
|
||||
this.min = min;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NbtMap nbtMap() {
|
||||
return NbtMap.builder()
|
||||
.putString("name", name)
|
||||
.putFloat("max", max)
|
||||
.putFloat("min", min)
|
||||
.putInt("type", 1)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties.type;
|
||||
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
|
||||
public class IntProperty implements PropertyType {
|
||||
private final String name;
|
||||
private final int max;
|
||||
private final int min;
|
||||
|
||||
public IntProperty(String name, int min, int max) {
|
||||
this.name = name;
|
||||
this.max = max;
|
||||
this.min = min;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NbtMap nbtMap() {
|
||||
return NbtMap.builder()
|
||||
.putString("name", name)
|
||||
.putInt("max", max)
|
||||
.putInt("min", min)
|
||||
.putInt("type", 0)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.geyser.entity.properties.type;
|
||||
|
||||
import org.cloudburstmc.nbt.NbtMap;
|
||||
|
||||
public interface PropertyType {
|
||||
NbtMap nbtMap();
|
||||
}
|
|
@ -39,6 +39,7 @@ import org.cloudburstmc.protocol.bedrock.packet.*;
|
|||
import org.geysermc.geyser.api.entity.type.GeyserEntity;
|
||||
import org.geysermc.geyser.entity.EntityDefinition;
|
||||
import org.geysermc.geyser.entity.GeyserDirtyMetadata;
|
||||
import org.geysermc.geyser.entity.properties.GeyserEntityPropertyManager;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
import org.geysermc.geyser.translator.text.MessageTranslator;
|
||||
import org.geysermc.geyser.util.EntityUtils;
|
||||
|
@ -117,6 +118,8 @@ public class Entity implements GeyserEntity {
|
|||
@Setter(AccessLevel.PROTECTED) // For players
|
||||
private boolean flagsDirty = false;
|
||||
|
||||
protected final GeyserEntityPropertyManager propertyManager;
|
||||
|
||||
public Entity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
||||
this.session = session;
|
||||
|
||||
|
@ -131,6 +134,8 @@ public class Entity implements GeyserEntity {
|
|||
|
||||
this.valid = false;
|
||||
|
||||
this.propertyManager = new GeyserEntityPropertyManager(definition.registeredProperties());
|
||||
|
||||
setPosition(position);
|
||||
setAirSupply(getMaxAir());
|
||||
|
||||
|
@ -348,6 +353,23 @@ public class Entity implements GeyserEntity {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the Bedrock entity properties to the client
|
||||
*/
|
||||
public void updateBedrockEntityProperties() {
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (propertyManager.hasProperties()) {
|
||||
SetEntityDataPacket entityDataPacket = new SetEntityDataPacket();
|
||||
entityDataPacket.setRuntimeEntityId(geyserId);
|
||||
propertyManager.applyIntProperties(entityDataPacket.getProperties().getIntProperties());
|
||||
propertyManager.applyFloatProperties(entityDataPacket.getProperties().getFloatProperties());
|
||||
session.sendUpstreamPacket(entityDataPacket);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFlags(ByteEntityMetadata entityMetadata) {
|
||||
byte xd = entityMetadata.getPrimitiveValue();
|
||||
setFlag(EntityFlag.ON_FIRE, ((xd & 0x01) == 0x01) && !getFlag(EntityFlag.FIRE_IMMUNE)); // Otherwise immune entities sometimes flicker onfire
|
||||
|
|
|
@ -28,11 +28,27 @@ package org.geysermc.geyser.entity.type.living.animal;
|
|||
import org.cloudburstmc.math.vector.Vector3f;
|
||||
import org.geysermc.geyser.entity.EntityDefinition;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.ArmadilloState;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ObjectEntityMetadata;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class ArmadilloEntity extends AnimalEntity {
|
||||
public ArmadilloEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
||||
public ArmadilloEntity(GeyserSession session, int entityId, long geyserId, UUID uuid,
|
||||
EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
||||
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
|
||||
}
|
||||
|
||||
// TODO: This is completely wrong; probably need to store the previous IDLE/ROLLING/SCARED state and check for transitions (pain)
|
||||
public void setArmadilloState(ObjectEntityMetadata<ArmadilloState> entityMetadata) {
|
||||
ArmadilloState armadilloState = entityMetadata.getValue();
|
||||
|
||||
switch (armadilloState) {
|
||||
case IDLE -> propertyManager.add("minecraft:armadillo_state", "unrolled");
|
||||
case ROLLING -> propertyManager.add("minecraft:armadillo_state", "rolled_up");
|
||||
case SCARED -> propertyManager.add("minecraft:armadillo_state", "rolled_up_peeking");
|
||||
}
|
||||
|
||||
updateBedrockEntityProperties();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,8 @@ public class BeeEntity extends AnimalEntity {
|
|||
// If the bee has stung
|
||||
dirtyMetadata.put(EntityDataTypes.MARK_VARIANT, (xd & 0x04) == 0x04 ? 1 : 0);
|
||||
// If the bee has nectar or not
|
||||
setFlag(EntityFlag.POWERED, (xd & 0x08) == 0x08);
|
||||
propertyManager.add("minecraft:has_nectar", (xd & 0x08) == 0x08);
|
||||
updateBedrockEntityProperties();
|
||||
}
|
||||
|
||||
public void setAngerTime(IntEntityMetadata entityMetadata) {
|
||||
|
|
|
@ -112,6 +112,11 @@ public final class Registries {
|
|||
*/
|
||||
public static final SimpleMappedRegistry<EntityType, EntityDefinition<?>> ENTITY_DEFINITIONS = SimpleMappedRegistry.create(RegistryLoaders.empty(() -> new EnumMap<>(EntityType.class)));
|
||||
|
||||
/**
|
||||
* A registry holding a list of all the known entity properties to be sent to the client after start game.
|
||||
*/
|
||||
public static final SimpleRegistry<Set<NbtMap>> BEDROCK_ENTITY_PROPERTIES = SimpleRegistry.create(RegistryLoaders.empty(HashSet::new));
|
||||
|
||||
/**
|
||||
* A map containing all Java entity identifiers and their respective Geyser definitions
|
||||
*/
|
||||
|
|
|
@ -638,6 +638,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
|
|||
public void connect() {
|
||||
startGame();
|
||||
sentSpawnPacket = true;
|
||||
syncEntityProperties();
|
||||
|
||||
// Set the hardcoded shield ID to the ID we just defined in StartGamePacket
|
||||
// upstream.getSession().getHardcodedBlockingId().set(this.itemMappings.getStoredItems().shield().getBedrockId());
|
||||
|
@ -1562,9 +1563,20 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
|
|||
startGamePacket.setRewindHistorySize(0);
|
||||
startGamePacket.setServerAuthoritativeBlockBreaking(false);
|
||||
|
||||
// Entity properties for older versions
|
||||
startGamePacket.getExperiments().add(new ExperimentData("upcoming_creator_features", true));
|
||||
|
||||
upstream.sendPacket(startGamePacket);
|
||||
}
|
||||
|
||||
private void syncEntityProperties() {
|
||||
for (NbtMap nbtMap : Registries.BEDROCK_ENTITY_PROPERTIES.get()) {
|
||||
SyncEntityPropertyPacket syncEntityPropertyPacket = new SyncEntityPropertyPacket();
|
||||
syncEntityPropertyPacket.setData(nbtMap);
|
||||
upstream.sendPacket(syncEntityPropertyPacket);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the next Bedrock item network ID to use for a new item
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue