Move registry snapshot read/write code to Snapshot class. Cleaner code.

This commit is contained in:
LexManos committed 2018-04-29 17:58:02 -07:00
1 parent 4df55f3bd3
commit f2fa21e6e2
2 files changed
+102 -98

No files matched your search

@@ -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<ResourceLocation, ForgeRegistry.Snapshot> e : RegistryManager.ACTIVE.takeSnapshot(true).entrySet())
{
NBTTagCompound data = new NBTTagCompound();
registries.setTag(e.getKey().toString(), data);
NBTTagList ids = new NBTTagList();
for (Entry<ResourceLocation, Integer> 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<ResourceLocation, ResourceLocation> 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<ResourceLocation, String> 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);
}
@@ -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<V extends IForgeRegistryEntry<V>> implements IForgeRe
public final Set<Integer> blocked = Sets.newHashSet();
public final Set<ResourceLocation> dummied = Sets.newHashSet();
public final Map<ResourceLocation, String> 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<ResourceLocation, Integer> map)