diff --git a/src/main/java/net/minecraftforge/fml/common/FMLContainer.java b/src/main/java/net/minecraftforge/fml/common/FMLContainer.java index 8f09c1c4ec..2d7794b07e 100644 --- a/src/main/java/net/minecraftforge/fml/common/FMLContainer.java +++ b/src/main/java/net/minecraftforge/fml/common/FMLContainer.java @@ -23,7 +23,6 @@ import java.io.File; import java.security.cert.Certificate; import java.util.Arrays; import java.util.Map; -import java.util.Map.Entry; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -44,7 +43,6 @@ import net.minecraftforge.registries.ForgeRegistry; import net.minecraftforge.registries.GameData; import net.minecraftforge.registries.RegistryManager; -import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import com.google.common.collect.Maps; @@ -127,54 +125,7 @@ public final class FMLContainer extends DummyModContainer implements WorldAccess for (Map.Entry e : RegistryManager.ACTIVE.takeSnapshot(true).entrySet()) { - NBTTagCompound data = new NBTTagCompound(); - registries.setTag(e.getKey().toString(), data); - - NBTTagList ids = new NBTTagList(); - for (Entry item : e.getValue().ids.entrySet()) - { - NBTTagCompound tag = new NBTTagCompound(); - tag.setString("K", item.getKey().toString()); - tag.setInteger("V", item.getValue()); - ids.appendTag(tag); - } - data.setTag("ids", ids); - - NBTTagList aliases = new NBTTagList(); - for (Entry entry : e.getValue().aliases.entrySet()) - { - NBTTagCompound tag = new NBTTagCompound(); - tag.setString("K", entry.getKey().toString()); - tag.setString("V", entry.getValue().toString()); - aliases.appendTag(tag); - } - data.setTag("aliases", aliases); - - NBTTagList overrides = new NBTTagList(); - for (Entry entry : e.getValue().overrides.entrySet()) - { - NBTTagCompound tag = new NBTTagCompound(); - tag.setString("K", entry.getKey().toString()); - tag.setString("V", entry.getValue().toString()); - overrides.appendTag(tag); - } - data.setTag("overrides", overrides); - - int[] blocked = new int[e.getValue().blocked.size()]; - int idx = 0; - for (Integer i : e.getValue().blocked) - { - blocked[idx++] = i; - } - data.setIntArray("blocked", blocked); - NBTTagList dummied = new NBTTagList(); - for (ResourceLocation entry : e.getValue().dummied) - { - NBTTagCompound tag = new NBTTagCompound(); - tag.setString("K", entry.toString()); - dummied.appendTag(tag); - } - data.setTag("dummied", dummied); + registries.setTag(e.getKey().toString(), e.getValue().write()); } return fmlData; } @@ -216,54 +167,7 @@ public final class FMLContainer extends DummyModContainer implements WorldAccess NBTTagCompound regs = tag.getCompoundTag("Registries"); for (String key : regs.getKeySet()) { - ForgeRegistry.Snapshot entry = new ForgeRegistry.Snapshot(); - NBTTagCompound ent = regs.getCompoundTag(key); - snapshot.put(new ResourceLocation(key), entry); - - NBTTagList list = ent.getTagList("ids", 10); - for (int x = 0; x < list.tagCount(); x++) - { - NBTTagCompound e = list.getCompoundTagAt(x); - entry.ids.put(new ResourceLocation(e.getString("K")), e.getInteger("V")); - } - - list = ent.getTagList("aliases", 10); - for (int x = 0; x < list.tagCount(); x++) - { - NBTTagCompound e = list.getCompoundTagAt(x); - String v = e.getString("V"); - if (v.indexOf(':') == -1) //Forge Bug: https://github.com/MinecraftForge/MinecraftForge/issues/4894 TODO: Remove in 1.13 - { - entry.overrides.put(new ResourceLocation(e.getString("K")), v); - } - else - { - entry.aliases.put(new ResourceLocation(e.getString("K")), new ResourceLocation(v)); - } - } - - list = ent.getTagList("overrides", 10); - for (int x = 0; x < list.tagCount(); x++) - { - NBTTagCompound e = list.getCompoundTagAt(x); - entry.overrides.put(new ResourceLocation(e.getString("K")), e.getString("V")); - } - - int[] blocked = regs.getCompoundTag(key).getIntArray("blocked"); - for (int i : blocked) - { - entry.blocked.add(i); - } - - if (regs.getCompoundTag(key).hasKey("dummied")) // Added in 1.8.9 dev, some worlds may not have it. - { - list = regs.getCompoundTag(key).getTagList("dummied",10); - for (int x = 0; x < list.tagCount(); x++) - { - NBTTagCompound e = list.getCompoundTagAt(x); - entry.dummied.add(new ResourceLocation(e.getString("K"))); - } - } + snapshot.put(new ResourceLocation(key), ForgeRegistry.Snapshot.read(regs.getCompoundTag(key))); } failedElements = GameData.injectSnapshot(snapshot, true, true); } diff --git a/src/main/java/net/minecraftforge/registries/ForgeRegistry.java b/src/main/java/net/minecraftforge/registries/ForgeRegistry.java index bfb7dc3962..2cf727843d 100644 --- a/src/main/java/net/minecraftforge/registries/ForgeRegistry.java +++ b/src/main/java/net/minecraftforge/registries/ForgeRegistry.java @@ -43,6 +43,10 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.NBTTagString; import net.minecraft.util.ResourceLocation; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent.MissingMappings; @@ -781,6 +785,102 @@ public class ForgeRegistry> implements IForgeRe public final Set blocked = Sets.newHashSet(); public final Set dummied = Sets.newHashSet(); public final Map overrides = Maps.newHashMap(); + + public NBTTagCompound write() + { + NBTTagCompound data = new NBTTagCompound(); + + NBTTagList ids = new NBTTagList(); + this.ids.entrySet().stream().sorted((o1, o2) -> o1.getKey().compareTo(o2.getKey())).forEach(e -> + { + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("K", e.getKey().toString()); + tag.setInteger("V", e.getValue()); + ids.appendTag(tag); + }); + data.setTag("ids", ids); + + NBTTagList aliases = new NBTTagList(); + this.aliases.entrySet().stream().sorted((o1, o2) -> o1.getKey().compareTo(o2.getKey())).forEach(e -> + { + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("K", e.getKey().toString()); + tag.setString("V", e.getKey().toString()); + aliases.appendTag(tag); + }); + data.setTag("aliases", aliases); + + NBTTagList overrides = new NBTTagList(); + this.overrides.entrySet().stream().sorted((o1, o2) -> o1.getKey().compareTo(o2.getKey())).forEach(e -> + { + NBTTagCompound tag = new NBTTagCompound(); + tag.setString("K", e.getKey().toString()); + tag.setString("V", e.getValue()); + overrides.appendTag(tag); + }); + data.setTag("overrides", overrides); + + int[] blocked = this.blocked.stream().mapToInt(x->x).sorted().toArray(); + data.setIntArray("blocked", blocked); + + NBTTagList dummied = new NBTTagList(); + this.dummied.stream().sorted().forEach(e -> dummied.appendTag(new NBTTagString(e.toString()))); + data.setTag("dummied", dummied); + + return data; + } + + public static Snapshot read(NBTTagCompound nbt) + { + Snapshot ret = new Snapshot(); + if (nbt == null) + { + return ret; + } + + NBTTagList list = nbt.getTagList("ids", 10); + list.forEach(e -> + { + NBTTagCompound comp = (NBTTagCompound)e; + ret.ids.put(new ResourceLocation(comp.getString("K")), comp.getInteger("V")); + }); + + list = nbt.getTagList("aliases", 10); + list.forEach(e -> + { + NBTTagCompound comp = (NBTTagCompound)e; + String v = comp.getString("V"); + if (v.indexOf(':') == -1) //Forge Bug: https://github.com/MinecraftForge/MinecraftForge/issues/4894 TODO: Remove in 1.13 + { + ret.overrides.put(new ResourceLocation(comp.getString("K")), v); + } + else + { + ret.aliases.put(new ResourceLocation(comp.getString("K")), new ResourceLocation(v)); + } + }); + + list = nbt.getTagList("overrides", 10); + list.forEach(e -> + { + NBTTagCompound comp = (NBTTagCompound)e; + ret.overrides.put(new ResourceLocation(comp.getString("K")), comp.getString("V")); + }); + + int[] blocked = nbt.getIntArray("blocked"); + for (int i : blocked) + { + ret.blocked.add(i); + } + + list = nbt.getTagList("dummied", 10); //10 - NBTTagCompound, Old format. New format is String list. For now we will just merge the old and new. TODO: Remove in 1.13 + list.forEach(e -> ret.dummied.add(new ResourceLocation(((NBTTagCompound)e).getString("K")))); + + list = nbt.getTagList("dummied", 8); //8 - NBTTagString, New format, less redundant/verbose + list.forEach(e -> ret.dummied.add(new ResourceLocation(((NBTTagString)e).getString()))); + + return ret; + } } public MissingMappings getMissingEvent(ResourceLocation name, Map map)