Aded optional Biome repair Fixer

This commit is contained in:
Frank 2021-12-10 16:46:02 +01:00
parent 7439286ae2
commit 330530de73

View file

@ -1,8 +1,12 @@
package ru.bclib;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.api.datafixer.DataFixerAPI;
import ru.bclib.api.datafixer.ForcedLevelPatch;
import ru.bclib.api.datafixer.MigrationProfile;
import ru.bclib.api.datafixer.Patch;
import ru.bclib.config.Configs;
import ru.bclib.interfaces.PatchFunction;
@ -10,6 +14,49 @@ import ru.bclib.interfaces.PatchFunction;
public final class BCLibPatch {
public static void register(){
DataFixerAPI.registerPatch(BiomeSourcePatch::new);
if (Configs.MAIN_CONFIG.getBoolean("data_fixer", "repairBiomesOnLoad", false)) {
DataFixerAPI.registerPatch(BiomeFixPatch::new);
}
}
}
final class BiomeFixPatch extends ForcedLevelPatch{
protected BiomeFixPatch() {
super(BCLib.MOD_ID, "0.5.0");
}
@Override
protected Boolean runLevelDatPatch(CompoundTag root, MigrationProfile profile) {
CompoundTag worldGenSettings = root.getCompound("Data").getCompound("WorldGenSettings");
CompoundTag dimensions = worldGenSettings.getCompound("dimensions");
long seed = worldGenSettings.getLong("seed");
boolean result = false;
if (!dimensions.contains("minecraft:the_nether")) {
BCLib.LOGGER.info("Repairing Nether biome source");
result = true;
CompoundTag dimRoot = new CompoundTag();
dimRoot.put("generator", BiomeSourcePatch.makeNetherGenerator(seed));
dimRoot.putString("type", "minecraft:the_nether");
dimensions.put("minecraft:the_nether", dimRoot);
} else {
result |= BiomeSourcePatch.repairNetherSource(dimensions, seed);
}
if (!dimensions.contains("minecraft:the_end")) {
BCLib.LOGGER.info("Repairing End biome source");
result = true;
CompoundTag dimRoot = new CompoundTag();
dimRoot.put("generator", BiomeSourcePatch.makeEndGenerator(seed));
dimRoot.putString("type", "minecraft:the_end");
dimensions.put("minecraft:the_end", dimRoot);
} else {
result |= BiomeSourcePatch.repairEndSource(dimensions, seed);
}
return result;
}
}
@ -22,37 +69,53 @@ final class BiomeSourcePatch extends Patch {
}
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() {
return (root, profile) -> {
CompoundTag worldGenSettings = root.getCompound("Data").getCompound("WorldGenSettings");
CompoundTag dimensions = worldGenSettings.getCompound("dimensions");
long seed = worldGenSettings.getLong("seed");
boolean result = false;
if (dimensions.contains("minecraft:the_nether")) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_nether");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(NETHER_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying Nether biome source patch");
dimRoot.put("generator", makeNetherGenerator(seed));
result = true;
}
}
if (dimensions.contains("minecraft:the_end")) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_end");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(END_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying End biome source patch");
dimRoot.put("generator", makeEndGenerator(seed));
result = true;
}
}
return result;
};
return BiomeSourcePatch::fixBiomeSources;
}
private CompoundTag makeNetherGenerator(long seed) {
private static boolean fixBiomeSources(CompoundTag root, MigrationProfile profile) {
CompoundTag worldGenSettings = root.getCompound("Data").getCompound("WorldGenSettings");
CompoundTag dimensions = worldGenSettings.getCompound("dimensions");
long seed = worldGenSettings.getLong("seed");
boolean result = false;
if (dimensions.contains("minecraft:the_nether")) {
result |= repairNetherSource(dimensions, seed);
}
if (dimensions.contains("minecraft:the_end")) {
result |= repairEndSource(dimensions, seed);
}
return result;
}
public static boolean repairEndSource(CompoundTag dimensions, long seed) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_end");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(END_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying End biome source patch");
dimRoot.put("generator", makeEndGenerator(seed));
return true;
}
return false;
}
public static boolean repairNetherSource(CompoundTag dimensions, long seed) {
CompoundTag dimRoot = dimensions.getCompound("minecraft:the_nether");
CompoundTag biomeSource = dimRoot.getCompound("generator").getCompound("biome_source");
if (!biomeSource.getString("type").equals(NETHER_BIOME_SOURCE)) {
BCLib.LOGGER.info("Applying Nether biome source patch");
dimRoot.put("generator", makeNetherGenerator(seed));
return true;
}
return false;
}
;
public static CompoundTag makeNetherGenerator(long seed) {
CompoundTag generator = new CompoundTag();
generator.putString("type", "minecraft:noise");
generator.putString("settings", "minecraft:nether");
@ -66,7 +129,7 @@ final class BiomeSourcePatch extends Patch {
return generator;
}
private CompoundTag makeEndGenerator(long seed) {
public static CompoundTag makeEndGenerator(long seed) {
CompoundTag generator = new CompoundTag();
generator.putString("type", "minecraft:noise");
generator.putString("settings", "minecraft:end");