More work, game loads and runs now.

Moved Registry events to directly AFTER PreInit instead of before.
This allows modders to register handlers for it without @EventBusSubscriber.
It also allows you to register custom things needed before the construction of blocks or items such as Fluids.
TODO: Move Fluids to a real registry.
@ObjectHolder can now be used on private fields.
Reworked FMLModIdMappingEvent to include data for all registries.
Tile Entities are now registrable.
This commit is contained in:
LexManos committed 2017-06-23 16:59:57 -05:00
1 parent a26d89c876
commit dee84dc1e4
21 files changed
+230 -278

No files matched your search

@@ -40,7 +40,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
private final BiMap<ResourceLocation, V> names = HashBiMap.create();
private final Class<V> superType;
private final Map<ResourceLocation, ResourceLocation> aliases = Maps.newHashMap();
private final Map<ResourceLocation, ?> slaves = Maps.newHashMap();
final Map<ResourceLocation, ?> slaves = Maps.newHashMap();
private final ResourceLocation defaultKey;
private final CreateCallback<V> create;
private final AddCallback<V> add;
@@ -427,13 +427,14 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
this.ids.clear();
this.names.clear();
this.availabilityMap.clear(0, this.availabilityMap.length());
this.defaultValue = null;
this.isFrozen = false;
boolean errored = false;
for (Entry<ResourceLocation, V> entry : from.names.entrySet())
{
int id = from.ids.inverse().get(entry.getKey());
int id = from.getID(entry.getKey());
int realId = add(id, entry.getValue());
if (id != realId && id != -1)
{
@@ -501,11 +502,20 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
return this.isFrozen;
}
/**
* Used to control the times where people can modify this registry.
* Users should only ever register things in the Register<?> events!
*/
public void freeze()
{
this.isFrozen = true;
}
public void unfreeze()
{
this.isFrozen = false;
}
RegistryEvent.Register<V> getRegisterEvent(ResourceLocation name)
{
return new RegistryEvent.Register<V>(name, this);
@@ -63,6 +63,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -101,6 +102,7 @@ public class GameData
private static final ResourceLocation BLOCKSTATE_TO_ID = new ResourceLocation("minecraft:blockstatetoid");
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());
static {
init();
@@ -192,8 +194,13 @@ public class GameData
final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(r.getKey());
loadRegistry(r.getKey(), RegistryManager.ACTIVE, RegistryManager.VANILLA, clazz, true);
}
RegistryManager.VANILLA.registries.forEach((name, reg) -> reg.validateContent(name));
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.freeze());
RegistryManager.VANILLA.registries.forEach((name, reg) ->
{
reg.validateContent(name);
reg.freeze();
});
RegistryManager.VANILLA.registries.forEach(LOCK_VANILLA);
RegistryManager.ACTIVE.registries.forEach(LOCK_VANILLA);
FMLLog.fine("Vanilla freeze snapshot created");
}
@@ -206,7 +213,11 @@ public class GameData
final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(r.getKey());
loadRegistry(r.getKey(), RegistryManager.ACTIVE, RegistryManager.FROZEN, clazz, true);
}
RegistryManager.FROZEN.registries.forEach((name, reg) -> reg.validateContent(name));
RegistryManager.FROZEN.registries.forEach((name, reg) ->
{
reg.validateContent(name);
reg.freeze();
});
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.freeze());
FMLLog.fine("All registries frozen");
}
@@ -227,7 +238,7 @@ public class GameData
loadRegistry(r.getKey(), RegistryManager.FROZEN, RegistryManager.ACTIVE, clazz, true);
}
// the id mapping has reverted, fire remap events for those that care about id changes
Loader.instance().fireRemapEvent(ImmutableMap.of(), ImmutableMap.of(), true);
Loader.instance().fireRemapEvent(ImmutableMap.of(), true);
// the id mapping has reverted, ensure we sync up the object holders
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
@@ -574,30 +585,25 @@ public class GameData
});
}
/*
// Validate that all the STAGING data is good
forAllRegistries(PersistentRegistry.STAGING, ValidateRegistryFunction.OPERATION);
STAGING.registries.forEach((name, reg) -> reg.validateContent(name));
// Load the STAGING registry into the ACTIVE registry
for (Map.Entry<ResourceLocation, FMLControlledNamespacedRegistry<?>> r : PersistentRegistry.ACTIVE.registries.entrySet())
for (Map.Entry<ResourceLocation, ForgeRegistry<? extends IForgeRegistryEntry<?>>> r : RegistryManager.ACTIVE.registries.entrySet())
{
final Class<? extends IForgeRegistryEntry> registrySuperType = PersistentRegistry.ACTIVE.getRegistrySuperType(r.getKey());
loadRegistry(r.getKey(), PersistentRegistry.STAGING, PersistentRegistry.ACTIVE, registrySuperType);
final Class<? extends IForgeRegistryEntry> registrySuperType = RegistryManager.ACTIVE.getSuperType(r.getKey());
loadRegistry(r.getKey(), STAGING, RegistryManager.ACTIVE, registrySuperType, true);
}
// Dump the active registry
forAllRegistries(PersistentRegistry.ACTIVE, DumpRegistryFunction.OPERATION);
RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.dump(name));
// Tell mods that the ids have changed
Loader.instance().fireRemapEvent(remaps.get(BLOCKS), remaps.get(ITEMS), false);
Loader.instance().fireRemapEvent(remaps, false);
// The id map changed, ensure we apply object holders
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
// Clean out the staging registry now, we're done with it
PersistentRegistry.STAGING.clean();
*/
// Return an empty list, because we're good
return ArrayListMultimap.create();
}
@@ -648,6 +654,10 @@ public class GameData
{
List<ResourceLocation> keys = Lists.newArrayList(RegistryManager.ACTIVE.registries.keySet());
Collections.sort(keys, (o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
if (filter.test(name))
((ForgeRegistry<?>)reg).unfreeze();
});
if (filter.test(BLOCKS))
{
@@ -666,5 +676,11 @@ public class GameData
MinecraftForge.EVENT_BUS.post(RegistryManager.ACTIVE.getRegistry(rl).getRegisterEvent(rl));
}
ObjectHolderRegistry.INSTANCE.applyObjectHolders(); // inject everything else
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
if (filter.test(name))
((ForgeRegistry<?>)reg).freeze();
});
}
}
@@ -10,6 +10,8 @@ import javax.annotation.Nullable;
import org.apache.commons.lang3.Validate;
import com.google.common.collect.Maps;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.RegistryNamespacedDefaultedByKey;
import net.minecraftforge.fml.common.FMLLog;
@@ -108,9 +110,6 @@ class NamespacedDefaultedWrapper<V extends IForgeRegistryEntry<V>> extends Regis
}
//internal
@Override //We override all public methods, this should be fine as null.
protected Map<ResourceLocation, V> createUnderlyingMap(){ return null; }
@Override
public void lock(){ this.locked = true; }
@@ -104,9 +104,6 @@ class NamespacedWrapper<V extends IForgeRegistryEntry<V>> extends RegistryNamesp
}
//internal
@Override //We override all public methods, this should be fine as null.
protected Map<ResourceLocation, V> createUnderlyingMap(){ return null; }
@Override
public void lock(){ this.locked = true; }
@@ -164,6 +164,7 @@ class ObjectHolderRef
private static Method fieldAccessorSet;
static Field makeWritable(Field f) throws Exception
{
f.setAccessible(true);
if (modifiersField == null)
{
Method getReflectionFactory = Class.forName("sun.reflect.ReflectionFactory").getDeclaredMethod("getReflectionFactory");
@@ -27,6 +27,7 @@ import java.util.Set;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.discovery.ASMDataTable;
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData;
import net.minecraftforge.fml.common.registry.GameRegistry;
@@ -52,6 +53,11 @@ public enum ObjectHolderRegistry
Set<ASMData> allObjectHolders = table.getAll(GameRegistry.ObjectHolder.class.getName());
Map<String, String> classModIds = Maps.newHashMap();
Map<String, Class<?>> classCache = Maps.newHashMap();
for (ASMData data : table.getAll(Mod.class.getName()))
{
String modid = (String)data.getAnnotationInfo().get("modid");
classModIds.put(data.getClassName(), modid);
}
for (ASMData data : allObjectHolders)
{
String className = data.getClassName();
@@ -119,7 +125,7 @@ public enum ObjectHolderRegistry
}
try
{
Field f = clazz.getField(annotationTarget);
Field f = clazz.getDeclaredField(annotationTarget);
addHolderReference(new ObjectHolderRef(f, new ResourceLocation(value), extractFromValue));
}
catch (Exception ex)