Entity attribute registry (#568)

This commit is contained in:
liach 2020-04-27 06:52:55 -05:00 committed by GitHub
parent 2fd224ca63
commit 5417aa6a29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 155 additions and 0 deletions

View file

@ -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.
*
* <p>All living entity types must have default attributes registered. See {@link
* FabricEntityTypeBuilder} for utility on entity type registration in general.</p>
*
* <p>A registered default attribute for an entity type can be retrieved through
* {@link net.minecraft.entity.attribute.DefaultAttributeRegistry#get(EntityType)}.</p>
*
* @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.
*
* <p>It can be used in a fashion similar to this:
* <blockquote><pre>
* EntityAttributeRegistry.INSTANCE.register(type, LivingEntity.createLivingAttributes());
* </pre></blockquote>
* </p>
*
* <p>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)}.</p>
*
* @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));
}
}
}

View file

@ -33,6 +33,9 @@ import net.fabricmc.fabric.impl.object.builder.FabricEntityType;
* Extended version of {@link EntityType.Builder} with added registration for
* server-&gt;client entity tracking values.
*
* <p>For living entities, they must have {@link FabricDefaultAttributeRegistry
* default attributes registered} after the entity type is registered.</p>
*
* @param <T> Entity class.
*/
// TODO more javadocs

View file

@ -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<EntityType<? extends LivingEntity>, DefaultAttributeContainer> getRegistry() {
throw new AssertionError("mixin dummy");
}
}

View file

@ -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<EntityType<? extends LivingEntity>, DefaultAttributeContainer> DEFAULT_ATTRIBUTE_REGISTRY;
@Inject(method = "<clinit>*", at = @At("TAIL"))
private static void injectAttributes(CallbackInfo ci) {
DEFAULT_ATTRIBUTE_REGISTRY = new HashMap<>(DEFAULT_ATTRIBUTE_REGISTRY);
}
}

View file

@ -4,6 +4,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"BlockSettingsHooks",
"DefaultAttributeRegistryAccessor",
"DefaultAttributeRegistryMixin",
"MaterialBuilderHooks",
"MixinBlock",
"MixinItem"