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 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 Entity class. */ // TODO more javadocs diff --git a/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryAccessor.java b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryAccessor.java new file mode 100644 index 000000000..13fac8ad0 --- /dev/null +++ b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryAccessor.java @@ -0,0 +1,35 @@ +/* + * 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.mixin.object.builder; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.DefaultAttributeContainer; +import net.minecraft.entity.attribute.DefaultAttributeRegistry; + +@Mixin(DefaultAttributeRegistry.class) +public interface DefaultAttributeRegistryAccessor { + @Accessor("DEFAULT_ATTRIBUTE_REGISTRY") + static Map, DefaultAttributeContainer> getRegistry() { + throw new AssertionError("mixin dummy"); + } +} diff --git a/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryMixin.java b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryMixin.java new file mode 100644 index 000000000..44060041c --- /dev/null +++ b/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/mixin/object/builder/DefaultAttributeRegistryMixin.java @@ -0,0 +1,46 @@ +/* + * 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.mixin.object.builder; + +import java.util.HashMap; +import java.util.Map; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.DefaultAttributeContainer; +import net.minecraft.entity.attribute.DefaultAttributeRegistry; + +@Mixin(DefaultAttributeRegistry.class) +public abstract class DefaultAttributeRegistryMixin { + @Shadow + @Final + @Mutable + private static Map, DefaultAttributeContainer> DEFAULT_ATTRIBUTE_REGISTRY; + + @Inject(method = "*", at = @At("TAIL")) + private static void injectAttributes(CallbackInfo ci) { + DEFAULT_ATTRIBUTE_REGISTRY = new HashMap<>(DEFAULT_ATTRIBUTE_REGISTRY); + } +} diff --git a/fabric-object-builders-v0/src/main/resources/fabric-object-builders-v0.mixins.json b/fabric-object-builders-v0/src/main/resources/fabric-object-builders-v0.mixins.json index f520110df..9f12607ee 100644 --- a/fabric-object-builders-v0/src/main/resources/fabric-object-builders-v0.mixins.json +++ b/fabric-object-builders-v0/src/main/resources/fabric-object-builders-v0.mixins.json @@ -4,6 +4,8 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "BlockSettingsHooks", + "DefaultAttributeRegistryAccessor", + "DefaultAttributeRegistryMixin", "MaterialBuilderHooks", "MixinBlock", "MixinItem"