Change biome spawn list entries to use factory method where possible (#5075)

This commit is contained in:
Ben Staddon authored and LexManos committed 2018-10-04 00:24:01 -07:00
1 parent 82262e462e
commit 214275baba
3 files changed
+40 -42

No files matched your search

@@ -19,10 +19,6 @@
package net.minecraftforge.fml.common.registry;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.Level;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList.EntityEggInfo;
import net.minecraft.entity.EntityLiving;
@@ -36,7 +32,10 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.network.internal.FMLMessage.EntitySpawnMessage;
import net.minecraftforge.registries.GameData;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
@@ -145,8 +144,9 @@ public class EntityRegistry
private static final EntityRegistry INSTANCE = new EntityRegistry();
private ListMultimap<ModContainer, EntityRegistration> entityRegistrations = ArrayListMultimap.create();
private BiMap<Class<? extends Entity>, EntityRegistration> entityClassRegistrations = HashBiMap.create();
private final ListMultimap<ModContainer, EntityRegistration> entityRegistrations = ArrayListMultimap.create();
private final BiMap<Class<? extends Entity>, EntityRegistration> entityClassRegistrations = HashBiMap.create();
private final Map<Class<? extends Entity>, EntityEntry> entityClassEntries = GameData.getEntityClassMap();
public static EntityRegistry instance()
{
@@ -374,15 +374,9 @@ public class EntityRegistry
//Helper function
@Nullable
public static EntityEntry getEntry(Class<? extends Entity> entry)
public static EntityEntry getEntry(Class<? extends Entity> entityClass)
{
//TODO: Slave map for faster lookup?
for (EntityEntry e : ForgeRegistries.ENTITIES)
{
if (e.getEntityClass() == entry)
return e;
}
return null;
return instance().entityClassEntries.get(entityClass);
}
// This is an internal method - do not touch.
@@ -70,6 +70,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@@ -111,8 +112,9 @@ public class GameData
private static final int MAX_RECIPE_ID = Integer.MAX_VALUE >> 5; // Varint CPacketRecipeInfo/SPacketRecipeBook
private static final int MAX_PROFESSION_ID = 1024; //TODO: Is this serialized anywhere anymore?
private static final ResourceLocation BLOCK_TO_ITEM = new ResourceLocation("minecraft:blocktoitemmap");
private static final ResourceLocation BLOCKSTATE_TO_ID = new ResourceLocation("minecraft:blockstatetoid");
private static final ResourceLocation BLOCK_TO_ITEM = new ResourceLocation("minecraft:blocktoitemmap");
private static final ResourceLocation BLOCKSTATE_TO_ID = new ResourceLocation("minecraft:blockstatetoid");
private static final ResourceLocation ENTITY_CLASS_TO_ENTRY = new ResourceLocation("forge:entity_class_to_entry");
private static boolean hasInit = false;
private static final boolean DISABLE_VANILLA_REGISTRIES = Boolean.parseBoolean(System.getProperty("forge.disableVanillaGameData", "false")); // Use for unit tests/debugging
private static final BiConsumer<ResourceLocation, ForgeRegistry<?>> LOCK_VANILLA = (name, reg) -> reg.slaves.values().stream().filter(o -> o instanceof ILockableRegistry).forEach(o -> ((ILockableRegistry)o).lock());
@@ -188,6 +190,12 @@ public class GameData
return GameRegistry.findRegistry(Block.class).getSlaveMap(BLOCKSTATE_TO_ID, ObjectIntIdentityMap.class);
}
@SuppressWarnings("unchecked")
public static Map<Class<? extends Entity>, EntityEntry> getEntityClassMap()
{
return GameRegistry.findRegistry(EntityEntry.class).getSlaveMap(ENTITY_CLASS_TO_ENTRY, Map.class);
}
public static <K extends IForgeRegistryEntry<K>> K register_impl(K value)
{
Validate.notNull(value, "Attempted to register a null object");
@@ -452,7 +460,7 @@ public class GameData
reg.register(id, key, new EntityEntry(clazz, oldName));
}
private static class EntityCallbacks implements IForgeRegistry.AddCallback<EntityEntry>
private static class EntityCallbacks implements IForgeRegistry.AddCallback<EntityEntry>, IForgeRegistry.ClearCallback<EntityEntry>, IForgeRegistry.CreateCallback<EntityEntry>
{
static final EntityCallbacks INSTANCE = new EntityCallbacks();
@@ -464,7 +472,24 @@ public class GameData
((EntityEntryBuilder.BuiltEntityEntry) entry).addedToRegistry();
}
if (entry.getEgg() != null)
{
EntityList.ENTITY_EGGS.put(entry.getRegistryName(), entry.getEgg());
}
@SuppressWarnings("unchecked")
Map<Class<? extends Entity>, EntityEntry> map = owner.getSlaveMap(ENTITY_CLASS_TO_ENTRY, Map.class);
map.put(entry.getEntityClass(), entry);
}
@Override
public void onClear(IForgeRegistryInternal<EntityEntry> owner, RegistryManager stage)
{
owner.getSlaveMap(ENTITY_CLASS_TO_ENTRY, Map.class).clear();
}
@Override
public void onCreate(IForgeRegistryInternal<EntityEntry> owner, RegistryManager stage)
{
owner.setSlaveMap(ENTITY_CLASS_TO_ENTRY, new IdentityHashMap<>());
}
}