Skip to content

Commit

Permalink
Fix Data Fixers not Applying on Chunks after First Load
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Mar 30, 2024
1 parent b839785 commit d85dd91
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static void loadDataFixers(File file, DataFixer fixer, SaveHandler save,

DataFixerHandler.onWorldLoad(save);

if (DataFixerHandler.fixNotAvailable()) return;
if (DataFixerHandler.hasNoNewFixes()) return;

if (Loader.isModLoaded(LabsValues.ENDER_STORAGE_MODID) && LabsConfig.modIntegration.enableEnderStorageIntegration)
DataFixerHandler.processEnderStorageInfo(fixer, save);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ public class DataFixerHandler {
public static LabsWorldFixData worldSavedData = null;
public static boolean checked = false;

/* Fixes that should be applied */
public static Map<IFixType, List<DataFix<?>>> neededFixes;

/* Fixes that should be logged */
public static Map<IFixType, List<DataFix<?>>> neededNewFixes;

/* Whether Mode is needed for New Fixes */
public static boolean modeNeeded = false;

private static Map<String, String> mods;
private static BiMap<Integer, ResourceLocation> blockHelperMap;

public static void preInit() {
Expand All @@ -81,8 +88,24 @@ public static void preInit() {
public static void onWorldLoad(SaveHandler save) {
checked = false;
modeNeeded = false;
neededFixes = null;
neededNewFixes = null;
NomiLabs.LOGGER.info("Checking Data Fixers...");

getModList(save);
if (mods.isEmpty()) return;

LabsFixes.init();
neededFixes = new Object2ObjectOpenHashMap<>();
for (var fixType : LabsFixes.fixes.keySet()) {
for (var fix : LabsFixes.fixes.get(fixType)) {
if (fix.validModList.apply(mods)) {
if (!neededFixes.containsKey(fixType)) neededFixes.put(fixType, new ObjectArrayList<>());
neededFixes.get(fixType).add(fix);
}
}
}

var mapFile = save.getMapFileFromName(LabsFixes.DATA_NAME);

if (mapFile.exists()) {
Expand All @@ -100,14 +123,13 @@ public static void onWorldLoad(SaveHandler save) {
NomiLabs.LOGGER.info("This world was saved without a data version. New Version: {}.", LabsFixes.CURRENT);
}

LabsFixes.init();
determineNeededFixesAndLog(save);
determineNeededFixesAndLog();

// Clear Block Helper Map, the ids are different for some saves
blockHelperMap = null;

if (neededFixes.isEmpty()) {
NomiLabs.LOGGER.info("This world does not need any data fixers, but it has no saved version, it is old, or this is a new world.");
if (neededNewFixes.isEmpty()) {
NomiLabs.LOGGER.info("This world does not need any new data fixers, but it has no saved version, it is old, or this is a new world.");
LabsWorldFixData.save(mapFile, DataFixerHandler.worldSavedData);
return;
}
Expand Down Expand Up @@ -138,8 +160,9 @@ public static void onWorldLoad(SaveHandler save) {
LabsWorldFixData.save(mapFile, DataFixerHandler.worldSavedData);
}

private static void determineNeededFixesAndLog(SaveHandler save) {
neededFixes = new Object2ObjectOpenHashMap<>();
private static void getModList(SaveHandler save) {
mods = new HashMap<>();

File levelDat = new File(save.getWorldDirectory(), "level.dat");

// If level.dat file does not exist, return.
Expand All @@ -149,7 +172,6 @@ private static void determineNeededFixesAndLog(SaveHandler save) {
if (!levelDat.exists())
return;

Map<String, String> mods = new HashMap<>();
NBTTagList modList;
try {
NBTTagCompound nbt = CompressedStreamTools.readCompressed(new FileInputStream(levelDat));
Expand All @@ -161,13 +183,18 @@ private static void determineNeededFixesAndLog(SaveHandler save) {
NomiLabs.LOGGER.fatal("Failed to read level.dat.", e);
return;
}

for (var mod : modList) {
if (!(mod instanceof NBTTagCompound compound)) continue;
if (!compound.hasKey("ModId", Constants.NBT.TAG_STRING) || !compound.hasKey("ModVersion", Constants.NBT.TAG_STRING))
continue;
mods.put(compound.getString("ModId"), compound.getString("ModVersion"));
}
}

private static void determineNeededFixesAndLog() {
neededNewFixes = new Object2ObjectOpenHashMap<>();

if (mods.isEmpty()) return;

// If Nomi Labs Version is same as current version, exit.
// This normally means it is a new world.
Expand All @@ -184,8 +211,8 @@ private static void determineNeededFixesAndLog(SaveHandler save) {
var fixes = LabsFixes.fixes.get(fixType);
for (var fix : fixes) {
if (fix.validVersion.apply(DataFixerHandler.worldSavedData.savedVersion) && fix.validModList.apply(mods)) {
if (!neededFixes.containsKey(fixType)) neededFixes.put(fixType, new ObjectArrayList<>());
neededFixes.get(fixType).add(fix);
if (!neededNewFixes.containsKey(fixType)) neededNewFixes.put(fixType, new ObjectArrayList<>());
neededNewFixes.get(fixType).add(fix);
if (fix.needsMode) modeNeeded = true;
NomiLabs.LOGGER.info("- {}: {}", fix.name, fix.description);
}
Expand Down Expand Up @@ -236,12 +263,18 @@ public static void processEnderStorageInfo(DataFixer fixer, SaveHandler save) {
NomiLabs.LOGGER.info("Finished Processing Ender Storage Info!");
}

public static boolean hasNoNewFixes() {
return worldSavedData == null || neededNewFixes == null || neededNewFixes.isEmpty();
}

public static boolean fixNotAvailable() {
return worldSavedData == null;
return neededFixes == null || neededFixes.isEmpty();
}

public static void close() {
worldSavedData = null;
checked = false;
neededFixes = null;
neededNewFixes = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
/**
* Definitions for all values, and all data fixes.
* <p>
* No Data Fixes are loaded if the 'FIX_VERSION' is below the version saved in the world, or the Nomi Labs Version in world
* is the same as the current Nomi Labs Version.
* When the modList matches, and the fix version the world was created with matches the fix, the fix is included.
* This is because items and blocks may not be loaded in the first load.
* However, only fixes where the previous version the world was loaded with matches will be included in Ender Storage Remapping.
*/
public class LabsFixes {
/**
Expand Down Expand Up @@ -133,7 +134,7 @@ public static void init() {
* false, // Whether the correct mode is required for the change to work right
*
* (version) -> version <= DEFAULT_VERSION, // Whether the previous version in the save means that this fix must be applied.
* // Note that this is not applied if the previous version is equal to the current overall fix version.
* // Note that this is not included in the fix list if the previous version is equal to the current overall fix version.
*
* (modList) -> true, // Inputs the previous modlist the world was loaded with (map of modid to modversion), return whether it is valid.
* // Note that the fix is only applied if the version AND the modlist is valid.
Expand Down Expand Up @@ -284,7 +285,7 @@ public static void init() {
* false, // Whether the correct mode is required for the change to work right
*
* (version) -> version <= DEFAULT_VERSION, // Whether the previous version in the save means that this fix must be applied.
* // Note that this is not applied if the previous version is equal to the current overall fix version.
* // Note that this is not included in the fix list if the previous version is equal to the current overall fix version.
*
* (modList) -> true, // Inputs the previous modlist the world was loaded with (map of modid to modversion), return whether it is valid.
* // Note that the fix is only applied if the version AND the modlist is valid.
Expand Down Expand Up @@ -342,7 +343,7 @@ public static void init() {
* false, // Whether the correct mode is required for the change to work right
*
* (version) -> version <= DEFAULT_VERSION, // Whether the previous version in the save means that this fix must be applied.
* // Note that this is not applied if the previous version is equal to the current overall fix version.
* // Note that this is not included in the fix list if the previous version is equal to the current overall fix version.
*
* (modList) -> true, // Inputs the previous modlist the world was loaded with (map of modid to modversion), return whether it is valid.
* // Note that the fix is only applied if the version AND the modlist is valid.
Expand Down

0 comments on commit d85dd91

Please sign in to comment.