Added entity attribute setup and creation events

This commit is contained in:
Kovak authored and LexManos committed 2021-02-15 18:40:32 -08:00
1 parent 58f93263e2
commit ba15737fe3
8 files changed
+290 -12

No files matched your search

@@ -23,6 +23,7 @@ import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
@@ -58,6 +59,8 @@ import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.ai.attributes.Attribute;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
import net.minecraft.fluid.*;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.loot.LootContext;
@@ -70,6 +73,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.LivingEntity;
@@ -141,6 +145,8 @@ import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.ItemAttributeModifierEvent;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
@@ -166,6 +172,7 @@ import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.NoteBlockEvent;
import net.minecraftforge.eventbus.api.Event.Result;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.packs.ResourcePackLoader;
import net.minecraftforge.registries.DataSerializerEntry;
@@ -1402,4 +1409,39 @@ public class ForgeHooks
}).result().orElse(dymData);
return data.set(DIMENSIONS_KEY, withInjected);
}
private static final Map<EntityType<? extends LivingEntity>, AttributeModifierMap> FORGE_ATTRIBUTES = new HashMap<>();
/** FOR INTERNAL USE ONLY, DO NOT CALL DIRECTLY */
@Deprecated
public static Map<EntityType<? extends LivingEntity>, AttributeModifierMap> getAttributesView()
{
return Collections.unmodifiableMap(FORGE_ATTRIBUTES);
}
/** FOR INTERNAL USE ONLY, DO NOT CALL DIRECTLY
* ONLY EXISTS FOR LEGACY REASONS SHOULD BE REMOVED IN 1.17
*/
@Deprecated /// Internal use only, Remove in 1.17
public static AttributeModifierMap putAttributesOld(EntityType<? extends LivingEntity> type, AttributeModifierMap map)
{
LOGGER.warn("Called deprecated GlobalEntityTypeAttributes#put for {}, use EntityAttributeCreationEvent instead.", type.getRegistryName());
return FORGE_ATTRIBUTES.put(type, map);
}
/** FOR INTERNAL USE ONLY, DO NOT CALL DIRECTLY */
@Deprecated
public static void modifyAttributes()
{
ModLoader.get().postEvent(new EntityAttributeCreationEvent(FORGE_ATTRIBUTES));
Map<EntityType<? extends LivingEntity>, AttributeModifierMap.MutableAttribute> finalMap = new HashMap<>();
ModLoader.get().postEvent(new EntityAttributeModificationEvent(finalMap));
finalMap.forEach((k, v) ->
{
AttributeModifierMap modifiers = GlobalEntityTypeAttributes.getAttributesForEntity(k);
AttributeModifierMap.MutableAttribute newMutable = modifiers != null ? new AttributeModifierMap.MutableAttribute(modifiers) : new AttributeModifierMap.MutableAttribute();
newMutable.combine(v);
FORGE_ATTRIBUTES.put(k, newMutable.create());
});
}
}
@@ -0,0 +1,53 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.event.entity;
import java.util.Map;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.event.lifecycle.IModBusEvent;
/**
* EntityAttributeCreationEvent.<br>
* Use this event to register attributes for your own EntityTypes.
* This event is fired after registration and before common setup.
* <br>
* Fired on the Mod bus {@link IModBusEvent}.<br>
**/
public class EntityAttributeCreationEvent extends Event implements IModBusEvent
{
private final Map<EntityType<? extends LivingEntity>, AttributeModifierMap> map;
public EntityAttributeCreationEvent(Map<EntityType<? extends LivingEntity>, AttributeModifierMap> map)
{
this.map = map;
}
public void put(EntityType<? extends LivingEntity> entity, AttributeModifierMap map)
{
if (GlobalEntityTypeAttributes.doesEntityHaveAttributes(entity))
throw new IllegalStateException("Duplicate GlobalEntityTypeAttributes entry: " + entity);
this.map.put(entity, map);
}
}
@@ -0,0 +1,83 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2021.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.event.entity;
import com.google.common.collect.ImmutableList;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.Attribute;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.event.lifecycle.IModBusEvent;
import net.minecraftforge.registries.ForgeRegistries;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* EntityAttributeModificationEvent.<br>
* Use this event to add attributes to existing entity types.
* This event is fired after registration and before common setup, and after {@link EntityAttributeCreationEvent}
* <br>
* Fired on the Mod bus {@link IModBusEvent}.<br>
**/
public class EntityAttributeModificationEvent extends Event implements IModBusEvent
{
private final Map<EntityType<? extends LivingEntity>, AttributeModifierMap.MutableAttribute> entityAttributes;
private final List<EntityType<? extends LivingEntity>> entityTypes;
@SuppressWarnings("unchecked")
public EntityAttributeModificationEvent(Map<EntityType<? extends LivingEntity>, AttributeModifierMap.MutableAttribute> mapIn)
{
this.entityAttributes = mapIn;
this.entityTypes = ImmutableList.copyOf(
ForgeRegistries.ENTITIES.getValues().stream()
.filter(GlobalEntityTypeAttributes::doesEntityHaveAttributes)
.map(entityType -> (EntityType<? extends LivingEntity>) entityType)
.collect(Collectors.toList())
);
}
public void add(EntityType<? extends LivingEntity> entityType, Attribute attribute, double value)
{
AttributeModifierMap.MutableAttribute attributes = entityAttributes.computeIfAbsent(entityType,
(type) -> new AttributeModifierMap.MutableAttribute());
attributes.createMutableAttribute(attribute, value);
}
public void add(EntityType<? extends LivingEntity> entityType, Attribute attribute)
{
add(entityType, attribute, attribute.getDefaultValue());
}
public boolean has(EntityType<? extends LivingEntity> entityType, Attribute attribute)
{
AttributeModifierMap globalMap = GlobalEntityTypeAttributes.getAttributesForEntity(entityType);
return globalMap.hasAttribute(attribute) || (entityAttributes.get(entityType) != null && entityAttributes.get(entityType).hasAttribute(attribute));
}
public List<EntityType<? extends LivingEntity>> getTypes()
{
return entityTypes;
}
}
@@ -30,7 +30,9 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.Attribute;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.GlobalEntityTypeAttributes;
import net.minecraft.entity.ai.brain.memory.MemoryModuleType;
import net.minecraft.entity.ai.brain.schedule.Activity;
@@ -68,13 +70,17 @@ import net.minecraft.world.gen.foliageplacer.FoliagePlacerType;
import net.minecraft.world.gen.placement.Placement;
import net.minecraft.world.gen.surfacebuilders.SurfaceBuilder;
import net.minecraft.world.gen.treedecorator.TreeDecoratorType;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.ForgeTagHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.world.ForgeWorldType;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.RegistryEvent.MissingMappings;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.ModLoadingStage;
import net.minecraftforge.fml.common.EnhancedRuntimeException;
@@ -228,7 +234,7 @@ public class GameData
{
return RegistryManager.ACTIVE.getRegistry(Block.class).getSlaveMap(BLOCKSTATE_TO_ID, ObjectIntIdentityMap.class);
}
@SuppressWarnings("unchecked")
public static Map<BlockState, PointOfInterestType> getBlockStatePointOfInterestTypeMap()
{
@@ -379,12 +385,15 @@ public class GameData
}, executor).handle((v, t)->t != null ? Collections.singletonList(t): Collections.emptyList());
}
@SuppressWarnings("deprecation")
public static CompletableFuture<List<Throwable>> checkForRevertToVanilla(final Executor executor, final CompletableFuture<List<Throwable>> listCompletableFuture) {
return listCompletableFuture.whenCompleteAsync((errors, except) -> {
if (except != null) {
LOGGER.fatal("Detected errors during registry event dispatch, rolling back to VANILLA state");
revertTo(RegistryManager.VANILLA, false);
LOGGER.fatal("Detected errors during registry event dispatch, roll back to VANILLA complete");
} else {
net.minecraftforge.common.ForgeHooks.modifyAttributes();
}
}, executor);
}
@@ -622,7 +631,7 @@ public class GameData
private static class PointOfInterestTypeCallbacks implements IForgeRegistry.AddCallback<PointOfInterestType> , IForgeRegistry.ClearCallback<PointOfInterestType>, IForgeRegistry.CreateCallback<PointOfInterestType>
{
static final PointOfInterestTypeCallbacks INSTANCE = new PointOfInterestTypeCallbacks();
@Override
public void onAdd(IForgeRegistryInternal<PointOfInterestType> owner, RegistryManager stage, int id, PointOfInterestType obj, @Nullable PointOfInterestType oldObj)
{
@@ -631,7 +640,7 @@ public class GameData
{
oldObj.getBlockStates().forEach(map::remove);
}
obj.getBlockStates().forEach((state) ->
obj.getBlockStates().forEach((state) ->
{
PointOfInterestType oldType = map.put(state, obj);
if (oldType != null)
@@ -653,7 +662,7 @@ public class GameData
owner.setSlaveMap(BLOCKSTATE_TO_POINT_OF_INTEREST_TYPE, new HashMap<>());
}
}
private static <T extends IForgeRegistryEntry<T>> void loadRegistry(final ResourceLocation registryName, final RegistryManager from, final RegistryManager to, final Class<T> regType, boolean freeze)
{
ForgeRegistry<T> fromRegistry = from.getRegistry(registryName);