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

@@ -20,10 +20,11 @@
package net.minecraftforge.client.event;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.IContextSetter;
/**
* Fired when the {@link net.minecraftforge.client.model.ModelLoader} is ready to receive registrations
*/
public class ModelRegistryEvent extends Event
public class ModelRegistryEvent extends Event implements IContextSetter
{
}
@@ -82,12 +82,14 @@ import com.google.common.eventbus.Subscribe;
import net.minecraftforge.fml.client.FMLFileResourcePack;
import net.minecraftforge.fml.client.FMLFolderResourcePack;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;
import net.minecraftforge.fml.common.AutomaticEventSubscriber;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.ICrashCallable;
import net.minecraftforge.fml.common.LoadController;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.ModMetadata;
import net.minecraftforge.fml.common.WorldAccessContainer;
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData;
@@ -103,6 +105,7 @@ import net.minecraftforge.fml.common.network.NetworkRegistry;
import javax.annotation.Nullable;
@EventBusSubscriber(modid = "forge")
public class ForgeModContainer extends DummyModContainer implements WorldAccessContainer
{
public static final String VERSION_CHECK_CAT = "version_checking";
@@ -444,6 +447,7 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
NetworkRegistry.INSTANCE.register(this, this.getClass(), "*", evt.getASMHarvestedData());
ForgeNetworkHandler.registerChannel(this, evt.getSide());
ConfigManager.sync(this.getModId(), Config.Type.INSTANCE);
MinecraftForge.EVENT_BUS.register(this);
}
@Subscribe
@@ -29,9 +29,6 @@ import net.minecraftforge.fml.common.discovery.asm.ModAnnotation;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.Level;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
@@ -56,6 +53,7 @@ public class AutomaticEventSubscriber
try
{
//noinspection unchecked
@SuppressWarnings("unchecked")
List<ModAnnotation.EnumHolder> sidesEnum = (List<ModAnnotation.EnumHolder>)targ.getAnnotationInfo().get("value");
EnumSet<Side> sides = DEFAULT;
if (sidesEnum != null) {
@@ -65,7 +63,7 @@ public class AutomaticEventSubscriber
}
}
if (sides == DEFAULT || sides.contains(side)) {
FMLLog.log.debug("Found @EventBusSubscriber class {}", targ.getClassName());
//FMLLog.log.debug("Found @EventBusSubscriber class {}", targ.getClassName());
String amodid = (String)targ.getAnnotationInfo().get("modid");
if (Strings.isNullOrEmpty(amodid)) {
amodid = ASMDataTable.getOwnerModID(mods, targ);
@@ -79,6 +77,7 @@ public class AutomaticEventSubscriber
FMLLog.log.debug("Skipping @EventBusSubscriber injection for {} since it is not for mod {}", targ.getClassName(), mod.getModId());
continue; //We're not injecting this guy
}
FMLLog.log.debug("Registering @EventBusSubscriber for {} for mod {}", targ.getClassName(), mod.getModId());
Class<?> subscriptionTarget = Class.forName(targ.getClassName(), true, mcl);
MinecraftForge.EVENT_BUS.register(subscriptionTarget);
FMLLog.log.debug("Injected @EventBusSubscriber class {}", targ.getClassName());
@@ -642,9 +642,9 @@ public class Loader
ObjectHolderRegistry.INSTANCE.findObjectHolders(discoverer.getASMTable());
ItemStackHolderInjector.INSTANCE.findHolders(discoverer.getASMTable());
CapabilityManager.INSTANCE.injectCapabilities(discoverer.getASMTable());
modController.distributeStateMessage(LoaderState.PREINITIALIZATION, discoverer.getASMTable(), canonicalConfigDir);
GameData.fireRegistryEvents();
FMLCommonHandler.instance().fireSidedRegistryEvents();
modController.distributeStateMessage(LoaderState.PREINITIALIZATION, discoverer.getASMTable(), canonicalConfigDir);
ObjectHolderRegistry.INSTANCE.applyObjectHolders();
ItemStackHolderInjector.INSTANCE.inject();
modController.transition(LoaderState.INITIALIZATION, false);
@@ -1003,11 +1003,11 @@ public class Loader
return true;
}
public void fireRemapEvent(Map<ResourceLocation, Integer[]> remapBlocks, Map<ResourceLocation, Integer[]> remapItems, boolean isFreezing)
public void fireRemapEvent(Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, boolean isFreezing)
{
if (modController!=null)
{
modController.propogateStateMessage(new FMLModIdMappingEvent(remapBlocks, remapItems, isFreezing));
modController.propogateStateMessage(new FMLModIdMappingEvent(remaps, isFreezing));
}
}
@@ -20,13 +20,13 @@
package net.minecraftforge.fml.common.event;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.util.ResourceLocation;
/**
@@ -42,48 +42,49 @@ import net.minecraft.util.ResourceLocation;
* @see net.minecraftforge.fml.common.Mod.EventHandler for how to subscribe to this event
* @author cpw
*/
public class FMLModIdMappingEvent extends FMLEvent {
public enum RemapTarget { BLOCK, ITEM }
public class FMLModIdMappingEvent extends FMLEvent
{
public class ModRemapping
{
public final ResourceLocation registry;
public final ResourceLocation key;
public final int oldId;
public final int newId;
public final String tag;
public final RemapTarget remapTarget;
public final ResourceLocation resourceLocation;
public ModRemapping(int oldId, int newId, ResourceLocation tag, RemapTarget type)
private ModRemapping(ResourceLocation registry, ResourceLocation key, int oldId, int newId)
{
this.registry = registry;
this.key = key;
this.oldId = oldId;
this.newId = newId;
this.tag = tag.toString();
this.remapTarget = type;
this.resourceLocation = tag;
}
}
public final ImmutableList<ModRemapping> remappedIds;
private final Map<ResourceLocation, ImmutableList<ModRemapping>> remaps;
private final ImmutableSet<ResourceLocation> keys;
public final boolean isFrozen;
public FMLModIdMappingEvent(Map<ResourceLocation, Integer[]> blocks, Map<ResourceLocation, Integer[]> items, boolean isFrozen)
public FMLModIdMappingEvent(Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps, boolean isFrozen)
{
this.isFrozen = isFrozen;
List<ModRemapping> remappings = Lists.newArrayList();
for (Entry<ResourceLocation, Integer[]> mapping : blocks.entrySet())
this.remaps = Maps.newHashMap();
remaps.forEach((name, rm) ->
{
remappings.add(new ModRemapping(mapping.getValue()[0], mapping.getValue()[1], mapping.getKey(), RemapTarget.BLOCK));
}
for (Entry<ResourceLocation, Integer[]> mapping : items.entrySet())
{
remappings.add(new ModRemapping(mapping.getValue()[0], mapping.getValue()[1], mapping.getKey(), RemapTarget.ITEM));
}
Collections.sort(remappings, new Comparator<ModRemapping>() {
@Override
public int compare(ModRemapping o1, ModRemapping o2)
{
return (o1.newId < o2.newId) ? -1 : ((o1.newId == o2.newId) ? 0 : 1);
}
List<ModRemapping> tmp = Lists.newArrayList();
rm.entrySet().forEach(e -> tmp.add(new ModRemapping(name, e.getKey(), e.getValue()[0], e.getValue()[1])));
Collections.sort(tmp, (o1, o2) -> (o1.newId < o2.newId) ? -1 : ((o1.newId == o2.newId) ? 0 : 1));
this.remaps.put(name, ImmutableList.copyOf(tmp));
});
remappedIds = ImmutableList.copyOf(remappings);
this.keys = ImmutableSet.copyOf(this.remaps.keySet());
}
public ImmutableSet<ResourceLocation> getRegistries()
{
return this.keys;
}
public ImmutableList<ModRemapping> getRemaps(ResourceLocation registry)
{
return this.remaps.get(registry);
}
}
@@ -241,8 +241,7 @@ public class GameRegistry
public static void registerTileEntity(Class<? extends TileEntity> tileEntityClass, String key)
{
throw new RuntimeException("TODO: TileEntity registration");
//TileEntity.register(key, tileEntityClass);
TileEntity.register(key, tileEntityClass);
}
public static void registerFuelHandler(IFuelHandler handler)
@@ -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)