mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 18:45:09 +00:00
Re-introduce patch for injecting registry mappings during world loading.
And re-introduce registry freezing to ONLY the Register events. Filter ObjectHolders by registry type and fire between every Register event.
This commit is contained in:
1 parent
cd21f7b927
commit
3b029ba031
10 files changed
+93
-44
No files matched your search
@@ -28,8 +28,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fml.ModThreadContext;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
@@ -39,7 +37,6 @@ import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
@@ -56,13 +53,6 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
|
||||
import net.minecraftforge.registries.IForgeRegistry.AddCallback;
|
||||
import net.minecraftforge.registries.IForgeRegistry.ClearCallback;
|
||||
import net.minecraftforge.registries.IForgeRegistry.CreateCallback;
|
||||
import net.minecraftforge.registries.IForgeRegistry.DummyFactory;
|
||||
import net.minecraftforge.registries.IForgeRegistry.MissingFactory;
|
||||
import net.minecraftforge.registries.IForgeRegistry.ValidateCallback;
|
||||
|
||||
public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRegistryInternal<V>, IForgeRegistryModifiable<V>
|
||||
{
|
||||
public static Marker REGISTRIES = MarkerManager.getMarker("REGISTRIES");
|
||||
@@ -95,10 +85,12 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
private V defaultValue = null;
|
||||
boolean isFrozen = false;
|
||||
|
||||
private final ResourceLocation name;
|
||||
private final RegistryBuilder<V> builder;
|
||||
|
||||
ForgeRegistry(RegistryManager stage, RegistryBuilder<V> builder)
|
||||
ForgeRegistry(RegistryManager stage, ResourceLocation name, RegistryBuilder<V> builder)
|
||||
{
|
||||
this.name = name;
|
||||
this.builder = builder;
|
||||
this.stage = stage;
|
||||
this.superType = builder.getType();
|
||||
@@ -154,6 +146,12 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getRegistryName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<V> getRegistrySuperType()
|
||||
{
|
||||
@@ -273,7 +271,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
|
||||
ForgeRegistry<V> copy(RegistryManager stage)
|
||||
{
|
||||
return new ForgeRegistry<>(stage, builder);
|
||||
return new ForgeRegistry<>(stage, name, builder);
|
||||
}
|
||||
|
||||
int add(int id, V value)
|
||||
|
||||
@@ -248,6 +248,7 @@ public class GameData
|
||||
// the id mapping has reverted, fire remap events for those that care about id changes
|
||||
//Loader.instance().fireRemapEvent(ImmutableMap.of(), true);
|
||||
|
||||
ObjectHolderRegistry.applyObjectHolders();
|
||||
// the id mapping has reverted, ensure we sync up the object holders
|
||||
LOGGER.debug("Frozen state restored.");
|
||||
}
|
||||
@@ -786,35 +787,25 @@ 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))
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.post(RegistryManager.ACTIVE.getRegistry(BLOCKS).getRegisterEvent(BLOCKS));
|
||||
}
|
||||
if (filter.test(ITEMS))
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.post(RegistryManager.ACTIVE.getRegistry(ITEMS).getRegisterEvent(ITEMS));
|
||||
}
|
||||
//Move Blocks to first, and Items to second.
|
||||
keys.remove(BLOCKS);
|
||||
keys.remove(ITEMS);
|
||||
|
||||
keys.add(0, BLOCKS);
|
||||
keys.add(1, ITEMS);
|
||||
|
||||
for (ResourceLocation rl : keys)
|
||||
{
|
||||
if (!filter.test(rl)) continue;
|
||||
if (rl == BLOCKS || rl == ITEMS) continue;
|
||||
MinecraftForge.EVENT_BUS.post(RegistryManager.ACTIVE.getRegistry(rl).getRegisterEvent(rl));
|
||||
ForgeRegistry<?> reg = RegistryManager.ACTIVE.getRegistry(rl);
|
||||
reg.unfreeze();
|
||||
MinecraftForge.EVENT_BUS.post(reg.getRegisterEvent(rl));
|
||||
reg.freeze();
|
||||
LOGGER.info("{} Applying holder lookups", rl.toString());
|
||||
ObjectHolderRegistry.applyObjectHolders(key -> rl.equals(key));
|
||||
LOGGER.info("{} Holder lookups applied", rl.toString());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
|
||||
if (filter.test(name))
|
||||
((ForgeRegistry<?>)reg).freeze();
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
public static ResourceLocation checkPrefix(String name)
|
||||
|
||||
@@ -35,6 +35,7 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public interface IForgeRegistry<V extends IForgeRegistryEntry<V>> extends Iterable<V>
|
||||
{
|
||||
ResourceLocation getRegistryName();
|
||||
Class<V> getRegistrySuperType();
|
||||
|
||||
void register(V value);
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.ResourceLocationException;
|
||||
@@ -34,7 +36,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class ObjectHolderRef implements Runnable
|
||||
public class ObjectHolderRef implements Consumer<Predicate<ResourceLocation>>
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private Field field;
|
||||
@@ -132,8 +134,11 @@ public class ObjectHolderRef implements Runnable
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
public void accept(Predicate<ResourceLocation> filter)
|
||||
{
|
||||
if (registry == null || !filter.test(registry.getRegistryName()))
|
||||
return;
|
||||
|
||||
Object thing;
|
||||
if (isValid && registry.containsKey(injectedObject) && !registry.isDummied(injectedObject))
|
||||
{
|
||||
|
||||
@@ -27,8 +27,11 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.forgespi.language.ModFileScanData;
|
||||
@@ -54,7 +57,7 @@ public class ObjectHolderRegistry
|
||||
* and hashCode function to de-duplicate callers here.
|
||||
* The default @ObjectHolder implementation uses the hashCode/equals for the field the annotation is on.
|
||||
*/
|
||||
public static void addHandler(Runnable ref)
|
||||
public static void addHandler(Consumer<Predicate<ResourceLocation>> ref)
|
||||
{
|
||||
objectHolders.add(ref);
|
||||
}
|
||||
@@ -68,7 +71,7 @@ public class ObjectHolderRegistry
|
||||
*
|
||||
* @return true if handler was matched and removed.
|
||||
*/
|
||||
public static boolean removeHandler(Runnable ref)
|
||||
public static boolean removeHandler(Consumer<Predicate<ResourceLocation>> ref)
|
||||
{
|
||||
return objectHolders.remove(ref);
|
||||
}
|
||||
@@ -78,7 +81,7 @@ public class ObjectHolderRegistry
|
||||
//==============================================================
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Set<Runnable> objectHolders = new HashSet<>();
|
||||
private static final Set<Consumer<Predicate<ResourceLocation>>> objectHolders = new HashSet<>();
|
||||
private static final Type OBJECT_HOLDER = Type.getType(ObjectHolder.class);
|
||||
private static final Type MOD = Type.getType(Mod.class);
|
||||
|
||||
@@ -174,8 +177,13 @@ public class ObjectHolderRegistry
|
||||
public static void applyObjectHolders()
|
||||
{
|
||||
LOGGER.info("Applying holder lookups");
|
||||
objectHolders.forEach(Runnable::run);
|
||||
applyObjectHolders(key -> true);
|
||||
LOGGER.info("Holder lookups applied");
|
||||
}
|
||||
|
||||
public static void applyObjectHolders(Predicate<ResourceLocation> filter)
|
||||
{
|
||||
objectHolders.forEach(e -> e.accept(filter));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class RegistryManager
|
||||
foundType, superTypes.get(foundType), name, builder.getType(), builder.getType());
|
||||
throw new IllegalArgumentException("Duplicate registry parent type found - you can only have one registry for a particular super type");
|
||||
}
|
||||
ForgeRegistry<V> reg = new ForgeRegistry<V>(this, builder);
|
||||
ForgeRegistry<V> reg = new ForgeRegistry<V>(this, name, builder);
|
||||
registries.put(name, reg);
|
||||
superTypes.put(builder.getType(), name);
|
||||
if (builder.getSaveToDisc())
|
||||
|
||||
Reference in new issue
Block a user