mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 08:17:35 +00:00
Rework world persistence hooks to fix the double registry injection when loading single player worlds. (#8234)
This commit is contained in:
1 parent
63a42bad98
commit
15d3df2818
6 files changed
+127
-222
No files matched your search
@@ -29,6 +29,8 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
@@ -62,6 +64,7 @@ import net.minecraft.world.entity.EquipmentSlot;
|
||||
import net.minecraft.world.item.*;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.storage.WorldData;
|
||||
import net.minecraft.world.level.storage.loot.LootContext;
|
||||
import net.minecraft.world.level.storage.loot.LootTable;
|
||||
import net.minecraft.world.level.storage.loot.LootTables;
|
||||
@@ -116,6 +119,7 @@ import net.minecraftforge.common.loot.IGlobalLootModifier;
|
||||
import net.minecraftforge.common.loot.LootModifierManager;
|
||||
import net.minecraftforge.common.loot.LootTableIdCondition;
|
||||
import net.minecraftforge.common.util.BlockSnapshot;
|
||||
import net.minecraftforge.common.util.MavenVersionStringHelper;
|
||||
import net.minecraftforge.common.world.BiomeGenerationSettingsBuilder;
|
||||
import net.minecraftforge.common.world.ForgeWorldPreset;
|
||||
import net.minecraftforge.common.world.MobSpawnSettingsBuilder;
|
||||
@@ -152,6 +156,8 @@ import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.event.world.NoteBlockEvent;
|
||||
import net.minecraftforge.eventbus.api.Event.Result;
|
||||
import net.minecraftforge.fluids.FluidAttributes;
|
||||
import net.minecraftforge.fml.ModContainer;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.resource.ResourcePackLoader;
|
||||
import net.minecraftforge.registries.DataSerializerEntry;
|
||||
@@ -159,6 +165,7 @@ import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.ForgeRegistry;
|
||||
import net.minecraftforge.registries.GameData;
|
||||
import net.minecraftforge.registries.IRegistryDelegate;
|
||||
import net.minecraftforge.registries.RegistryManager;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -189,6 +196,7 @@ public class ForgeHooks
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
@SuppressWarnings("unused")
|
||||
private static final Marker FORGEHOOKS = MarkerManager.getMarker("FORGEHOOKS");
|
||||
private static final Marker WORLDPERSISTENCE = MarkerManager.getMarker("WP");
|
||||
|
||||
public static boolean canContinueUsing(@Nonnull ItemStack from, @Nonnull ItemStack to)
|
||||
{
|
||||
@@ -1242,4 +1250,86 @@ public class ForgeHooks
|
||||
MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringSection(entity, packedOldPos, packedNewPos));
|
||||
}
|
||||
|
||||
public static void writeAdditionalLevelSaveData(WorldData worldData, CompoundTag levelTag)
|
||||
{
|
||||
CompoundTag fmlData = new CompoundTag();
|
||||
ListTag modList = new ListTag();
|
||||
ModList.get().getMods().forEach(mi ->
|
||||
{
|
||||
final CompoundTag mod = new CompoundTag();
|
||||
mod.putString("ModId", mi.getModId());
|
||||
mod.putString("ModVersion", MavenVersionStringHelper.artifactVersionToString(mi.getVersion()));
|
||||
modList.add(mod);
|
||||
});
|
||||
fmlData.put("LoadingModList", modList);
|
||||
|
||||
CompoundTag registries = new CompoundTag();
|
||||
fmlData.put("Registries", registries);
|
||||
LOGGER.debug(WORLDPERSISTENCE, "Gathering id map for writing to world save {}", worldData.getLevelName());
|
||||
|
||||
for (Map.Entry<ResourceLocation, ForgeRegistry.Snapshot> e : RegistryManager.ACTIVE.takeSnapshot(true).entrySet())
|
||||
{
|
||||
registries.put(e.getKey().toString(), e.getValue().write());
|
||||
}
|
||||
LOGGER.debug(WORLDPERSISTENCE, "ID Map collection complete {}", worldData.getLevelName());
|
||||
levelTag.put("fml", fmlData);
|
||||
}
|
||||
|
||||
public static void readAdditionalLevelSaveData(CompoundTag rootTag)
|
||||
{
|
||||
CompoundTag tag = rootTag.getCompound("fml");
|
||||
if (tag.contains("LoadingModList"))
|
||||
{
|
||||
ListTag modList = tag.getList("LoadingModList", net.minecraft.nbt.Tag.TAG_COMPOUND);
|
||||
for (int i = 0; i < modList.size(); i++)
|
||||
{
|
||||
CompoundTag mod = modList.getCompound(i);
|
||||
String modId = mod.getString("ModId");
|
||||
if (Objects.equals("minecraft", modId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
String modVersion = mod.getString("ModVersion");
|
||||
Optional<? extends ModContainer> container = ModList.get().getModContainerById(modId);
|
||||
if (container.isEmpty())
|
||||
{
|
||||
LOGGER.error(WORLDPERSISTENCE,"This world was saved with mod {} which appears to be missing, things may not work well", modId);
|
||||
continue;
|
||||
}
|
||||
if (!Objects.equals(modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion())))
|
||||
{
|
||||
LOGGER.warn(WORLDPERSISTENCE,"This world was saved with mod {} version {} and it is now at version {}, things may not work well", modId, modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Multimap<ResourceLocation, ResourceLocation> failedElements = null;
|
||||
|
||||
if (tag.contains("Registries"))
|
||||
{
|
||||
Map<ResourceLocation, ForgeRegistry.Snapshot> snapshot = new HashMap<>();
|
||||
CompoundTag regs = tag.getCompound("Registries");
|
||||
for (String key : regs.getAllKeys())
|
||||
{
|
||||
snapshot.put(new ResourceLocation(key), ForgeRegistry.Snapshot.read(regs.getCompound(key)));
|
||||
}
|
||||
failedElements = GameData.injectSnapshot(snapshot, true, true);
|
||||
}
|
||||
|
||||
if (failedElements != null && !failedElements.isEmpty())
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("Forge Mod Loader could not load this save.\n\n")
|
||||
.append("There are ").append(failedElements.size()).append(" unassigned registry entries in this save.\n")
|
||||
.append("You will not be able to load until they are present again.\n\n");
|
||||
|
||||
failedElements.asMap().forEach((name, entries) ->
|
||||
{
|
||||
buf.append("Missing ").append(name).append(":\n");
|
||||
entries.forEach(rl -> buf.append(" ").append(rl).append("\n"));
|
||||
});
|
||||
LOGGER.error(WORLDPERSISTENCE, buf.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,7 +50,6 @@ import net.minecraftforge.fml.*;
|
||||
import net.minecraftforge.fml.config.ModConfig;
|
||||
import net.minecraftforge.fml.event.lifecycle.*;
|
||||
import net.minecraftforge.registries.*;
|
||||
import net.minecraftforge.internal.WorldPersistenceHooks;
|
||||
import net.minecraftforge.network.NetworkConstants;
|
||||
import net.minecraftforge.event.server.ServerStoppingEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
@@ -98,7 +97,7 @@ import org.apache.logging.log4j.MarkerManager;
|
||||
import java.util.*;
|
||||
|
||||
@Mod("forge")
|
||||
public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
|
||||
public class ForgeMod
|
||||
{
|
||||
public static final String VERSION_CHECK_CAT = "version_checking";
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
@@ -145,8 +144,6 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
|
||||
CrashReportCallables.registerCrashCallable("FML", ForgeVersion::getSpec);
|
||||
CrashReportCallables.registerCrashCallable("Forge", ()->ForgeVersion.getGroup()+":"+ForgeVersion.getVersion());
|
||||
|
||||
WorldPersistenceHooks.addHook(this);
|
||||
WorldPersistenceHooks.addHook(new FMLWorldPersistenceHook());
|
||||
final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
modEventBus.addListener(this::registerCapabilities);
|
||||
modEventBus.addListener(this::preInit);
|
||||
@@ -203,38 +200,11 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
|
||||
WorldWorkerManager.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag getDataForWriting(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo)
|
||||
{
|
||||
CompoundTag forgeData = new CompoundTag();
|
||||
CompoundTag dims = new CompoundTag();
|
||||
//TODO Dimensions
|
||||
// DimensionManager.writeRegistry(dims);
|
||||
if (!dims.isEmpty())
|
||||
forgeData.put("dims", dims);
|
||||
return forgeData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readData(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo, CompoundTag tag)
|
||||
{
|
||||
//TODO Dimensions
|
||||
// if (tag.contains("dims", 10))
|
||||
// DimensionManager.readRegistry(tag.getCompound("dims"));
|
||||
// DimensionManager.processScheduledDeletions(levelSave);
|
||||
}
|
||||
|
||||
public void mappingChanged(RegistryEvent.IdMappingEvent evt)
|
||||
{
|
||||
Ingredient.invalidateAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getModId()
|
||||
{
|
||||
return ForgeVersion.MOD_ID;
|
||||
}
|
||||
|
||||
public void gatherData(GatherDataEvent event)
|
||||
{
|
||||
DataGenerator gen = event.getGenerator();
|
||||
@@ -312,98 +282,4 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
|
||||
Registry.register(Registry.LOOT_CONDITION_TYPE, new ResourceLocation("forge:loot_table_id"), LootTableIdCondition.LOOT_TABLE_ID);
|
||||
Registry.register(Registry.LOOT_CONDITION_TYPE, new ResourceLocation("forge:can_tool_perform_action"), CanToolPerformAction.LOOT_CONDITION_TYPE);
|
||||
}
|
||||
|
||||
private static final class FMLWorldPersistenceHook implements WorldPersistenceHooks.WorldPersistenceHook
|
||||
{
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Marker WORLDPERSISTENCE = MarkerManager.getMarker("WP");
|
||||
|
||||
@Override
|
||||
public String getModId()
|
||||
{
|
||||
return "fml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag getDataForWriting(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo)
|
||||
{
|
||||
CompoundTag fmlData = new CompoundTag();
|
||||
ListTag modList = new ListTag();
|
||||
ModList.get().getMods().forEach(mi->
|
||||
{
|
||||
final CompoundTag mod = new CompoundTag();
|
||||
mod.putString("ModId", mi.getModId());
|
||||
mod.putString("ModVersion", MavenVersionStringHelper.artifactVersionToString(mi.getVersion()));
|
||||
modList.add(mod);
|
||||
});
|
||||
fmlData.put("LoadingModList", modList);
|
||||
|
||||
CompoundTag registries = new CompoundTag();
|
||||
fmlData.put("Registries", registries);
|
||||
LOGGER.debug(WORLDPERSISTENCE,"Gathering id map for writing to world save {}", serverInfo.getLevelName());
|
||||
|
||||
for (Map.Entry<ResourceLocation, ForgeRegistry.Snapshot> e : RegistryManager.ACTIVE.takeSnapshot(true).entrySet())
|
||||
{
|
||||
registries.put(e.getKey().toString(), e.getValue().write());
|
||||
}
|
||||
LOGGER.debug(WORLDPERSISTENCE,"ID Map collection complete {}", serverInfo.getLevelName());
|
||||
return fmlData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readData(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo, CompoundTag tag)
|
||||
{
|
||||
if (tag.contains("LoadingModList"))
|
||||
{
|
||||
ListTag modList = tag.getList("LoadingModList", (byte)10);
|
||||
for (int i = 0; i < modList.size(); i++)
|
||||
{
|
||||
CompoundTag mod = modList.getCompound(i);
|
||||
String modId = mod.getString("ModId");
|
||||
if (Objects.equals("minecraft", modId)) {
|
||||
continue;
|
||||
}
|
||||
String modVersion = mod.getString("ModVersion");
|
||||
Optional<? extends ModContainer> container = ModList.get().getModContainerById(modId);
|
||||
if (!container.isPresent())
|
||||
{
|
||||
LOGGER.error(WORLDPERSISTENCE,"This world was saved with mod {} which appears to be missing, things may not work well", modId);
|
||||
continue;
|
||||
}
|
||||
if (!Objects.equals(modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion())))
|
||||
{
|
||||
LOGGER.warn(WORLDPERSISTENCE,"This world was saved with mod {} version {} and it is now at version {}, things may not work well", modId, modVersion, MavenVersionStringHelper.artifactVersionToString(container.get().getModInfo().getVersion()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Multimap<ResourceLocation, ResourceLocation> failedElements = null;
|
||||
|
||||
if (tag.contains("Registries")) // 1.8, genericed out the 'registries' list
|
||||
{
|
||||
Map<ResourceLocation, ForgeRegistry.Snapshot> snapshot = new HashMap<>();
|
||||
CompoundTag regs = tag.getCompound("Registries");
|
||||
for (String key : regs.getAllKeys())
|
||||
{
|
||||
snapshot.put(new ResourceLocation(key), ForgeRegistry.Snapshot.read(regs.getCompound(key)));
|
||||
}
|
||||
failedElements = GameData.injectSnapshot(snapshot, true, true);
|
||||
}
|
||||
|
||||
if (failedElements != null && !failedElements.isEmpty())
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("Forge Mod Loader could not load this save.\n\n")
|
||||
.append("There are ").append(failedElements.size()).append(" unassigned registry entries in this save.\n")
|
||||
.append("You will not be able to load until they are present again.\n\n");
|
||||
|
||||
failedElements.asMap().forEach((name, entries) ->
|
||||
{
|
||||
buf.append("Missing ").append(name).append(":\n");
|
||||
entries.forEach(rl -> buf.append(" ").append(rl).append("\n"));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.internal;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.level.storage.WorldData;
|
||||
import net.minecraft.world.level.storage.LevelStorageSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WorldPersistenceHooks
|
||||
{
|
||||
private static List<WorldPersistenceHook> worldPersistenceHooks = new ArrayList<>();
|
||||
|
||||
public static void addHook(WorldPersistenceHook hook) {
|
||||
worldPersistenceHooks.add(hook);
|
||||
}
|
||||
|
||||
public static void handleWorldDataSave(final LevelStorageSource.LevelStorageAccess levelSave, final WorldData serverInfo, final CompoundTag tagCompound)
|
||||
{
|
||||
worldPersistenceHooks.forEach(wac->tagCompound.put(wac.getModId(), wac.getDataForWriting(levelSave, serverInfo)));
|
||||
}
|
||||
|
||||
public static void handleWorldDataLoad(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo, CompoundTag tagCompound)
|
||||
{
|
||||
worldPersistenceHooks.forEach(wac->wac.readData(levelSave, serverInfo, tagCompound.getCompound(wac.getModId())));
|
||||
}
|
||||
|
||||
public interface WorldPersistenceHook
|
||||
{
|
||||
String getModId();
|
||||
CompoundTag getDataForWriting(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo);
|
||||
void readData(LevelStorageSource.LevelStorageAccess levelSave, WorldData serverInfo, CompoundTag tag);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user