From 5417aa6a291fb781a588807ea83c2ad037da6ab6 Mon Sep 17 00:00:00 2001 From: liach <7806504+liach@users.noreply.github.com> Date: Mon, 27 Apr 2020 06:52:55 -0500 Subject: [PATCH] Entity attribute registry (#568) --- .../FabricDefaultAttributeRegistry.java | 69 +++++++++++++++++++ .../api/entity/FabricEntityTypeBuilder.java | 3 + .../DefaultAttributeRegistryAccessor.java | 35 ++++++++++ .../DefaultAttributeRegistryMixin.java | 46 +++++++++++++ .../fabric-object-builders-v0.mixins.json | 2 + 5 files changed, 155 insertions(+) create mode 100644 fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricDefaultAttributeRegistry.java create mode 100644 fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryAccessor.java create mode 100644 fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryMixin.java diff --git a/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricDefaultAttributeRegistry.java b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricDefaultAttributeRegistry.java new file mode 100644 index 000000000..fb780baac --- /dev/null +++ b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricDefaultAttributeRegistry.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.entity; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.DefaultAttributeContainer; +import net.minecraft.util.registry.Registry; + +import net.fabricmc.fabric.mixin.object.builder.DefaultAttributeRegistryAccessor; + +/** + * Allows registering custom default attributes for living entities. + * + *
All living entity types must have default attributes registered. See {@link + * FabricEntityTypeBuilder} for utility on entity type registration in general.
+ * + *A registered default attribute for an entity type can be retrieved through + * {@link net.minecraft.entity.attribute.DefaultAttributeRegistry#get(EntityType)}.
+ * + * @see net.minecraft.entity.attribute.DefaultAttributeRegistry + * @deprecated Experimental feature, may be removed or changed without further notice. + * Vanilla snapshot feature, subject to vanilla change. + */ +@Deprecated +public final class FabricDefaultAttributeRegistry { + /** + * Private logger, not exposed. + */ + private static final Logger LOGGER = LogManager.getLogger(); + + /** + * Registers a default attribute for a type of living entity. + * + *It can be used in a fashion similar to this: + *
+ * + * + *+ * EntityAttributeRegistry.INSTANCE.register(type, LivingEntity.createLivingAttributes()); + *
If a registration overrides another, a debug log message will be emitted. Existing registrations + * can be checked at {@link net.minecraft.entity.attribute.DefaultAttributeRegistry#hasDefinitionFor(EntityType)}.
+ * + * @param type the entity type + * @param builder the builder that creates the default attribute + */ + public static void register(EntityType extends LivingEntity> type, DefaultAttributeContainer.Builder builder) { + if (DefaultAttributeRegistryAccessor.getRegistry().put(type, builder.build()) != null) { + LOGGER.debug("Overriding existing registration for entity type {}", Registry.ENTITY_TYPE.getId(type)); + } + } +} diff --git a/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java index edda1c0e3..51a932bd4 100644 --- a/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java +++ b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/entity/FabricEntityTypeBuilder.java @@ -33,6 +33,9 @@ import net.fabricmc.fabric.impl.object.builder.FabricEntityType; * Extended version of {@link EntityType.Builder} with added registration for * server->client entity tracking values. * + *For living entities, they must have {@link FabricDefaultAttributeRegistry + * default attributes registered} after the entity type is registered.
+ * * @param