From 3b029ba031c76fb3691abbd83f85cbfa934efeca Mon Sep 17 00:00:00 2001 From: LexManos Date: Wed, 16 Jan 2019 12:28:13 -0800 Subject: [PATCH] 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. --- .../chunk/storage/AnvilChunkLoader.java.patch | 2 +- .../world/storage/SaveFormatOld.java.patch | 27 +++++++++++++ .../world/storage/SaveHandler.java.patch | 17 ++++++++ .../registries/ForgeRegistry.java | 22 +++++------ .../minecraftforge/registries/GameData.java | 39 +++++++------------ .../registries/IForgeRegistry.java | 1 + .../registries/ObjectHolderRef.java | 9 ++++- .../registries/ObjectHolderRegistry.java | 16 ++++++-- .../registries/RegistryManager.java | 2 +- src/main/resources/forge.exc | 2 + 10 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 patches/minecraft/net/minecraft/world/storage/SaveFormatOld.java.patch diff --git a/patches/minecraft/net/minecraft/world/chunk/storage/AnvilChunkLoader.java.patch b/patches/minecraft/net/minecraft/world/chunk/storage/AnvilChunkLoader.java.patch index b673a7bb06..5dc5ce6ff6 100644 --- a/patches/minecraft/net/minecraft/world/chunk/storage/AnvilChunkLoader.java.patch +++ b/patches/minecraft/net/minecraft/world/chunk/storage/AnvilChunkLoader.java.patch @@ -74,4 +74,4 @@ + public int getPendingSaveCount() { + return this.field_75828_a.size(); + } - } \ No newline at end of file + } diff --git a/patches/minecraft/net/minecraft/world/storage/SaveFormatOld.java.patch b/patches/minecraft/net/minecraft/world/storage/SaveFormatOld.java.patch new file mode 100644 index 0000000000..ffd4299c5c --- /dev/null +++ b/patches/minecraft/net/minecraft/world/storage/SaveFormatOld.java.patch @@ -0,0 +1,27 @@ +--- a/net/minecraft/world/storage/SaveFormatOld.java ++++ b/net/minecraft/world/storage/SaveFormatOld.java +@@ -86,13 +86,23 @@ + + @Nullable + public static WorldInfo func_186353_a(File p_186353_0_, DataFixer p_186353_1_) { ++ return getWorldData(p_186353_0_, p_186353_1_, null); ++ } ++ ++ @Nullable ++ public static WorldInfo getWorldData(File p_186353_0_, DataFixer p_186353_1_, @Nullable SaveHandler saveHandler) { + try { + NBTTagCompound nbttagcompound = CompressedStreamTools.func_74796_a(new FileInputStream(p_186353_0_)); + NBTTagCompound nbttagcompound1 = nbttagcompound.func_74775_l("Data"); + NBTTagCompound nbttagcompound2 = nbttagcompound1.func_150297_b("Player", 10) ? nbttagcompound1.func_74775_l("Player") : null; + nbttagcompound1.func_82580_o("Player"); + int i = nbttagcompound1.func_150297_b("DataVersion", 99) ? nbttagcompound1.func_74762_e("DataVersion") : -1; +- return new WorldInfo(NBTUtil.func_210822_a(p_186353_1_, DataFixTypes.LEVEL, nbttagcompound1, i), p_186353_1_, i, nbttagcompound2); ++ WorldInfo ret = new WorldInfo(NBTUtil.func_210822_a(p_186353_1_, DataFixTypes.LEVEL, nbttagcompound1, i), p_186353_1_, i, nbttagcompound2); ++ if (saveHandler != null) ++ net.minecraftforge.fml.WorldPersistenceHooks.handleWorldDataLoad(saveHandler, ret, nbttagcompound); ++ return ret; ++ } catch (net.minecraftforge.fml.StartupQuery.AbortedException e) { ++ throw e; + } catch (Exception exception) { + field_151479_b.error("Exception reading {}", p_186353_0_, exception); + return null; diff --git a/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch b/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch index e8f7bf824c..e6788b63b8 100644 --- a/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch +++ b/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch @@ -1,5 +1,22 @@ --- a/net/minecraft/world/storage/SaveHandler.java +++ b/net/minecraft/world/storage/SaveHandler.java +@@ -96,14 +96,14 @@ + public WorldInfo func_75757_d() { + File file1 = new File(this.field_75770_b, "level.dat"); + if (file1.exists()) { +- WorldInfo worldinfo = SaveFormatOld.func_186353_a(file1, this.field_186341_a); ++ WorldInfo worldinfo = SaveFormatOld.getWorldData(file1, this.field_186341_a, this); + if (worldinfo != null) { + return worldinfo; + } + } + + file1 = new File(this.field_75770_b, "level.dat_old"); +- return file1.exists() ? SaveFormatOld.func_186353_a(file1, this.field_186341_a) : null; ++ return file1.exists() ? SaveFormatOld.getWorldData(file1, this.field_186341_a, this) : null; + } + + public void func_75755_a(WorldInfo p_75755_1_, @Nullable NBTTagCompound p_75755_2_) { @@ -150,6 +150,7 @@ } diff --git a/src/main/java/net/minecraftforge/registries/ForgeRegistry.java b/src/main/java/net/minecraftforge/registries/ForgeRegistry.java index afd55dd299..ba0a1acc7e 100644 --- a/src/main/java/net/minecraftforge/registries/ForgeRegistry.java +++ b/src/main/java/net/minecraftforge/registries/ForgeRegistry.java @@ -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> implements IForgeRegistryInternal, IForgeRegistryModifiable { public static Marker REGISTRIES = MarkerManager.getMarker("REGISTRIES"); @@ -95,10 +85,12 @@ public class ForgeRegistry> implements IForgeRe private V defaultValue = null; boolean isFrozen = false; + private final ResourceLocation name; private final RegistryBuilder builder; - ForgeRegistry(RegistryManager stage, RegistryBuilder builder) + ForgeRegistry(RegistryManager stage, ResourceLocation name, RegistryBuilder builder) { + this.name = name; this.builder = builder; this.stage = stage; this.superType = builder.getType(); @@ -154,6 +146,12 @@ public class ForgeRegistry> implements IForgeRe }; } + @Override + public ResourceLocation getRegistryName() + { + return this.name; + } + @Override public Class getRegistrySuperType() { @@ -273,7 +271,7 @@ public class ForgeRegistry> implements IForgeRe ForgeRegistry copy(RegistryManager stage) { - return new ForgeRegistry<>(stage, builder); + return new ForgeRegistry<>(stage, name, builder); } int add(int id, V value) diff --git a/src/main/java/net/minecraftforge/registries/GameData.java b/src/main/java/net/minecraftforge/registries/GameData.java index 8fc13aef32..4f641fc54f 100644 --- a/src/main/java/net/minecraftforge/registries/GameData.java +++ b/src/main/java/net/minecraftforge/registries/GameData.java @@ -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 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) diff --git a/src/main/java/net/minecraftforge/registries/IForgeRegistry.java b/src/main/java/net/minecraftforge/registries/IForgeRegistry.java index 8a9cf14196..b5f31cdd3f 100644 --- a/src/main/java/net/minecraftforge/registries/IForgeRegistry.java +++ b/src/main/java/net/minecraftforge/registries/IForgeRegistry.java @@ -35,6 +35,7 @@ import javax.annotation.Nullable; */ public interface IForgeRegistry> extends Iterable { + ResourceLocation getRegistryName(); Class getRegistrySuperType(); void register(V value); diff --git a/src/main/java/net/minecraftforge/registries/ObjectHolderRef.java b/src/main/java/net/minecraftforge/registries/ObjectHolderRef.java index 5ded8f2fe4..425f211de5 100644 --- a/src/main/java/net/minecraftforge/registries/ObjectHolderRef.java +++ b/src/main/java/net/minecraftforge/registries/ObjectHolderRef.java @@ -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> { 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 filter) { + if (registry == null || !filter.test(registry.getRegistryName())) + return; + Object thing; if (isValid && registry.containsKey(injectedObject) && !registry.isDummied(injectedObject)) { diff --git a/src/main/java/net/minecraftforge/registries/ObjectHolderRegistry.java b/src/main/java/net/minecraftforge/registries/ObjectHolderRegistry.java index bc67eea8c2..6cd0a74659 100644 --- a/src/main/java/net/minecraftforge/registries/ObjectHolderRegistry.java +++ b/src/main/java/net/minecraftforge/registries/ObjectHolderRegistry.java @@ -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> 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> ref) { return objectHolders.remove(ref); } @@ -78,7 +81,7 @@ public class ObjectHolderRegistry //============================================================== private static final Logger LOGGER = LogManager.getLogger(); - private static final Set objectHolders = new HashSet<>(); + private static final Set>> 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 filter) + { + objectHolders.forEach(e -> e.accept(filter)); + } + } diff --git a/src/main/java/net/minecraftforge/registries/RegistryManager.java b/src/main/java/net/minecraftforge/registries/RegistryManager.java index 6b99206b6d..88ffe16d12 100644 --- a/src/main/java/net/minecraftforge/registries/RegistryManager.java +++ b/src/main/java/net/minecraftforge/registries/RegistryManager.java @@ -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 reg = new ForgeRegistry(this, builder); + ForgeRegistry reg = new ForgeRegistry(this, name, builder); registries.put(name, reg); superTypes.put(builder.getType(), name); if (builder.getSaveToDisc()) diff --git a/src/main/resources/forge.exc b/src/main/resources/forge.exc index a32e241f8f..b4ecf3b034 100644 --- a/src/main/resources/forge.exc +++ b/src/main/resources/forge.exc @@ -70,6 +70,8 @@ net/minecraft/client/renderer/block/model/BakedQuad.([IILnet/minecraft/uti #net/minecraft/client/renderer/texture/TextureMap.(Ljava/lang/String;Lnet/minecraft/client/renderer/texture/ITextureMapPopulator;Z)V=|p_i46100_1_,p_i46100_2_,skipFirst #net/minecraft/client/renderer/texture/TextureMap.loadTexture(Lnet/minecraft/client/renderer/texture/Stitcher;Lnet/minecraft/client/resources/IResourceManager;Lnet/minecraft/util/ResourceLocation;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;Lnet/minecraftforge/fml/common/ProgressManager$ProgressBar;II)I=|stitcher,p_110571_1_,location,textureatlassprite,bar,j,k +net/minecraft/world/storage/SaveFormatOld.getWorldData(Ljava/io/File;Lcom/mojang/datafixers/DataFixer;Lnet/minecraft/world/storage/SaveHandler;)Lnet/minecraft/world/storage/WorldInfo;=|p_186353_0_,p_186353_1_,saveHandler + net/minecraft/world/storage/loot/LootPool.([Lnet/minecraft/world/storage/loot/LootEntry;[Lnet/minecraft/world/storage/loot/conditions/LootCondition;Lnet/minecraft/world/storage/loot/RandomValueRange;Lnet/minecraft/world/storage/loot/RandomValueRange;Ljava/lang/String;)V=|p_i46643_1_,p_i46643_2_,p_i46643_3_,p_i46643_4_ net/minecraft/world/storage/loot/LootEntry.(II[Lnet/minecraft/world/storage/loot/conditions/LootCondition;Ljava/lang/String;)V=|p_i46642_1_,p_i46642_2_,p_i46642_3_,entryName net/minecraft/world/storage/loot/LootEntryItem.(Lnet/minecraft/item/Item;II[Lnet/minecraft/world/storage/loot/functions/LootFunction;[Lnet/minecraft/world/storage/loot/conditions/LootCondition;Ljava/lang/String;)V=|p_i46644_1_,p_i46644_2_,p_i46644_3_,p_i46644_4_,p_i46644_5_,entryName